Skip to content

Commit 26b9258

Browse files
committed
correct plugin exsit validation
1 parent 757bd25 commit 26b9258

File tree

3 files changed

+46
-37
lines changed

3 files changed

+46
-37
lines changed

src/index.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import { validate } from 'schema-utils';
44

55
import schema from './plugin-options.json';
6-
import { MODULE_TYPE, compareModulesByIdentifier } from './utils';
6+
import { MODULE_TYPE, compareModulesByIdentifier, provideLoaderContext } from './utils';
77

8-
const pluginName = 'mini-css-extract-plugin';
8+
export const pluginName = 'mini-css-extract-plugin';
9+
export const pluginSymbol = Symbol(pluginName);
910

1011
const REGEXP_CHUNKHASH = /\[chunkhash(?::(\d+))?\]/i;
1112
const REGEXP_CONTENTHASH = /\[contenthash(?::(\d+))?\]/i;
@@ -384,6 +385,11 @@ class MiniCssExtractPlugin {
384385
const CssModule = MiniCssExtractPlugin.getCssModule(webpack);
385386
const CssDependency = MiniCssExtractPlugin.getCssDependency(webpack);
386387

388+
provideLoaderContext(compiler, `${pluginName} loader context`, (loaderContext)=>{
389+
// eslint-disable-next-line no-param-reassign
390+
loaderContext[pluginSymbol] = true;
391+
}, false)
392+
387393
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
388394
class CssModuleFactory {
389395
// eslint-disable-next-line class-methods-use-this

src/loader.js

+18-35
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ import path from 'path';
33
import loaderUtils from 'loader-utils';
44
import { validate } from 'schema-utils';
55

6-
import { findModuleById, evalModuleCode } from './utils';
6+
import { findModuleById, evalModuleCode, provideLoaderContext } from './utils';
77
import schema from './loader-options.json';
88

9-
import MiniCssExtractPlugin from './index';
10-
11-
const pluginName = 'mini-css-extract-plugin';
9+
import MiniCssExtractPlugin, {pluginName, pluginSymbol} from './index';
1210

1311
function hotLoader(content, context) {
1412
const accept = context.locals
@@ -39,6 +37,12 @@ export function pitch(request) {
3937
baseDataPath: 'options',
4038
});
4139

40+
if (!this[pluginSymbol]) {
41+
throw new Error(
42+
"You forgot to add 'mini-css-extract-plugin' plugin (i.e. `{ plugins: [new MiniCssExtractPlugin()] }`), please read https://github.com/webpack-contrib/mini-css-extract-plugin#getting-started"
43+
);
44+
}
45+
4246
const loaders = this.loaders.slice(this.loaderIndex + 1);
4347

4448
this.addDependency(this.resourcePath);
@@ -104,33 +108,18 @@ export function pitch(request) {
104108

105109
new LimitChunkCountPlugin({ maxChunks: 1 }).apply(childCompiler);
106110

107-
const NormalModule = webpack.NormalModule
108-
? webpack.NormalModule
109-
: // eslint-disable-next-line global-require
110-
require('webpack/lib/NormalModule');
111-
112-
childCompiler.hooks.thisCompilation.tap(
113-
`${pluginName} loader`,
114-
(compilation) => {
115-
const normalModuleHook =
116-
typeof NormalModule.getCompilationHooks !== 'undefined'
117-
? NormalModule.getCompilationHooks(compilation).loader
118-
: compilation.hooks.normalModuleLoader;
119-
120-
normalModuleHook.tap(`${pluginName} loader`, (loaderContext, module) => {
121-
if (module.request === request) {
122-
// eslint-disable-next-line no-param-reassign
123-
module.loaders = loaders.map((loader) => {
124-
return {
125-
loader: loader.path,
126-
options: loader.options,
127-
ident: loader.ident,
128-
};
129-
});
130-
}
111+
provideLoaderContext(childCompiler, `${pluginName} loader`, (_, module) => {
112+
if (module.request === request) {
113+
// eslint-disable-next-line no-param-reassign
114+
module.loaders = loaders.map((loader) => {
115+
return {
116+
loader: loader.path,
117+
options: loader.options,
118+
ident: loader.ident,
119+
};
131120
});
132121
}
133-
);
122+
});
134123

135124
let source;
136125

@@ -203,12 +192,6 @@ export function pitch(request) {
203192
const count = identifierCountMap.get(dependency.identifier) || 0;
204193
const CssDependency = MiniCssExtractPlugin.getCssDependency(webpack);
205194

206-
if (!CssDependency) {
207-
throw new Error(
208-
"You forgot to add 'mini-css-extract-plugin' plugin (i.e. `{ plugins: [new MiniCssExtractPlugin()] }`), please read https://github.com/webpack-contrib/mini-css-extract-plugin#getting-started"
209-
);
210-
}
211-
212195
this._module.addDependency(
213196
(lastDep = new CssDependency(dependency, dependency.context, count))
214197
);

src/utils.js

+20
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,27 @@ function compareModulesByIdentifier(a, b) {
4949
return compareIds(a.identifier(), b.identifier());
5050
}
5151

52+
function provideLoaderContext(compiler, name, handler, thisCompilation = true){
53+
const NormalModule = compiler.webpack && compiler.webpack.NormalModule
54+
? compiler.webpack.NormalModule
55+
: // eslint-disable-next-line global-require
56+
require('webpack/lib/NormalModule');
57+
58+
compiler.hooks[thisCompilation ? 'thisCompilation': 'compilation'].tap(
59+
name,
60+
(compilation) => {
61+
const normalModuleHook =
62+
typeof NormalModule.getCompilationHooks !== 'undefined'
63+
? NormalModule.getCompilationHooks(compilation).loader
64+
: compilation.hooks.normalModuleLoader;
65+
66+
normalModuleHook.tap(name, (loaderContext, module) => handler(loaderContext, module, compilation));
67+
}
68+
);
69+
}
70+
5271
export {
72+
provideLoaderContext,
5373
MODULE_TYPE,
5474
findModuleById,
5575
evalModuleCode,

0 commit comments

Comments
 (0)