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 e1f4ad6
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 107 deletions.
37 changes: 13 additions & 24 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 @@ -2914,6 +2897,15 @@ export interface HttpUriOptions {
*/
upgrade?: boolean;
}
/**
* Options object for es6 import.meta features.
*/
export interface ImportMetaOptions {
/**
* Include a polyfill for the 'import.meta.url' variable.
*/
url?: false | true;
}
/**
* Parser options for javascript modules.
*/
Expand All @@ -2926,9 +2918,6 @@ export interface JavascriptParserOptions {
* Enable/disable special handling for browserify bundles.
*/
browserify?: boolean;
/**
* Enable/disable parsing of CommonJs syntax.
*/
commonjs?: boolean;
/**
* Enable/disable parsing of magic comments in CommonJs syntax.
Expand Down Expand Up @@ -2966,6 +2955,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 +3105,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
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 @@ -23,24 +23,17 @@ const ConstDependency = require("./ConstDependency");
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../NormalModule")} NormalModule */
/** @typedef {import("../javascript/JavascriptParser")} Parser */
/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */

const getCriticalDependencyWarning = memoize(() =>
require("./CriticalDependencyWarning")
);

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.

22 changes: 4 additions & 18 deletions schemas/WebpackOptions.json
Expand Up @@ -1505,17 +1505,6 @@
"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"
Expand Down Expand Up @@ -1583,7 +1572,6 @@
"type": "boolean"
},
"commonjs": {
"description": "Enable/disable parsing of CommonJs syntax.",
"type": "boolean"
},
"commonjsMagicComments": {
Expand Down Expand Up @@ -1630,6 +1618,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 +2117,6 @@
"generator": {
"$ref": "#/definitions/GeneratorOptionsByModuleType"
},
"importMeta": {
"$ref": "#/definitions/ImportMeta"
},
"noParse": {
"$ref": "#/definitions/NoParse"
},
Expand Down Expand Up @@ -2220,9 +2209,6 @@
"generator": {
"$ref": "#/definitions/GeneratorOptionsByModuleType"
},
"importMeta": {
"$ref": "#/definitions/ImportMeta"
},
"noParse": {
"$ref": "#/definitions/NoParse"
},
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
30 changes: 5 additions & 25 deletions types.d.ts
Expand Up @@ -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
Expand Down Expand Up @@ -5431,10 +5421,6 @@ declare interface JavascriptParserOptions {
* Enable/disable special handling for browserify bundles.
*/
browserify?: boolean;

/**
* Enable/disable parsing of CommonJs syntax.
*/
commonjs?: boolean;

/**
Expand Down Expand Up @@ -5482,6 +5468,11 @@ declare interface JavascriptParserOptions {
*/
importExportsPresence?: false | "auto" | "error" | "warn";

/**
* Enable/disable evaluating import.meta.
*/
importMeta?: boolean;

/**
* Include polyfills or mocks for various node stuff.
*/
Expand Down Expand Up @@ -6402,7 +6393,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",
Expand Down Expand Up @@ -7064,11 +7054,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.
*/
Expand Down Expand Up @@ -7149,11 +7134,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.
*/
Expand Down

0 comments on commit e1f4ad6

Please sign in to comment.