Skip to content

Commit c09e82f

Browse files
committed
add importMeta to JavascriptParserOptions:
- enable/disable import.meta parsing - when disabled insert output.importMetaName
1 parent 332fb11 commit c09e82f

11 files changed

Lines changed: 113 additions & 111 deletions

File tree

declarations/WebpackOptions.d.ts

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,6 @@ export type RuleSetRules = ("..." | RuleSetRule)[];
351351
*/
352352
export type GeneratorOptionsByModuleType = GeneratorOptionsByModuleTypeKnown &
353353
GeneratorOptionsByModuleTypeUnknown;
354-
/**
355-
* Options object for es6 import.meta features.
356-
*/
357-
export type ImportMeta = false | ImportMetaOptions;
358354
/**
359355
* Don't parse files matching. It's matched against the full resolved request.
360356
*/
@@ -1246,10 +1242,6 @@ export interface ModuleOptions {
12461242
* Specify options for each generator.
12471243
*/
12481244
generator?: GeneratorOptionsByModuleType;
1249-
/**
1250-
* Options object for es6 import.meta features.
1251-
*/
1252-
importMeta?: ImportMeta;
12531245
/**
12541246
* Don't parse files matching. It's matched against the full resolved request.
12551247
*/
@@ -1592,15 +1584,6 @@ export interface ResolvePluginInstance {
15921584
apply: (resolver: import("enhanced-resolve").Resolver) => void;
15931585
[k: string]: any;
15941586
}
1595-
/**
1596-
* Options object for es6 import.meta features.
1597-
*/
1598-
export interface ImportMetaOptions {
1599-
/**
1600-
* Include a polyfill for the 'import.meta.url' variable.
1601-
*/
1602-
url?: false | true;
1603-
}
16041587
/**
16051588
* Options object for node compatibility features.
16061589
*/
@@ -2966,6 +2949,10 @@ export interface JavascriptParserOptions {
29662949
* Specifies the behavior of invalid export names in "import ... from ...".
29672950
*/
29682951
importExportsPresence?: "error" | "warn" | "auto" | false;
2952+
/**
2953+
* Enable/disable evaluating import.meta.
2954+
*/
2955+
importMeta?: boolean;
29692956
/**
29702957
* Include polyfills or mocks for various node stuff.
29712958
*/
@@ -3112,10 +3099,6 @@ export interface ModuleOptionsNormalized {
31123099
* Specify options for each generator.
31133100
*/
31143101
generator: GeneratorOptionsByModuleType;
3115-
/**
3116-
* Options object for es6 import.meta features.
3117-
*/
3118-
importMeta?: ImportMeta;
31193102
/**
31203103
* Don't parse files matching. It's matched against the full resolved request.
31213104
*/

lib/WebpackOptionsApply.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ class WebpackOptionsApply extends OptionsApply {
362362
new RequireContextPlugin().apply(compiler);
363363
new ImportPlugin().apply(compiler);
364364
new SystemPlugin().apply(compiler);
365-
new ImportMetaPlugin(options.module.importMeta).apply(compiler);
365+
new ImportMetaPlugin().apply(compiler);
366366
new URLPlugin().apply(compiler);
367367
new WorkerPlugin(
368368
options.output.workerChunkLoading,

lib/config/defaults.js

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ const {
2323
/** @typedef {import("../../declarations/WebpackOptions").ExperimentsNormalized} ExperimentsNormalized */
2424
/** @typedef {import("../../declarations/WebpackOptions").ExternalsPresets} ExternalsPresets */
2525
/** @typedef {import("../../declarations/WebpackOptions").ExternalsType} ExternalsType */
26-
/** @typedef {import("../../declarations/WebpackOptions").ImportMeta} ImportMeta */
2726
/** @typedef {import("../../declarations/WebpackOptions").InfrastructureLogging} InfrastructureLogging */
2827
/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
2928
/** @typedef {import("../../declarations/WebpackOptions").Library} Library */
@@ -496,16 +495,15 @@ const applyModuleDefaults = (
496495
D(module, "unsafeCache", false);
497496
}
498497

499-
D(module.parser, "importMeta", {});
500-
applyMetaDefaults(module.parser.importMeta);
501-
502498
F(module.parser, "asset", () => ({}));
503499
F(module.parser.asset, "dataUrlCondition", () => ({}));
504500
if (typeof module.parser.asset.dataUrlCondition === "object") {
505501
D(module.parser.asset.dataUrlCondition, "maxSize", 8096);
506502
}
507503

508-
F(module.parser, "javascript", () => ({}));
504+
F(module.parser, "javascript", () => ({
505+
importMeta: true
506+
}));
509507
applyJavascriptParserOptionsDefaults(module.parser.javascript, {
510508
futureDefaults
511509
});
@@ -1086,15 +1084,6 @@ const applyNodeDefaults = (node, { futureDefaults, targetProperties }) => {
10861084
});
10871085
};
10881086

1089-
/**
1090-
* @param {ImportMeta} meta options
1091-
* @returns {void}
1092-
*/
1093-
const applyMetaDefaults = meta => {
1094-
if (meta === false) return;
1095-
D(meta, "url", true);
1096-
};
1097-
10981087
/**
10991088
* @param {Performance} performance options
11001089
* @param {Object} options options

lib/config/normalization.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ const getNormalizedWebpackOptions = config => {
219219
module: nestedConfig(config.module, module => ({
220220
noParse: module.noParse,
221221
unsafeCache: module.unsafeCache,
222-
importMeta: nestedConfig(module.importMeta, importMeta => importMeta),
223222
parser: keyedNestedConfig(module.parser, cloneObject, {
224223
javascript: parserOptions => ({
225224
unknownContextRequest: module.unknownContextRequest,

lib/dependencies/ImportMetaPlugin.js

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const propertyAccess = require("../util/propertyAccess");
2020
const ConstDependency = require("./ConstDependency");
2121

2222
/** @typedef {import("estree").MemberExpression} MemberExpression */
23+
/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
2324
/** @typedef {import("../Compiler")} Compiler */
2425
/** @typedef {import("../NormalModule")} NormalModule */
2526
/** @typedef {import("../javascript/JavascriptParser")} Parser */
@@ -29,18 +30,10 @@ const getCriticalDependencyWarning = memoize(() =>
2930
);
3031

3132
class ImportMetaPlugin {
32-
/**
33-
* @param {import("../../declarations/WebpackOptions").ImportMeta} options options
34-
*/
35-
constructor(options) {
36-
this.options = options;
37-
}
38-
3933
/**
4034
* @param {Compiler} compiler compiler
4135
*/
4236
apply(compiler) {
43-
const options = this.options;
4437
compiler.hooks.compilation.tap(
4538
"ImportMetaPlugin",
4639
(compilation, { normalModuleFactory }) => {
@@ -52,12 +45,38 @@ class ImportMetaPlugin {
5245
return pathToFileURL(module.resource).toString();
5346
};
5447
/**
55-
* @param {Parser} parser parser
56-
* @param {Object} parserOptions parserOptions
48+
* @param {Parser} parser parser parser
49+
* @param {JavascriptParserOptions} parserOptions parserOptions
5750
* @returns {void}
5851
*/
59-
const parserHandler = (parser, parserOptions) => {
60-
if (options === false) return;
52+
const parserHandler = (parser, { importMeta }) => {
53+
if (importMeta === false) {
54+
const { importMetaName } = compilation.outputOptions;
55+
56+
parser.hooks.expression
57+
.for("import.meta")
58+
.tap("ImportMetaPlugin", metaProperty => {
59+
const dep = new ConstDependency(
60+
importMetaName,
61+
metaProperty.range
62+
);
63+
dep.loc = metaProperty.loc;
64+
parser.state.module.addPresentationalDependency(dep);
65+
return true;
66+
});
67+
parser.hooks.unhandledExpressionMemberChain
68+
.for("import.meta")
69+
.tap("ImportMetaPlugin", (expr, members) => {
70+
const dep = new ConstDependency(
71+
`${importMetaName}${propertyAccess(members, 0)}`,
72+
expr.range
73+
);
74+
dep.loc = expr.loc;
75+
parser.state.module.addPresentationalDependency(dep);
76+
return true;
77+
});
78+
return;
79+
}
6180

6281
/// import.meta direct ///
6382
parser.hooks.typeof
@@ -105,27 +124,25 @@ class ImportMetaPlugin {
105124
parser.hooks.expression
106125
.for("import.meta.url")
107126
.tap("ImportMetaPlugin", expr => {
108-
if (options.url) {
109-
const dep = new ConstDependency(
110-
JSON.stringify(getUrl(parser.state.module)),
111-
expr.range
112-
);
113-
dep.loc = expr.loc;
114-
parser.state.module.addPresentationalDependency(dep);
115-
}
127+
const dep = new ConstDependency(
128+
JSON.stringify(getUrl(parser.state.module)),
129+
expr.range
130+
);
131+
dep.loc = expr.loc;
132+
parser.state.module.addPresentationalDependency(dep);
116133
return true;
117134
});
118135
parser.hooks.evaluateTypeof
119136
.for("import.meta.url")
120137
.tap("ImportMetaPlugin", evaluateToString("string"));
121-
122138
parser.hooks.evaluateIdentifier
123139
.for("import.meta.url")
124140
.tap("ImportMetaPlugin", expr => {
125141
return new BasicEvaluatedExpression()
126142
.setString(getUrl(parser.state.module))
127143
.setRange(expr.range);
128144
});
145+
129146
/// import.meta.webpack ///
130147
const webpackVersion = parseInt(
131148
require("../../package.json").version,

schemas/WebpackOptions.check.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

schemas/WebpackOptions.json

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,32 +1505,10 @@
15051505
"description": "The name of the native import() function (can be exchanged for a polyfill).",
15061506
"type": "string"
15071507
},
1508-
"ImportMeta": {
1509-
"description": "Options object for es6 import.meta features.",
1510-
"anyOf": [
1511-
{
1512-
"enum": [false]
1513-
},
1514-
{
1515-
"$ref": "#/definitions/ImportMetaOptions"
1516-
}
1517-
]
1518-
},
15191508
"ImportMetaName": {
15201509
"description": "The name of the native import.meta object (can be exchanged for a polyfill).",
15211510
"type": "string"
15221511
},
1523-
"ImportMetaOptions": {
1524-
"description": "Options object for es6 import.meta features.",
1525-
"type": "object",
1526-
"additionalProperties": false,
1527-
"properties": {
1528-
"url": {
1529-
"description": "Include a polyfill for the 'import.meta.url' variable.",
1530-
"enum": [false, true]
1531-
}
1532-
}
1533-
},
15341512
"InfrastructureLogging": {
15351513
"description": "Options for infrastructure level logging.",
15361514
"type": "object",
@@ -1630,6 +1608,10 @@
16301608
"description": "Specifies the behavior of invalid export names in \"import ... from ...\".",
16311609
"enum": ["error", "warn", "auto", false]
16321610
},
1611+
"importMeta": {
1612+
"description": "Enable/disable evaluating import.meta.",
1613+
"type": "boolean"
1614+
},
16331615
"node": {
16341616
"$ref": "#/definitions/Node"
16351617
},
@@ -2125,9 +2107,6 @@
21252107
"generator": {
21262108
"$ref": "#/definitions/GeneratorOptionsByModuleType"
21272109
},
2128-
"importMeta": {
2129-
"$ref": "#/definitions/ImportMeta"
2130-
},
21312110
"noParse": {
21322111
"$ref": "#/definitions/NoParse"
21332112
},
@@ -2220,9 +2199,6 @@
22202199
"generator": {
22212200
"$ref": "#/definitions/GeneratorOptionsByModuleType"
22222201
},
2223-
"importMeta": {
2224-
"$ref": "#/definitions/ImportMeta"
2225-
},
22262202
"noParse": {
22272203
"$ref": "#/definitions/NoParse"
22282204
},

test/__snapshots__/Cli.basictest.js.snap

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,6 +1612,19 @@ Object {
16121612
"multiple": false,
16131613
"simpleType": "string",
16141614
},
1615+
"module-parser-javascript-auto-import-meta": Object {
1616+
"configs": Array [
1617+
Object {
1618+
"description": "Enable/disable evaluating import.meta.",
1619+
"multiple": false,
1620+
"path": "module.parser.javascript/auto.importMeta",
1621+
"type": "boolean",
1622+
},
1623+
],
1624+
"description": "Enable/disable evaluating import.meta.",
1625+
"multiple": false,
1626+
"simpleType": "boolean",
1627+
},
16151628
"module-parser-javascript-auto-node": Object {
16161629
"configs": Array [
16171630
Object {
@@ -2163,6 +2176,19 @@ Object {
21632176
"multiple": false,
21642177
"simpleType": "string",
21652178
},
2179+
"module-parser-javascript-dynamic-import-meta": Object {
2180+
"configs": Array [
2181+
Object {
2182+
"description": "Enable/disable evaluating import.meta.",
2183+
"multiple": false,
2184+
"path": "module.parser.javascript/dynamic.importMeta",
2185+
"type": "boolean",
2186+
},
2187+
],
2188+
"description": "Enable/disable evaluating import.meta.",
2189+
"multiple": false,
2190+
"simpleType": "boolean",
2191+
},
21662192
"module-parser-javascript-dynamic-node": Object {
21672193
"configs": Array [
21682194
Object {
@@ -2675,6 +2701,19 @@ Object {
26752701
"multiple": false,
26762702
"simpleType": "string",
26772703
},
2704+
"module-parser-javascript-esm-import-meta": Object {
2705+
"configs": Array [
2706+
Object {
2707+
"description": "Enable/disable evaluating import.meta.",
2708+
"multiple": false,
2709+
"path": "module.parser.javascript/esm.importMeta",
2710+
"type": "boolean",
2711+
},
2712+
],
2713+
"description": "Enable/disable evaluating import.meta.",
2714+
"multiple": false,
2715+
"simpleType": "boolean",
2716+
},
26782717
"module-parser-javascript-esm-node": Object {
26792718
"configs": Array [
26802719
Object {
@@ -3132,6 +3171,19 @@ Object {
31323171
"multiple": false,
31333172
"simpleType": "string",
31343173
},
3174+
"module-parser-javascript-import-meta": Object {
3175+
"configs": Array [
3176+
Object {
3177+
"description": "Enable/disable evaluating import.meta.",
3178+
"multiple": false,
3179+
"path": "module.parser.javascript.importMeta",
3180+
"type": "boolean",
3181+
},
3182+
],
3183+
"description": "Enable/disable evaluating import.meta.",
3184+
"multiple": false,
3185+
"simpleType": "boolean",
3186+
},
31353187
"module-parser-javascript-node": Object {
31363188
"configs": Array [
31373189
Object {

test/configCases/module/externals/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ it("should allow to use externals in concatenated modules", () => {
88
expect(value).toBe(40);
99
});
1010

11-
it("all bundled files should have same url, when module.importMeta.url === false", () => {
11+
it("all bundled files should have same url, when parser.javascript.importMeta === false", () => {
1212
expect(localMetaUrl).toBe(metaUrl)
1313
});

test/configCases/module/externals/webpack.config.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
/** @type {import("../../../../").Configuration} */
22
module.exports = {
33
module: {
4-
importMeta: {
5-
url: false
4+
parser: {
5+
javascript: {
6+
importMeta: false
7+
}
68
}
79
},
810
entry: {

0 commit comments

Comments
 (0)