Skip to content

Commit 73bb431

Browse files
committed
- add new config section importMeta
- use it in ImportMetaPlugin to switch it off as necessary
1 parent 4abe329 commit 73bb431

File tree

11 files changed

+117
-9
lines changed

11 files changed

+117
-9
lines changed

declarations/WebpackOptions.d.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,10 @@ 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;
354358
/**
355359
* Don't parse files matching. It's matched against the full resolved request.
356360
*/
@@ -1242,6 +1246,10 @@ export interface ModuleOptions {
12421246
* Specify options for each generator.
12431247
*/
12441248
generator?: GeneratorOptionsByModuleType;
1249+
/**
1250+
* Options object for es6 import.meta features.
1251+
*/
1252+
importMeta?: ImportMeta;
12451253
/**
12461254
* Don't parse files matching. It's matched against the full resolved request.
12471255
*/
@@ -1584,6 +1592,15 @@ export interface ResolvePluginInstance {
15841592
apply: (resolver: import("enhanced-resolve").Resolver) => void;
15851593
[k: string]: any;
15861594
}
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+
}
15871604
/**
15881605
* Options object for node compatibility features.
15891606
*/
@@ -3095,6 +3112,10 @@ export interface ModuleOptionsNormalized {
30953112
* Specify options for each generator.
30963113
*/
30973114
generator: GeneratorOptionsByModuleType;
3115+
/**
3116+
* Options object for es6 import.meta features.
3117+
*/
3118+
importMeta?: ImportMeta;
30983119
/**
30993120
* Don't parse files matching. It's matched against the full resolved request.
31003121
*/

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().apply(compiler);
365+
new ImportMetaPlugin(options.module.importMeta).apply(compiler);
366366
new URLPlugin().apply(compiler);
367367
new WorkerPlugin(
368368
options.output.workerChunkLoading,

lib/config/defaults.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ 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 */
2627
/** @typedef {import("../../declarations/WebpackOptions").InfrastructureLogging} InfrastructureLogging */
2728
/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
2829
/** @typedef {import("../../declarations/WebpackOptions").Library} Library */
@@ -495,6 +496,9 @@ const applyModuleDefaults = (
495496
D(module, "unsafeCache", false);
496497
}
497498

499+
D(module.parser, "importMeta", {});
500+
applyMetaDefaults(module.parser.importMeta);
501+
498502
F(module.parser, "asset", () => ({}));
499503
F(module.parser.asset, "dataUrlCondition", () => ({}));
500504
if (typeof module.parser.asset.dataUrlCondition === "object") {
@@ -1082,6 +1086,15 @@ const applyNodeDefaults = (node, { futureDefaults, targetProperties }) => {
10821086
});
10831087
};
10841088

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+
10851098
/**
10861099
* @param {Performance} performance options
10871100
* @param {Object} options options

lib/config/normalization.js

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

lib/dependencies/ImportMetaPlugin.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,18 @@ const getCriticalDependencyWarning = memoize(() =>
2929
);
3030

3131
class ImportMetaPlugin {
32+
/**
33+
* @param {import("../../declarations/WebpackOptions").ImportMeta} options options
34+
*/
35+
constructor(options) {
36+
this.options = options;
37+
}
38+
3239
/**
3340
* @param {Compiler} compiler compiler
3441
*/
3542
apply(compiler) {
43+
const options = this.options;
3644
compiler.hooks.compilation.tap(
3745
"ImportMetaPlugin",
3846
(compilation, { normalModuleFactory }) => {
@@ -49,6 +57,8 @@ class ImportMetaPlugin {
4957
* @returns {void}
5058
*/
5159
const parserHandler = (parser, parserOptions) => {
60+
if (options === false) return;
61+
5262
/// import.meta direct ///
5363
parser.hooks.typeof
5464
.for("import.meta")
@@ -106,14 +116,16 @@ class ImportMetaPlugin {
106116
parser.hooks.evaluateTypeof
107117
.for("import.meta.url")
108118
.tap("ImportMetaPlugin", evaluateToString("string"));
109-
parser.hooks.evaluateIdentifier
110-
.for("import.meta.url")
111-
.tap("ImportMetaPlugin", expr => {
112-
return new BasicEvaluatedExpression()
113-
.setString(getUrl(parser.state.module))
114-
.setRange(expr.range);
115-
});
116119

120+
if (options.url) {
121+
parser.hooks.evaluateIdentifier
122+
.for("import.meta.url")
123+
.tap("ImportMetaPlugin", expr => {
124+
return new BasicEvaluatedExpression()
125+
.setString(getUrl(parser.state.module))
126+
.setRange(expr.range);
127+
});
128+
}
117129
/// import.meta.webpack ///
118130
const webpackVersion = parseInt(
119131
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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,10 +1505,32 @@
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+
},
15081519
"ImportMetaName": {
15091520
"description": "The name of the native import.meta object (can be exchanged for a polyfill).",
15101521
"type": "string"
15111522
},
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+
},
15121534
"InfrastructureLogging": {
15131535
"description": "Options for infrastructure level logging.",
15141536
"type": "object",
@@ -2103,6 +2125,9 @@
21032125
"generator": {
21042126
"$ref": "#/definitions/GeneratorOptionsByModuleType"
21052127
},
2128+
"importMeta": {
2129+
"$ref": "#/definitions/ImportMeta"
2130+
},
21062131
"noParse": {
21072132
"$ref": "#/definitions/NoParse"
21082133
},
@@ -2195,6 +2220,9 @@
21952220
"generator": {
21962221
"$ref": "#/definitions/GeneratorOptionsByModuleType"
21972222
},
2223+
"importMeta": {
2224+
"$ref": "#/definitions/ImportMeta"
2225+
},
21982226
"noParse": {
21992227
"$ref": "#/definitions/NoParse"
22002228
},
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import imported from "./imported.mjs";
22
import value from "./module";
3+
import { metaUrl } from "./meta";
34

45
it("should allow to use externals in concatenated modules", () => {
56
expect(imported).toBe(42);
67
expect(value).toBe(40);
78
});
9+
10+
it("all bundled files should have same url, when module.importMeta.url === false", () => {
11+
export const localMetaUrl = import.meta.url;
12+
expect(localMetaUrl).toBe(metaUrl)
13+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const metaUrl = import.meta.url;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
/** @type {import("../../../../").Configuration} */
22
module.exports = {
3+
module: {
4+
importMeta: {
5+
url: false
6+
}
7+
},
38
entry: {
49
main: "./index.js",
510
imported: {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy