Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(karma-webpack): handle outputPath correctly #360

Merged
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions src/karma-webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ var isBlocked = false

const normalize = (file) => file.replace(/\\/g, '/')

var getJsOutput = (outputPathArray) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- var getJsOutput
+ var getOutputPath
- ... = (outputPathArray) =>
+  ... = (outputPath) =>

for (var _i = 0; _i < outputPathArray.length; _i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- var _i
+ var i

if (outputPathArray[_i].indexOf(".js") != -1) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How des this work? Could you give a five examples please? What happens if e.g file.js.map takes precedence?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- !=
+ !==

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How des this work? Could you give a five examples please? What happens if e.g file.js.map
takes precedence?

You are right. I did not consider this case actually? I have in my webpack config 3 entries. like
entry: {
app: ${scriptsPath}/ibe/${IBEClient}/app.ts,
vendor: ${scriptsPath}/vendor.browser.ts,
polyfills: ${scriptsPath}/polyfills.browser.ts,
}
So, I move the entries to Karma config to get the same output form webpack. and remove it form webpack config. mostly I didn't see this case because is the .js output is always before file.js.map output in all my cases. The problem I faced is if the webpack shanks has .css.map as first output.

In any case i will add .js.map to if condition which should solve this case also :)

return outputPathArray[_i]
}
}
return null
}

var escapeRegExp = function(str) {
// See details here https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&')
Expand Down Expand Up @@ -130,7 +139,10 @@ function Plugin(
if ({}.hasOwnProperty.call(this.entries, entry)) {
var entryPath = this.entries[entry]
var outputPath = stats.assetsByChunkName[entry]
this.outputs[entryPath] = outputPath
if (Array.isArray(outputPath))
outputPath = getJsOutput(outputPath)
if (outputPath != null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- !=
+ !==

this.outputs[entryPath] = outputPath
}
}

Expand Down Expand Up @@ -237,11 +249,7 @@ Plugin.prototype.readFile = function(file, callback) {
var doRead = function() {
if (optionsCount > 1) {
async.times(optionsCount, function(idx, callback) {
if (Array.isArray(this.outputs[file])) {
middleware.fileSystem.readFile(path.join(os.tmpdir(), '_karma_webpack_', String(idx), this.outputs[file][0]), callback);
} else {
middleware.fileSystem.readFile(path.join(os.tmpdir(), '_karma_webpack_', String(idx), this.outputs[file]), callback);
}
middleware.fileSystem.readFile(path.join(os.tmpdir(), '_karma_webpack_', String(idx), this.outputs[file]), callback)
}.bind(this), function(err, contents) {
if (err) {
return callback(err)
Expand All @@ -258,12 +266,7 @@ Plugin.prototype.readFile = function(file, callback) {
})
} else {
try {
var fileContents = ''
if (Array.isArray(this.outputs[file])) {
fileContents = middleware.fileSystem.readFileSync(path.join(os.tmpdir(), '_karma_webpack_', this.outputs[file][0]));
} else {
fileContents = middleware.fileSystem.readFileSync(path.join(os.tmpdir(), '_karma_webpack_', this.outputs[file]));
}
var fileContents = middleware.fileSystem.readFileSync(path.join(os.tmpdir(), '_karma_webpack_', this.outputs[file]))

callback(undefined, fileContents)
} catch (e) {
Expand Down Expand Up @@ -306,11 +309,7 @@ function createPreprocesor(/* config.basePath */ basePath, webpackPlugin) {
}

var outputPath = webpackPlugin.outputs[normalize(filename)]
if( Array.isArray(outputPath)){
file.path = normalize(path.join(basePath, outputPath[0]));
} else {
file.path = normalize(path.join(basePath, outputPath));
}
file.path = normalize(path.join(basePath, outputPath))

done(err, content && content.toString())
})
Expand Down