Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F101479188
ReadFileCompileAsyncWasmPlugin.js
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Mon, Feb 10, 20:48
Size
2 KB
Mime Type
text/x-c++
Expires
Wed, Feb 12, 20:48 (2 d)
Engine
blob
Format
Raw Data
Handle
24160737
Attached To
rOACCT Open Access Compliance Check Tool (OACCT)
ReadFileCompileAsyncWasmPlugin.js
View Options
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const RuntimeGlobals = require("../RuntimeGlobals");
const Template = require("../Template");
const AsyncWasmChunkLoadingRuntimeModule = require("../wasm-async/AsyncWasmChunkLoadingRuntimeModule");
/** @typedef {import("../Compiler")} Compiler */
class ReadFileCompileAsyncWasmPlugin {
/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.thisCompilation.tap(
"ReadFileCompileAsyncWasmPlugin",
compilation => {
const globalWasmLoading = compilation.outputOptions.wasmLoading;
const isEnabledForChunk = chunk => {
const options = chunk.getEntryOptions();
const wasmLoading =
(options && options.wasmLoading) || globalWasmLoading;
return wasmLoading === "async-node";
};
const generateLoadBinaryCode = path =>
Template.asString([
"new Promise(function (resolve, reject) {",
Template.indent([
"var { readFile } = require('fs');",
"var { join } = require('path');",
"",
"try {",
Template.indent([
`readFile(join(__dirname, ${path}), function(err, buffer){`,
Template.indent([
"if (err) return reject(err);",
"",
"// Fake fetch response",
"resolve({",
Template.indent(["arrayBuffer() { return buffer; }"]),
"});"
]),
"});"
]),
"} catch (err) { reject(err); }"
]),
"})"
]);
compilation.hooks.runtimeRequirementInTree
.for(RuntimeGlobals.instantiateWasm)
.tap("ReadFileCompileAsyncWasmPlugin", (chunk, set) => {
if (!isEnabledForChunk(chunk)) return;
const chunkGraph = compilation.chunkGraph;
if (
!chunkGraph.hasModuleInGraph(
chunk,
m => m.type === "webassembly/async"
)
) {
return;
}
set.add(RuntimeGlobals.publicPath);
compilation.addRuntimeModule(
chunk,
new AsyncWasmChunkLoadingRuntimeModule({
generateLoadBinaryCode,
supportsStreaming: false
})
);
});
}
);
}
}
module.exports = ReadFileCompileAsyncWasmPlugin;
Event Timeline
Log In to Comment