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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(types): wasm and runtime #17267

Merged
merged 3 commits into from
May 31, 2023
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
4 changes: 2 additions & 2 deletions lib/AsyncDependenciesBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ class AsyncDependenciesBlock extends DependenciesBlock {
}

/**
* @returns {string} The name of the chunk
* @returns {string | undefined} The name of the chunk
*/
get chunkName() {
return this.groupOptions.name;
}

/**
* @param {string} value The new chunk name
* @param {string | undefined} value The new chunk name
* @returns {void}
*/
set chunkName(value) {
Expand Down
1 change: 1 addition & 0 deletions lib/AutomaticPrefetchPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class AutomaticPrefetchPlugin {
);
}
);
/** @type {{context: string, request: string}[] | null} */
let lastModules = null;
compiler.hooks.afterCompile.tap("AutomaticPrefetchPlugin", compilation => {
lastModules = [];
Expand Down
4 changes: 4 additions & 0 deletions lib/BannerPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const validate = createSchemaValidation(
}
);

/**
* @param {string} str string to wrap
* @returns {string} wrapped string
*/
const wrapComment = str => {
if (!str.includes("\n")) {
return Template.toComment(str);
Expand Down
4 changes: 2 additions & 2 deletions lib/RuntimeTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -794,10 +794,10 @@ class RuntimeTemplate {
* @param {Module} options.originModule the origin module
* @param {boolean|undefined} options.asiSafe true, if location is safe for ASI, a bracket can be emitted
* @param {boolean} options.isCall true, if expression will be called
* @param {boolean} options.callContext when false, call context will not be preserved
* @param {boolean | null} options.callContext when false, call context will not be preserved
* @param {boolean} options.defaultInterop when true and accessing the default exports, interop code will be generated
* @param {string} options.importVar the identifier name of the import variable
* @param {InitFragment[]} options.initFragments init fragments will be added here
* @param {InitFragment<TODO>[]} options.initFragments init fragments will be added here
* @param {RuntimeSpec} options.runtime runtime for which this code will be generated
* @param {Set<string>} options.runtimeRequirements if set, will be filled with runtime requirements
* @returns {string} expression
Expand Down
19 changes: 17 additions & 2 deletions lib/node/CommonJsChunkLoadingPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@
const RuntimeGlobals = require("../RuntimeGlobals");
const StartupChunkDependenciesPlugin = require("../runtime/StartupChunkDependenciesPlugin");

/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../Compiler")} Compiler */

/** @typedef {Object} CommonJsChunkLoadingPluginOptions
* @property {boolean} [asyncChunkLoading] enable async chunk loading
*/

class CommonJsChunkLoadingPlugin {
constructor(options) {
options = options || {};
/**
* @param {CommonJsChunkLoadingPluginOptions} [options] options
*/
constructor(options = {}) {
this._asyncChunkLoading = options.asyncChunkLoading;
}

Expand All @@ -36,6 +43,10 @@ class CommonJsChunkLoadingPlugin {
"CommonJsChunkLoadingPlugin",
compilation => {
const globalChunkLoading = compilation.outputOptions.chunkLoading;
/**
* @param {Chunk} chunk chunk
* @returns {boolean} true, if wasm loading is enabled for the chunk
*/
const isEnabledForChunk = chunk => {
const options = chunk.getEntryOptions();
const chunkLoading =
Expand All @@ -45,6 +56,10 @@ class CommonJsChunkLoadingPlugin {
return chunkLoading === chunkLoadingValue;
};
const onceForChunkSet = new WeakSet();
/**
* @param {Chunk} chunk chunk
* @param {Set<string>} set runtime requirements
*/
const handler = (chunk, set) => {
if (onceForChunkSet.has(chunk)) return;
onceForChunkSet.add(chunk);
Expand Down
12 changes: 10 additions & 2 deletions lib/node/NodeTemplatePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ const EnableChunkLoadingPlugin = require("../javascript/EnableChunkLoadingPlugin

/** @typedef {import("../Compiler")} Compiler */

/**
* @typedef {Object} NodeTemplatePluginOptions
* @property {boolean} [asyncChunkLoading] enable async chunk loading
*/

class NodeTemplatePlugin {
constructor(options) {
this._options = options || {};
/**
* @param {NodeTemplatePluginOptions} [options] options object
*/
constructor(options = {}) {
this._options = options;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/node/NodeWatchFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class NodeWatchFileSystem {
* @param {Iterable<string>} missing watched exitance entries
* @param {number} startTime timestamp of start time
* @param {WatchOptions} options options object
* @param {function(Error=, Map<string, FileSystemInfoEntry>, Map<string, FileSystemInfoEntry>, Set<string>, Set<string>): void} callback aggregated callback
* @param {function((Error | null)=, Map<string, FileSystemInfoEntry>, Map<string, FileSystemInfoEntry>, Set<string>, Set<string>): void} callback aggregated callback
* @param {function(string, number): void} callbackUndelayed callback when the first change was detected
* @returns {Watcher} a watcher
*/
Expand Down
5 changes: 4 additions & 1 deletion lib/node/ReadFileChunkLoadingRuntimeModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const { getUndoPath } = require("../util/identifier");
/** @typedef {import("../Chunk")} Chunk */

class ReadFileChunkLoadingRuntimeModule extends RuntimeModule {
/**
* @param {ReadonlySet<string>} runtimeRequirements runtime requirements
*/
constructor(runtimeRequirements) {
super("readFile chunk loading", RuntimeModule.STAGE_ATTACH);
this.runtimeRequirements = runtimeRequirements;
Expand Down Expand Up @@ -78,7 +81,7 @@ class ReadFileChunkLoadingRuntimeModule extends RuntimeModule {
);
const rootOutputDir = getUndoPath(
outputName,
this.compilation.outputOptions.path,
/** @type {string} */ (this.compilation.outputOptions.path),
false
);

Expand Down
8 changes: 8 additions & 0 deletions lib/node/ReadFileCompileAsyncWasmPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const RuntimeGlobals = require("../RuntimeGlobals");
const Template = require("../Template");
const AsyncWasmLoadingRuntimeModule = require("../wasm-async/AsyncWasmLoadingRuntimeModule");

/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../Compiler")} Compiler */

class ReadFileCompileAsyncWasmPlugin {
Expand All @@ -27,6 +28,10 @@ class ReadFileCompileAsyncWasmPlugin {
"ReadFileCompileAsyncWasmPlugin",
compilation => {
const globalWasmLoading = compilation.outputOptions.wasmLoading;
/**
* @param {Chunk} chunk chunk
* @returns {boolean} true, if wasm loading is enabled for the chunk
*/
const isEnabledForChunk = chunk => {
const options = chunk.getEntryOptions();
const wasmLoading =
Expand All @@ -35,6 +40,9 @@ class ReadFileCompileAsyncWasmPlugin {
: globalWasmLoading;
return wasmLoading === this._type;
};
/**
* @type {(path: string) => string}
*/
const generateLoadBinaryCode = this._import
? path =>
Template.asString([
Expand Down
21 changes: 19 additions & 2 deletions lib/node/ReadFileCompileWasmPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,22 @@ const RuntimeGlobals = require("../RuntimeGlobals");
const Template = require("../Template");
const WasmChunkLoadingRuntimeModule = require("../wasm-sync/WasmChunkLoadingRuntimeModule");

/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../Compiler")} Compiler */

/**
* @typedef {Object} ReadFileCompileWasmPluginOptions
* @property {boolean} [mangleImports] mangle imports
*/

// TODO webpack 6 remove

class ReadFileCompileWasmPlugin {
constructor(options) {
this.options = options || {};
/**
* @param {ReadFileCompileWasmPluginOptions} [options] options object
*/
constructor(options = {}) {
this.options = options;
}

/**
Expand All @@ -29,6 +38,10 @@ class ReadFileCompileWasmPlugin {
"ReadFileCompileWasmPlugin",
compilation => {
const globalWasmLoading = compilation.outputOptions.wasmLoading;
/**
* @param {Chunk} chunk chunk
* @returns {boolean} true, when wasm loading is enabled for the chunk
*/
const isEnabledForChunk = chunk => {
const options = chunk.getEntryOptions();
const wasmLoading =
Expand All @@ -37,6 +50,10 @@ class ReadFileCompileWasmPlugin {
: globalWasmLoading;
return wasmLoading === "async-node";
};
/**
* @param {string} path path to wasm file
* @returns {string} generated code to load the wasm file
*/
const generateLoadBinaryCode = path =>
Template.asString([
"new Promise(function (resolve, reject) {",
Expand Down
5 changes: 4 additions & 1 deletion lib/node/RequireChunkLoadingRuntimeModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const { getUndoPath } = require("../util/identifier");
/** @typedef {import("../Chunk")} Chunk */

class RequireChunkLoadingRuntimeModule extends RuntimeModule {
/**
* @param {ReadonlySet<string>} runtimeRequirements runtime requirements
*/
constructor(runtimeRequirements) {
super("require chunk loading", RuntimeModule.STAGE_ATTACH);
this.runtimeRequirements = runtimeRequirements;
Expand Down Expand Up @@ -78,7 +81,7 @@ class RequireChunkLoadingRuntimeModule extends RuntimeModule {
);
const rootOutputDir = getUndoPath(
outputName,
this.compilation.outputOptions.path,
/** @type {string} */ (this.compilation.outputOptions.path),
true
);

Expand Down
9 changes: 9 additions & 0 deletions lib/wasm-async/AsyncWasmLoadingRuntimeModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ const RuntimeGlobals = require("../RuntimeGlobals");
const RuntimeModule = require("../RuntimeModule");
const Template = require("../Template");

/**
* @typedef {Object} AsyncWasmLoadingRuntimeModuleOptions
* @property {function(string): string} generateLoadBinaryCode
* @property {boolean} supportsStreaming
*/

class AsyncWasmLoadingRuntimeModule extends RuntimeModule {
/**
* @param {AsyncWasmLoadingRuntimeModuleOptions} options options
*/
constructor({ generateLoadBinaryCode, supportsStreaming }) {
super("wasm loading", RuntimeModule.STAGE_NORMAL);
this.generateLoadBinaryCode = generateLoadBinaryCode;
Expand Down
10 changes: 9 additions & 1 deletion lib/wasm-async/AsyncWebAssemblyGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ const Generator = require("../Generator");

const TYPES = new Set(["webassembly"]);

/**
* @typedef {Object} AsyncWebAssemblyGeneratorOptions
* @property {boolean} [mangleImports] mangle imports
*/

class AsyncWebAssemblyGenerator extends Generator {
/**
* @param {AsyncWebAssemblyGeneratorOptions} options options
*/
constructor(options) {
super();
this.options = options;
Expand Down Expand Up @@ -46,7 +54,7 @@ class AsyncWebAssemblyGenerator extends Generator {
* @returns {Source} generated code
*/
generate(module, generateContext) {
return module.originalSource();
return /** @type {Source} */ (module.originalSource());
}
}

Expand Down
16 changes: 13 additions & 3 deletions lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const Template = require("../Template");
const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");

/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("../../declarations/WebpackOptions").OutputNormalized} OutputOptions */
/** @typedef {import("../DependencyTemplates")} DependencyTemplates */
/** @typedef {import("../Generator").GenerateContext} GenerateContext */
/** @typedef {import("../Module")} Module */
Expand All @@ -21,7 +22,14 @@ const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDe

const TYPES = new Set(["webassembly"]);

/**
* @typedef {{ request: string, importVar: string }} ImportObjRequestItem
*/

class AsyncWebAssemblyJavascriptGenerator extends Generator {
/**
* @param {OutputOptions["webassemblyModuleFilename"]} filenameTemplate template for the WebAssembly module filename
*/
constructor(filenameTemplate) {
super();
this.filenameTemplate = filenameTemplate;
Expand Down Expand Up @@ -61,9 +69,9 @@ class AsyncWebAssemblyJavascriptGenerator extends Generator {
runtimeRequirements.add(RuntimeGlobals.moduleId);
runtimeRequirements.add(RuntimeGlobals.exports);
runtimeRequirements.add(RuntimeGlobals.instantiateWasm);
/** @type {InitFragment[]} */
/** @type {InitFragment<InitFragment<string>>[]} */
const initFragments = [];
/** @type {Map<Module, { request: string, importVar: string }>} */
/** @type {Map<Module, ImportObjRequestItem>} */
const depModules = new Map();
/** @type {Map<string, WebAssemblyImportDependency[]>} */
const wasmDepsByRequest = new Map();
Expand Down Expand Up @@ -113,7 +121,9 @@ class AsyncWebAssemblyJavascriptGenerator extends Generator {
([request, deps]) => {
const exportItems = deps.map(dep => {
const importedModule = moduleGraph.getModule(dep);
const importVar = depModules.get(importedModule).importVar;
const importVar =
/** @type {ImportObjRequestItem} */
(depModules.get(importedModule)).importVar;
return `${JSON.stringify(
dep.name
)}: ${runtimeTemplate.exportFromImport({
Expand Down
21 changes: 19 additions & 2 deletions lib/wasm-async/AsyncWebAssemblyModulesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const { compareModulesByIdentifier } = require("../util/comparators");
const memoize = require("../util/memoize");

/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("../../declarations/WebpackOptions").OutputNormalized} OutputOptions */
/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../ChunkGraph")} ChunkGraph */
/** @typedef {import("../CodeGenerationResults")} CodeGenerationResults */
Expand All @@ -25,6 +26,7 @@ const memoize = require("../util/memoize");
/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
/** @typedef {import("../Template").RenderManifestEntry} RenderManifestEntry */
/** @typedef {import("../Template").RenderManifestOptions} RenderManifestOptions */
/** @typedef {import("../WebpackError")} WebpackError */

const getAsyncWebAssemblyGenerator = memoize(() =>
require("./AsyncWebAssemblyGenerator")
Expand All @@ -51,6 +53,11 @@ const getAsyncWebAssemblyParser = memoize(() =>
* @property {SyncWaterfallHook<[Source, Module, WebAssemblyRenderContext]>} renderModuleContent
*/

/**
* @typedef {Object} AsyncWebAssemblyModulesPluginOptions
* @property {boolean} [mangleImports] mangle imports
*/

/** @type {WeakMap<Compilation, CompilationHooks>} */
const compilationHooksMap = new WeakMap();

Expand Down Expand Up @@ -81,6 +88,9 @@ class AsyncWebAssemblyModulesPlugin {
return hooks;
}

/**
* @param {AsyncWebAssemblyModulesPluginOptions} options options
*/
constructor(options) {
this.options = options;
}
Expand Down Expand Up @@ -140,7 +150,8 @@ class AsyncWebAssemblyModulesPlugin {
)) {
if (module.type === WEBASSEMBLY_MODULE_TYPE_ASYNC) {
const filenameTemplate =
outputOptions.webassemblyModuleFilename;
/** @type {NonNullable<OutputOptions["webassemblyModuleFilename"]>} */
(outputOptions.webassemblyModuleFilename);

result.push({
render: () =>
Expand Down Expand Up @@ -178,6 +189,12 @@ class AsyncWebAssemblyModulesPlugin {
);
}

/**
* @param {Module} module the rendered module
* @param {WebAssemblyRenderContext} renderContext options object
* @param {CompilationHooks} hooks hooks
* @returns {Source} the newly generated source from rendering
*/
renderModule(module, renderContext, hooks) {
const { codeGenerationResults, chunk } = renderContext;
try {
Expand All @@ -192,7 +209,7 @@ class AsyncWebAssemblyModulesPlugin {
"AsyncWebAssemblyModulesPlugin.getCompilationHooks().renderModuleContent"
);
} catch (e) {
e.module = module;
/** @type {WebpackError} */ (e).module = module;
throw e;
}
}
Expand Down