Skip to content

Commit 04a4660

Browse files
committed
use chunkInitFragments
1 parent 9da9aed commit 04a4660

7 files changed

Lines changed: 108 additions & 81 deletions

File tree

lib/DependencyTemplate.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* @property {RuntimeSpec} runtime current runtimes, for which code is generated
2929
* @property {InitFragment<GenerateContext>[]} initFragments mutable array of init fragments for the current module
3030
* @property {ConcatenationScope=} concatenationScope when in a concatenated module, information about other concatenated modules
31+
* @property {() => Map<any, any>} getData getData
3132
*/
3233

3334
class DependencyTemplate {

lib/NodeStuffPlugin.js

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const {
1515
} = require("./javascript/JavascriptParserHelpers");
1616
const { relative } = require("./util/fs");
1717
const { parseResource } = require("./util/identifier");
18-
const InitFragment = require("./InitFragment");
18+
const NodeUrlDependency = require("./dependencies/NodeUrlDependency");
1919

2020
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
2121
/** @typedef {import("./Compiler")} Compiler */
@@ -42,6 +42,11 @@ class NodeStuffPlugin {
4242
compiler.hooks.compilation.tap(
4343
"NodeStuffPlugin",
4444
(compilation, { normalModuleFactory }) => {
45+
compilation.dependencyTemplates.set(
46+
NodeUrlDependency,
47+
new NodeUrlDependency.Template()
48+
);
49+
4550
const handler = (parser, parserOptions) => {
4651
if (parserOptions.node === false) return;
4752

@@ -76,52 +81,32 @@ class NodeStuffPlugin {
7681
});
7782
}
7883

79-
const setCjsModuleConstant = (expressionName, fn, warning) => {
80-
parser.hooks.expression
81-
.for(expressionName)
82-
.tap("NodeStuffPlugin", expr => {
83-
const dep = new CachedConstDependency(
84-
JSON.stringify(fn(parser.state.module)),
85-
expr.range,
86-
expressionName
87-
);
88-
dep.loc = expr.loc;
89-
parser.state.module.addPresentationalDependency(dep);
90-
91-
// TODO webpack 6 remove
92-
if (warning) {
93-
parser.state.module.addWarning(
94-
new NodeStuffInWebError(dep.loc, expressionName, warning)
84+
const setModuleConstant =
85+
Dependency => (expressionName, fn, warning) => {
86+
parser.hooks.expression
87+
.for(expressionName)
88+
.tap("NodeStuffPlugin", expr => {
89+
const dep = new Dependency(
90+
JSON.stringify(fn(parser.state.module)),
91+
expr.range,
92+
expressionName
9593
);
96-
}
97-
98-
return true;
99-
});
100-
};
101-
102-
const setEsmModuleConstant = (expressionName, fn) => {
103-
parser.hooks.expression
104-
.for(expressionName)
105-
.tap("NodeStuffPlugin", expr => {
106-
const dep = new CachedConstDependency(
107-
JSON.stringify(fn(parser.state.module)),
108-
expr.range,
109-
expressionName,
110-
[
111-
new InitFragment(
112-
'import url from "url";',
113-
InitFragment.STAGE_CONSTANTS,
114-
0,
115-
"import url"
116-
)
117-
]
118-
);
119-
dep.loc = expr.loc;
120-
parser.state.module.addPresentationalDependency(dep);
121-
122-
return true;
123-
});
124-
};
94+
dep.loc = expr.loc;
95+
parser.state.module.addPresentationalDependency(dep);
96+
97+
// TODO webpack 6 remove
98+
if (warning) {
99+
parser.state.module.addWarning(
100+
new NodeStuffInWebError(dep.loc, expressionName, warning)
101+
);
102+
}
103+
104+
return true;
105+
});
106+
};
107+
108+
const setCjsModuleConstant = setModuleConstant(CachedConstDependency);
109+
const setEsmModuleConstant = setModuleConstant(NodeUrlDependency);
125110

126111
const setCjsConstant = (expressionName, value, warning) =>
127112
setCjsModuleConstant(expressionName, () => value, warning);

lib/dependencies/CachedConstDependency.js

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const NullDependency = require("./NullDependency");
1313
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
1414
/** @typedef {import("../ChunkGraph")} ChunkGraph */
1515
/** @typedef {import("../Dependency")} Dependency */
16-
/** @typedef {import("../Generator").GenerateContext} GenerateContext */
1716
/** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
1817
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
1918
/** @typedef {import("../DependencyTemplates")} DependencyTemplates */
@@ -22,19 +21,12 @@ const NullDependency = require("./NullDependency");
2221
/** @typedef {import("../util/Hash")} Hash */
2322

2423
class CachedConstDependency extends NullDependency {
25-
/**
26-
* @param {string} expression expression
27-
* @param {number|[number, number]} range range
28-
* @param {string} identifier identifier
29-
* @param {InitFragment<GenerateContext>[]=} initFragments init fragments
30-
*/
31-
constructor(expression, range, identifier, initFragments) {
24+
constructor(expression, range, identifier) {
3225
super();
3326

3427
this.expression = expression;
3528
this.range = range;
3629
this.identifier = identifier;
37-
this.initFragments = initFragments;
3830
}
3931

4032
/**
@@ -44,14 +36,9 @@ class CachedConstDependency extends NullDependency {
4436
* @returns {void}
4537
*/
4638
updateHash(hash, context) {
47-
hash.update(`${this.identifier}`);
48-
hash.update(`${this.range}`);
49-
hash.update(`${this.expression}`);
50-
51-
if (this.initFragments) {
52-
for (const fragment of this.initFragments)
53-
hash.update(`${fragment.key}_${fragment.position}`);
54-
}
39+
hash.update(this.identifier + "");
40+
hash.update(this.range + "");
41+
hash.update(this.expression + "");
5542
}
5643

5744
serialize(context) {
@@ -60,7 +47,6 @@ class CachedConstDependency extends NullDependency {
6047
write(this.expression);
6148
write(this.range);
6249
write(this.identifier);
63-
write(this.initFragments);
6450

6551
super.serialize(context);
6652
}
@@ -71,7 +57,6 @@ class CachedConstDependency extends NullDependency {
7157
this.expression = read();
7258
this.range = read();
7359
this.identifier = read();
74-
this.initFragments = read();
7560

7661
super.deserialize(context);
7762
}
@@ -91,29 +76,16 @@ CachedConstDependency.Template = class CachedConstDependencyTemplate extends (
9176
* @param {DependencyTemplateContext} templateContext the context object
9277
* @returns {void}
9378
*/
94-
apply(
95-
dependency,
96-
source,
97-
{ runtimeTemplate, dependencyTemplates, initFragments }
98-
) {
79+
apply(dependency, source, { runtimeTemplate, initFragments }) {
9980
const dep = /** @type {CachedConstDependency} */ (dependency);
10081

101-
let position = 0;
102-
103-
if (dep.initFragments && dep.initFragments.length > 0) {
104-
for (const fragment of dep.initFragments) {
105-
initFragments.push(fragment);
106-
}
107-
position = dep.initFragments[dep.initFragments.length - 1].position + 1;
108-
}
109-
11082
initFragments.push(
11183
new InitFragment(
11284
`${runtimeTemplate.supportsConst() ? "const" : "var"} ${
11385
dep.identifier
11486
} = ${dep.expression};\n`,
11587
InitFragment.STAGE_CONSTANTS,
116-
position,
88+
0,
11789
`const ${dep.identifier}`
11890
)
11991
);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Ivan Kopeykin @vankop
4+
*/
5+
6+
"use strict";
7+
8+
const InitFragment = require("../InitFragment");
9+
const makeSerializable = require("../util/makeSerializable");
10+
const CachedConstDependency = require("./CachedConstDependency");
11+
12+
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
13+
/** @typedef {import("../Dependency")} Dependency */
14+
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
15+
/** @typedef {import("../javascript/JavascriptModulesPlugin").ChunkRenderContext} ChunkRenderContext */
16+
17+
class NodeUrlDependency extends CachedConstDependency {}
18+
19+
makeSerializable(
20+
NodeUrlDependency,
21+
"webpack/lib/dependencies/NodeUrlDependency"
22+
);
23+
24+
NodeUrlDependency.Template = class NodeUrlDependencyTemplate extends (
25+
CachedConstDependency.Template
26+
) {
27+
/**
28+
* @param {Dependency} dependency the dependency for which the template should be applied
29+
* @param {ReplaceSource} source the current replace source which can be modified
30+
* @param {DependencyTemplateContext} templateContext the context object
31+
* @returns {void}
32+
*/
33+
apply(dependency, source, templateContext) {
34+
super.apply(dependency, source, templateContext);
35+
const { getData } = templateContext;
36+
const data = getData();
37+
/** @type {InitFragment<ChunkRenderContext>[]} */
38+
let chunkInitFragments = data.get("chunkInitFragments");
39+
40+
if (!chunkInitFragments) {
41+
chunkInitFragments = [];
42+
data.set("chunkInitFragments", chunkInitFragments);
43+
}
44+
45+
chunkInitFragments.push(
46+
new InitFragment(
47+
'import url from "url";',
48+
InitFragment.STAGE_CONSTANTS,
49+
0,
50+
"import url"
51+
)
52+
);
53+
}
54+
};
55+
56+
module.exports = NodeUrlDependency;

lib/javascript/JavascriptGenerator.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ class JavascriptGenerator extends Generator {
199199
runtime: generateContext.runtime,
200200
runtimeRequirements: generateContext.runtimeRequirements,
201201
concatenationScope: generateContext.concatenationScope,
202-
initFragments
202+
initFragments,
203+
getData: generateContext.getData
203204
};
204205

205206
template.apply(dependency, source, templateContext);

lib/util/internalSerializables.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ module.exports = {
4545
require("../dependencies/AMDRequireItemDependency"),
4646
"dependencies/CachedConstDependency": () =>
4747
require("../dependencies/CachedConstDependency"),
48+
"dependencies/NodeUrlDependency": () =>
49+
require("../dependencies/NodeUrlDependency"),
4850
"dependencies/CreateScriptUrlDependency": () =>
4951
require("../dependencies/CreateScriptUrlDependency"),
5052
"dependencies/CommonJsRequireContextDependency": () =>

test/__snapshots__/Cli.test.js.snap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,7 @@ Object {
14231423
true,
14241424
"warn-mock",
14251425
"mock",
1426+
"node-module",
14261427
"eval-only",
14271428
],
14281429
},
@@ -1443,6 +1444,7 @@ Object {
14431444
true,
14441445
"warn-mock",
14451446
"mock",
1447+
"node-module",
14461448
"eval-only",
14471449
],
14481450
},
@@ -1917,6 +1919,7 @@ Object {
19171919
true,
19181920
"warn-mock",
19191921
"mock",
1922+
"node-module",
19201923
"eval-only",
19211924
],
19221925
},
@@ -1937,6 +1940,7 @@ Object {
19371940
true,
19381941
"warn-mock",
19391942
"mock",
1943+
"node-module",
19401944
"eval-only",
19411945
],
19421946
},
@@ -2372,6 +2376,7 @@ Object {
23722376
true,
23732377
"warn-mock",
23742378
"mock",
2379+
"node-module",
23752380
"eval-only",
23762381
],
23772382
},
@@ -2392,6 +2397,7 @@ Object {
23922397
true,
23932398
"warn-mock",
23942399
"mock",
2400+
"node-module",
23952401
"eval-only",
23962402
],
23972403
},
@@ -2772,6 +2778,7 @@ Object {
27722778
true,
27732779
"warn-mock",
27742780
"mock",
2781+
"node-module",
27752782
"eval-only",
27762783
],
27772784
},
@@ -2792,6 +2799,7 @@ Object {
27922799
true,
27932800
"warn-mock",
27942801
"mock",
2802+
"node-module",
27952803
"eval-only",
27962804
],
27972805
},
@@ -3865,6 +3873,7 @@ Object {
38653873
true,
38663874
"warn-mock",
38673875
"mock",
3876+
"node-module",
38683877
"eval-only",
38693878
],
38703879
},
@@ -3885,6 +3894,7 @@ Object {
38853894
true,
38863895
"warn-mock",
38873896
"mock",
3897+
"node-module",
38883898
"eval-only",
38893899
],
38903900
},

0 commit comments

Comments
 (0)