/
wasm-node.mjs
57 lines (49 loc) · 1.51 KB
/
wasm-node.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { Environment, napi } from 'napi-wasm';
import { await_promise_sync, createBundleAsync } from './async.mjs';
import fs from 'fs';
import {webcrypto as crypto} from 'node:crypto';
let wasmBytes = fs.readFileSync(new URL('lightningcss_node.wasm', import.meta.url));
let wasmModule = new WebAssembly.Module(wasmBytes);
let instance = new WebAssembly.Instance(wasmModule, {
env: {
...napi,
await_promise_sync,
__getrandom_custom: (ptr, len) => {
let buf = env.memory.subarray(ptr, ptr + len);
crypto.getRandomValues(buf);
},
},
});
instance.exports.register_module();
let env = new Environment(instance);
let wasm = env.exports;
let bundleAsyncInternal = createBundleAsync(env);
export default async function init() {
// do nothing. for backward compatibility.
}
export function transform(options) {
return wasm.transform(options);
}
export function transformStyleAttribute(options) {
return wasm.transformStyleAttribute(options);
}
export function bundle(options) {
return wasm.bundle({
...options,
resolver: {
read: (filePath) => fs.readFileSync(filePath, 'utf8')
}
});
}
export async function bundleAsync(options) {
if (!options.resolver?.read) {
options.resolver = {
...options.resolver,
read: (filePath) => fs.readFileSync(filePath, 'utf8')
};
}
return bundleAsyncInternal(options);
}
export { browserslistToTargets } from './browserslistToTargets.js'
export { Features } from './flags.js'
export { composeVisitors } from './composeVisitors.js';