Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #16561 #16562

Merged
merged 12 commits into from Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions declarations/WebpackOptions.d.ts
Expand Up @@ -82,6 +82,10 @@ export type FilenameTemplate =
* Specifies the layer in which modules of this entrypoint are placed.
*/
export type Layer = null | string;
/**
* Add a container for define/require functions in the AMD module.
*/
export type AmdContainer = string;
/**
* Add a comment in the UMD wrapper.
*/
Expand Down Expand Up @@ -1087,6 +1091,10 @@ export interface EntryDescription {
* Options for library.
*/
export interface LibraryOptions {
/**
* Add a container for define/require functions in the AMD module.
*/
amdContainer?: AmdContainer;
/**
* Add a comment in the UMD wrapper.
*/
Expand Down Expand Up @@ -1960,6 +1968,10 @@ export interface OptimizationSplitChunksCacheGroup {
* Options affecting the output of the compilation. `output` options tell webpack how to write the compiled files to disk.
*/
export interface Output {
/**
* Add a container for define/require functions in the AMD module.
*/
amdContainer?: AmdContainer;
/**
* The filename of asset modules as relative path inside the 'output.path' directory.
*/
Expand Down
8 changes: 8 additions & 0 deletions declarations/plugins/container/ContainerPlugin.d.ts
Expand Up @@ -16,6 +16,10 @@ export type ExposesItem = string;
* Modules that should be exposed by this container.
*/
export type ExposesItems = ExposesItem[];
/**
* Add a container for define/require functions in the AMD module.
*/
export type AmdContainer = string;
/**
* Add a comment in the UMD wrapper.
*/
Expand Down Expand Up @@ -114,6 +118,10 @@ export interface ExposesConfig {
* Options for library.
*/
export interface LibraryOptions {
/**
* Add a container for define/require functions in the AMD module.
*/
amdContainer?: AmdContainer;
/**
* Add a comment in the UMD wrapper.
*/
Expand Down
8 changes: 8 additions & 0 deletions declarations/plugins/container/ModuleFederationPlugin.d.ts
Expand Up @@ -16,6 +16,10 @@ export type ExposesItem = string;
* Modules that should be exposed by this container.
*/
export type ExposesItems = ExposesItem[];
/**
* Add a container for define/require functions in the AMD module.
*/
export type AmdContainer = string;
/**
* Add a comment in the UMD wrapper.
*/
Expand Down Expand Up @@ -171,6 +175,10 @@ export interface ExposesConfig {
* Options for library.
*/
export interface LibraryOptions {
/**
* Add a container for define/require functions in the AMD module.
*/
amdContainer?: AmdContainer;
/**
* Add a comment in the UMD wrapper.
*/
Expand Down
4 changes: 4 additions & 0 deletions lib/config/normalization.js
Expand Up @@ -340,6 +340,10 @@ const getNormalizedWebpackOptions = config => {
output.auxiliaryComment !== undefined
? output.auxiliaryComment
: libraryBase.auxiliaryComment,
amdContainer:
output.amdContainer !== undefined
? output.amdContainer
: libraryBase.amdContainer,
export:
output.libraryExport !== undefined
? output.libraryExport
Expand Down
28 changes: 22 additions & 6 deletions lib/library/AmdLibraryPlugin.js
Expand Up @@ -29,6 +29,7 @@ const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
/**
* @typedef {Object} AmdLibraryPluginParsed
* @property {string} name
* @property {string} amdContainer
*/

/**
Expand All @@ -52,7 +53,7 @@ class AmdLibraryPlugin extends AbstractLibraryPlugin {
* @returns {T | false} preprocess as needed by overriding
*/
parseOptions(library) {
const { name } = library;
const { name, amdContainer } = library;
if (this.requireAsWrapper) {
if (name) {
throw new Error(
Expand All @@ -67,7 +68,8 @@ class AmdLibraryPlugin extends AbstractLibraryPlugin {
}
}
return {
name: /** @type {string=} */ (name)
name: /** @type {string=} */ (name),
amdContainer: /** @type {string=} */ (amdContainer)
};
}

Expand Down Expand Up @@ -111,9 +113,14 @@ class AmdLibraryPlugin extends AbstractLibraryPlugin {
(iife || !chunk.hasRuntime() ? " return " : "\n");
const fnEnd = iife ? ";\n}" : "\n}";

let amdContainerPrefix = "";
if (options.amdContainer) {
amdContainerPrefix = `${options.amdContainer}.`;
}

if (this.requireAsWrapper) {
return new ConcatSource(
`require(${externalsDepsArray}, ${fnStart}`,
`${amdContainerPrefix}require(${externalsDepsArray}, ${fnStart}`,
source,
`${fnEnd});`
);
Expand All @@ -123,18 +130,24 @@ class AmdLibraryPlugin extends AbstractLibraryPlugin {
});

return new ConcatSource(
`define(${JSON.stringify(name)}, ${externalsDepsArray}, ${fnStart}`,
`${amdContainerPrefix}define(${JSON.stringify(
name
)}, ${externalsDepsArray}, ${fnStart}`,
source,
`${fnEnd});`
);
} else if (externalsArguments) {
return new ConcatSource(
`define(${externalsDepsArray}, ${fnStart}`,
`${amdContainerPrefix}define(${externalsDepsArray}, ${fnStart}`,
source,
`${fnEnd});`
);
} else {
return new ConcatSource(`define(${fnStart}`, source, `${fnEnd});`);
return new ConcatSource(
`${amdContainerPrefix}define(${fnStart}`,
source,
`${fnEnd});`
);
}
}

Expand All @@ -155,6 +168,9 @@ class AmdLibraryPlugin extends AbstractLibraryPlugin {
chunk
});
hash.update(name);
} else if (options.amdContainer) {
hash.update("amdContainer");
hash.update(options.amdContainer);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion schemas/WebpackOptions.check.js

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions schemas/WebpackOptions.json
Expand Up @@ -13,6 +13,11 @@
}
]
},
"AmdContainer": {
"description": "Add a container for define/require functions in the AMD module.",
"type": "string",
"minLength": 1
},
"AssetFilterItemTypes": {
"description": "Filtering value, regexp or function.",
"cli": {
Expand Down Expand Up @@ -2001,6 +2006,9 @@
"type": "object",
"additionalProperties": false,
"properties": {
"amdContainer": {
"$ref": "#/definitions/AmdContainer"
},
"auxiliaryComment": {
"$ref": "#/definitions/AuxiliaryComment"
},
Expand Down Expand Up @@ -3046,6 +3054,16 @@
"type": "object",
"additionalProperties": false,
"properties": {
"amdContainer": {
"cli": {
"exclude": true
},
"oneOf": [
{
"$ref": "#/definitions/AmdContainer"
}
]
},
"assetModuleFilename": {
"$ref": "#/definitions/AssetModuleFilename"
},
Expand Down
2 changes: 1 addition & 1 deletion schemas/plugins/container/ContainerPlugin.check.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions schemas/plugins/container/ContainerPlugin.json
@@ -1,5 +1,10 @@
{
"definitions": {
"AmdContainer": {
"description": "Add a container for define/require functions in the AMD module.",
"type": "string",
"minLength": 1
},
"AuxiliaryComment": {
"description": "Add a comment in the UMD wrapper.",
"anyOf": [
Expand Down Expand Up @@ -199,6 +204,9 @@
"type": "object",
"additionalProperties": false,
"properties": {
"amdContainer": {
"$ref": "#/definitions/AmdContainer"
},
"auxiliaryComment": {
"$ref": "#/definitions/AuxiliaryComment"
},
Expand Down
2 changes: 1 addition & 1 deletion schemas/plugins/container/ModuleFederationPlugin.check.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions schemas/plugins/container/ModuleFederationPlugin.json
@@ -1,5 +1,10 @@
{
"definitions": {
"AmdContainer": {
"description": "Add a container for define/require functions in the AMD module.",
"type": "string",
"minLength": 1
},
"AuxiliaryComment": {
"description": "Add a comment in the UMD wrapper.",
"anyOf": [
Expand Down Expand Up @@ -225,6 +230,9 @@
"type": "object",
"additionalProperties": false,
"properties": {
"amdContainer": {
"$ref": "#/definitions/AmdContainer"
},
"auxiliaryComment": {
"$ref": "#/definitions/AuxiliaryComment"
},
Expand Down
5 changes: 5 additions & 0 deletions test/Defaults.unittest.js
Expand Up @@ -1050,6 +1050,7 @@ describe("snapshots", () => {
@@ ... @@
- "library": undefined,
+ "library": Object {
+ "amdContainer": undefined,
+ "auxiliaryComment": undefined,
+ "export": undefined,
+ "name": Array [
Expand Down Expand Up @@ -1093,6 +1094,7 @@ describe("snapshots", () => {
@@ ... @@
- "library": undefined,
+ "library": Object {
+ "amdContainer": undefined,
+ "auxiliaryComment": undefined,
+ "export": undefined,
+ "name": Array [
Expand Down Expand Up @@ -1139,6 +1141,7 @@ describe("snapshots", () => {
@@ ... @@
- "library": undefined,
+ "library": Object {
+ "amdContainer": undefined,
+ "auxiliaryComment": undefined,
+ "export": undefined,
+ "name": Array [
Expand Down Expand Up @@ -1188,6 +1191,7 @@ describe("snapshots", () => {
@@ ... @@
- "library": undefined,
+ "library": Object {
+ "amdContainer": undefined,
+ "auxiliaryComment": undefined,
+ "export": undefined,
+ "name": Object {
Expand Down Expand Up @@ -1238,6 +1242,7 @@ describe("snapshots", () => {
@@ ... @@
- "library": undefined,
+ "library": Object {
+ "amdContainer": undefined,
+ "auxiliaryComment": undefined,
+ "export": undefined,
+ "name": Object {
Expand Down
2 changes: 1 addition & 1 deletion test/Validation.test.js
Expand Up @@ -498,7 +498,7 @@ describe("Validation", () => {
expect(msg).toMatchInlineSnapshot(`
"Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
- configuration.output has an unknown property 'ecmaVersion'. These properties are valid:
object { assetModuleFilename?, asyncChunks?, auxiliaryComment?, charset?, chunkFilename?, chunkFormat?, chunkLoadTimeout?, chunkLoading?, chunkLoadingGlobal?, clean?, compareBeforeEmit?, crossOriginLoading?, cssChunkFilename?, cssFilename?, devtoolFallbackModuleFilenameTemplate?, devtoolModuleFilenameTemplate?, devtoolNamespace?, enabledChunkLoadingTypes?, enabledLibraryTypes?, enabledWasmLoadingTypes?, environment?, filename?, globalObject?, hashDigest?, hashDigestLength?, hashFunction?, hashSalt?, hotUpdateChunkFilename?, hotUpdateGlobal?, hotUpdateMainFilename?, iife?, importFunctionName?, importMetaName?, library?, libraryExport?, libraryTarget?, module?, path?, pathinfo?, publicPath?, scriptType?, sourceMapFilename?, sourcePrefix?, strictModuleErrorHandling?, strictModuleExceptionHandling?, trustedTypes?, umdNamedDefine?, uniqueName?, wasmLoading?, webassemblyModuleFilename?, workerChunkLoading?, workerWasmLoading? }
object { amdContainer?, assetModuleFilename?, asyncChunks?, auxiliaryComment?, charset?, chunkFilename?, chunkFormat?, chunkLoadTimeout?, chunkLoading?, chunkLoadingGlobal?, clean?, compareBeforeEmit?, crossOriginLoading?, cssChunkFilename?, cssFilename?, devtoolFallbackModuleFilenameTemplate?, devtoolModuleFilenameTemplate?, devtoolNamespace?, enabledChunkLoadingTypes?, enabledLibraryTypes?, enabledWasmLoadingTypes?, environment?, filename?, globalObject?, hashDigest?, hashDigestLength?, hashFunction?, hashSalt?, hotUpdateChunkFilename?, hotUpdateGlobal?, hotUpdateMainFilename?, iife?, importFunctionName?, importMetaName?, library?, libraryExport?, libraryTarget?, module?, path?, pathinfo?, publicPath?, scriptType?, sourceMapFilename?, sourcePrefix?, strictModuleErrorHandling?, strictModuleExceptionHandling?, trustedTypes?, umdNamedDefine?, uniqueName?, wasmLoading?, webassemblyModuleFilename?, workerChunkLoading?, workerWasmLoading? }
-> Options affecting the output of the compilation. \`output\` options tell webpack how to write the compiled files to disk.
Did you mean output.environment (output.ecmaVersion was a temporary configuration option during webpack 5 beta)?"
`)
Expand Down
13 changes: 13 additions & 0 deletions test/__snapshots__/Cli.basictest.js.snap
Expand Up @@ -6141,6 +6141,19 @@ Object {
"multiple": false,
"simpleType": "string",
},
"output-library-amd-container": Object {
"configs": Array [
Object {
"description": "Add a container for define/require functions in the AMD module.",
"multiple": false,
"path": "output.library.amdContainer",
"type": "string",
},
],
"description": "Add a container for define/require functions in the AMD module.",
"multiple": false,
"simpleType": "string",
},
"output-library-auxiliary-comment": Object {
"configs": Array [
Object {
Expand Down
8 changes: 8 additions & 0 deletions test/configCases/target/amd-container-named/index.js
@@ -0,0 +1,8 @@
it("should run", function() {});

it("should name define", function() {
var fs = require("fs");
var source = fs.readFileSync(__filename, "utf-8");

expect(source).toMatch("window['clientContainer'].define(\"clientContainer\",");
});
22 changes: 22 additions & 0 deletions test/configCases/target/amd-container-named/webpack.config.js
@@ -0,0 +1,22 @@
const webpack = require("../../../../");
/** @type {import("../../../../types").Configuration} */
module.exports = {
output: {
library: {
type: "amd",
name: "clientContainer",
amdContainer: "window['clientContainer']"
}
},
node: {
__dirname: false,
__filename: false
},
plugins: [
new webpack.BannerPlugin({
raw: true,
banner:
"function define(name, deps, fn) { fn(); }\nconst window = {};\nwindow['clientContainer'] = { define };\n"
})
]
};
8 changes: 8 additions & 0 deletions test/configCases/target/amd-container-unnamed/index.js
@@ -0,0 +1,8 @@
it("should run", function() {});

it("should name define", function() {
var fs = require("fs");
var source = fs.readFileSync(__filename, "utf-8");

expect(source).toMatch(/window\['clientContainer'\]\.define\(\[[^\]]*\], (function)?\(/);
});
21 changes: 21 additions & 0 deletions test/configCases/target/amd-container-unnamed/webpack.config.js
@@ -0,0 +1,21 @@
const webpack = require("../../../../");
/** @type {import("../../../../types").Configuration} */
module.exports = {
output: {
library: {
type: "amd",
amdContainer: "window['clientContainer']"
}
},
node: {
__dirname: false,
__filename: false
},
plugins: [
new webpack.BannerPlugin({
raw: true,
banner:
"function define(deps, fn) { fn(); }\nconst window = {};\nwindow['clientContainer'] = { define };\n"
})
]
};