Skip to content

Commit 149333f

Browse files
authored
Merge pull request #14998 from JakobJingleheimer/support-cjs-static-named-exports
Enhancement: Improve CJS → ESM support
2 parents 016ec7b + 83a0e61 commit 149333f

20 files changed

Lines changed: 106 additions & 26 deletions

declarations/WebpackOptions.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export type LibraryExport = string[] | string;
9595
*/
9696
export type LibraryName = string[] | string | LibraryCustomUmdObject;
9797
/**
98-
* Type of library (types included by default are 'var', 'module', 'assign', 'assign-properties', 'this', 'window', 'self', 'global', 'commonjs', 'commonjs2', 'commonjs-module', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).
98+
* Type of library (types included by default are 'var', 'module', 'assign', 'assign-properties', 'this', 'window', 'self', 'global', 'commonjs', 'commonjs2', 'commonjs-module', 'commonjs-static', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).
9999
*/
100100
export type LibraryType =
101101
| (
@@ -110,6 +110,7 @@ export type LibraryType =
110110
| "commonjs"
111111
| "commonjs2"
112112
| "commonjs-module"
113+
| "commonjs-static"
113114
| "amd"
114115
| "amd-require"
115116
| "umd"
@@ -189,6 +190,7 @@ export type ExternalsType =
189190
| "commonjs"
190191
| "commonjs2"
191192
| "commonjs-module"
193+
| "commonjs-static"
192194
| "amd"
193195
| "amd-require"
194196
| "umd"
@@ -1085,7 +1087,7 @@ export interface LibraryOptions {
10851087
*/
10861088
name?: LibraryName;
10871089
/**
1088-
* Type of library (types included by default are 'var', 'module', 'assign', 'assign-properties', 'this', 'window', 'self', 'global', 'commonjs', 'commonjs2', 'commonjs-module', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).
1090+
* Type of library (types included by default are 'var', 'module', 'assign', 'assign-properties', 'this', 'window', 'self', 'global', 'commonjs', 'commonjs2', 'commonjs-module', 'commonjs-static', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).
10891091
*/
10901092
type: LibraryType;
10911093
/**
@@ -2077,7 +2079,7 @@ export interface Output {
20772079
*/
20782080
libraryExport?: LibraryExport;
20792081
/**
2080-
* Type of library (types included by default are 'var', 'module', 'assign', 'assign-properties', 'this', 'window', 'self', 'global', 'commonjs', 'commonjs2', 'commonjs-module', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).
2082+
* Type of library (types included by default are 'var', 'module', 'assign', 'assign-properties', 'this', 'window', 'self', 'global', 'commonjs', 'commonjs2', 'commonjs-module', 'commonjs-static', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).
20812083
*/
20822084
libraryTarget?: LibraryType;
20832085
/**

declarations/plugins/container/ContainerPlugin.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export type LibraryExport = string[] | string;
2929
*/
3030
export type LibraryName = string[] | string | LibraryCustomUmdObject;
3131
/**
32-
* Type of library (types included by default are 'var', 'module', 'assign', 'assign-properties', 'this', 'window', 'self', 'global', 'commonjs', 'commonjs2', 'commonjs-module', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).
32+
* Type of library (types included by default are 'var', 'module', 'assign', 'assign-properties', 'this', 'window', 'self', 'global', 'commonjs', 'commonjs2', 'commonjs-module', 'commonjs-static', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).
3333
*/
3434
export type LibraryType =
3535
| (
@@ -44,6 +44,7 @@ export type LibraryType =
4444
| "commonjs"
4545
| "commonjs2"
4646
| "commonjs-module"
47+
| "commonjs-static"
4748
| "amd"
4849
| "amd-require"
4950
| "umd"
@@ -126,7 +127,7 @@ export interface LibraryOptions {
126127
*/
127128
name?: LibraryName;
128129
/**
129-
* Type of library (types included by default are 'var', 'module', 'assign', 'assign-properties', 'this', 'window', 'self', 'global', 'commonjs', 'commonjs2', 'commonjs-module', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).
130+
* Type of library (types included by default are 'var', 'module', 'assign', 'assign-properties', 'this', 'window', 'self', 'global', 'commonjs', 'commonjs2', 'commonjs-module', 'commonjs-static', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).
130131
*/
131132
type: LibraryType;
132133
/**

declarations/plugins/container/ContainerReferencePlugin.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export type ExternalsType =
1818
| "commonjs"
1919
| "commonjs2"
2020
| "commonjs-module"
21+
| "commonjs-static"
2122
| "amd"
2223
| "amd-require"
2324
| "umd"

declarations/plugins/container/ModuleFederationPlugin.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export type LibraryExport = string[] | string;
2929
*/
3030
export type LibraryName = string[] | string | LibraryCustomUmdObject;
3131
/**
32-
* Type of library (types included by default are 'var', 'module', 'assign', 'assign-properties', 'this', 'window', 'self', 'global', 'commonjs', 'commonjs2', 'commonjs-module', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).
32+
* Type of library (types included by default are 'var', 'module', 'assign', 'assign-properties', 'this', 'window', 'self', 'global', 'commonjs', 'commonjs2', 'commonjs-module', 'commonjs-static', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).
3333
*/
3434
export type LibraryType =
3535
| (
@@ -44,6 +44,7 @@ export type LibraryType =
4444
| "commonjs"
4545
| "commonjs2"
4646
| "commonjs-module"
47+
| "commonjs-static"
4748
| "amd"
4849
| "amd-require"
4950
| "umd"
@@ -70,6 +71,7 @@ export type ExternalsType =
7071
| "commonjs"
7172
| "commonjs2"
7273
| "commonjs-module"
74+
| "commonjs-static"
7375
| "amd"
7476
| "amd-require"
7577
| "umd"
@@ -182,7 +184,7 @@ export interface LibraryOptions {
182184
*/
183185
name?: LibraryName;
184186
/**
185-
* Type of library (types included by default are 'var', 'module', 'assign', 'assign-properties', 'this', 'window', 'self', 'global', 'commonjs', 'commonjs2', 'commonjs-module', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).
187+
* Type of library (types included by default are 'var', 'module', 'assign', 'assign-properties', 'this', 'window', 'self', 'global', 'commonjs', 'commonjs2', 'commonjs-module', 'commonjs-static', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).
186188
*/
187189
type: LibraryType;
188190
/**

lib/ExternalModule.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ class ExternalModule extends Module {
550550
case "commonjs":
551551
case "commonjs2":
552552
case "commonjs-module":
553+
case "commonjs-static":
553554
return getSourceForCommonJsExternal(request);
554555
case "node-commonjs":
555556
return this.buildInfo.module

lib/library/AssignLibraryPlugin.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const accessWithInit = (accessor, existingLength, initLast = false) => {
9292
* @property {LibraryType} type
9393
* @property {string[] | "global"} prefix name prefix
9494
* @property {string | false} declare declare name as variable
95-
* @property {"error"|"copy"|"assign"} unnamed behavior for unnamed library name
95+
* @property {"error"|"static"|"copy"|"assign"} unnamed behavior for unnamed library name
9696
* @property {"copy"|"assign"=} named behavior for named library name
9797
*/
9898

@@ -261,19 +261,42 @@ class AssignLibraryPlugin extends AbstractLibraryPlugin {
261261
* @param {LibraryContext<T>} libraryContext context
262262
* @returns {Source} source with library export
263263
*/
264-
renderStartup(source, module, { chunk }, { options, compilation }) {
264+
renderStartup(
265+
source,
266+
module,
267+
{ moduleGraph, chunk },
268+
{ options, compilation }
269+
) {
265270
const fullNameResolved = this._getResolvedFullName(
266271
options,
267272
chunk,
268273
compilation
269274
);
275+
const staticExports = this.unnamed === "static";
270276
const exportAccess = options.export
271277
? propertyAccess(
272278
Array.isArray(options.export) ? options.export : [options.export]
273279
)
274280
: "";
275281
const result = new ConcatSource(source);
276-
if (options.name ? this.named === "copy" : this.unnamed === "copy") {
282+
if (staticExports) {
283+
const exportsInfo = moduleGraph.getExportsInfo(module);
284+
const exportTarget = accessWithInit(
285+
fullNameResolved,
286+
this._getPrefix(compilation).length,
287+
true
288+
);
289+
for (const exportInfo of exportsInfo.orderedExports) {
290+
if (!exportInfo.provided) continue;
291+
const nameAccess = propertyAccess([exportInfo.name]);
292+
result.add(
293+
`${exportTarget}${nameAccess} = __webpack_exports__${exportAccess}${nameAccess};\n`
294+
);
295+
}
296+
result.add(
297+
`Object.defineProperty(${exportTarget}, "__esModule", { value: true });\n`
298+
);
299+
} else if (options.name ? this.named === "copy" : this.unnamed === "copy") {
277300
result.add(
278301
`var __webpack_export_target__ = ${accessWithInit(
279302
fullNameResolved,

lib/library/EnableLibraryPlugin.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,17 @@ class EnableLibraryPlugin {
167167
}).apply(compiler);
168168
break;
169169
}
170+
case "commonjs-static": {
171+
//@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
172+
const AssignLibraryPlugin = require("./AssignLibraryPlugin");
173+
new AssignLibraryPlugin({
174+
type,
175+
prefix: ["exports"],
176+
declare: false,
177+
unnamed: "static"
178+
}).apply(compiler);
179+
break;
180+
}
170181
case "commonjs2":
171182
case "commonjs-module": {
172183
//@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697

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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,7 @@
10501050
"commonjs",
10511051
"commonjs2",
10521052
"commonjs-module",
1053+
"commonjs-static",
10531054
"amd",
10541055
"amd-require",
10551056
"umd",
@@ -1919,7 +1920,7 @@
19191920
"required": ["type"]
19201921
},
19211922
"LibraryType": {
1922-
"description": "Type of library (types included by default are 'var', 'module', 'assign', 'assign-properties', 'this', 'window', 'self', 'global', 'commonjs', 'commonjs2', 'commonjs-module', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).",
1923+
"description": "Type of library (types included by default are 'var', 'module', 'assign', 'assign-properties', 'this', 'window', 'self', 'global', 'commonjs', 'commonjs2', 'commonjs-module', 'commonjs-static', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).",
19231924
"anyOf": [
19241925
{
19251926
"enum": [
@@ -1934,6 +1935,7 @@
19341935
"commonjs",
19351936
"commonjs2",
19361937
"commonjs-module",
1938+
"commonjs-static",
19371939
"amd",
19381940
"amd-require",
19391941
"umd",

0 commit comments

Comments
 (0)