(options: RollupWasmOptions = {})
| 10 | import { getHelpersModule, HELPERS_ID } from './helper'; |
| 11 | |
| 12 | export function wasm(options: RollupWasmOptions = {}): Plugin { |
| 13 | const { |
| 14 | sync = [], |
| 15 | maxFileSize = 14 * 1024, |
| 16 | publicPath = '', |
| 17 | targetEnv = 'auto', |
| 18 | fileName = '[hash][extname]' |
| 19 | } = options; |
| 20 | |
| 21 | const syncFiles = sync.map((x) => path.resolve(x)); |
| 22 | const copies = Object.create(null); |
| 23 | const filter = createFilter(options.include, options.exclude); |
| 24 | |
| 25 | return { |
| 26 | name: 'wasm', |
| 27 | |
| 28 | resolveId(id) { |
| 29 | if (id === HELPERS_ID) { |
| 30 | return id; |
| 31 | } |
| 32 | |
| 33 | return null; |
| 34 | }, |
| 35 | |
| 36 | load(id) { |
| 37 | if (id === HELPERS_ID) { |
| 38 | return getHelpersModule(targetEnv); |
| 39 | } |
| 40 | |
| 41 | if (!filter(id)) { |
| 42 | return null; |
| 43 | } |
| 44 | |
| 45 | if (!/\.wasm$/.test(id)) { |
| 46 | return null; |
| 47 | } |
| 48 | this.addWatchFile(id); |
| 49 | |
| 50 | return Promise.all([fs.promises.stat(id), fs.promises.readFile(id)]).then( |
| 51 | ([stats, buffer]) => { |
| 52 | if (targetEnv === 'auto-inline') { |
| 53 | return buffer.toString('binary'); |
| 54 | } |
| 55 | |
| 56 | if ((maxFileSize && stats.size > maxFileSize) || maxFileSize === 0) { |
| 57 | const hash = createHash('sha1').update(buffer).digest('hex').substr(0, 16); |
| 58 | const ext = path.extname(id); |
| 59 | const name = path.basename(id, ext); |
| 60 | |
| 61 | const outputFileName = fileName |
| 62 | .replace(/\[hash\]/g, hash) |
| 63 | .replace(/\[extname\]/g, ext) |
| 64 | .replace(/\[name\]/g, name); |
| 65 | |
| 66 | const publicFilepath = `${publicPath}${outputFileName}`; |
| 67 | |
| 68 | // only copy if the file is not marked `sync`, `sync` files are always inlined |
| 69 | if (syncFiles.indexOf(id) === -1) { |
nothing calls this directly
no test coverage detected