Skip to content

Commit e59a0e1

Browse files
committed
feat: ability to disable extract modules, less code difference
1 parent 5b3fbef commit e59a0e1

File tree

2 files changed

+112
-119
lines changed

2 files changed

+112
-119
lines changed

README.md

+1-9
Original file line numberDiff line numberDiff line change
@@ -345,15 +345,7 @@ module.exports = {
345345

346346
#### Disable Extracting Specified CSS from Chunks
347347

348-
You may disable extracting css from all chunks with `disableExtract` option.
349-
350-
```javascript
351-
const miniCssExtractPlugin = new MiniCssExtractPlugin({
352-
disableExtract: true,
353-
});
354-
```
355-
356-
You may also disable extracting css modules programmatically by passing a function.
348+
You may disable extracting css modules programmatically by passing a function.
357349

358350
```javascript
359351
const miniCssExtractPlugin = new MiniCssExtractPlugin({

src/loader.js

+111-110
Original file line numberDiff line numberDiff line change
@@ -69,133 +69,134 @@ export function pitch(request) {
6969

7070
if (this[`${MODULE_TYPE}/disableExtract`]()) {
7171
callback();
72-
} else {
73-
const childFilename = '*'; // eslint-disable-line no-path-concat
74-
const publicPath =
75-
typeof options.publicPath === 'string'
76-
? options.publicPath === '' || options.publicPath.endsWith('/')
77-
? options.publicPath
78-
: `${options.publicPath}/`
79-
: typeof options.publicPath === 'function'
80-
? options.publicPath(this.resourcePath, this.rootContext)
81-
: this._compilation.outputOptions.publicPath;
82-
const outputOptions = {
83-
filename: childFilename,
84-
publicPath,
85-
};
86-
const childCompiler = this._compilation.createChildCompiler(
87-
`${pluginName} ${request}`,
88-
outputOptions
89-
);
90-
91-
new NodeTemplatePlugin(outputOptions).apply(childCompiler);
92-
new LibraryTemplatePlugin(null, 'commonjs2').apply(childCompiler);
93-
new NodeTargetPlugin().apply(childCompiler);
94-
new SingleEntryPlugin(this.context, `!!${request}`, pluginName).apply(
95-
childCompiler
96-
);
97-
new LimitChunkCountPlugin({ maxChunks: 1 }).apply(childCompiler);
98-
99-
// We set loaderContext[MODULE_TYPE] = false to indicate we already in
100-
// a child compiler so we don't spawn another child compilers from there.
101-
childCompiler.hooks.thisCompilation.tap(
102-
`${pluginName} loader`,
103-
(compilation) => {
104-
compilation.hooks.normalModuleLoader.tap(
105-
`${pluginName} loader`,
106-
(loaderContext, module) => {
72+
return;
73+
}
74+
75+
const childFilename = '*'; // eslint-disable-line no-path-concat
76+
const publicPath =
77+
typeof options.publicPath === 'string'
78+
? options.publicPath === '' || options.publicPath.endsWith('/')
79+
? options.publicPath
80+
: `${options.publicPath}/`
81+
: typeof options.publicPath === 'function'
82+
? options.publicPath(this.resourcePath, this.rootContext)
83+
: this._compilation.outputOptions.publicPath;
84+
const outputOptions = {
85+
filename: childFilename,
86+
publicPath,
87+
};
88+
const childCompiler = this._compilation.createChildCompiler(
89+
`${pluginName} ${request}`,
90+
outputOptions
91+
);
92+
93+
new NodeTemplatePlugin(outputOptions).apply(childCompiler);
94+
new LibraryTemplatePlugin(null, 'commonjs2').apply(childCompiler);
95+
new NodeTargetPlugin().apply(childCompiler);
96+
new SingleEntryPlugin(this.context, `!!${request}`, pluginName).apply(
97+
childCompiler
98+
);
99+
new LimitChunkCountPlugin({ maxChunks: 1 }).apply(childCompiler);
100+
101+
// We set loaderContext[MODULE_TYPE] = false to indicate we already in
102+
// a child compiler so we don't spawn another child compilers from there.
103+
childCompiler.hooks.thisCompilation.tap(
104+
`${pluginName} loader`,
105+
(compilation) => {
106+
compilation.hooks.normalModuleLoader.tap(
107+
`${pluginName} loader`,
108+
(loaderContext, module) => {
109+
// eslint-disable-next-line no-param-reassign
110+
loaderContext.emitFile = this.emitFile;
111+
loaderContext[MODULE_TYPE] = false; // eslint-disable-line no-param-reassign
112+
113+
if (module.request === request) {
107114
// eslint-disable-next-line no-param-reassign
108-
loaderContext.emitFile = this.emitFile;
109-
loaderContext[MODULE_TYPE] = false; // eslint-disable-line no-param-reassign
110-
111-
if (module.request === request) {
112-
// eslint-disable-next-line no-param-reassign
113-
module.loaders = loaders.map((loader) => {
114-
return {
115-
loader: loader.path,
116-
options: loader.options,
117-
ident: loader.ident,
118-
};
119-
});
120-
}
115+
module.loaders = loaders.map((loader) => {
116+
return {
117+
loader: loader.path,
118+
options: loader.options,
119+
ident: loader.ident,
120+
};
121+
});
121122
}
122-
);
123-
}
124-
);
123+
}
124+
);
125+
}
126+
);
125127

126-
let source;
128+
let source;
127129

128-
childCompiler.hooks.afterCompile.tap(pluginName, (compilation) => {
129-
source =
130-
compilation.assets[childFilename] &&
131-
compilation.assets[childFilename].source();
130+
childCompiler.hooks.afterCompile.tap(pluginName, (compilation) => {
131+
source =
132+
compilation.assets[childFilename] &&
133+
compilation.assets[childFilename].source();
132134

133-
// Remove all chunk assets
134-
compilation.chunks.forEach((chunk) => {
135-
chunk.files.forEach((file) => {
136-
delete compilation.assets[file]; // eslint-disable-line no-param-reassign
137-
});
135+
// Remove all chunk assets
136+
compilation.chunks.forEach((chunk) => {
137+
chunk.files.forEach((file) => {
138+
delete compilation.assets[file]; // eslint-disable-line no-param-reassign
138139
});
139140
});
141+
});
140142

141-
childCompiler.runAsChild((err, entries, compilation) => {
142-
if (err) {
143-
return callback(err);
144-
}
143+
childCompiler.runAsChild((err, entries, compilation) => {
144+
if (err) {
145+
return callback(err);
146+
}
145147

146-
if (compilation.errors.length > 0) {
147-
return callback(compilation.errors[0]);
148-
}
148+
if (compilation.errors.length > 0) {
149+
return callback(compilation.errors[0]);
150+
}
149151

150-
compilation.fileDependencies.forEach((dep) => {
151-
this.addDependency(dep);
152-
}, this);
152+
compilation.fileDependencies.forEach((dep) => {
153+
this.addDependency(dep);
154+
}, this);
153155

154-
compilation.contextDependencies.forEach((dep) => {
155-
this.addContextDependency(dep);
156-
}, this);
156+
compilation.contextDependencies.forEach((dep) => {
157+
this.addContextDependency(dep);
158+
}, this);
157159

158-
if (!source) {
159-
return callback(new Error("Didn't get a result from child compiler"));
160-
}
160+
if (!source) {
161+
return callback(new Error("Didn't get a result from child compiler"));
162+
}
161163

162-
let text;
163-
let locals;
164-
165-
try {
166-
text = exec(this, source, request);
167-
locals = text && text.locals;
168-
if (!Array.isArray(text)) {
169-
text = [[null, text]];
170-
} else {
171-
text = text.map((line) => {
172-
const module = findModuleById(compilation.modules, line[0]);
173-
174-
return {
175-
identifier: module.identifier(),
176-
content: line[1],
177-
media: line[2],
178-
sourceMap: line[3],
179-
};
180-
});
181-
}
182-
this[MODULE_TYPE](text);
183-
} catch (e) {
184-
return callback(e);
164+
let text;
165+
let locals;
166+
167+
try {
168+
text = exec(this, source, request);
169+
locals = text && text.locals;
170+
if (!Array.isArray(text)) {
171+
text = [[null, text]];
172+
} else {
173+
text = text.map((line) => {
174+
const module = findModuleById(compilation.modules, line[0]);
175+
176+
return {
177+
identifier: module.identifier(),
178+
content: line[1],
179+
media: line[2],
180+
sourceMap: line[3],
181+
};
182+
});
185183
}
184+
this[MODULE_TYPE](text);
185+
} catch (e) {
186+
return callback(e);
187+
}
186188

187-
let resultSource = `// extracted by ${pluginName}`;
188-
const result = locals
189-
? `\nmodule.exports = ${JSON.stringify(locals)};`
190-
: '';
189+
let resultSource = `// extracted by ${pluginName}`;
190+
const result = locals
191+
? `\nmodule.exports = ${JSON.stringify(locals)};`
192+
: '';
191193

192-
resultSource += options.hmr
193-
? hotLoader(result, { context: this.context, options, locals })
194-
: result;
194+
resultSource += options.hmr
195+
? hotLoader(result, { context: this.context, options, locals })
196+
: result;
195197

196-
return callback(null, resultSource);
197-
});
198-
}
198+
return callback(null, resultSource);
199+
});
199200
}
200201

201202
export default function(source) {

0 commit comments

Comments
 (0)