@gjsify/lightningcss-wasm 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.mjs ADDED
@@ -0,0 +1,110 @@
1
+ // @gjsify/lightningcss-wasm — GJS-compatible loader for the lightningcss
2
+ // WebAssembly bundle.
3
+ //
4
+ // Adapted from refs/lightningcss/wasm/wasm-node.mjs (Devon Govett, MIT).
5
+ // Modifications:
6
+ // - Reads the .wasm via @gjsify/fs (Soup-free, GJS-native) instead of
7
+ // Node's fs.readFileSync.
8
+ // - getRandomValues comes from globalThis.crypto, which @gjsify/webcrypto
9
+ // registers under --globals auto (the Web Crypto API spec, not Node's
10
+ // `node:crypto` shim).
11
+ // - SpiderMonkey 140 has the synchronous `new WebAssembly.{Module,Instance}`
12
+ // constructors but lacks instantiateStreaming for `file://` URLs, so we
13
+ // use the constructors directly.
14
+ //
15
+ // Pure-JS — bundled by `gjsify build --library esm` so consumers can
16
+ // import the resulting ESM with no native deps.
17
+
18
+ import { Environment, napi } from './napi-wasm.mjs';
19
+ import { await_promise_sync, createBundleAsync } from './async.mjs';
20
+ import { fileURLToPath } from 'node:url';
21
+ import fs from 'node:fs';
22
+
23
+ const wasmUrl = new URL('../wasm/lightningcss_node.wasm', import.meta.url);
24
+
25
+ // `gjsify build` rewrites `import.meta.url` to be portable. At runtime
26
+ // we read the bytes via the host's fs polyfill — under GJS that's
27
+ // @gjsify/fs (Gio.File backed); under Node it's the real `fs`.
28
+ const wasmBytes = fs.readFileSync(fileURLToPath(wasmUrl));
29
+ const wasmModule = new WebAssembly.Module(wasmBytes);
30
+
31
+ let env;
32
+ const instance = new WebAssembly.Instance(wasmModule, {
33
+ env: {
34
+ ...napi,
35
+ await_promise_sync,
36
+ __getrandom_v03_custom: (ptr, len) => {
37
+ const buf = env.memory.subarray(ptr, ptr + len);
38
+ // globalThis.crypto from @gjsify/webcrypto/register on GJS;
39
+ // node:crypto webcrypto on Node 19+.
40
+ globalThis.crypto.getRandomValues(buf);
41
+ },
42
+ },
43
+ });
44
+ instance.exports.register_module();
45
+ env = new Environment(instance);
46
+ const wasm = env.exports;
47
+ const bundleAsyncInternal = createBundleAsync(env);
48
+
49
+ export default async function init() {
50
+ // No-op. Kept for API compatibility with the npm `lightningcss-wasm`
51
+ // package whose browser-side loader needs an explicit init().
52
+ }
53
+
54
+ export function transform(options) {
55
+ return wrap(wasm.transform, options);
56
+ }
57
+
58
+ export function transformStyleAttribute(options) {
59
+ return wrap(wasm.transformStyleAttribute, options);
60
+ }
61
+
62
+ export function bundle(options) {
63
+ return wrap(wasm.bundle, {
64
+ ...options,
65
+ resolver: {
66
+ read: (filePath) => fs.readFileSync(filePath, 'utf8'),
67
+ },
68
+ });
69
+ }
70
+
71
+ export async function bundleAsync(options) {
72
+ if (!options.resolver?.read) {
73
+ options.resolver = {
74
+ ...options.resolver,
75
+ read: (filePath) => fs.readFileSync(filePath, 'utf8'),
76
+ };
77
+ }
78
+ return wrap(bundleAsyncInternal, options);
79
+ }
80
+
81
+ function wrap(call, options) {
82
+ if (typeof options.visitor === 'function') {
83
+ let deps = [];
84
+ options.visitor = options.visitor({
85
+ addDependency(dep) {
86
+ deps.push(dep);
87
+ },
88
+ });
89
+
90
+ let result = call(options);
91
+ if (result instanceof Promise) {
92
+ result = result.then((res) => {
93
+ if (deps.length) {
94
+ res.dependencies ??= [];
95
+ res.dependencies.push(...deps);
96
+ }
97
+ return res;
98
+ });
99
+ } else if (deps.length) {
100
+ result.dependencies ??= [];
101
+ result.dependencies.push(...deps);
102
+ }
103
+ return result;
104
+ }
105
+ return call(options);
106
+ }
107
+
108
+ export { browserslistToTargets } from './browserslistToTargets.js';
109
+ export { Features } from './flags.js';
110
+ export { composeVisitors } from './composeVisitors.js';