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): correctly map entries to outputted assets (config.output) #347

Merged
merged 3 commits into from
Sep 3, 2018
Merged
Changes from all 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
82 changes: 52 additions & 30 deletions src/karma-webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -111,6 +111,8 @@ function Plugin(
this.files = [];
this.basePath = basePath;
this.waiting = [];
this.entries = new Map();
this.outputs = new Map();
this.plugin = { name: 'KarmaWebpack' };

let compiler;
Expand Down Expand Up @@ -157,6 +159,20 @@ function Plugin(
applyStats.forEach((stats) => {
stats = stats.toJson();

this.outputs.clear();

const entries = Object.keys(stats.assetsByChunkName);
for (let i = 0; i < entries.length; i++) {
const entry = entries[i];

if (this.entries.has(entry)) {
const entryPath = this.entries.get(entry);
const outputPath = stats.assetsByChunkName[entry];

this.outputs.set(entryPath, outputPath);
}
}

assets.push(...stats.assets);
if (stats.assets.length === 0) {
noAssets = true;
Expand Down Expand Up @@ -237,6 +253,8 @@ Plugin.prototype.addFile = function(entry) {
};

Plugin.prototype.make = function(compilation, callback) {
this.entries.clear();

async.forEach(
this.files.slice(),
(file, callback) => {
Expand All @@ -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.entries.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
);
Expand All @@ -287,7 +308,7 @@ Plugin.prototype.readFile = function(file, callback) {
os.tmpdir(),
'_karma_webpack_',
String(idx),
file.replace(/\\/g, '/')
this.outputs.get(file)
),
callback
);
Expand All @@ -313,7 +334,7 @@ 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.outputs.get(file))
);

callback(null, fileContents);
Expand Down Expand Up @@ -355,17 +376,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.outputs.get(filename);
file.path = path.join(basePath, outputPath);

done(err, content && content.toString());
});
};
}

Expand Down