@@ -6,6 +6,8 @@ import { Plugin } from 'rollup';
66
77import { RollupWasmOptions } from '../types' ;
88
9+ import { getHelpersModule , HELPERS_ID } from './helper' ;
10+
911export function wasm ( options : RollupWasmOptions = { } ) : Plugin {
1012 const { sync = [ ] , maxFileSize = 14 * 1024 , publicPath = '' } = options ;
1113
@@ -15,7 +17,19 @@ export function wasm(options: RollupWasmOptions = {}): Plugin {
1517 return {
1618 name : 'wasm' ,
1719
20+ resolveId ( id ) {
21+ if ( id === HELPERS_ID ) {
22+ return id ;
23+ }
24+
25+ return null ;
26+ } ,
27+
1828 load ( id ) {
29+ if ( id === HELPERS_ID ) {
30+ return getHelpersModule ( ) ;
31+ }
32+
1933 if ( ! / \. w a s m $ / . test ( id ) ) {
2034 return null ;
2135 }
@@ -46,59 +60,6 @@ export function wasm(options: RollupWasmOptions = {}): Plugin {
4660 ) ;
4761 } ,
4862
49- banner : `
50- function _loadWasmModule (sync, filepath, src, imports) {
51- function _instantiateOrCompile(source, imports, stream) {
52- var instantiateFunc = stream ? WebAssembly.instantiateStreaming : WebAssembly.instantiate;
53- var compileFunc = stream ? WebAssembly.compileStreaming : WebAssembly.compile;
54-
55- if (imports) {
56- return instantiateFunc(source, imports)
57- } else {
58- return compileFunc(source)
59- }
60- }
61-
62- var buf = null
63- var isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null
64-
65- if (filepath && isNode) {
66- var fs = eval('require("fs")')
67- var path = eval('require("path")')
68-
69- return new Promise((resolve, reject) => {
70- fs.readFile(path.resolve(__dirname, filepath), (error, buffer) => {
71- if (error != null) {
72- reject(error)
73- }
74-
75- resolve(_instantiateOrCompile(buffer, imports, false))
76- });
77- });
78- } else if (filepath) {
79- return _instantiateOrCompile(fetch(filepath), imports, true)
80- }
81-
82- if (isNode) {
83- buf = Buffer.from(src, 'base64')
84- } else {
85- var raw = globalThis.atob(src)
86- var rawLength = raw.length
87- buf = new Uint8Array(new ArrayBuffer(rawLength))
88- for(var i = 0; i < rawLength; i++) {
89- buf[i] = raw.charCodeAt(i)
90- }
91- }
92-
93- if(sync) {
94- var mod = new WebAssembly.Module(buf)
95- return imports ? new WebAssembly.Instance(mod, imports) : mod
96- } else {
97- return _instantiateOrCompile(buf, imports, false)
98- }
99- }
100- ` . trim ( ) ,
101-
10263 transform ( code , id ) {
10364 if ( code && / \. w a s m $ / . test ( id ) ) {
10465 const isSync = syncFiles . indexOf ( id ) !== - 1 ;
@@ -115,7 +76,8 @@ export function wasm(options: RollupWasmOptions = {}): Plugin {
11576 src = null ;
11677 }
11778
118- return `export default function(imports){return _loadWasmModule(${ + isSync } , ${ publicFilepath } , ${ src } , imports)}` ;
79+ return `import { _loadWasmModule } from ${ JSON . stringify ( HELPERS_ID ) } ;
80+ export default function(imports){return _loadWasmModule(${ + isSync } , ${ publicFilepath } , ${ src } , imports)}` ;
11981 }
12082 return null ;
12183 } ,
0 commit comments