Skip to content

Commit 1fc05ec

Browse files
committed
fix: remove findWasmBinary from bundles
Causing issue with bundlers Signed-off-by Gordon Smith <[email protected]>
1 parent 3d1a723 commit 1fc05ec

File tree

10 files changed

+335
-312
lines changed

10 files changed

+335
-312
lines changed

packages/base91/esbuild.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { neutralTpl } from "@hpcc-js/esbuild-plugins";
22
import { sfxWasm } from "@hpcc-js/esbuild-plugins/sfx-wrapper";
3+
import { replaceFunction } from "../../utils/esbuild-plugins.js";
34

45
// config ---
5-
await neutralTpl("src/index.ts", "dist/index", { plugins: [sfxWasm()] });
6+
await neutralTpl("src/index.ts", "dist/index", {
7+
plugins: [
8+
replaceFunction({
9+
'findWasmBinary': 'const findWasmBinary=()=>"";'
10+
}),
11+
sfxWasm()
12+
]
13+
});

packages/duckdb/esbuild.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { browserTpl } from "@hpcc-js/esbuild-plugins";
22
import { sfxWasm } from "@hpcc-js/esbuild-plugins/sfx-wrapper";
3+
import { replaceFunction } from "../../utils/esbuild-plugins.js";
34

45
// config ---
5-
await browserTpl("src/index.ts", "dist/index", { plugins: [sfxWasm()] });
6+
await browserTpl("src/index.ts", "dist/index", {
7+
plugins: [
8+
replaceFunction({
9+
'findWasmBinary': 'const findWasmBinary=()=>"";'
10+
}),
11+
sfxWasm()
12+
]
13+
});

packages/expat/esbuild.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { neutralTpl } from "@hpcc-js/esbuild-plugins";
22
import { sfxWasm } from "@hpcc-js/esbuild-plugins/sfx-wrapper";
3+
import { replaceFunction } from "../../utils/esbuild-plugins.js";
34

45
// config ---
5-
await neutralTpl("src/index.ts", "dist/index", { plugins: [sfxWasm()] });
6+
await neutralTpl("src/index.ts", "dist/index", {
7+
plugins: [
8+
replaceFunction({
9+
'findWasmBinary': 'const findWasmBinary=()=>"";'
10+
}),
11+
sfxWasm()
12+
]
13+
});

packages/graphviz/esbuild.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { neutralTpl } from "@hpcc-js/esbuild-plugins";
22
import { sfxWasm } from "@hpcc-js/esbuild-plugins/sfx-wrapper";
3+
import { replaceFunction } from "../../utils/esbuild-plugins.js";
34

4-
// config ---
5-
await neutralTpl("src/index.ts", "dist/index", { plugins: [sfxWasm()] });
5+
await neutralTpl("src/index.ts", "dist/index", {
6+
plugins: [
7+
replaceFunction({
8+
'findWasmBinary': 'const findWasmBinary=()=>"";'
9+
}),
10+
sfxWasm()
11+
]
12+
});

packages/llama/esbuild.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { neutralTpl } from "@hpcc-js/esbuild-plugins";
22
import { sfxWasm } from "@hpcc-js/esbuild-plugins/sfx-wrapper";
3+
import { replaceFunction } from "../../utils/esbuild-plugins.js";
34

45
// config ---
5-
await neutralTpl("src/index.ts", "dist/index", { plugins: [sfxWasm()] });
6+
await neutralTpl("src/index.ts", "dist/index", {
7+
plugins: [
8+
replaceFunction({
9+
'findWasmBinary': 'const findWasmBinary=()=>"";'
10+
}),
11+
sfxWasm()
12+
]
13+
});

packages/wasm/esbuild.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import { bothTpl } from "@hpcc-js/esbuild-plugins";
2+
import { replaceFunction } from "../../utils/esbuild-plugins.js";
3+
4+
const replaceConfig = {
5+
plugins: [
6+
replaceFunction({
7+
'findWasmBinary': 'const findWasmBinary=()=>"";'
8+
})
9+
]
10+
};
211

312
// config ---
413
await Promise.all([
5-
bothTpl("src/base91.ts", "dist/base91", { libraryName: "@hpcc-js/wasm/base91" }),
6-
bothTpl("src/duckdb.ts", "dist/duckdb", { libraryName: "@hpcc-js/wasm/duckdb" }),
7-
bothTpl("src/graphviz.ts", "dist/graphviz", { libraryName: "@hpcc-js/wasm/graphviz" }),
8-
bothTpl("src/expat.ts", "dist/expat", { libraryName: "@hpcc-js/wasm/expat" }),
9-
bothTpl("src/zstd.ts", "dist/zstd", { libraryName: "@hpcc-js/wasm/zstd" })
14+
bothTpl("src/base91.ts", "dist/base91", { libraryName: "@hpcc-js/wasm/base91", ...replaceConfig }),
15+
bothTpl("src/duckdb.ts", "dist/duckdb", { libraryName: "@hpcc-js/wasm/duckdb", ...replaceConfig }),
16+
bothTpl("src/graphviz.ts", "dist/graphviz", { libraryName: "@hpcc-js/wasm/graphviz", ...replaceConfig }),
17+
bothTpl("src/expat.ts", "dist/expat", { libraryName: "@hpcc-js/wasm/expat", ...replaceConfig }),
18+
bothTpl("src/zstd.ts", "dist/zstd", { libraryName: "@hpcc-js/wasm/zstd", ...replaceConfig })
1019
]);
11-
await bothTpl("src/index.ts", "dist/index", { libraryName: "@hpcc-js/wasm", external: ["./base91.js", "./duckdb.js", "./expat.js", "./graphviz.js", "./zstd.js"] });
20+
await bothTpl("src/index.ts", "dist/index", { libraryName: "@hpcc-js/wasm", external: ["./base91.js", "./duckdb.js", "./expat.js", "./graphviz.js", "./zstd.js"], ...replaceConfig });

packages/zstd/esbuild.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { neutralTpl } from "@hpcc-js/esbuild-plugins";
22
import { sfxWasm } from "@hpcc-js/esbuild-plugins/sfx-wrapper";
3+
import { replaceFunction } from "../../utils/esbuild-plugins.js";
34

45
// config ---
5-
await neutralTpl("src/index.ts", "dist/index", { plugins: [sfxWasm()] });
6+
await neutralTpl("src/index.ts", "dist/index", {
7+
plugins: [
8+
replaceFunction({
9+
'findWasmBinary': 'const findWasmBinary=()=>"";'
10+
}),
11+
sfxWasm()
12+
]
13+
});

utils/esbuild-plugins.js

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
import { readFile } from 'fs/promises';
2+
3+
const processedFiles = new Map();
4+
5+
function escapeRegExp(string) {
6+
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
7+
}
8+
9+
function findMatchingBrace(contents, openBraceIndex) {
10+
let braceCount = 1;
11+
let currentIndex = openBraceIndex + 1;
12+
let inString = false;
13+
let stringChar = '';
14+
let inComment = false;
15+
let commentType = '';
16+
17+
while (currentIndex < contents.length && braceCount > 0) {
18+
const char = contents[currentIndex];
19+
const nextChar = contents[currentIndex + 1];
20+
21+
if (!inComment && (char === '"' || char === "'" || char === '`')) {
22+
if (!inString) {
23+
inString = true;
24+
stringChar = char;
25+
} else if (char === stringChar && contents[currentIndex - 1] !== '\\') {
26+
inString = false;
27+
stringChar = '';
28+
}
29+
}
30+
31+
if (!inString && !inComment) {
32+
if (char === '/' && nextChar === '/') {
33+
inComment = true;
34+
commentType = 'line';
35+
currentIndex++;
36+
} else if (char === '/' && nextChar === '*') {
37+
inComment = true;
38+
commentType = 'block';
39+
currentIndex++;
40+
}
41+
} else if (inComment) {
42+
if (commentType === 'line' && char === '\n') {
43+
inComment = false;
44+
commentType = '';
45+
} else if (commentType === 'block' && char === '*' && nextChar === '/') {
46+
inComment = false;
47+
commentType = '';
48+
currentIndex++;
49+
}
50+
}
51+
52+
if (!inString && !inComment) {
53+
if (char === '{') {
54+
braceCount++;
55+
} else if (char === '}') {
56+
braceCount--;
57+
}
58+
}
59+
60+
currentIndex++;
61+
}
62+
63+
return braceCount === 0 ? currentIndex - 1 : -1;
64+
}
65+
66+
function replaceFunctionByName(contents, functionName, replacement, patterns) {
67+
let result = contents;
68+
69+
for (const pattern of patterns) {
70+
pattern.lastIndex = 0;
71+
72+
let match;
73+
while ((match = pattern.exec(result)) !== null) {
74+
const startIndex = match.index;
75+
const matchEnd = match.index + match[0].length;
76+
77+
const closingBraceIndex = findMatchingBrace(result, matchEnd - 1);
78+
79+
if (closingBraceIndex !== -1) {
80+
result = result.slice(0, startIndex) + replacement + result.slice(closingBraceIndex + 1);
81+
82+
pattern.lastIndex = 0;
83+
break;
84+
}
85+
}
86+
}
87+
88+
return result;
89+
}
90+
export const replaceFunction = (replacements) => {
91+
if (!replacements || typeof replacements !== 'object') {
92+
throw new Error('replaceFunction: replacements must be an object');
93+
}
94+
95+
const functionPatterns = Object.keys(replacements).map(functionName => ({
96+
name: functionName,
97+
replacement: replacements[functionName],
98+
patterns: [
99+
new RegExp(`function\\s+${escapeRegExp(functionName)}\\s*\\([^)]*\\)\\s*{`, 'g'),
100+
new RegExp(`const\\s+${escapeRegExp(functionName)}\\s*=\\s*function\\s*\\([^)]*\\)\\s*{`, 'g'),
101+
new RegExp(`let\\s+${escapeRegExp(functionName)}\\s*=\\s*function\\s*\\([^)]*\\)\\s*{`, 'g'),
102+
new RegExp(`var\\s+${escapeRegExp(functionName)}\\s*=\\s*function\\s*\\([^)]*\\)\\s*{`, 'g'),
103+
new RegExp(`${escapeRegExp(functionName)}\\s*:\\s*function\\s*\\([^)]*\\)\\s*{`, 'g'),
104+
new RegExp(`${escapeRegExp(functionName)}\\s*=\\s*\\([^)]*\\)\\s*=>\\s*{`, 'g'),
105+
new RegExp(`const\\s+${escapeRegExp(functionName)}\\s*=\\s*\\([^)]*\\)\\s*=>\\s*{`, 'g'),
106+
new RegExp(`let\\s+${escapeRegExp(functionName)}\\s*=\\s*\\([^)]*\\)\\s*=>\\s*{`, 'g'),
107+
new RegExp(`var\\s+${escapeRegExp(functionName)}\\s*=\\s*\\([^)]*\\)\\s*=>\\s*{`, 'g')
108+
]
109+
}));
110+
111+
return {
112+
name: 'replace-function',
113+
setup(build) {
114+
build.onLoad({ filter: /\.js$/ }, async (args) => {
115+
try {
116+
const cacheKey = `${args.path}:${JSON.stringify(replacements)}`;
117+
if (processedFiles.has(cacheKey)) {
118+
return processedFiles.get(cacheKey);
119+
}
120+
121+
let contents = await readFile(args.path, 'utf8');
122+
let modified = false;
123+
124+
const hasTargetFunction = functionPatterns.some(({ name }) =>
125+
contents.includes(name)
126+
);
127+
128+
if (!hasTargetFunction) {
129+
const result = { contents, loader: 'js' };
130+
processedFiles.set(cacheKey, result);
131+
return result;
132+
}
133+
134+
for (const { name, replacement, patterns } of functionPatterns) {
135+
const result = replaceFunctionByName(contents, name, replacement, patterns);
136+
if (result !== contents) {
137+
contents = result;
138+
modified = true;
139+
}
140+
}
141+
142+
const result = { contents, loader: 'js' };
143+
144+
processedFiles.set(cacheKey, result);
145+
146+
return result;
147+
} catch (error) {
148+
console.warn(`[replace-function] Failed to process ${args.path}:`, error.message);
149+
return null;
150+
}
151+
});
152+
},
153+
};
154+
};
155+
156+
export function clearCache() {
157+
processedFiles.clear();
158+
}

0 commit comments

Comments
 (0)