Skip to content

Commit 0d5fa42

Browse files
authored
Merge pull request #664 from LIT-Protocol/feature/lit-3913-js-sdk-investigate-why-v7-alpha-isnt-installing
fix/v7: Remove preinstall script to avoid local build when installing as a dependency
2 parents c9d6c0d + 71e1217 commit 0d5fa42

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

packages/wasm/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lit-protocol/wasm",
3-
"version": "6.4.0",
3+
"version": "6.7.0",
44
"type": "commonjs",
55
"homepage": "https://github.com/Lit-Protocol/js-sdk",
66
"repository": {
@@ -25,7 +25,6 @@
2525
"universal"
2626
],
2727
"scripts": {
28-
"preinstall": "yarn rust:build",
2928
"rust:build": "wasm-pack build ./rust --target web --release --out-name wasm-internal && yarn rust:postbuild",
3029
"rust:postbuild": "node scripts/copyWasmBinary.mjs && rm -rf src/pkg && mkdir src/pkg && mv rust/pkg/wasm-internal.js src/pkg && mv rust/pkg/wasm-internal.d.ts src/pkg",
3130
"rust:build:debug": "wasm-pack build ./rust --target web --dev --out-name wasm-internal && yarn rust:postbuild"

packages/wasm/scripts/copyWasmBinary.mjs

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@ import * as pako from 'pako';
44
const WASM_MODULE_PATH = 'rust/pkg/wasm-internal_bg.wasm';
55
const WASM_BINDING_PATH = 'rust/pkg/wasm-internal.js';
66
const CHUNK_SIZE = 100;
7-
const REMOVE_LINES = [
8-
`
9-
if (typeof input === 'undefined') {
10-
input = new URL('wasm-internal_bg.wasm', import.meta.url);
11-
}
12-
`,
13-
`
14-
if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) {
15-
input = fetch(input);
16-
}
17-
`,
7+
const COMMENT_OUT_LINES = [
8+
// This regex matches the block that checks if `module_or_path` is undefined and assigns a URL to it.
9+
/if\s*\(\s*typeof\s+module_or_path\s*===\s*['"`]undefined['"`]\s*\)\s*{\s*module_or_path\s*=\s*new\s+URL\s*\(\s*['"`]wasm-internal_bg\.wasm['"`],\s*import\.meta\.url\s*\);\s*}/g,
10+
11+
// This regex matches the block that checks if `module_or_path` is a string, `Request`, or `URL` and assigns it to the result of a `fetch`.
12+
/if\s*\(\s*typeof\s+module_or_path\s*===\s*['"`]string['"`]\s*\|\|\s*\(typeof\s+Request\s*===\s*['"`]function['"`]\s*&&\s*module_or_path\s*instanceof\s+Request\)\s*\|\|\s*\(typeof\s+URL\s*===\s*['"`]function['"`]\s*&&\s*module_or_path\s*instanceof\s+URL\)\s*\)\s*{\s*module_or_path\s*=\s*fetch\s*\(module_or_path\);\s*}/g,
1813
];
1914

2015
function main() {
@@ -31,7 +26,7 @@ function main() {
3126
}
3227

3328
let bindingModuleString = `
34-
// @ts-nocheck
29+
// @ts-nocheck - autogenerated from copyWasmBinary.mjs
3530
import * as pako from 'pako';
3631
`;
3732
bindingModuleString += '\n';
@@ -46,13 +41,38 @@ export function getModule() {
4641

4742
bindingModuleString += wasmBindingModule;
4843

49-
for (const removeItem of REMOVE_LINES) {
50-
const regex = new RegExp(
51-
removeItem.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&'),
52-
'g'
53-
); // Escape special characters
44+
let allReplacementsDone = true; // Track whether all replacements were successful
45+
46+
for (const commentOutItem of COMMENT_OUT_LINES) {
47+
const originalString = bindingModuleString; // Store the original string before replacement
48+
49+
bindingModuleString = bindingModuleString.replace(
50+
commentOutItem,
51+
(match) => {
52+
const headerComment =
53+
'// NOTE: This line is commented out automatically by copyWasmBinary.mjs\n';
54+
const commentedBlock = match
55+
.split('\n') // Split the matched block into lines
56+
.map((line) => `// ${line}`) // Add "//" to each line
57+
.join('\n'); // Re-join the lines into a single string
58+
59+
return `${headerComment}${commentedBlock}`; // Prepend the header comment to the commented block
60+
}
61+
);
62+
63+
// If no replacement was done (i.e., original string remains the same), mark as failure
64+
if (originalString === bindingModuleString) {
65+
allReplacementsDone = false;
66+
}
67+
}
5468

55-
bindingModuleString = bindingModuleString.replace(regex, '');
69+
// throw an error if all replacements were NOT successful
70+
if (!allReplacementsDone) {
71+
throw new Error(
72+
'❗️❗️ Failed to comment out all specified lines in the wasm binding module.'
73+
);
74+
} else {
75+
console.log('✅ All replacements were successful!');
5676
}
5777

5878
console.log('Writing wasm module');

0 commit comments

Comments
 (0)