Skip to content

Commit

Permalink
Merge 8870cb5 into 35d57e9
Browse files Browse the repository at this point in the history
  • Loading branch information
falsandtru committed May 10, 2020
2 parents 35d57e9 + 8870cb5 commit 1c0fabc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
4 changes: 4 additions & 0 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ const replaceWinPath = (path) => {

exports.normalizeWinPath = process.platform === 'win32' ? replaceWinPath : _.identity

exports.getFileType = (file) => {
return file.type || path.extname(file.path.split(/[?#]/, 1)[0] || '').substring(1)
}

exports.mkdirIfNotExists = (directory, done) => {
// TODO(vojta): handle if it's a file
/* eslint-disable handle-callback-err */
Expand Down
41 changes: 20 additions & 21 deletions lib/middleware/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
* - setting propert caching headers
*/

const path = require('path')
const url = require('url')
const helper = require('../helper')

Expand Down Expand Up @@ -163,31 +162,31 @@ function createKarmaMiddleware (

const scriptTags = []
for (const file of files.included) {
let filePath = file.path
const fileType = file.type || path.extname(filePath).substring(1)
const urlPath = file.isUrl
? file.path
: requestUrl === '/context.html'
? filePathToUrlPath(file.path, basePath, urlRoot, proxyPath) + '?' + file.sha
: filePathToUrlPath(file.path, basePath, urlRoot, proxyPath)
const fileType = helper.getFileType(file)

if (helper.isDefined(fileType) && !FILE_TYPES.includes(fileType)) {
log.warn(`Invalid file type (${fileType}), defaulting to js.`)
}

if (!file.isUrl) {
filePath = filePathToUrlPath(filePath, basePath, urlRoot, proxyPath)

if (requestUrl === '/context.html') {
filePath += '?' + file.sha
}
}

if (fileType === 'css') {
scriptTags.push(`<link type="text/css" href="${filePath}" rel="stylesheet">`)
} else if (fileType === 'dom') {
scriptTags.push(file.content)
} else if (fileType === 'html') {
scriptTags.push(`<link href="${filePath}" rel="import">`)
} else {
const scriptType = (SCRIPT_TYPE[fileType] || 'text/javascript')
const crossOriginAttribute = includeCrossOriginAttribute ? 'crossorigin="anonymous"' : ''
scriptTags.push(`<script type="${scriptType}" src="${filePath}" ${crossOriginAttribute}></script>`)
switch (fileType) {
case 'dom':
scriptTags.push(file.content)
break
case 'css':
scriptTags.push(`<link type="text/css" href="${urlPath}" rel="stylesheet">`)
break
case 'html':
scriptTags.push(`<link href="${urlPath}" rel="import">`)
break
default:
const scriptType = (SCRIPT_TYPE[fileType] || 'text/javascript')
const crossOriginAttribute = includeCrossOriginAttribute ? 'crossorigin="anonymous"' : ''
scriptTags.push(`<script type="${scriptType}" src="${urlPath}" ${crossOriginAttribute}></script>`)
}
}

Expand Down
9 changes: 9 additions & 0 deletions test/unit/helper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ describe('helper', () => {
})
})

describe('getFileType', () => {
it('should extract file type', () => {
expect(helper.getFileType({ path: 'a.js' })).to.equal('js')
expect(helper.getFileType({ path: 'a.js#' })).to.equal('js')
expect(helper.getFileType({ path: 'a.js?#' })).to.equal('js')
expect(helper.getFileType({ type: 'js', path: 'a' })).to.equal('js')
})
})

describe('mkdirIfNotExists', () => {
const fsMock = require('mocks').fs
const loadFile = require('mocks').loadFile
Expand Down

0 comments on commit 1c0fabc

Please sign in to comment.