Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #11629 from xiaoxiaojx/feat/issue_11571
  • Loading branch information
sokra committed Jan 18, 2021
2 parents a08eca7 + 2f6bf31 commit dfe9cd3
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 19 deletions.
7 changes: 4 additions & 3 deletions declarations/WebpackOptions.d.ts
Expand Up @@ -95,13 +95,14 @@ export type LibraryExport = string[] | string;
*/
export type LibraryName = string[] | string | LibraryCustomUmdObject;
/**
* Type of library (types included by default are 'var', 'module', 'assign', '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', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).
*/
export type LibraryType =
| (
| "var"
| "module"
| "assign"
| "assign-properties"
| "this"
| "window"
| "self"
Expand Down Expand Up @@ -960,7 +961,7 @@ export interface LibraryOptions {
*/
name?: LibraryName;
/**
* Type of library (types included by default are 'var', 'module', 'assign', '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', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).
*/
type: LibraryType;
/**
Expand Down Expand Up @@ -1887,7 +1888,7 @@ export interface Output {
*/
libraryExport?: LibraryExport;
/**
* Type of library (types included by default are 'var', 'module', 'assign', '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', '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,13 +29,14 @@ export type LibraryExport = string[] | string;
*/
export type LibraryName = string[] | string | LibraryCustomUmdObject;
/**
* Type of library (types included by default are 'var', 'module', 'assign', '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', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).
*/
export type LibraryType =
| (
| "var"
| "module"
| "assign"
| "assign-properties"
| "this"
| "window"
| "self"
Expand Down Expand Up @@ -113,7 +114,7 @@ export interface LibraryOptions {
*/
name?: LibraryName;
/**
* Type of library (types included by default are 'var', 'module', 'assign', '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', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).
*/
type: LibraryType;
/**
Expand Down
5 changes: 3 additions & 2 deletions declarations/plugins/container/ModuleFederationPlugin.d.ts
Expand Up @@ -29,13 +29,14 @@ export type LibraryExport = string[] | string;
*/
export type LibraryName = string[] | string | LibraryCustomUmdObject;
/**
* Type of library (types included by default are 'var', 'module', 'assign', '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', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).
*/
export type LibraryType =
| (
| "var"
| "module"
| "assign"
| "assign-properties"
| "this"
| "window"
| "self"
Expand Down Expand Up @@ -168,7 +169,7 @@ export interface LibraryOptions {
*/
name?: LibraryName;
/**
* Type of library (types included by default are 'var', 'module', 'assign', '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', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).
*/
type: LibraryType;
/**
Expand Down
4 changes: 3 additions & 1 deletion lib/library/AssignLibraryPlugin.js
Expand Up @@ -87,6 +87,7 @@ const accessWithInit = (accessor, existingLength, initLast = false) => {
* @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 {"copy"|"assign"=} named behavior for named library name
*/

/**
Expand All @@ -110,6 +111,7 @@ class AssignLibraryPlugin extends AbstractLibraryPlugin {
this.prefix = options.prefix;
this.declare = options.declare;
this.unnamed = options.unnamed;
this.named = options.named || "assign";
}

/**
Expand Down Expand Up @@ -161,7 +163,7 @@ class AssignLibraryPlugin extends AbstractLibraryPlugin {
}
result.add(`${this.declare} ${base};`);
}
if (!options.name && this.unnamed === "copy") {
if (options.name ? this.named === "copy" : this.unnamed === "copy") {
result.add(
`(function(e, a) { for(var i in a) e[i] = a[i]; if(a.__esModule) Object.defineProperty(e, "__esModule", { value: true }); }(${accessWithInit(
fullNameResolved,
Expand Down
12 changes: 12 additions & 0 deletions lib/library/EnableLibraryPlugin.js
Expand Up @@ -87,6 +87,18 @@ class EnableLibraryPlugin {
}).apply(compiler);
break;
}
case "assign-properties": {
//@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
const AssignLibraryPlugin = require("./AssignLibraryPlugin");
new AssignLibraryPlugin({
type,
prefix: [],
declare: false,
unnamed: "error",
named: "copy"
}).apply(compiler);
break;
}
case "assign": {
//@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
const AssignLibraryPlugin = require("./AssignLibraryPlugin");
Expand Down
3 changes: 2 additions & 1 deletion schemas/WebpackOptions.json
Expand Up @@ -1252,13 +1252,14 @@
"required": ["type"]
},
"LibraryType": {
"description": "Type of library (types included by default are 'var', 'module', 'assign', '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', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).",
"anyOf": [
{
"enum": [
"var",
"module",
"assign",
"assign-properties",
"this",
"window",
"self",
Expand Down
3 changes: 2 additions & 1 deletion schemas/plugins/container/ContainerPlugin.json
Expand Up @@ -201,13 +201,14 @@
"required": ["type"]
},
"LibraryType": {
"description": "Type of library (types included by default are 'var', 'module', 'assign', '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', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).",
"anyOf": [
{
"enum": [
"var",
"module",
"assign",
"assign-properties",
"this",
"window",
"self",
Expand Down
3 changes: 2 additions & 1 deletion schemas/plugins/container/ModuleFederationPlugin.json
Expand Up @@ -225,13 +225,14 @@
"required": ["type"]
},
"LibraryType": {
"description": "Type of library (types included by default are 'var', 'module', 'assign', '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', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).",
"anyOf": [
{
"enum": [
"var",
"module",
"assign",
"assign-properties",
"this",
"window",
"self",
Expand Down
14 changes: 8 additions & 6 deletions test/__snapshots__/Cli.test.js.snap
Expand Up @@ -3351,14 +3351,15 @@ Object {
"output-enabled-library-types": Object {
"configs": Array [
Object {
"description": "Type of library (types included by default are 'var', 'module', 'assign', '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', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).",
"multiple": true,
"path": "output.enabledLibraryTypes[]",
"type": "enum",
"values": Array [
"var",
"module",
"assign",
"assign-properties",
"this",
"window",
"self",
Expand All @@ -3375,13 +3376,13 @@ Object {
],
},
Object {
"description": "Type of library (types included by default are 'var', 'module', 'assign', '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', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).",
"multiple": true,
"path": "output.enabledLibraryTypes[]",
"type": "string",
},
],
"description": "Type of library (types included by default are 'var', 'module', 'assign', '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', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).",
"multiple": true,
"simpleType": "string",
},
Expand Down Expand Up @@ -3932,14 +3933,15 @@ Object {
"output-library-type": Object {
"configs": Array [
Object {
"description": "Type of library (types included by default are 'var', 'module', 'assign', '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', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).",
"multiple": false,
"path": "output.library.type",
"type": "enum",
"values": Array [
"var",
"module",
"assign",
"assign-properties",
"this",
"window",
"self",
Expand All @@ -3956,13 +3958,13 @@ Object {
],
},
Object {
"description": "Type of library (types included by default are 'var', 'module', 'assign', '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', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).",
"multiple": false,
"path": "output.library.type",
"type": "string",
},
],
"description": "Type of library (types included by default are 'var', 'module', 'assign', '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', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).",
"multiple": false,
"simpleType": "string",
},
Expand Down
9 changes: 9 additions & 0 deletions test/configCases/library/assign-properties/index.js
@@ -0,0 +1,9 @@
it("should define global object with property", function() {
expect(process.env["assign_properties_env"]).toEqual("1");

expect(Object.keys(process.env).length).toBeGreaterThan(1);
});

module.exports = {
assign_properties_env: "1"
}
7 changes: 7 additions & 0 deletions test/configCases/library/assign-properties/webpack.config.js
@@ -0,0 +1,7 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
output: {
library: ["process", "env"],
libraryTarget: "assign-properties"
}
};
4 changes: 2 additions & 2 deletions types.d.ts
Expand Up @@ -4978,7 +4978,7 @@ declare interface LibraryOptions {
name?: string | string[] | LibraryCustomUmdObject;

/**
* Type of library (types included by default are 'var', 'module', 'assign', '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', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).
*/
type: string;

Expand Down Expand Up @@ -6851,7 +6851,7 @@ declare interface Output {
libraryExport?: string | string[];

/**
* Type of library (types included by default are 'var', 'module', 'assign', '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', 'amd', 'amd-require', 'umd', 'umd2', 'jsonp', 'system', but others might be added by plugins).
*/
libraryTarget?: string;

Expand Down

0 comments on commit dfe9cd3

Please sign in to comment.