Skip to content

Commit 82a71be

Browse files
committed
Cleanup, refactoring, types
1 parent f876564 commit 82a71be

14 files changed

Lines changed: 307 additions & 140 deletions

declarations.d.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,66 @@ declare module "chrome-trace-event" {
2929
}
3030
}
3131

32+
// There are no typings for @webassemblyjs/ast
33+
declare module "@webassemblyjs/ast" {
34+
export function traverse(
35+
ast: any,
36+
visitor: { [name: string]: (context: { node: Node }) => void }
37+
);
38+
export class Node {
39+
index: number;
40+
}
41+
export class Identifier extends Node {
42+
value: string;
43+
}
44+
export class ModuleImport extends Node {
45+
module: string;
46+
descr: {
47+
type: string;
48+
valtype: string;
49+
};
50+
name: string;
51+
}
52+
export class ModuleExport extends Node {
53+
name: string;
54+
}
55+
export class IndexLiteral extends Node {}
56+
export class NumberLiteral extends Node {}
57+
export class Global extends Node {}
58+
export class FuncParam extends Node {}
59+
export class Instruction extends Node {}
60+
export class CallInstruction extends Instruction {}
61+
export class ObjectInstruction extends Instruction {}
62+
export class Func extends Node {
63+
params: any;
64+
result: any;
65+
}
66+
export class TypeInstructionFunc extends Node {}
67+
export class IndexInFuncSection extends Node {}
68+
export function indexLiteral(index: number): IndexLiteral;
69+
export function numberLiteral(num: number): NumberLiteral;
70+
export function global(globalType: string, nodes: Node[]): Global;
71+
export function identifier(indentifier: string): Identifier;
72+
export function funcParam(valType: string, id: Identifier): FuncParam;
73+
export function instruction(inst: string, args: Node[]): Instruction;
74+
export function callInstruction(funcIndex: IndexLiteral): CallInstruction;
75+
export function objectInstruction(
76+
kind: string,
77+
type: string,
78+
init: Node[]
79+
): ObjectInstruction;
80+
export function func(initFuncId, funcParams, funcResults, funcBody): Func;
81+
export function typeInstructionFunc(params, result): TypeInstructionFunc;
82+
export function indexInFuncSection(index: IndexLiteral): IndexInFuncSection;
83+
export function moduleExport(
84+
identifier: string,
85+
type: string,
86+
index: IndexLiteral
87+
): ModuleExport;
88+
89+
export function getSectionMetadata(ast: any, section: string);
90+
}
91+
3292
/**
3393
* Global variable declarations
3494
* @todo Once this issue is resolved, remove these globals and add JSDoc onsite instead

lib/Compilation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ class Compilation extends Tapable {
230230
this.requestShortener
231231
);
232232
this.moduleTemplates = {
233-
javascript: new ModuleTemplate(this.runtimeTemplate),
234-
webassembly: new ModuleTemplate(this.runtimeTemplate)
233+
javascript: new ModuleTemplate(this.runtimeTemplate, "javascript"),
234+
webassembly: new ModuleTemplate(this.runtimeTemplate, "webassembly")
235235
};
236236

237237
this.semaphore = new Semaphore(options.parallelism || 100);

lib/Generator.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
"use strict";
6+
7+
/** @typedef {import("./Module")} Module */
8+
/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
9+
/** @typedef {import("webpack-sources").Source} Source */
10+
11+
/**
12+
*
13+
*/
14+
class Generator {
15+
static byType(map) {
16+
return new ByTypeGenerator(map);
17+
}
18+
19+
/**
20+
* @abstract
21+
* @param {Module} module module for which the code should be generated
22+
* @param {Map<Function, any>} dependencyTemplates mapping from dependencies to templates
23+
* @param {RuntimeTemplate} runtimeTemplate the runtime template
24+
* @param {string} type which kind of code should be generated
25+
* @returns {Source} generated code
26+
*/
27+
generate(module, dependencyTemplates, runtimeTemplate, type) {
28+
throw new Error("Generator.generate: must be overriden");
29+
}
30+
}
31+
32+
class ByTypeGenerator extends Generator {
33+
constructor(map) {
34+
super();
35+
this.map = map;
36+
}
37+
38+
generate(module, dependencyTemplates, runtimeTemplate, type) {
39+
const generator = this.map[type];
40+
if (!generator) {
41+
throw new Error(`Generator.byType: no generator specified for ${type}`);
42+
}
43+
return generator.generate(
44+
module,
45+
dependencyTemplates,
46+
runtimeTemplate,
47+
type
48+
);
49+
}
50+
}
51+
52+
module.exports = Generator;

lib/ModuleTemplate.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
const { Tapable, SyncWaterfallHook, SyncHook } = require("tapable");
88

