forked from parcel-bundler/lightningcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
77 lines (65 loc) · 2.25 KB
/
index.mjs
File metadata and controls
77 lines (65 loc) · 2.25 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { Environment, napi } from 'napi-wasm';
import { await_promise_sync, createBundleAsync } from './async.mjs';
let wasm, bundleAsyncInternal;
export default async function init(input) {
input = input ?? new URL('lightningcss_node.wasm', import.meta.url);
if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) {
input = fetchOrReadFromFs(input);
}
const { instance } = await load(await input, {
env: {
...napi,
await_promise_sync
}
});
let env = new Environment(instance);
wasm = env.exports;
bundleAsyncInternal = createBundleAsync(env);
}
export function transform(options) {
return wasm.transform(options);
}
export function transformStyleAttribute(options) {
return wasm.transformStyleAttribute(options);
}
export function bundle(options) {
return wasm.bundle(options);
}
export function bundleAsync(options) {
return bundleAsyncInternal(options);
}
export { browserslistToTargets } from './browserslistToTargets.js';
export { Features } from './flags.js';
export { composeVisitors } from './composeVisitors.js';
async function load(module, imports) {
if (typeof Response === 'function' && module instanceof Response) {
if (typeof WebAssembly.instantiateStreaming === 'function') {
try {
return await WebAssembly.instantiateStreaming(module, imports);
} catch (e) {
if (module.headers.get('Content-Type') != 'application/wasm') {
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
} else {
throw e;
}
}
}
const bytes = await module.arrayBuffer();
return await WebAssembly.instantiate(bytes, imports);
} else {
const instance = await WebAssembly.instantiate(module, imports);
if (instance instanceof WebAssembly.Instance) {
return { instance, module };
} else {
return instance;
}
}
}
async function fetchOrReadFromFs(inputPath) {
try {
const fs = await import('fs');
return fs.readFileSync(inputPath);
} catch {
return fetch(inputPath);
}
};