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 b55db4f
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 9 deletions.
25 changes: 25 additions & 0 deletions declarations/WebpackOptions.d.ts
Expand Up @@ -225,6 +225,10 @@ export type IgnoreWarnings = (
compilation: import("../lib/Compilation")
) => boolean)
)[];
/**
* Options object for es6 import.meta features.
*/
export type ImportMeta = false | ImportMetaOptions;
/**
* Filtering values.
*/
Expand Down Expand Up @@ -827,6 +831,10 @@ export interface WebpackOptions {
* Ignore specific warnings.
*/
ignoreWarnings?: IgnoreWarnings;
/**
* Options object for es6 import.meta features.
*/
importMeta?: ImportMeta;
/**
* Options for infrastructure level logging.
*/
Expand Down Expand Up @@ -1179,6 +1187,15 @@ export interface ExternalsPresets {
*/
webAsync?: boolean;
}
/**
* Options object for es6 import.meta features.
*/
export interface ImportMetaOptions {
/**
* Include a polyfill for the 'import.meta.url' variable.
*/
url?: false | true;
}
/**
* Options for infrastructure level logging.
*/
Expand Down Expand Up @@ -2949,6 +2966,10 @@ export interface JavascriptParserOptions {
* Specifies the behavior of invalid export names in "import ... from ...".
*/
importExportsPresence?: "error" | "warn" | "auto" | false;
/**
* Options object for es6 import.meta features.
*/
importMeta?: ImportMeta;
/**
* Include polyfills or mocks for various node stuff.
*/
Expand Down Expand Up @@ -3365,6 +3386,10 @@ export interface WebpackOptionsNormalized {
* Ignore specific warnings.
*/
ignoreWarnings?: IgnoreWarningsNormalized;
/**
* Options object for es6 import.meta features.
*/
importMeta?: ImportMeta;
/**
* Options for infrastructure level logging.
*/
Expand Down
4 changes: 3 additions & 1 deletion lib/WebpackOptionsApply.js
Expand Up @@ -362,7 +362,9 @@ class WebpackOptionsApply extends OptionsApply {
new RequireContextPlugin().apply(compiler);
new ImportPlugin().apply(compiler);
new SystemPlugin().apply(compiler);
new ImportMetaPlugin().apply(compiler);
if (options.importMeta !== false) {
new ImportMetaPlugin().apply(compiler);
}
new URLPlugin().apply(compiler);
new WorkerPlugin(
options.output.workerChunkLoading,
Expand Down
14 changes: 14 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 @@ -157,6 +158,7 @@ const applyWebpackOptionsDefaults = options => {
F(options, "devtool", () => (development ? "eval" : false));
D(options, "watch", false);
D(options, "profile", false);
D(options, "importMeta", {});
D(options, "parallelism", 100);
D(options, "recordsInputPath", false);
D(options, "recordsOutputPath", false);
Expand Down Expand Up @@ -231,6 +233,8 @@ const applyWebpackOptionsDefaults = options => {
targetProperties
});

applyMetaDefaults(options.importMeta);

F(options, "performance", () =>
production &&
targetProperties &&
Expand Down Expand Up @@ -1082,6 +1086,16 @@ 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 @@ -243,6 +243,7 @@ const getNormalizedWebpackOptions = config => {
rules: nestedArray(module.rules, r => [...r])
})),
name: config.name,
importMeta: nestedConfig(config.importMeta, importMeta => importMeta),
node: nestedConfig(
config.node,
node =>
Expand Down
28 changes: 21 additions & 7 deletions lib/dependencies/ImportMetaPlugin.js
Expand Up @@ -29,10 +29,15 @@ const getCriticalDependencyWarning = memoize(() =>
);

class ImportMetaPlugin {
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 +54,13 @@ class ImportMetaPlugin {
* @returns {void}
*/
const parserHandler = (parser, parserOptions) => {
if (parserOptions.importMeta === false) return;

let localOptions = options;
if (parserOptions.module) {
localOptions = { ...localOptions, ...parserOptions.importMeta };
}

/// import.meta direct ///
parser.hooks.typeof
.for("import.meta")
Expand Down Expand Up @@ -106,14 +118,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 (localOptions.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.

31 changes: 31 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 @@ -1608,6 +1630,9 @@
"description": "Specifies the behavior of invalid export names in \"import ... from ...\".",
"enum": ["error", "warn", "auto", false]
},
"importMeta": {
"$ref": "#/definitions/ImportMeta"
},
"node": {
"$ref": "#/definitions/Node"
},
Expand Down Expand Up @@ -5085,6 +5110,9 @@
"ignoreWarnings": {
"$ref": "#/definitions/IgnoreWarningsNormalized"
},
"importMeta": {
"$ref": "#/definitions/ImportMeta"
},
"infrastructureLogging": {
"$ref": "#/definitions/InfrastructureLogging"
},
Expand Down Expand Up @@ -5231,6 +5259,9 @@
"ignoreWarnings": {
"$ref": "#/definitions/IgnoreWarnings"
},
"importMeta": {
"$ref": "#/definitions/ImportMeta"
},
"infrastructureLogging": {
"$ref": "#/definitions/InfrastructureLogging"
},
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 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;
3 changes: 3 additions & 0 deletions test/configCases/module/externals/webpack.config.js
Expand Up @@ -13,6 +13,9 @@ module.exports = {
output: {
filename: "[name].mjs"
},
importMeta: {
url: false
},
externals: "./imported.mjs",
experiments: {
outputModule: true
Expand Down
26 changes: 26 additions & 0 deletions types.d.ts
Expand Up @@ -2157,6 +2157,11 @@ declare interface Configuration {
| ((warning: WebpackError, compilation: Compilation) => boolean)
)[];

/**
* Options object for es6 import.meta features.
*/
importMeta?: false | ImportMetaOptions;

/**
* Options for infrastructure level logging.
*/
Expand Down Expand Up @@ -4628,6 +4633,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 @@ -5472,6 +5487,11 @@ declare interface JavascriptParserOptions {
*/
importExportsPresence?: false | "auto" | "error" | "warn";

/**
* Options object for es6 import.meta features.
*/
importMeta?: false | ImportMetaOptions;

/**
* Include polyfills or mocks for various node stuff.
*/
Expand Down Expand Up @@ -6392,6 +6412,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 @@ -12223,6 +12244,11 @@ declare interface WebpackOptionsNormalized {
compilation: Compilation
) => boolean)[];

/**
* Options object for es6 import.meta features.
*/
importMeta?: false | ImportMetaOptions;

/**
* Options for infrastructure level logging.
*/
Expand Down

0 comments on commit b55db4f

Please sign in to comment.