99
module.exports = class ModuleTemplate extends Tapable {
10-
constructor(runtimeTemplate) {
10+
constructor(runtimeTemplate, type) {
1111
super();
1212
this.runtimeTemplate = runtimeTemplate;
13+
this.type = type;
1314
this.hooks = {
1415
content: new SyncWaterfallHook([
1516
"source",
@@ -42,7 +43,8 @@ module.exports = class ModuleTemplate extends Tapable {
4243
render(module, dependencyTemplates, options) {
4344
const moduleSource = module.source(
4445
dependencyTemplates,
45-
this.runtimeTemplate
46+
this.runtimeTemplate,
47+
this.type
4648
);
4749
const moduleSourcePostContent = this.hooks.content.call(
4850
moduleSource,

lib/NormalModule.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ class NonErrorEmittedError extends WebpackError {
6161
}
6262
}
6363

64+
/**
65+
* @typedef {Object} CachedSourceEntry
66+
* @property {any} source the generated source
67+
* @property {string} hash the hash value
68+
*/
69+
6470
class NormalModule extends Module {
6571
constructor({
6672
type,
@@ -90,8 +96,8 @@ class NormalModule extends Module {
9096
this.error = null;
9197
this._source = null;
9298
this.buildTimestamp = undefined;
93-
this._cachedSource = undefined;
94-
this._cachedSourceHash = undefined;
99+
/** @private @type {Map<string, CachedSourceEntry>} */
100+
this._cachedSources = new Map();
95101

96102
// Options for the NormalModule set by plugins
97103
// TODO refactor this -> options object filled from Factory
@@ -343,8 +349,7 @@ class NormalModule extends Module {
343349
};
344350

345351
return this.doBuild(options, compilation, resolver, fs, err => {
346-
this._cachedSource = undefined;
347-
this._cachedSourceHash = undefined;
352+
this._cachedSources.clear();
348353

349354
// if we have an error mark module as failed and exit
350355
if (err) {
@@ -403,22 +408,26 @@ class NormalModule extends Module {
403408
return `${this.hash}-${dtHash}`;
404409
}
405410

406-
source(dependencyTemplates, runtimeTemplate) {
411+
source(dependencyTemplates, runtimeTemplate, type = "javascript") {
407412
const hashDigest = this.getHashDigest(dependencyTemplates);
408-
if (this._cachedSourceHash === hashDigest) {
413+
const cacheEntry = this._cachedSources.get(type);
414+
if (cacheEntry !== undefined && cacheEntry.hash === hashDigest) {
409415
// We can reuse the cached source
410-
return this._cachedSource;
416+
return cacheEntry.source;
411417
}
412418

413419
const source = this.generator.generate(
414420
this,
415421
dependencyTemplates,
416-
runtimeTemplate
422+
runtimeTemplate,
423+
type
417424
);
418425

419426
const cachedSource = new CachedSource(source);
420-
this._cachedSource = cachedSource;
421-
this._cachedSourceHash = hashDigest;
427+
this._cachedSources.set(type, {
428+
source: cachedSource,
429+
hash: hashDigest
430+
});
422431
return cachedSource;
423432
}
424433

lib/WebpackOptionsApply.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const OptionsApply = require("./OptionsApply");
88

99
const JavascriptModulesPlugin = require("./JavascriptModulesPlugin");
1010
const JsonModulesPlugin = require("./JsonModulesPlugin");
11-
const WebAssemblyModulesPlugin = require("./WebAssemblyModulesPlugin");
11+
const WebAssemblyModulesPlugin = require("./wasm/WebAssemblyModulesPlugin");
1212

1313
const LoaderTargetPlugin = require("./LoaderTargetPlugin");
1414
const FunctionModulePlugin = require("./FunctionModulePlugin");

lib/node/ReadFileCompileWasmTemplatePlugin.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"use strict";
66

77
const BaseWasmMainTemplatePlugin = require("../BaseWasmMainTemplatePlugin");
8-
const WasmModuleTemplatePlugin = require("../wasm/WasmModuleTemplatePlugin");
98

109
class ReadFileCompileWasmTemplatePlugin {
1110
apply(compiler) {
@@ -37,9 +36,6 @@ class ReadFileCompileWasmTemplatePlugin {
3736
compilation.mainTemplate,
3837
generateLoadBinaryCode
3938
);
40-
new WasmModuleTemplatePlugin().apply(
41-
compilation.moduleTemplates.javascript
42-
);
4339
}
4440
);
4541
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
*/
4+
"use strict";
5+
6+
const WebpackError = require("../WebpackError");
7+
8+
module.exports = class UnsupportedWebAssemblyFeatureError extends WebpackError {
9+
/** @param {string} message Error message */
10+
constructor(message) {
11+
super();
12+
this.name = "UnsupportedWebAssemblyFeatureError";
13+
this.message = message;
14+
this.hideStack = true;
15+
16+
Error.captureStackTrace(this, this.constructor);
17+
}
18+
};

lib/wasm/WasmModuleTemplatePlugin.js

Lines changed: 0 additions & 94 deletions
This file was deleted.

0 commit comments

Comments
 (0)