fix target expression

fix externals

add test case
This commit is contained in:
Tobias Koppers 2022-01-10 14:33:34 +01:00
parent 75c9912d38
commit 102014bf54
18 changed files with 74 additions and 21 deletions

View File

@ -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 =
| (
@ -110,6 +110,7 @@ export type LibraryType =
| "commonjs"
| "commonjs2"
| "commonjs-module"
| "commonjs-static"
| "amd"
| "amd-require"
| "umd"
@ -189,6 +190,7 @@ export type ExternalsType =
| "commonjs"
| "commonjs2"
| "commonjs-module"
| "commonjs-static"
| "amd"
| "amd-require"
| "umd"
@ -1077,7 +1079,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;
/**
@ -2061,7 +2063,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;
/**

View File

@ -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 =
| (
@ -44,6 +44,7 @@ export type LibraryType =
| "commonjs"
| "commonjs2"
| "commonjs-module"
| "commonjs-static"
| "amd"
| "amd-require"
| "umd"
@ -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;
/**

View File

@ -18,6 +18,7 @@ export type ExternalsType =
| "commonjs"
| "commonjs2"
| "commonjs-module"
| "commonjs-static"
| "amd"
| "amd-require"
| "umd"

View File

@ -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 =
| (
@ -44,6 +44,7 @@ export type LibraryType =
| "commonjs"
| "commonjs2"
| "commonjs-module"
| "commonjs-static"
| "amd"
| "amd-require"
| "umd"
@ -70,6 +71,7 @@ export type ExternalsType =
| "commonjs"
| "commonjs2"
| "commonjs-module"
| "commonjs-static"
| "amd"
| "amd-require"
| "umd"
@ -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;
/**

View File

@ -541,6 +541,7 @@ class ExternalModule extends Module {
case "commonjs":
case "commonjs2":
case "commonjs-module":
case "commonjs-static":
return getSourceForCommonJsExternal(request);
case "node-commonjs":
return this.buildInfo.module

View File

@ -261,13 +261,18 @@ class AssignLibraryPlugin extends AbstractLibraryPlugin {
* @param {LibraryContext<T>} libraryContext context
* @returns {Source} source with library export
*/
renderStartup(source, module, { moduleGraph, chunk }, { options, compilation }) {
renderStartup(
source,
module,
{ moduleGraph, chunk },
{ options, compilation }
) {
const fullNameResolved = this._getResolvedFullName(
options,
chunk,
compilation
);
const staticExports = this.unnamed === 'static';
const staticExports = this.unnamed === "static";
const exportAccess = options.export
? propertyAccess(
Array.isArray(options.export) ? options.export : [options.export]
@ -276,14 +281,20 @@ class AssignLibraryPlugin extends AbstractLibraryPlugin {
const result = new ConcatSource(source);
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(
`module.exports.${exportInfo.name} = ${exportInfo.name};\n`
`${exportTarget}${nameAccess} = __webpack_exports__${exportAccess}${nameAccess};\n`
);
}
result.add(
`Object.defineProperty(module.exports, "__esModule", { value: true });\n`
`Object.defineProperty(${exportTarget}, "__esModule", { value: true });\n`
);
} else if (options.name ? this.named === "copy" : this.unnamed === "copy") {
result.add(

File diff suppressed because one or more lines are too long

View File

@ -1880,7 +1880,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": [
@ -1895,6 +1895,7 @@
"commonjs",
"commonjs2",
"commonjs-module",
"commonjs-static",
"amd",
"amd-require",
"umd",

File diff suppressed because one or more lines are too long

View File

@ -218,7 +218,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": [
@ -233,6 +233,7 @@
"commonjs",
"commonjs2",
"commonjs-module",
"commonjs-static",
"amd",
"amd-require",
"umd",

File diff suppressed because one or more lines are too long

View File

@ -13,6 +13,7 @@
"commonjs",
"commonjs2",
"commonjs-module",
"commonjs-static",
"amd",
"amd-require",
"umd",

View File

@ -3,4 +3,4 @@
* DO NOT MODIFY BY HAND.
* Run `yarn special-lint-fix` to update
*/
"use strict";function o(r,{instancePath:e="",parentData:s,parentDataProperty:m,rootData:t=r}={}){return"var"!==r&&"module"!==r&&"assign"!==r&&"this"!==r&&"window"!==r&&"self"!==r&&"global"!==r&&"commonjs"!==r&&"commonjs2"!==r&&"commonjs-module"!==r&&"amd"!==r&&"amd-require"!==r&&"umd"!==r&&"umd2"!==r&&"jsonp"!==r&&"system"!==r&&"promise"!==r&&"import"!==r&&"script"!==r&&"node-commonjs"!==r?(o.errors=[{params:{}}],!1):(o.errors=null,!0)}module.exports=o,module.exports.default=o;
"use strict";function o(r,{instancePath:s="",parentData:m,parentDataProperty:t,rootData:e=r}={}){return"var"!==r&&"module"!==r&&"assign"!==r&&"this"!==r&&"window"!==r&&"self"!==r&&"global"!==r&&"commonjs"!==r&&"commonjs2"!==r&&"commonjs-module"!==r&&"commonjs-static"!==r&&"amd"!==r&&"amd-require"!==r&&"umd"!==r&&"umd2"!==r&&"jsonp"!==r&&"system"!==r&&"promise"!==r&&"import"!==r&&"script"!==r&&"node-commonjs"!==r?(o.errors=[{params:{}}],!1):(o.errors=null,!0)}module.exports=o,module.exports.default=o;

File diff suppressed because one or more lines are too long

View File

@ -112,6 +112,7 @@
"commonjs",
"commonjs2",
"commonjs-module",
"commonjs-static",
"amd",
"amd-require",
"umd",
@ -243,7 +244,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": [
@ -258,6 +259,7 @@
"commonjs",
"commonjs2",
"commonjs-module",
"commonjs-static",
"amd",
"amd-require",
"umd",

View File

@ -293,6 +293,15 @@ module.exports = (env, { testPath }) => [
devtool: "eval-source-map",
externals: ["external"]
},
{
output: {
uniqueName: "commonjs-static-external",
filename: "commonjs-static-external.js",
libraryTarget: "commonjs-static",
iife: false
},
externals: ["external"]
},
{
output: {
uniqueName: "index",

View File

@ -275,6 +275,23 @@ module.exports = (env, { testPath }) => [
})
]
},
{
resolve: {
alias: {
library: path.resolve(
testPath,
"../0-create-library/commonjs-static-external.js"
),
external: path.resolve(__dirname, "node_modules/external.js")
}
},
plugins: [
new webpack.DefinePlugin({
NAME: JSON.stringify("commonjs-static with external"),
TEST_EXTERNAL: true
})
]
},
{
resolve: {
alias: {

8
types.d.ts vendored
View File

@ -2095,6 +2095,7 @@ declare interface Configuration {
| "commonjs"
| "commonjs2"
| "commonjs-module"
| "commonjs-static"
| "amd"
| "amd-require"
| "umd"
@ -3853,6 +3854,7 @@ type ExternalsType =
| "commonjs"
| "commonjs2"
| "commonjs-module"
| "commonjs-static"
| "amd"
| "amd-require"
| "umd"
@ -6005,7 +6007,7 @@ declare interface LibraryOptions {
name?: 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).
*/
type: string;
@ -6708,6 +6710,7 @@ declare interface ModuleFederationPluginOptions {
| "commonjs"
| "commonjs2"
| "commonjs-module"
| "commonjs-static"
| "amd"
| "amd-require"
| "umd"
@ -8245,7 +8248,7 @@ declare interface Output {
libraryExport?: string | string[];
/**
* 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?: string;
@ -12007,6 +12010,7 @@ declare interface WebpackOptionsNormalized {
| "commonjs"
| "commonjs2"
| "commonjs-module"
| "commonjs-static"
| "amd"
| "amd-require"
| "umd"