-
Notifications
You must be signed in to change notification settings - Fork 219
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): correctly map entries
to outputted assets
(config.output
)
#347
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,11 +90,11 @@ function Plugin( | |
} | ||
|
||
if (!webpackOptions.output.filename) { | ||
webpackOptions.output.filename = '[name].js' | ||
webpackOptions.output.filename = '[name].js'; | ||
} | ||
|
||
if (!webpackOptions.output.chunkFilename) { | ||
webpackOptions.output.chunkFilename = '[id].bundle.js' | ||
webpackOptions.output.chunkFilename = '[id].bundle.js'; | ||
} | ||
|
||
// For webpack 4+, optimization.splitChunks and optimization.runtimeChunk must be false. | ||
|
@@ -111,6 +111,8 @@ function Plugin( | |
this.files = []; | ||
this.basePath = basePath; | ||
this.waiting = []; | ||
this.entryFilesMap = new Map(); | ||
this.outputFilesMap = new Map(); | ||
this.plugin = { name: 'KarmaWebpack' }; | ||
|
||
let compiler; | ||
|
@@ -157,6 +159,20 @@ function Plugin( | |
applyStats.forEach((stats) => { | ||
stats = stats.toJson(); | ||
|
||
this.outputFilesMap.clear(); | ||
|
||
const assetKeys = Object.keys(stats.assetsByChunkName); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
for (let i = 0; i < assetKeys.length; i++) { | ||
const entryName = assetKeys[i]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
if (this.entryFilesMap.has(entryName)) { | ||
const entryPath = this.entryFilesMap.get(entryName); | ||
const outputPath = stats.assetsByChunkName[entryName]; | ||
|
||
this.outputFilesMap.set(entryPath, outputPath); | ||
} | ||
} | ||
|
||
assets.push(...stats.assets); | ||
if (stats.assets.length === 0) { | ||
noAssets = true; | ||
|
@@ -237,6 +253,8 @@ Plugin.prototype.addFile = function(entry) { | |
}; | ||
|
||
Plugin.prototype.make = function(compilation, callback) { | ||
this.entryFilesMap.clear(); | ||
|
||
async.forEach( | ||
this.files.slice(), | ||
(file, callback) => { | ||
|
@@ -250,24 +268,27 @@ Plugin.prototype.make = function(compilation, callback) { | |
|
||
const dep = new SingleEntryDependency(entry); | ||
|
||
compilation.addEntry( | ||
'', | ||
dep, | ||
path.relative(this.basePath, file).replace(/\\/g, '/'), | ||
(err) => { | ||
// If the module fails because of an File not found error, remove the test file | ||
if ( | ||
dep.module && | ||
dep.module.error && | ||
dep.module.error.error && | ||
dep.module.error.error.code === 'ENOENT' | ||
) { | ||
this.files = this.files.filter((f) => file !== f); | ||
invalidate(this.middleware); | ||
} | ||
callback(err); | ||
} | ||
const filename = path.relative(this.basePath, file).replace(/\\/g, '/'); | ||
const name = path.join( | ||
path.dirname(filename), | ||
path.basename(filename, path.extname(filename)) | ||
); | ||
|
||
this.entryFilesMap.set(name, filename); | ||
|
||
compilation.addEntry('', dep, name, (err) => { | ||
// If the module fails because of an File not found error, remove the test file | ||
if ( | ||
dep.module && | ||
dep.module.error && | ||
dep.module.error.error && | ||
dep.module.error.error.code === 'ENOENT' | ||
) { | ||
this.files = this.files.filter((f) => file !== f); | ||
invalidate(this.middleware); | ||
} | ||
callback(err); | ||
}); | ||
}, | ||
callback | ||
); | ||
|
@@ -313,7 +334,11 @@ Plugin.prototype.readFile = function(file, callback) { | |
} else { | ||
try { | ||
const fileContents = middleware.fileSystem.readFileSync( | ||
path.join(os.tmpdir(), '_karma_webpack_', file.replace(/\\/g, '/')) | ||
path.join( | ||
os.tmpdir(), | ||
'_karma_webpack_', | ||
this.outputFilesMap.get(file) | ||
) | ||
); | ||
|
||
callback(null, fileContents); | ||
|
@@ -355,17 +380,18 @@ function createPreprocesor(/* config.basePath */ basePath, webpackPlugin) { | |
invalidate(webpackPlugin.middleware); | ||
} | ||
|
||
const filename = path.relative(basePath, file.originalPath); | ||
// read blocks until bundle is done | ||
webpackPlugin.readFile( | ||
path.relative(basePath, file.originalPath), | ||
(err, content) => { | ||
if (err) { | ||
throw err; | ||
} | ||
|
||
done(err, content && content.toString()); | ||
webpackPlugin.readFile(filename, (err, content) => { | ||
if (err) { | ||
throw err; | ||
} | ||
); | ||
|
||
const outputPath = webpackPlugin.outputFilesMap.get(filename); | ||
file.path = path.join(basePath, outputPath); | ||
|
||
done(err, content && content.toString()); | ||
}); | ||
}; | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this.entries = new Map()
this.outputs = new Map()