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

Enhancement: Improve CJS → ESM support #14998

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 5 additions & 3 deletions declarations/WebpackOptions.d.ts
Expand Up @@ -95,7 +95,7 @@ export type LibraryExport = string[] | string;
*/
export type LibraryName = string[] | string | LibraryCustomUmdObject;
/**
* 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).
* 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).
*/
export type LibraryType =
| (
Expand All @@ -110,6 +110,7 @@ export type LibraryType =
| "commonjs"
| "commonjs2"
| "commonjs-module"
| "commonjs-static"
| "amd"
| "amd-require"
| "umd"
Expand Down Expand Up @@ -189,6 +190,7 @@ export type ExternalsType =
| "commonjs"
| "commonjs2"
| "commonjs-module"
| "commonjs-static"
| "amd"
| "amd-require"
| "umd"
Expand Down Expand Up @@ -1085,7 +1087,7 @@ export interface LibraryOptions {
*/
name?: LibraryName;
/**
* 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).
* 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).
*/
type: LibraryType;
/**
Expand Down Expand Up @@ -2077,7 +2079,7 @@ export interface Output {
*/
libraryExport?: LibraryExport;
/**
* 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).
* 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).
*/
libraryTarget?: LibraryType;
/**
Expand Down
5 changes: 3 additions & 2 deletions declarations/plugins/container/ContainerPlugin.d.ts
Expand Up @@ -29,7 +29,7 @@ export type LibraryExport = string[] | string;
*/
export type LibraryName = string[] | string | LibraryCustomUmdObject;
/**
* 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).
* 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).
*/
export type LibraryType =
| (
Expand All @@ -44,6 +44,7 @@ export type LibraryType =
| "commonjs"
| "commonjs2"
| "commonjs-module"
| "commonjs-static"
| "amd"
| "amd-require"
| "umd"
Expand Down Expand Up @@ -126,7 +127,7 @@ export interface LibraryOptions {
*/
name?: LibraryName;
/**
* 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).
* 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).
*/
type: LibraryType;
/**
Expand Down
Expand Up @@ -18,6 +18,7 @@ export type ExternalsType =
| "commonjs"
| "commonjs2"
| "commonjs-module"
| "commonjs-static"
| "amd"
| "amd-require"
| "umd"
Expand Down
6 changes: 4 additions & 2 deletions declarations/plugins/container/ModuleFederationPlugin.d.ts
Expand Up @@ -29,7 +29,7 @@ export type LibraryExport = string[] | string;
*/
export type LibraryName = string[] | string | LibraryCustomUmdObject;
/**
* 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).
* 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).
*/
export type LibraryType =
| (
Expand All @@ -44,6 +44,7 @@ export type LibraryType =
| "commonjs"
| "commonjs2"
| "commonjs-module"
| "commonjs-static"
| "amd"
| "amd-require"
| "umd"
Expand All @@ -70,6 +71,7 @@ export type ExternalsType =
| "commonjs"
| "commonjs2"
| "commonjs-module"
| "commonjs-static"
| "amd"
| "amd-require"
| "umd"
Expand Down Expand Up @@ -182,7 +184,7 @@ export interface LibraryOptions {
*/
name?: LibraryName;
/**
* 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).
* 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).
*/
type: LibraryType;
/**
Expand Down
1 change: 1 addition & 0 deletions lib/ExternalModule.js
Expand Up @@ -550,6 +550,7 @@ class ExternalModule extends Module {
case "commonjs":
case "commonjs2":
case "commonjs-module":
case "commonjs-static":
return getSourceForCommonJsExternal(request);
case "node-commonjs":
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sokra what is this node-commonjs? I don't see it documented anywhere (just when it was added in #13622)

return this.buildInfo.module
Expand Down
29 changes: 26 additions & 3 deletions lib/library/AssignLibraryPlugin.js
Expand Up @@ -92,7 +92,7 @@ const accessWithInit = (accessor, existingLength, initLast = false) => {
* @property {LibraryType} type
* @property {string[] | "global"} prefix name prefix
* @property {string | false} declare declare name as variable
* @property {"error"|"copy"|"assign"} unnamed behavior for unnamed library name
* @property {"error"|"static"|"copy"|"assign"} unnamed behavior for unnamed library name
* @property {"copy"|"assign"=} named behavior for named library name
*/

Expand Down Expand Up @@ -261,19 +261,42 @@ class AssignLibraryPlugin extends AbstractLibraryPlugin {
* @param {LibraryContext<T>} libraryContext context
* @returns {Source} source with library export
*/
renderStartup(source, module, { chunk }, { options, compilation }) {
renderStartup(
source,
module,
{ moduleGraph, chunk },
{ options, compilation }
) {
const fullNameResolved = this._getResolvedFullName(
options,
chunk,
compilation
);
const staticExports = this.unnamed === "static";
const exportAccess = options.export
? propertyAccess(
Array.isArray(options.export) ? options.export : [options.export]
)
: "";
const result = new ConcatSource(source);
if (options.name ? this.named === "copy" : this.unnamed === "copy") {
if (staticExports) {
const exportsInfo = moduleGraph.getExportsInfo(module);
const exportTarget = accessWithInit(
fullNameResolved,
this._getPrefix(compilation).length,
true
);
for (const exportInfo of exportsInfo.orderedExports) {
if (!exportInfo.provided) continue;
const nameAccess = propertyAccess([exportInfo.name]);
result.add(
`${exportTarget}${nameAccess} = __webpack_exports__${exportAccess}${nameAccess};\n`
);
}
result.add(
`Object.defineProperty(${exportTarget}, "__esModule", { value: true });\n`
);
} else if (options.name ? this.named === "copy" : this.unnamed === "copy") {
result.add(
`var __webpack_export_target__ = ${accessWithInit(
fullNameResolved,
Expand Down
11 changes: 11 additions & 0 deletions lib/library/EnableLibraryPlugin.js
Expand Up @@ -167,6 +167,17 @@ class EnableLibraryPlugin {
}).apply(compiler);
break;
}
case "commonjs-static": {
//@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
const AssignLibraryPlugin = require("./AssignLibraryPlugin");
new AssignLibraryPlugin({
type,
prefix: ["exports"],
declare: false,
unnamed: "static"
}).apply(compiler);
break;
}
case "commonjs2":
case "commonjs-module": {
//@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
Expand Down
2 changes: 1 addition & 1 deletion schemas/WebpackOptions.check.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion schemas/WebpackOptions.json
Expand Up @@ -1050,6 +1050,7 @@
"commonjs",
"commonjs2",
"commonjs-module",
"commonjs-static",
"amd",
"amd-require",
"umd",
Expand Down Expand Up @@ -1919,7 +1920,7 @@
"required": ["type"]
},
"LibraryType": {
"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).",
"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).",
"anyOf": [
{
"enum": [
Expand All @@ -1934,6 +1935,7 @@
"commonjs",
"commonjs2",
"commonjs-module",
"commonjs-static",
"amd",
"amd-require",
"umd",
Expand Down