Skip to content

Commit

Permalink
- add new config section importMeta
Browse files Browse the repository at this point in the history
- use it in ImportMetaPlugin to switch it off as necessary
  • Loading branch information
pavelsavara committed Jan 25, 2022
1 parent 4abe329 commit 73bb431
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 9 deletions.
21 changes: 21 additions & 0 deletions declarations/WebpackOptions.d.ts
Expand Up @@ -351,6 +351,10 @@ 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 @@ -1242,6 +1246,10 @@ 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 @@ -1584,6 +1592,15 @@ 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 @@ -3095,6 +3112,10 @@ 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().apply(compiler);
new ImportMetaPlugin(options.module.importMeta).apply(compiler);
new URLPlugin().apply(compiler);
new WorkerPlugin(
options.output.workerChunkLoading,
Expand Down
13 changes: 13 additions & 0 deletions lib/config/defaults.js
Expand Up @@ -23,6 +23,7 @@ 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 @@ -495,6 +496,9 @@ 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") {
Expand Down Expand Up @@ -1082,6 +1086,15 @@ 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: 1 addition & 0 deletions lib/config/normalization.js
Expand Up @@ -219,6 +219,7 @@ 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
26 changes: 19 additions & 7 deletions lib/dependencies/ImportMetaPlugin.js
Expand Up @@ -29,10 +29,18 @@ 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 @@ -49,6 +57,8 @@ class ImportMetaPlugin {
* @returns {void}
*/
const parserHandler = (parser, parserOptions) => {
if (options === false) return;

/// import.meta direct ///
parser.hooks.typeof
.for("import.meta")
Expand Down Expand Up @@ -106,14 +116,16 @@ class ImportMetaPlugin {
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);
});

if (options.url) {
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.

28 changes: 28 additions & 0 deletions schemas/WebpackOptions.json
Expand Up @@ -1505,10 +1505,32 @@
"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 @@ -2103,6 +2125,9 @@
"generator": {
"$ref": "#/definitions/GeneratorOptionsByModuleType"
},
"importMeta": {
"$ref": "#/definitions/ImportMeta"
},
"noParse": {
"$ref": "#/definitions/NoParse"
},
Expand Down Expand Up @@ -2195,6 +2220,9 @@
"generator": {
"$ref": "#/definitions/GeneratorOptionsByModuleType"
},
"importMeta": {
"$ref": "#/definitions/ImportMeta"
},
"noParse": {
"$ref": "#/definitions/NoParse"
},
Expand Down
6 changes: 6 additions & 0 deletions test/configCases/module/externals/index.js
@@ -1,7 +1,13 @@
import imported from "./imported.mjs";
import value from "./module";
import { metaUrl } from "./meta";

it("should allow to use externals in concatenated modules", () => {
expect(imported).toBe(42);
expect(value).toBe(40);
});

it("all bundled files should have same url, when module.importMeta.url === false", () => {
export const localMetaUrl = import.meta.url;
expect(localMetaUrl).toBe(metaUrl)
});
1 change: 1 addition & 0 deletions test/configCases/module/externals/meta.js
@@ -0,0 +1 @@
export const metaUrl = import.meta.url;
5 changes: 5 additions & 0 deletions test/configCases/module/externals/webpack.config.js
@@ -1,5 +1,10 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
module: {
importMeta: {
url: false
}
},
entry: {
main: "./index.js",
imported: {
Expand Down
21 changes: 21 additions & 0 deletions types.d.ts
Expand Up @@ -4628,6 +4628,16 @@ 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 @@ -6392,6 +6402,7 @@ 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 @@ -7053,6 +7064,11 @@ 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 @@ -7133,6 +7149,11 @@ 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 73bb431

Please sign in to comment.