Skip to content

Commit

Permalink
fix: emitted file name and originalName with mini-css-extract-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
piecyk committed Sep 22, 2018
1 parent 53b793a commit 2e07ad9
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/WebpackAssetsManifest.js
Expand Up @@ -128,7 +128,37 @@ class WebpackAssetsManifest
}

// compilation.assets contains the results of the build
compiler.hooks.compilation.tap(PLUGIN_NAME, this.handleCompilation.bind(this));
// compiler.hooks.compilation.tap(PLUGIN_NAME, this.handleCompilation.bind(this));

const isCss = (s) => /^.*\.css$/.test(s);
const MINI_EXTRACT_CSS_LOADER_MODULE_TYPE = 'css/mini-extract';

compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) =>
compilation.hooks.normalModuleLoader.tap(
PLUGIN_NAME,
(loaderContext, module) => {
const emitFile = loaderContext.emitFile;
const identifier = module.identifier();
// TODO: check this, basic we want to skip updating `assetNames` cache
// when using `css/mini-extract`
const isValid =
loaderContext[MINI_EXTRACT_CSS_LOADER_MODULE_TYPE] !== undefined
? !isCss(identifier)
: true;

loaderContext.emitFile = (name, content, sourceMap) => {
if (isValid) {
const originalName = path.join(
path.dirname(name),
path.basename(identifier)
);
this.assetNames.set(name, originalName);
}
return emitFile.call(module, name, content, sourceMap);
};
}
)
);

// Add manifest.json to compiler.assets
compiler.hooks.emit.tapAsync(PLUGIN_NAME, this.handleEmit.bind(this));
Expand Down

0 comments on commit 2e07ad9

Please sign in to comment.