1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | var fs = require('fs/promises');
|
6 | var crypto = require('crypto');
|
7 |
|
8 | function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
9 |
|
10 | var fs__default = _interopDefaultLegacy(fs);
|
11 |
|
12 | const LOADER_ID = '\0rawWasmLoader.js';
|
13 | const loader = `export function __resolvePath(filePath) {
|
14 | const isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null;
|
15 | if (isNode) {
|
16 | const path = require('path');
|
17 | return path.resolve(__dirname, filePath);
|
18 | } else return '/' + filePath;
|
19 | }`;
|
20 | function rawWasm(options = {}) {
|
21 | const { copy = true, publicPath = '/' } = options;
|
22 | const files = new Map();
|
23 | return {
|
24 | name: 'raw-wasm',
|
25 | resolveId(id) {
|
26 | return id === LOADER_ID ? id : null;
|
27 | },
|
28 | load: async function (id) {
|
29 | if (id === LOADER_ID) {
|
30 | return loader;
|
31 | }
|
32 | if (!/\.wasm$/.test(id)) {
|
33 | return null;
|
34 | }
|
35 | const [fileBuffer, sourceMapBuffer] = await Promise.allSettled([
|
36 | fs__default['default'].readFile(id),
|
37 | fs__default['default'].readFile(id + '.map'),
|
38 | ]).then((results) => results.map((result) => (result.status == 'fulfilled' ? result.value : null)));
|
39 | if (!fileBuffer)
|
40 | throw new Error('failed tp load file ' + id);
|
41 | const hash = crypto.createHash('sha1').update(fileBuffer).digest('hex').substr(0, 16);
|
42 | const filename = `${hash}.wasm`;
|
43 | const path = publicPath.substring(/^\//.test(publicPath) ? 1 : 0) + (/\/$/.test(publicPath) ? '' : '/');
|
44 | files.set(id, {
|
45 | filename,
|
46 | sourceMap: sourceMapBuffer && {
|
47 | source: sourceMapBuffer,
|
48 | fileName: id.split('/').pop() + '.map',
|
49 | },
|
50 | buffer: fileBuffer,
|
51 | });
|
52 | return `import {__resolvePath} from ${JSON.stringify(LOADER_ID)};export default __resolvePath('${path}${filename}')`;
|
53 | },
|
54 | generateBundle: async function () {
|
55 | if (!copy)
|
56 | return;
|
57 | await Promise.all([...files.values()].map(async ({ filename, sourceMap, buffer }) => {
|
58 | this.emitFile({
|
59 | type: 'asset',
|
60 | source: buffer,
|
61 | name: 'Rollup WASM Asset',
|
62 | fileName: filename,
|
63 | });
|
64 | if (sourceMap)
|
65 | this.emitFile({
|
66 | type: 'asset',
|
67 | source: sourceMap.source,
|
68 | name: 'Rollup WASM Asset SourceMap',
|
69 | fileName: sourceMap.fileName,
|
70 | });
|
71 | }));
|
72 | },
|
73 | };
|
74 | }
|
75 |
|
76 | exports.default = rawWasm;
|
77 | exports.rawWasm = rawWasm;
|
78 | module.exports = Object.assign(exports.default, exports);
|