Skip to content

Commit

Permalink
refactor(middleware): handle paths as URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
falsandtru committed May 10, 2020
1 parent bcaae16 commit 8870cb5
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions lib/middleware/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,31 +162,31 @@ function createKarmaMiddleware (

const scriptTags = []
for (const file of files.included) {
let filePath = file.path
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

0 comments on commit 8870cb5

Please sign in to comment.