diff --git a/lib/helper.js b/lib/helper.js index 34b349efd..b1313d5a3 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -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 */ diff --git a/lib/middleware/karma.js b/lib/middleware/karma.js index dd1ee1d00..ebe492bb5 100644 --- a/lib/middleware/karma.js +++ b/lib/middleware/karma.js @@ -11,7 +11,6 @@ * - setting propert caching headers */ -const path = require('path') const url = require('url') const helper = require('../helper') @@ -163,31 +162,27 @@ 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(``) + scriptTags.push(``) } else if (fileType === 'dom') { scriptTags.push(file.content) } else if (fileType === 'html') { - scriptTags.push(``) + scriptTags.push(``) } else { const scriptType = (SCRIPT_TYPE[fileType] || 'text/javascript') const crossOriginAttribute = includeCrossOriginAttribute ? 'crossorigin="anonymous"' : '' - scriptTags.push(``) + scriptTags.push(``) } } diff --git a/test/unit/helper.spec.js b/test/unit/helper.spec.js index 8c5808c50..50318d150 100644 --- a/test/unit/helper.spec.js +++ b/test/unit/helper.spec.js @@ -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