Skip to content

Commit

Permalink
add importMeta to JavascriptParserOptions:
Browse files Browse the repository at this point in the history
- enable/disable import.meta parsing
- when disabled insert output.importMetaName
  • Loading branch information
vankop committed Jan 27, 2022
1 parent 332fb11 commit c09e82f
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 111 deletions.
25 changes: 4 additions & 21 deletions declarations/WebpackOptions.d.ts
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/WebpackOptionsApply.js
Expand Up @@ -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,
Expand Down
17 changes: 3 additions & 14 deletions lib/config/defaults.js
Expand Up @@ -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 */
Expand Down Expand Up @@ -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
});
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion lib/config/normalization.js
Expand Up @@ -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,
Expand Down
59 changes: 38 additions & 21 deletions lib/dependencies/ImportMetaPlugin.js
Expand Up @@ -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 */
Expand All @@ -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 }) => {
Expand All @@ -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
Expand Down Expand Up @@ -105,27 +124,25 @@ 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 => {
return new BasicEvaluatedExpression()
.setString(getUrl(parser.state.module))
.setRange(expr.range);
});

/// import.meta.webpack ///
const webpackVersion = parseInt(
require("../../package.json").version,
Expand Down
2 changes: 1 addition & 1 deletion schemas/WebpackOptions.check.js

Large diffs are not rendered by default.

32 changes: 4 additions & 28 deletions schemas/WebpackOptions.json
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
},
Expand Down Expand Up @@ -2125,9 +2107,6 @@
"generator": {
"$ref": "#/definitions/GeneratorOptionsByModuleType"
},
"importMeta": {
"$ref": "#/definitions/ImportMeta"
},
"noParse": {
"$ref": "#/definitions/NoParse"
},
Expand Down Expand Up @@ -2220,9 +2199,6 @@
"generator": {
"$ref": "#/definitions/GeneratorOptionsByModuleType"
},
"importMeta": {
"$ref": "#/definitions/ImportMeta"
},
"noParse": {
"$ref": "#/definitions/NoParse"
},
Expand Down
52 changes: 52 additions & 0 deletions test/__snapshots__/Cli.basictest.js.snap
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion test/configCases/module/externals/index.js
Expand Up @@ -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)
});
6 changes: 4 additions & 2 deletions test/configCases/module/externals/webpack.config.js
@@ -1,8 +1,10 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
module: {
importMeta: {
url: false
parser: {
javascript: {
importMeta: false
}
}
},
entry: {
Expand Down

0 comments on commit c09e82f

Please sign in to comment.