mirror of https://github.com/webpack/webpack.git
- add new config section importMeta
- use it in ImportMetaPlugin to switch it off as necessary
This commit is contained in:
parent
4abe329dcd
commit
73bb43139d
|
@ -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.
|
||||
*/
|
||||
|
@ -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.
|
||||
*/
|
||||
|
@ -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.
|
||||
*/
|
||||
|
@ -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.
|
||||
*/
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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 */
|
||||
|
@ -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") {
|
||||
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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 }) => {
|
||||
|
@ -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")
|
||||
|
@ -106,6 +116,8 @@ class ImportMetaPlugin {
|
|||
parser.hooks.evaluateTypeof
|
||||
.for("import.meta.url")
|
||||
.tap("ImportMetaPlugin", evaluateToString("string"));
|
||||
|
||||
if (options.url) {
|
||||
parser.hooks.evaluateIdentifier
|
||||
.for("import.meta.url")
|
||||
.tap("ImportMetaPlugin", expr => {
|
||||
|
@ -113,7 +125,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
|
@ -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",
|
||||
|
@ -2103,6 +2125,9 @@
|
|||
"generator": {
|
||||
"$ref": "#/definitions/GeneratorOptionsByModuleType"
|
||||
},
|
||||
"importMeta": {
|
||||
"$ref": "#/definitions/ImportMeta"
|
||||
},
|
||||
"noParse": {
|
||||
"$ref": "#/definitions/NoParse"
|
||||
},
|
||||
|
@ -2195,6 +2220,9 @@
|
|||
"generator": {
|
||||
"$ref": "#/definitions/GeneratorOptionsByModuleType"
|
||||
},
|
||||
"importMeta": {
|
||||
"$ref": "#/definitions/ImportMeta"
|
||||
},
|
||||
"noParse": {
|
||||
"$ref": "#/definitions/NoParse"
|
||||
},
|
||||
|
|
|
@ -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)
|
||||
});
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
export const metaUrl = import.meta.url;
|
|
@ -1,5 +1,10 @@
|
|||
/** @type {import("../../../../").Configuration} */
|
||||
module.exports = {
|
||||
module: {
|
||||
importMeta: {
|
||||
url: false
|
||||
}
|
||||
},
|
||||
entry: {
|
||||
main: "./index.js",
|
||||
imported: {
|
||||
|
|
|
@ -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
|
||||
|
@ -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",
|
||||
|
@ -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.
|
||||
*/
|
||||
|
@ -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.
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue