@jacobknightley/fabric-format 0.0.2 → 0.0.3
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.
|
@@ -8,6 +8,56 @@ import { RUFF_WASM_CONFIG } from './config.js';
|
|
|
8
8
|
// Dynamic import for ruff WASM (loaded on demand)
|
|
9
9
|
let ruffModule = null;
|
|
10
10
|
let workspace = null;
|
|
11
|
+
/**
|
|
12
|
+
* Detect if we're running in Node.js
|
|
13
|
+
*/
|
|
14
|
+
function isNodeEnvironment() {
|
|
15
|
+
return typeof process !== 'undefined' &&
|
|
16
|
+
process.versions != null &&
|
|
17
|
+
process.versions.node != null;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Find the WASM file path relative to the ruff-wasm-web package in Node.js.
|
|
21
|
+
* Uses indirect dynamic imports to avoid bundler static analysis.
|
|
22
|
+
*/
|
|
23
|
+
async function findWasmFileForNode() {
|
|
24
|
+
// Use Function constructor to create dynamic import that bundlers can't statically analyze
|
|
25
|
+
// This is intentional - these modules only exist in Node.js, not in browsers
|
|
26
|
+
const dynamicImport = new Function('specifier', 'return import(specifier)');
|
|
27
|
+
const { createRequire } = await dynamicImport('module');
|
|
28
|
+
const { dirname, join } = await dynamicImport('path');
|
|
29
|
+
const { readFile } = await dynamicImport('fs/promises');
|
|
30
|
+
// Get the path to ruff-wasm-web package
|
|
31
|
+
// We need import.meta.url to create a require function
|
|
32
|
+
// Use a fallback for bundled environments (though this path shouldn't be hit in browsers)
|
|
33
|
+
let ruffWasmDir;
|
|
34
|
+
try {
|
|
35
|
+
const require = createRequire(import.meta.url);
|
|
36
|
+
const ruffWasmPath = require.resolve('@astral-sh/ruff-wasm-web');
|
|
37
|
+
ruffWasmDir = dirname(ruffWasmPath);
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
// Fallback: try to find it via node_modules traversal
|
|
41
|
+
const { fileURLToPath } = await dynamicImport('url');
|
|
42
|
+
const currentDir = dirname(fileURLToPath(import.meta.url));
|
|
43
|
+
// Walk up to find node_modules
|
|
44
|
+
let searchDir = currentDir;
|
|
45
|
+
const { existsSync } = await dynamicImport('fs');
|
|
46
|
+
while (searchDir !== dirname(searchDir)) {
|
|
47
|
+
const candidate = join(searchDir, 'node_modules', '@astral-sh', 'ruff-wasm-web');
|
|
48
|
+
if (existsSync(candidate)) {
|
|
49
|
+
ruffWasmDir = candidate;
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
searchDir = dirname(searchDir);
|
|
53
|
+
}
|
|
54
|
+
if (!ruffWasmDir) {
|
|
55
|
+
throw new Error('Could not locate @astral-sh/ruff-wasm-web package');
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const wasmPath = join(ruffWasmDir, 'ruff_wasm_bg.wasm');
|
|
59
|
+
return readFile(wasmPath);
|
|
60
|
+
}
|
|
11
61
|
/**
|
|
12
62
|
* Python formatter using Ruff WASM.
|
|
13
63
|
*/
|
|
@@ -43,9 +93,13 @@ export class PythonFormatter {
|
|
|
43
93
|
// Use async initialization with provided URL
|
|
44
94
|
await ruffModule.default({ module_or_path: this.wasmOptions.wasmUrl });
|
|
45
95
|
}
|
|
96
|
+
else if (isNodeEnvironment()) {
|
|
97
|
+
// Node.js: Load WASM file from disk
|
|
98
|
+
const wasmBinary = await findWasmFileForNode();
|
|
99
|
+
ruffModule.initSync({ module: wasmBinary });
|
|
100
|
+
}
|
|
46
101
|
else {
|
|
47
|
-
//
|
|
48
|
-
// This works in Node.js and ESM environments but may fail in bundled IIFE
|
|
102
|
+
// Browser: let ruff-wasm-web use import.meta.url to find the WASM file
|
|
49
103
|
await ruffModule.default();
|
|
50
104
|
}
|
|
51
105
|
// Create workspace with config
|
package/package.json
CHANGED