Skip to content

Commit 7a6e950

Browse files
authored
Merge pull request #16562 from long76/patch-1
Introduce amdContainer support
2 parents 9875719 + 4eaa6ab commit 7a6e950

21 files changed

Lines changed: 208 additions & 10 deletions

declarations/WebpackOptions.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ export type FilenameTemplate =
8282
* Specifies the layer in which modules of this entrypoint are placed.
8383
*/
8484
export type Layer = null | string;
85+
/**
86+
* Add a container for define/require functions in the AMD module.
87+
*/
88+
export type AmdContainer = string;
8589
/**
8690
* Add a comment in the UMD wrapper.
8791
*/
@@ -1091,6 +1095,10 @@ export interface EntryDescription {
10911095
* Options for library.
10921096
*/
10931097
export interface LibraryOptions {
1098+
/**
1099+
* Add a container for define/require functions in the AMD module.
1100+
*/
1101+
amdContainer?: AmdContainer;
10941102
/**
10951103
* Add a comment in the UMD wrapper.
10961104
*/
@@ -1964,6 +1972,10 @@ export interface OptimizationSplitChunksCacheGroup {
19641972
* Options affecting the output of the compilation. `output` options tell webpack how to write the compiled files to disk.
19651973
*/
19661974
export interface Output {
1975+
/**
1976+
* Add a container for define/require functions in the AMD module.
1977+
*/
1978+
amdContainer?: AmdContainer;
19671979
/**
19681980
* The filename of asset modules as relative path inside the 'output.path' directory.
19691981
*/

declarations/plugins/container/ContainerPlugin.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export type ExposesItem = string;
1616
* Modules that should be exposed by this container.
1717
*/
1818
export type ExposesItems = ExposesItem[];
19+
/**
20+
* Add a container for define/require functions in the AMD module.
21+
*/
22+
export type AmdContainer = string;
1923
/**
2024
* Add a comment in the UMD wrapper.
2125
*/
@@ -114,6 +118,10 @@ export interface ExposesConfig {
114118
* Options for library.
115119
*/
116120
export interface LibraryOptions {
121+
/**
122+
* Add a container for define/require functions in the AMD module.
123+
*/
124+
amdContainer?: AmdContainer;
117125
/**
118126
* Add a comment in the UMD wrapper.
119127
*/

declarations/plugins/container/ModuleFederationPlugin.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export type ExposesItem = string;
1616
* Modules that should be exposed by this container.
1717
*/
1818
export type ExposesItems = ExposesItem[];
19+
/**
20+
* Add a container for define/require functions in the AMD module.
21+
*/
22+
export type AmdContainer = string;
1923
/**
2024
* Add a comment in the UMD wrapper.
2125
*/
@@ -171,6 +175,10 @@ export interface ExposesConfig {
171175
* Options for library.
172176
*/
173177
export interface LibraryOptions {
178+
/**
179+
* Add a container for define/require functions in the AMD module.
180+
*/
181+
amdContainer?: AmdContainer;
174182
/**
175183
* Add a comment in the UMD wrapper.
176184
*/

lib/config/normalization.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@ const getNormalizedWebpackOptions = config => {
340340
output.auxiliaryComment !== undefined
341341
? output.auxiliaryComment
342342
: libraryBase.auxiliaryComment,
343+
amdContainer:
344+
output.amdContainer !== undefined
345+
? output.amdContainer
346+
: libraryBase.amdContainer,
343347
export:
344348
output.libraryExport !== undefined
345349
? output.libraryExport

lib/library/AmdLibraryPlugin.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
2929
/**
3030
* @typedef {Object} AmdLibraryPluginParsed
3131
* @property {string} name
32+
* @property {string} amdContainer
3233
*/
3334

3435
/**
@@ -52,7 +53,7 @@ class AmdLibraryPlugin extends AbstractLibraryPlugin {
5253
* @returns {T | false} preprocess as needed by overriding
5354
*/
5455
parseOptions(library) {
55-
const { name } = library;
56+
const { name, amdContainer } = library;
5657
if (this.requireAsWrapper) {
5758
if (name) {
5859
throw new Error(
@@ -67,7 +68,8 @@ class AmdLibraryPlugin extends AbstractLibraryPlugin {
6768
}
6869
}
6970
return {
70-
name: /** @type {string=} */ (name)
71+
name: /** @type {string=} */ (name),
72+
amdContainer: /** @type {string=} */ (amdContainer)
7173
};
7274
}
7375

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

116+
let amdContainerPrefix = "";
117+
if (options.amdContainer) {
118+
amdContainerPrefix = `${options.amdContainer}.`;
119+
}
120+
114121
if (this.requireAsWrapper) {
115122
return new ConcatSource(
116-
`require(${externalsDepsArray}, ${fnStart}`,
123+
`${amdContainerPrefix}require(${externalsDepsArray}, ${fnStart}`,
117124
source,
118125
`${fnEnd});`
119126
);
@@ -123,18 +130,24 @@ class AmdLibraryPlugin extends AbstractLibraryPlugin {
123130
});
124131

125132
return new ConcatSource(
126-
`define(${JSON.stringify(name)}, ${externalsDepsArray}, ${fnStart}`,
133+
`${amdContainerPrefix}define(${JSON.stringify(
134+
name
135+
)}, ${externalsDepsArray}, ${fnStart}`,
127136
source,
128137
`${fnEnd});`
129138
);
130139
} else if (externalsArguments) {
131140
return new ConcatSource(
132-
`define(${externalsDepsArray}, ${fnStart}`,
141+
`${amdContainerPrefix}define(${externalsDepsArray}, ${fnStart}`,
133142
source,
134143
`${fnEnd});`
135144
);
136145
} else {
137-
return new ConcatSource(`define(${fnStart}`, source, `${fnEnd});`);
146+
return new ConcatSource(
147+
`${amdContainerPrefix}define(${fnStart}`,
148+
source,
149+
`${fnEnd});`
150+
);
138151
}
139152
}
140153

@@ -155,6 +168,9 @@ class AmdLibraryPlugin extends AbstractLibraryPlugin {
155168
chunk
156169
});
157170
hash.update(name);
171+
} else if (options.amdContainer) {
172+
hash.update("amdContainer");
173+
hash.update(options.amdContainer);
158174
}
159175
}
160176
}

schemas/WebpackOptions.check.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

schemas/WebpackOptions.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
}
1414
]
1515
},
16+
"AmdContainer": {
17+
"description": "Add a container for define/require functions in the AMD module.",
18+
"type": "string",
19+
"minLength": 1
20+
},
1621
"AssetFilterItemTypes": {
1722
"description": "Filtering value, regexp or function.",
1823
"cli": {
@@ -2001,6 +2006,9 @@
20012006
"type": "object",
20022007
"additionalProperties": false,
20032008
"properties": {
2009+
"amdContainer": {
2010+
"$ref": "#/definitions/AmdContainer"
2011+
},
20042012
"auxiliaryComment": {
20052013
"$ref": "#/definitions/AuxiliaryComment"
20062014
},
@@ -3046,6 +3054,16 @@
30463054
"type": "object",
30473055
"additionalProperties": false,
30483056
"properties": {
3057+
"amdContainer": {
3058+
"cli": {
3059+
"exclude": true
3060+
},
3061+
"oneOf": [
3062+
{
3063+
"$ref": "#/definitions/AmdContainer"
3064+
}
3065+
]
3066+
},
30493067
"assetModuleFilename": {
30503068
"$ref": "#/definitions/AssetModuleFilename"
30513069
},

schemas/plugins/container/ContainerPlugin.check.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

schemas/plugins/container/ContainerPlugin.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
{
22
"definitions": {
3+
"AmdContainer": {
4+
"description": "Add a container for define/require functions in the AMD module.",
5+
"type": "string",
6+
"minLength": 1
7+
},
38
"AuxiliaryComment": {
49
"description": "Add a comment in the UMD wrapper.",
510
"anyOf": [
@@ -199,6 +204,9 @@
199204
"type": "object",
200205
"additionalProperties": false,
201206
"properties": {
207+
"amdContainer": {
208+
"$ref": "#/definitions/AmdContainer"
209+
},
202210
"auxiliaryComment": {
203211
"$ref": "#/definitions/AuxiliaryComment"
204212
},

schemas/plugins/container/ModuleFederationPlugin.check.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)