UNPKG

assemblyscript-loader-loader

Version:

A webpack loader that compiles AssemblyScript to WebAssembly.

85 lines 3.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const memfs_1 = require("memfs"); const path_1 = tslib_1.__importDefault(require("path")); const asc_1 = tslib_1.__importDefault(require("assemblyscript/cli/asc")); const loader_utils_1 = tslib_1.__importDefault(require("loader-utils")); const schema_utils_1 = tslib_1.__importDefault(require("schema-utils")); const option_1 = require("./option"); const loaderpkg = '@assemblyscript/loader'; const configuration = { name: 'assemblyscript-loader-loader', }; const getInBundleWasmModule = (buf) => ` import loader from '${loaderpkg}'; export default async (imports) => { const buffer = new ArrayBuffer(${buf.length}); const uint8 = new Uint8Array(buffer); uint8.set([${buf.join(',')}]); const { exports } = await loader.instantiate(buffer, imports || {}); return exports; };`; const getFileWasmModule = (fetchUrl) => ` import loader from '${loaderpkg}'; export default async (imports) => { const { exports } = await loader.instantiateStreaming(fetch(${fetchUrl}), imports || {}); return exports; }`; function assemblyscriptLoader() { const options = loader_utils_1.default.getOptions(this) || {}; const callback = this.async(); // eslint-disable-next-line const buildPath = this._compiler.outputPath; const outputPath = path_1.default.join(buildPath, `${path_1.default.parse(this.resourcePath).name}.wasm`); const ascArgv = [ path_1.default.relative(process.cwd(), this.resourcePath), '-o', path_1.default.relative(process.cwd(), outputPath), ...option_1.transOptionToAscOption(options), ]; schema_utils_1.default(option_1.schemaOptions, options, configuration); this.addDependency(loaderpkg); asc_1.default.ready.then(() => { asc_1.default.main(ascArgv, { writeFile: (filename, contents) => { const dirPath = path_1.default.dirname(outputPath); if (!memfs_1.fs.existsSync(dirPath)) memfs_1.fs.mkdirpSync(dirPath); memfs_1.fs.writeFileSync(outputPath, contents); }, }, (err) => { if (err) { callback(err); return 0; } const size = memfs_1.fs.statSync(outputPath).size; const wasmFile = memfs_1.fs.readFileSync(outputPath); // 默认 1000 const limit = Number(options.limit) || 1000; if (size <= limit) { callback(null, getInBundleWasmModule(Buffer.from(wasmFile))); } else { const fileName = loader_utils_1.default.interpolateName(this, typeof options.name === 'string' && options.name ? options.name : '[name].[hash:7].wasm', { context: this.rootContext, content: wasmFile, }); let fetchUrl = `__webpack_public_path__ + ${JSON.stringify(fileName)}`; if (options.publicPath && typeof options.publicPath === 'string') { fetchUrl = JSON.stringify(path_1.default.posix.join(options.publicPath, fileName)); } else if (options.useInWorker) { fetchUrl = `self.location.origin + '/${fileName}'`; } this.emitFile(fileName, wasmFile, undefined); callback(null, getFileWasmModule(fetchUrl)); } return 0; }); }); } exports.default = assemblyscriptLoader; ; //# sourceMappingURL=index.js.map