Skip to content

refactor: replace literal plugin name in strings with const #157

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

Merged
merged 1 commit into from
May 28, 2018
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class MiniCssExtractPlugin {
chunk,
contentHashType: NS,
},
identifier: `mini-css-extract-plugin.${chunk.id}`,
identifier: `${pluginName}.${chunk.id}`,
hash: chunk.contentHash[NS],
});
}
Expand All @@ -199,7 +199,7 @@ class MiniCssExtractPlugin {
chunk,
contentHashType: NS,
},
identifier: `mini-css-extract-plugin.${chunk.id}`,
identifier: `${pluginName}.${chunk.id}`,
hash: chunk.contentHash[NS],
});
}
Expand Down Expand Up @@ -312,7 +312,7 @@ class MiniCssExtractPlugin {
return Template.asString([
source,
'',
'// mini-css-extract-plugin CSS loading',
`// ${pluginName} CSS loading`,
`var cssChunks = ${JSON.stringify(chunkMap)};`,
'if(installedCssChunks[chunkId]) promises.push(installedCssChunks[chunkId]);',
'else if(installedCssChunks[chunkId] !== 0 && cssChunks[chunkId]) {',
Expand Down
40 changes: 18 additions & 22 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin';
import LimitChunkCountPlugin from 'webpack/lib/optimize/LimitChunkCountPlugin';

const NS = path.dirname(fs.realpathSync(__filename));
const pluginName = 'mini-css-extract-plugin';

const exec = (loaderContext, code, filename) => {
const module = new NativeModule(filename, loaderContext);
Expand Down Expand Up @@ -42,25 +43,23 @@ export function pitch(request) {
publicPath,
};
const childCompiler = this._compilation.createChildCompiler(
`mini-css-extract-plugin ${request}`,
`${pluginName} ${request}`,
outputOptions
);
new NodeTemplatePlugin(outputOptions).apply(childCompiler);
new LibraryTemplatePlugin(null, 'commonjs2').apply(childCompiler);
new NodeTargetPlugin().apply(childCompiler);
new SingleEntryPlugin(
this.context,
`!!${request}`,
'mini-css-extract-plugin'
).apply(childCompiler);
new SingleEntryPlugin(this.context, `!!${request}`, pluginName).apply(
childCompiler
);
new LimitChunkCountPlugin({ maxChunks: 1 }).apply(childCompiler);
// We set loaderContext[NS] = false to indicate we already in
// a child compiler so we don't spawn another child compilers from there.
childCompiler.hooks.thisCompilation.tap(
'mini-css-extract-plugin loader',
`${pluginName} loader`,
(compilation) => {
compilation.hooks.normalModuleLoader.tap(
'mini-css-extract-plugin loader',
`${pluginName} loader`,
(loaderContext, module) => {
loaderContext[NS] = false; // eslint-disable-line no-param-reassign
if (module.request === request) {
Expand All @@ -79,21 +78,18 @@ export function pitch(request) {
);

let source;
childCompiler.hooks.afterCompile.tap(
'mini-css-extract-plugin',
(compilation) => {
source =
compilation.assets[childFilename] &&
compilation.assets[childFilename].source();
childCompiler.hooks.afterCompile.tap(pluginName, (compilation) => {
source =
compilation.assets[childFilename] &&
compilation.assets[childFilename].source();

// Remove all chunk assets
compilation.chunks.forEach((chunk) => {
chunk.files.forEach((file) => {
delete compilation.assets[file]; // eslint-disable-line no-param-reassign
});
// Remove all chunk assets
compilation.chunks.forEach((chunk) => {
chunk.files.forEach((file) => {
delete compilation.assets[file]; // eslint-disable-line no-param-reassign
});
}
);
});
});

const callback = this.async();
childCompiler.runAsChild((err, entries, compilation) => {
Expand Down Expand Up @@ -133,7 +129,7 @@ export function pitch(request) {
} catch (e) {
return callback(e);
}
let resultSource = '// extracted by mini-css-extract-plugin';
let resultSource = `// extracted by ${pluginName}`;
if (locals && typeof resultSource !== 'undefined') {
resultSource += `\nmodule.exports = ${JSON.stringify(locals)};`;
}
Expand Down