add importMeta to JavascriptParserOptions:

- enable/disable import.meta parsing
- when disabled insert output.importMetaName
This commit is contained in:
Ivan Kopeykin 2022-01-27 22:27:10 +03:00
parent 332fb11067
commit c09e82fcfe
11 changed files with 113 additions and 111 deletions

View File

@ -351,10 +351,6 @@ export type RuleSetRules = ("..." | RuleSetRule)[];
*/
export type GeneratorOptionsByModuleType = GeneratorOptionsByModuleTypeKnown &
GeneratorOptionsByModuleTypeUnknown;
/**
* Options object for es6 import.meta features.
*/
export type ImportMeta = false | ImportMetaOptions;
/**
* Don't parse files matching. It's matched against the full resolved request.
*/
@ -1246,10 +1242,6 @@ export interface ModuleOptions {
* Specify options for each generator.
*/
generator?: GeneratorOptionsByModuleType;
/**
* Options object for es6 import.meta features.
*/
importMeta?: ImportMeta;
/**
* Don't parse files matching. It's matched against the full resolved request.
*/
@ -1592,15 +1584,6 @@ export interface ResolvePluginInstance {
apply: (resolver: import("enhanced-resolve").Resolver) => void;
[k: string]: any;
}
/**
* Options object for es6 import.meta features.
*/
export interface ImportMetaOptions {
/**
* Include a polyfill for the 'import.meta.url' variable.
*/
url?: false | true;
}
/**
* Options object for node compatibility features.
*/
@ -2966,6 +2949,10 @@ export interface JavascriptParserOptions {
* Specifies the behavior of invalid export names in "import ... from ...".
*/
importExportsPresence?: "error" | "warn" | "auto" | false;
/**
* Enable/disable evaluating import.meta.
*/
importMeta?: boolean;
/**
* Include polyfills or mocks for various node stuff.
*/
@ -3112,10 +3099,6 @@ export interface ModuleOptionsNormalized {
* Specify options for each generator.
*/
generator: GeneratorOptionsByModuleType;
/**
* Options object for es6 import.meta features.
*/
importMeta?: ImportMeta;
/**
* Don't parse files matching. It's matched against the full resolved request.
*/

View File

@ -362,7 +362,7 @@ class WebpackOptionsApply extends OptionsApply {
new RequireContextPlugin().apply(compiler);
new ImportPlugin().apply(compiler);
new SystemPlugin().apply(compiler);
new ImportMetaPlugin(options.module.importMeta).apply(compiler);
new ImportMetaPlugin().apply(compiler);
new URLPlugin().apply(compiler);
new WorkerPlugin(
options.output.workerChunkLoading,

View File

@ -23,7 +23,6 @@ const {
/** @typedef {import("../../declarations/WebpackOptions").ExperimentsNormalized} ExperimentsNormalized */
/** @typedef {import("../../declarations/WebpackOptions").ExternalsPresets} ExternalsPresets */
/** @typedef {import("../../declarations/WebpackOptions").ExternalsType} ExternalsType */
/** @typedef {import("../../declarations/WebpackOptions").ImportMeta} ImportMeta */
/** @typedef {import("../../declarations/WebpackOptions").InfrastructureLogging} InfrastructureLogging */
/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
/** @typedef {import("../../declarations/WebpackOptions").Library} Library */
@ -496,16 +495,15 @@ const applyModuleDefaults = (
D(module, "unsafeCache", false);
}
D(module.parser, "importMeta", {});
applyMetaDefaults(module.parser.importMeta);
F(module.parser, "asset", () => ({}));
F(module.parser.asset, "dataUrlCondition", () => ({}));
if (typeof module.parser.asset.dataUrlCondition === "object") {
D(module.parser.asset.dataUrlCondition, "maxSize", 8096);
}
F(module.parser, "javascript", () => ({}));
F(module.parser, "javascript", () => ({
importMeta: true
}));
applyJavascriptParserOptionsDefaults(module.parser.javascript, {
futureDefaults
});
@ -1086,15 +1084,6 @@ const applyNodeDefaults = (node, { futureDefaults, targetProperties }) => {
});
};
/**
* @param {ImportMeta} meta options
* @returns {void}
*/
const applyMetaDefaults = meta => {
if (meta === false) return;
D(meta, "url", true);
};
/**
* @param {Performance} performance options
* @param {Object} options options

View File

@ -219,7 +219,6 @@ const getNormalizedWebpackOptions = config => {
module: nestedConfig(config.module, module => ({
noParse: module.noParse,
unsafeCache: module.unsafeCache,
importMeta: nestedConfig(module.importMeta, importMeta => importMeta),
parser: keyedNestedConfig(module.parser, cloneObject, {
javascript: parserOptions => ({
unknownContextRequest: module.unknownContextRequest,

View File

@ -20,6 +20,7 @@ const propertyAccess = require("../util/propertyAccess");
const ConstDependency = require("./ConstDependency");
/** @typedef {import("estree").MemberExpression} MemberExpression */
/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../NormalModule")} NormalModule */
/** @typedef {import("../javascript/JavascriptParser")} Parser */
@ -29,18 +30,10 @@ const getCriticalDependencyWarning = memoize(() =>
);
class ImportMetaPlugin {
/**
* @param {import("../../declarations/WebpackOptions").ImportMeta} options options
*/
constructor(options) {
this.options = options;
}
/**
* @param {Compiler} compiler compiler
*/
apply(compiler) {
const options = this.options;
compiler.hooks.compilation.tap(
"ImportMetaPlugin",
(compilation, { normalModuleFactory }) => {
@ -52,12 +45,38 @@ class ImportMetaPlugin {
return pathToFileURL(module.resource).toString();
};
/**
* @param {Parser} parser parser
* @param {Object} parserOptions parserOptions
* @param {Parser} parser parser parser
* @param {JavascriptParserOptions} parserOptions parserOptions
* @returns {void}
*/
const parserHandler = (parser, parserOptions) => {
if (options === false) return;
const parserHandler = (parser, { importMeta }) => {
if (importMeta === false) {
const { importMetaName } = compilation.outputOptions;
parser.hooks.expression
.for("import.meta")
.tap("ImportMetaPlugin", metaProperty => {
const dep = new ConstDependency(
importMetaName,
metaProperty.range
);
dep.loc = metaProperty.loc;
parser.state.module.addPresentationalDependency(dep);
return true;
});
parser.hooks.unhandledExpressionMemberChain
.for("import.meta")
.tap("ImportMetaPlugin", (expr, members) => {
const dep = new ConstDependency(
`${importMetaName}${propertyAccess(members, 0)}`,
expr.range
);
dep.loc = expr.loc;
parser.state.module.addPresentationalDependency(dep);
return true;
});
return;
}
/// import.meta direct ///
parser.hooks.typeof
@ -105,20 +124,17 @@ class ImportMetaPlugin {
parser.hooks.expression
.for("import.meta.url")
.tap("ImportMetaPlugin", expr => {
if (options.url) {
const dep = new ConstDependency(
JSON.stringify(getUrl(parser.state.module)),
expr.range
);
dep.loc = expr.loc;
parser.state.module.addPresentationalDependency(dep);
}
const dep = new ConstDependency(
JSON.stringify(getUrl(parser.state.module)),
expr.range
);
dep.loc = expr.loc;
parser.state.module.addPresentationalDependency(dep);
return true;
});
parser.hooks.evaluateTypeof
.for("import.meta.url")
.tap("ImportMetaPlugin", evaluateToString("string"));
parser.hooks.evaluateIdentifier
.for("import.meta.url")
.tap("ImportMetaPlugin", expr => {
@ -126,6 +142,7 @@ class ImportMetaPlugin {
.setString(getUrl(parser.state.module))
.setRange(expr.range);
});
/// import.meta.webpack ///
const webpackVersion = parseInt(
require("../../package.json").version,

File diff suppressed because one or more lines are too long

View File

@ -1505,32 +1505,10 @@
"description": "The name of the native import() function (can be exchanged for a polyfill).",
"type": "string"
},
"ImportMeta": {
"description": "Options object for es6 import.meta features.",
"anyOf": [
{
"enum": [false]
},
{
"$ref": "#/definitions/ImportMetaOptions"
}
]
},
"ImportMetaName": {
"description": "The name of the native import.meta object (can be exchanged for a polyfill).",
"type": "string"
},
"ImportMetaOptions": {
"description": "Options object for es6 import.meta features.",
"type": "object",
"additionalProperties": false,
"properties": {
"url": {
"description": "Include a polyfill for the 'import.meta.url' variable.",
"enum": [false, true]
}
}
},
"InfrastructureLogging": {
"description": "Options for infrastructure level logging.",
"type": "object",
@ -1630,6 +1608,10 @@
"description": "Specifies the behavior of invalid export names in \"import ... from ...\".",
"enum": ["error", "warn", "auto", false]
},
"importMeta": {
"description": "Enable/disable evaluating import.meta.",
"type": "boolean"
},
"node": {
"$ref": "#/definitions/Node"
},
@ -2125,9 +2107,6 @@
"generator": {
"$ref": "#/definitions/GeneratorOptionsByModuleType"
},
"importMeta": {
"$ref": "#/definitions/ImportMeta"
},
"noParse": {
"$ref": "#/definitions/NoParse"
},
@ -2220,9 +2199,6 @@
"generator": {
"$ref": "#/definitions/GeneratorOptionsByModuleType"
},
"importMeta": {
"$ref": "#/definitions/ImportMeta"
},
"noParse": {
"$ref": "#/definitions/NoParse"
},

View File

@ -1612,6 +1612,19 @@ Object {
"multiple": false,
"simpleType": "string",
},
"module-parser-javascript-auto-import-meta": Object {
"configs": Array [
Object {
"description": "Enable/disable evaluating import.meta.",
"multiple": false,
"path": "module.parser.javascript/auto.importMeta",
"type": "boolean",
},
],
"description": "Enable/disable evaluating import.meta.",
"multiple": false,
"simpleType": "boolean",
},
"module-parser-javascript-auto-node": Object {
"configs": Array [
Object {
@ -2163,6 +2176,19 @@ Object {
"multiple": false,
"simpleType": "string",
},
"module-parser-javascript-dynamic-import-meta": Object {
"configs": Array [
Object {
"description": "Enable/disable evaluating import.meta.",
"multiple": false,
"path": "module.parser.javascript/dynamic.importMeta",
"type": "boolean",
},
],
"description": "Enable/disable evaluating import.meta.",
"multiple": false,
"simpleType": "boolean",
},
"module-parser-javascript-dynamic-node": Object {
"configs": Array [
Object {
@ -2675,6 +2701,19 @@ Object {
"multiple": false,
"simpleType": "string",
},
"module-parser-javascript-esm-import-meta": Object {
"configs": Array [
Object {
"description": "Enable/disable evaluating import.meta.",
"multiple": false,
"path": "module.parser.javascript/esm.importMeta",
"type": "boolean",
},
],
"description": "Enable/disable evaluating import.meta.",
"multiple": false,
"simpleType": "boolean",
},
"module-parser-javascript-esm-node": Object {
"configs": Array [
Object {
@ -3132,6 +3171,19 @@ Object {
"multiple": false,
"simpleType": "string",
},
"module-parser-javascript-import-meta": Object {
"configs": Array [
Object {
"description": "Enable/disable evaluating import.meta.",
"multiple": false,
"path": "module.parser.javascript.importMeta",
"type": "boolean",
},
],
"description": "Enable/disable evaluating import.meta.",
"multiple": false,
"simpleType": "boolean",
},
"module-parser-javascript-node": Object {
"configs": Array [
Object {

View File

@ -8,6 +8,6 @@ it("should allow to use externals in concatenated modules", () => {
expect(value).toBe(40);
});
it("all bundled files should have same url, when module.importMeta.url === false", () => {
it("all bundled files should have same url, when parser.javascript.importMeta === false", () => {
expect(localMetaUrl).toBe(metaUrl)
});

View File

@ -1,8 +1,10 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
module: {
importMeta: {
url: false
parser: {
javascript: {
importMeta: false
}
}
},
entry: {

26
types.d.ts vendored
View File

@ -4628,16 +4628,6 @@ type IgnorePluginOptions =
*/
checkResource: (resource: string, context: string) => boolean;
};
/**
* Options object for es6 import.meta features.
*/
declare interface ImportMetaOptions {
/**
* Include a polyfill for the 'import.meta.url' variable.
*/
url?: boolean;
}
declare interface ImportModuleOptions {
/**
* the target layer
@ -5482,6 +5472,11 @@ declare interface JavascriptParserOptions {
*/
importExportsPresence?: false | "auto" | "error" | "warn";
/**
* Enable/disable evaluating import.meta.
*/
importMeta?: boolean;
/**
* Include polyfills or mocks for various node stuff.
*/
@ -6402,7 +6397,6 @@ declare interface LoaderRunnerLoaderContext<OptionsType> {
/**
* An array of all the loaders. It is writeable in the pitch phase.
* loaders = [{request: string, path: string, query: string, module: function}]
*
* In the example:
* [
* { request: "/abc/loader1.js?xyz",
@ -7064,11 +7058,6 @@ declare interface ModuleOptions {
*/
generator?: GeneratorOptionsByModuleType;
/**
* Options object for es6 import.meta features.
*/
importMeta?: false | ImportMetaOptions;
/**
* Don't parse files matching. It's matched against the full resolved request.
*/
@ -7149,11 +7138,6 @@ declare interface ModuleOptionsNormalized {
*/
generator: GeneratorOptionsByModuleType;
/**
* Options object for es6 import.meta features.
*/
importMeta?: false | ImportMetaOptions;
/**
* Don't parse files matching. It's matched against the full resolved request.
*/