Skip to content

Commit d6e9624

Browse files
author
Sven SAULEAU
authored
Merge branch 'master' into feat-wasm-global-in-global-initilizer2
2 parents 38604c3 + bfdb769 commit d6e9624

99 files changed

Lines changed: 933 additions & 320 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.prettierignore‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Ignore all paths.
2+
**/*.*
3+
4+
# Enable prettier for the following paths.
5+
!setup/**/*.js
6+
!lib/**/*.js
7+
!bin/*.js
8+
!hot/*.js
9+
!buildin/*.js
10+
!test/*.js
11+
!test/**/webpack.config.js
12+
!examples/**/webpack.config.js
13+
!schemas/**/*.js
14+
!declarations.d.ts

‎declarations.d.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ declare module "@webassemblyjs/ast" {
6464
export class ModuleExportDescr extends Node {}
6565
export class IndexLiteral extends Node {}
6666
export class NumberLiteral extends Node {}
67+
export class FloatLiteral extends Node {}
6768
export class Global extends Node {}
6869
export class FuncParam extends Node {
6970
valtype: string;
@@ -82,6 +83,7 @@ declare module "@webassemblyjs/ast" {
8283
export class IndexInFuncSection extends Node {}
8384
export function indexLiteral(index: number): IndexLiteral;
8485
export function numberLiteralFromRaw(num: number): NumberLiteral;
86+
export function floatLiteral(value: number, nan?: boolean, inf?: boolean, raw?: string): FloatLiteral;
8587
export function global(globalType: string, nodes: Node[]): Global;
8688
export function identifier(indentifier: string): Identifier;
8789
export function funcParam(valType: string, id: Identifier): FuncParam;

‎examples/README.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@
120120
If you think an example is missing, please report it as issue. :)
121121

122122
# Building an Example
123-
1. Run `npm install` in the root of the project.
124-
2. Run `npm link webpack` in the root of the project.
125-
3. Run `node build.js` in the specific example directory. (Ex: `cd examples/commonjs && node build.js`)
123+
1. Run `yarn` in the root of the project.
124+
2. Run `yarn link webpack` in the root of the project.
125+
3. Run `yarn add --dev webpack-cli` in the root of the project.
126+
4. Run `node build.js` in the specific example directory. (Ex: `cd examples/commonjs && node build.js`)
126127

127128
Note: To build all examples run `npm run build:examples`

‎lib/AmdMainTemplatePlugin.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,21 @@
88
const { ConcatSource } = require("webpack-sources");
99
const Template = require("./Template");
1010

11+
/** @typedef {import("./Compilation")} Compilation */
12+
1113
class AmdMainTemplatePlugin {
14+
/**
15+
* @param {string} name the library name
16+
*/
1217
constructor(name) {
18+
/** @type {string} */
1319
this.name = name;
1420
}
1521

22+
/**
23+
* @param {Compilation} compilation the compilation instance
24+
* @returns {void}
25+
*/
1626
apply(compilation) {
1727
const { mainTemplate, chunkTemplate } = compilation;
1828

‎lib/AsyncDependencyToInitialChunkError.js‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@
66

77
const WebpackError = require("./WebpackError");
88

9-
module.exports = class AsyncDependencyToInitialChunkError extends WebpackError {
9+
/** @typedef {import("./Module")} Module */
10+
11+
class AsyncDependencyToInitialChunkError extends WebpackError {
12+
/**
13+
* Creates an instance of AsyncDependencyToInitialChunkError.
14+
* @param {string} chunkName Name of Chunk
15+
* @param {Module} module module tied to dependency
16+
* @param {TODO} loc location of dependency
17+
*/
1018
constructor(chunkName, module, loc) {
1119
super();
1220

@@ -18,4 +26,6 @@ module.exports = class AsyncDependencyToInitialChunkError extends WebpackError {
1826

1927
Error.captureStackTrace(this, this.constructor);
2028
}
21-
};
29+
}
30+
31+
module.exports = AsyncDependencyToInitialChunkError;

‎lib/CaseSensitiveModulesWarning.js‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66

77
const WebpackError = require("./WebpackError");
88

9-
module.exports = class CaseSensitiveModulesWarning extends WebpackError {
9+
/** @typedef {import("./Module")} Module */
10+
11+
class CaseSensitiveModulesWarning extends WebpackError {
12+
/**
13+
* Creates an instance of CaseSensitiveModulesWarning.
14+
* @param {Module[]} modules modules that were detected
15+
*/
1016
constructor(modules) {
1117
super();
1218

@@ -23,6 +29,11 @@ ${modulesList}`;
2329
Error.captureStackTrace(this, this.constructor);
2430
}
2531

32+
/**
33+
* @private
34+
* @param {Module[]} modules the modules to be sorted
35+
* @returns {Module[]} sorted version of original modules
36+
*/
2637
_sort(modules) {
2738
return modules.slice().sort((a, b) => {
2839
a = a.identifier();
@@ -36,6 +47,11 @@ ${modulesList}`;
3647
});
3748
}
3849

50+
/**
51+
* @private
52+
* @param {Module[]} modules each module from throw
53+
* @returns {string} each message from provided moduels
54+
*/
3955
_moduleMessages(modules) {
4056
return modules
4157
.map(m => {
@@ -50,4 +66,6 @@ ${modulesList}`;
5066
})
5167
.join("\n");
5268
}
53-
};
69+
}
70+
71+
module.exports = CaseSensitiveModulesWarning;

‎lib/ExportPropertyMainTemplatePlugin.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,28 @@
66

77
const { ConcatSource } = require("webpack-sources");
88

9+
/** @typedef {import("./Compilation")} Compilation */
10+
11+
/**
12+
* @param {string[]} accessor the accessor to convert to path
13+
* @returns {string} the path
14+
*/
915
const accessorToObjectAccess = accessor => {
1016
return accessor.map(a => `[${JSON.stringify(a)}]`).join("");
1117
};
1218

1319
class ExportPropertyMainTemplatePlugin {
20+
/**
21+
* @param {string|string[]} property the name of the property to export
22+
*/
1423
constructor(property) {
1524
this.property = property;
1625
}
1726

27+
/**
28+
* @param {Compilation} compilation the compilation instance
29+
* @returns {void}
30+
*/
1831
apply(compilation) {
1932
const { mainTemplate, chunkTemplate } = compilation;
2033

‎lib/FlagDependencyUsagePlugin.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const isSubset = (biggerSet, subset) => {
2020
class FlagDependencyUsagePlugin {
2121
apply(compiler) {
2222
compiler.hooks.compilation.tap("FlagDependencyUsagePlugin", compilation => {
23-
compilation.hooks.optimizeModulesAdvanced.tap(
23+
compilation.hooks.optimizeDependencies.tap(
2424
"FlagDependencyUsagePlugin",
2525
modules => {
2626
const processModule = (module, usedExports) => {
@@ -86,9 +86,9 @@ class FlagDependencyUsagePlugin {
8686
}
8787

8888
const queue = [];
89-
for (const chunk of compilation.chunks) {
90-
if (chunk.entryModule) {
91-
processModule(chunk.entryModule, true);
89+
for (const preparedEntrypoint of compilation._preparedEntrypoints) {
90+
if (preparedEntrypoint.module) {
91+
processModule(preparedEntrypoint.module, true);
9292
}
9393
}
9494

‎lib/LibraryTemplatePlugin.js‎

Lines changed: 66 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,45 @@
66

77
const SetVarMainTemplatePlugin = require("./SetVarMainTemplatePlugin");
88

9+
/** @typedef {import("./Compiler")} Compiler */
10+
11+
/**
12+
* @param {string[]} accessor the accessor to convert to path
13+
* @returns {string} the path
14+
*/
915
const accessorToObjectAccess = accessor => {
10-
return accessor
11-
.map(a => {
12-
return `[${JSON.stringify(a)}]`;
13-
})
14-
.join("");
16+
return accessor.map(a => `[${JSON.stringify(a)}]`).join("");
1517
};
1618

17-
const accessorAccess = (base, accessor, joinWith) => {
18-
accessor = [].concat(accessor);
19-
return accessor
20-
.map((a, idx) => {
21-
a = base
22-
? base + accessorToObjectAccess(accessor.slice(0, idx + 1))
23-
: accessor[0] + accessorToObjectAccess(accessor.slice(1, idx + 1));
24-
if (idx === accessor.length - 1) return a;
19+
/**
20+
* @param {string=} base the path prefix
21+
* @param {string|string[]} accessor the accessor
22+
* @param {string=} joinWith the element separator
23+
* @returns {string} the path
24+
*/
25+
const accessorAccess = (base, accessor, joinWith = "; ") => {
26+
const accessors = Array.isArray(accessor) ? accessor : [accessor];
27+
return accessors
28+
.map((_, idx) => {
29+
const a = base
30+
? base + accessorToObjectAccess(accessors.slice(0, idx + 1))
31+
: accessors[0] + accessorToObjectAccess(accessors.slice(1, idx + 1));
32+
if (idx === accessors.length - 1) return a;
2533
if (idx === 0 && typeof base === "undefined")
2634
return `${a} = typeof ${a} === "object" ? ${a} : {}`;
2735
return `${a} = ${a} || {}`;
2836
})
29-
.join(joinWith || "; ");
37+
.join(joinWith);
3038
};
3139

3240
class LibraryTemplatePlugin {
41+
/**
42+
* @param {string} name name of library
43+
* @param {string} target type of library
44+
* @param {boolean} umdNamedDefine setting this to true will name the UMD module
45+
* @param {string|TODO} auxiliaryComment comment in the UMD wrapper
46+
* @param {string|string[]} exportProperty which export should be exposed as library
47+
*/
3348
constructor(name, target, umdNamedDefine, auxiliaryComment, exportProperty) {
3449
this.name = name;
3550
this.target = target;
@@ -38,77 +53,95 @@ class LibraryTemplatePlugin {
3853
this.exportProperty = exportProperty;
3954
}
4055

56+
/**
57+
* @param {Compiler} compiler the compiler instance
58+
* @returns {void}
59+
*/
4160
apply(compiler) {
4261
compiler.hooks.thisCompilation.tap("LibraryTemplatePlugin", compilation => {
4362
if (this.exportProperty) {
44-
var ExportPropertyMainTemplatePlugin = require("./ExportPropertyMainTemplatePlugin");
63+
const ExportPropertyMainTemplatePlugin = require("./ExportPropertyMainTemplatePlugin");
4564
new ExportPropertyMainTemplatePlugin(this.exportProperty).apply(
4665
compilation
4766
);
4867
}
4968
switch (this.target) {
5069
case "var":
5170
new SetVarMainTemplatePlugin(
52-
`var ${accessorAccess(false, this.name)}`
71+
`var ${accessorAccess(undefined, this.name)}`,
72+
false
5373
).apply(compilation);
5474
break;
5575
case "assign":
5676
new SetVarMainTemplatePlugin(
57-
accessorAccess(undefined, this.name)
77+
accessorAccess(undefined, this.name),
78+
false
5879
).apply(compilation);
5980
break;
6081
case "this":
6182
case "self":
6283
case "window":
63-
if (this.name)
84+
if (this.name) {
6485
new SetVarMainTemplatePlugin(
65-
accessorAccess(this.target, this.name)
86+
accessorAccess(this.target, this.name),
87+
false
6688
).apply(compilation);
67-
else
89+
} else {
6890
new SetVarMainTemplatePlugin(this.target, true).apply(compilation);
91+
}
6992
break;
7093
case "global":
71-
if (this.name)
94+
if (this.name) {
7295
new SetVarMainTemplatePlugin(
7396
accessorAccess(
7497
compilation.runtimeTemplate.outputOptions.globalObject,
7598
this.name
76-
)
99+
),
100+
false
77101
).apply(compilation);
78-
else
102+
} else {
79103
new SetVarMainTemplatePlugin(
80104
compilation.runtimeTemplate.outputOptions.globalObject,
81105
true
82106
).apply(compilation);
107+
}
83108
break;
84109
case "commonjs":
85-
if (this.name)
110+
if (this.name) {
86111
new SetVarMainTemplatePlugin(
87-
accessorAccess("exports", this.name)
112+
accessorAccess("exports", this.name),
113+
false
88114
).apply(compilation);
89-
else new SetVarMainTemplatePlugin("exports", true).apply(compilation);
115+
} else {
116+
new SetVarMainTemplatePlugin("exports", true).apply(compilation);
117+
}
90118
break;
91119
case "commonjs2":
92120
case "commonjs-module":
93-
new SetVarMainTemplatePlugin("module.exports").apply(compilation);
121+
new SetVarMainTemplatePlugin("module.exports", false).apply(
122+
compilation
123+
);
94124
break;
95-
case "amd":
96-
var AmdMainTemplatePlugin = require("./AmdMainTemplatePlugin");
125+
case "amd": {
126+
const AmdMainTemplatePlugin = require("./AmdMainTemplatePlugin");
97127
new AmdMainTemplatePlugin(this.name).apply(compilation);
98128
break;
129+
}
99130
case "umd":
100-
case "umd2":
101-
var UmdMainTemplatePlugin = require("./UmdMainTemplatePlugin");
131+
case "umd2": {
132+
const UmdMainTemplatePlugin = require("./UmdMainTemplatePlugin");
102133
new UmdMainTemplatePlugin(this.name, {
103134
optionalAmdExternalAsGlobal: this.target === "umd2",
104135
namedDefine: this.umdNamedDefine,
105136
auxiliaryComment: this.auxiliaryComment
106137
}).apply(compilation);
107138
break;
108-
case "jsonp":
109-
var JsonpExportMainTemplatePlugin = require("./web/JsonpExportMainTemplatePlugin");
139+
}
140+
case "jsonp": {
141+
const JsonpExportMainTemplatePlugin = require("./web/JsonpExportMainTemplatePlugin");
110142
new JsonpExportMainTemplatePlugin(this.name).apply(compilation);
111143
break;
144+
}
112145
default:
113146
throw new Error(`${this.target} is not a valid Library target`);
114147
}

‎lib/ModuleDependencyError.js‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@
77
const WebpackError = require("./WebpackError");
88
const formatLocation = require("./formatLocation");
99

10-
module.exports = class ModuleDependencyError extends WebpackError {
10+
/** @typedef {import("./Module")} Module */
11+
12+
class ModuleDependencyError extends WebpackError {
13+
/**
14+
* Creates an instance of ModuleDependencyError.
15+
* @param {Module} module module tied to dependency
16+
* @param {Error} err error thrown
17+
* @param {TODO} loc location of dependency
18+
*/
1119
constructor(module, err, loc) {
1220
super();
1321

@@ -22,4 +30,6 @@ module.exports = class ModuleDependencyError extends WebpackError {
2230

2331
Error.captureStackTrace(this, this.constructor);
2432
}
25-
};
33+
}
34+
35+
module.exports = ModuleDependencyError;

0 commit comments

Comments
 (0)