Skip to content

Commit 188cf5c

Browse files
committed
feat: ability to disable extract async modules, lint fix
1 parent 39b36e9 commit 188cf5c

File tree

10 files changed

+35
-35
lines changed

10 files changed

+35
-35
lines changed

src/index.js

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ class MiniCssExtractPlugin {
152152
apply(compiler) {
153153
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
154154
const asyncModuleToBeRebuild = new Set();
155+
// eslint-disable-next-line no-param-reassign
155156
compilation[MODULE_TYPE] = {
156157
asyncModuleToBeRebuild,
157158
};
@@ -450,42 +451,40 @@ class MiniCssExtractPlugin {
450451
);
451452

452453
const len = `// extracted by ${pluginName}`.length;
453-
mainTemplate.hooks.beforeStartup.tap(
454-
pluginName,
455-
(source, chunk, hash) => {
456-
for (const _m of asyncModuleToBeRebuild) {
457-
const issuerDeps = _m.issuer.dependencies;
458-
let firstIndex = -1;
459-
const content = [];
460-
461-
for (let i = issuerDeps.length - 1; i >= 0; i--) {
462-
const {module} = issuerDeps[i];
463-
if (asyncModuleToBeRebuild.has(module)) {
464-
firstIndex = i;
465-
content.push(module.content.replace(/(?:[\r\n]+)/g, '\\n'));
466-
issuerDeps.splice(i, 1);
467-
}
454+
mainTemplate.hooks.beforeStartup.tap(pluginName, (source) => {
455+
for (const moduleToBeRebuild of asyncModuleToBeRebuild) {
456+
const issuerDeps = moduleToBeRebuild.issuer.dependencies;
457+
let firstIndex = -1;
458+
const content = [];
459+
460+
for (let i = issuerDeps.length - 1; i >= 0; i--) {
461+
const { module } = issuerDeps[i];
462+
if (asyncModuleToBeRebuild.has(module)) {
463+
firstIndex = i;
464+
content.push(module.content.replace(/(?:[\r\n]+)/g, '\\n'));
465+
issuerDeps.splice(i, 1);
468466
}
467+
}
469468

470-
if (firstIndex > -1) {
471-
issuerDeps.splice(
472-
firstIndex,
469+
if (firstIndex > -1) {
470+
issuerDeps.splice(
471+
firstIndex,
472+
0,
473+
new ReplaceDependency(`module.exports = "${content.join('')}";`, [
473474
0,
474-
new ReplaceDependency(
475-
`module.exports = "${content.join('')}";`,
476-
[0, len]
477-
)
478-
);
479-
}
475+
len,
476+
])
477+
);
480478
}
481-
return source;
482479
}
483-
);
480+
481+
return source;
482+
});
484483
});
485484
}
486485

487486
shouldDisableAsync({ module }) {
488-
const {disableAsync} = this.options;
487+
const { disableAsync } = this.options;
489488
let shouldDisable = false;
490489
if (disableAsync === true) {
491490
shouldDisable = true;

src/lib/ReplaceDependency.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const NullDependency = require('webpack/lib/dependencies/NullDependency');
22

33
class ReplaceDependency extends NullDependency {
44
constructor(replacement, range) {
5+
/* eslint-disable class-methods-use-this */
56
super();
67
this.replacement = replacement;
78
this.range = range;

test/cases/disable-async-bool/async.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ import './in-async.css';
33
import './in-async2.css';
44
import './both-async.css';
55

6-
console.log('async.js');
6+
// console.log('async.js');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
console.log('in-async.js');
1+
// console.log('in-async.js');

test/cases/disable-async-bool/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ import('./async');
55

66
import('./async.css');
77

8-
console.log('index.js');
8+
// console.log('index.js');

test/cases/disable-async-func/async.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ import './both-async.css';
66
import './both-page-async-disabled.css';
77
import './both-page-async.css';
88

9-
console.log('async.js');
9+
// console.log('async.js');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import './both-page-async-disabled.css';
22
import './both-page-async.css';
33

4-
console.log('async2.js');
4+
// console.log('async2.js');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
console.log('in-async.js');
1+
// console.log('in-async.js');

test/cases/disable-async-func/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ import('./async');
55

66
import('./async.css');
77

8-
console.log('index.js');
8+
// console.log('index.js');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import('./async2');
22

3-
console.log('index2');
3+
// console.log('index2');

0 commit comments

Comments
 (0)