@re2tools/re2-wasm-esm 1.0.2 → 1.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.
- package/README.md +84 -0
- package/dist/loader.d.ts.map +1 -1
- package/dist/loader.js +9 -1
- package/package.json +10 -3
package/README.md
CHANGED
|
@@ -53,6 +53,90 @@ With a bundler (Vite, Webpack, etc.), copy `node_modules/@re2tools/re2-wasm-esm/
|
|
|
53
53
|
await init({ baseUrl: "https://your-cdn.com/@re2tools/re2-wasm-esm/" });
|
|
54
54
|
```
|
|
55
55
|
|
|
56
|
+
### React (16.8+, 17, 18)
|
|
57
|
+
|
|
58
|
+
This package is React-agnostic (no React dependency). Use it in React 16, 17, or 18 by calling `init()` once, then `getRE2()` in components or helpers.
|
|
59
|
+
|
|
60
|
+
**Option A — init in entry (recommended, React 16/17)**
|
|
61
|
+
|
|
62
|
+
```tsx
|
|
63
|
+
// src/index.tsx
|
|
64
|
+
import React from "react";
|
|
65
|
+
import ReactDOM from "react-dom";
|
|
66
|
+
import { init, getRE2 } from "@re2tools/re2-wasm-esm";
|
|
67
|
+
import App from "./App";
|
|
68
|
+
|
|
69
|
+
async function bootstrap() {
|
|
70
|
+
await init();
|
|
71
|
+
ReactDOM.render(<App />, document.getElementById("root"));
|
|
72
|
+
}
|
|
73
|
+
bootstrap();
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
**React 18** — use `createRoot` instead of `render`:
|
|
77
|
+
|
|
78
|
+
```tsx
|
|
79
|
+
import { createRoot } from "react-dom/client";
|
|
80
|
+
// after await init():
|
|
81
|
+
createRoot(document.getElementById("root")!).render(<App />);
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
**Option B — init in a component (React 16.8+ for hooks)**
|
|
85
|
+
|
|
86
|
+
```tsx
|
|
87
|
+
import { useState, useEffect } from "react";
|
|
88
|
+
import { init, getRE2 } from "@re2tools/re2-wasm-esm";
|
|
89
|
+
|
|
90
|
+
function useRE2() {
|
|
91
|
+
const [ready, setReady] = useState(false);
|
|
92
|
+
useEffect(() => {
|
|
93
|
+
init().then(() => setReady(true));
|
|
94
|
+
}, []);
|
|
95
|
+
return ready ? getRE2() : null;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// In a component: const RE2 = useRE2(); use RE2 when non-null.
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Use RE2 with the **`u`** (unicode) flag: `new RE2("pattern", "u")` or `new RE2(/pattern/u)`.
|
|
102
|
+
|
|
103
|
+
### Storybook (fix 404 for re2-glue.js / re2.wasm)
|
|
104
|
+
|
|
105
|
+
Storybook serves from its own dev server, so the loader can’t find `re2.wasm` and `re2-glue.js` unless you serve them and pass their base URL.
|
|
106
|
+
|
|
107
|
+
**1. Serve the files**
|
|
108
|
+
|
|
109
|
+
Copy the two files into a folder your app (or Storybook) serves as static:
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
mkdir -p public/re2
|
|
113
|
+
cp node_modules/@re2tools/re2-wasm-esm/dist/re2.wasm node_modules/@re2tools/re2-wasm-esm/dist/re2-glue.js public/re2/
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
If your project has no `public/`, use whatever directory Storybook uses for static files (e.g. `static/`), or add `staticDirs` in `.storybook/main.js`:
|
|
117
|
+
|
|
118
|
+
```js
|
|
119
|
+
// .storybook/main.js (or main.ts)
|
|
120
|
+
module.exports = {
|
|
121
|
+
staticDirs: ['public'], // or ['static']
|
|
122
|
+
// ...
|
|
123
|
+
};
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
**2. Tell the loader where they are**
|
|
127
|
+
|
|
128
|
+
In `.storybook/preview.js` (or `preview.ts`), init with that base path:
|
|
129
|
+
|
|
130
|
+
```js
|
|
131
|
+
import { init } from "@re2tools/re2-wasm-esm";
|
|
132
|
+
|
|
133
|
+
await init({ baseUrl: "/re2/" });
|
|
134
|
+
|
|
135
|
+
export const parameters = { /* ... */ };
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
If your app is not at the root (e.g. Storybook base path), use that base: `init({ baseUrl: "/storybook/re2/" })` or use a relative URL that resolves to the same origin where the files are served.
|
|
139
|
+
|
|
56
140
|
### API
|
|
57
141
|
|
|
58
142
|
- **`init(options?: { wasmUrl?, glueUrl?, baseUrl? })`**
|
package/dist/loader.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;KAClC;IAED,IAAI,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC;CACjD;AAYD,8DAA8D;AAC9D,wBAAgB,QAAQ,CAAC,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAAG,IAAI,GAAG,IAAI,CAEzE;AAED,wBAAgB,UAAU,IAAI,OAAO,CAEpC;AAED,6DAA6D;AAC7D,eAAO,IAAI,gBAAgB,EAAE,OAAc,CAAC;AAE5C,MAAM,WAAW,aAAa;IAC5B,UAAU,EAAE,KACV,OAAO,EAAE,MAAM,EACf,UAAU,EAAE,OAAO,EACnB,SAAS,EAAE,OAAO,EAClB,MAAM,EAAE,OAAO,KACZ;QACH,EAAE,IAAI,OAAO,CAAC;QACd,KAAK,IAAI,MAAM,CAAC;QAChB,OAAO,IAAI,MAAM,CAAC;QAClB,KAAK,CACH,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,kBAAkB,EAAE,OAAO,GAC1B;YAAE,KAAK,EAAE,MAAM,CAAC;YAAC,KAAK,EAAE,MAAM,CAAC;YAAC,MAAM,EAAE,CAAC,MAAM,GAAG,SAAS,CAAC,EAAE,CAAA;SAAE,CAAC;QACp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OAAO,CAAC,MAAM,CAAC,EACpC,WAAW,CAAC,EAAE;IAAE,WAAW,EAAE,eAAe,CAAC;IAAC,OAAO,EAAE,WAAW,CAAA;CAAE,EACpE,YAAY,CAAC,EAAE,MAAM,GACpB,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CA+FlC;AAED;;;;GAIG;AACH,wBAAsB,IAAI,CAAC,OAAO,CAAC,EAAE;IACnC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,GAAG,OAAO,CAAC,IAAI,CAAC,CAqChB;AAED,wBAAgB,SAAS,IAAI,aAAa,CAOzC;AAED,wBAAgB,SAAS,IAAI,OAAO,CAAC,aAAa,CAAC,CAMlD"}
|
package/dist/loader.js
CHANGED
|
@@ -111,7 +111,15 @@ export function createLoader(getWasmBuffer, getGlueScript, nodeOptions, re2Scrip
|
|
|
111
111
|
.catch(rej);
|
|
112
112
|
}
|
|
113
113
|
else {
|
|
114
|
-
|
|
114
|
+
// Browser: preload WASM into Module.wasmBinary so Emscripten glue
|
|
115
|
+
// does not try to sync-fetch (which fails in bundlers/Storybook).
|
|
116
|
+
getWasmBuffer()
|
|
117
|
+
.then((buffer) => {
|
|
118
|
+
Module.wasmBinary = buffer;
|
|
119
|
+
return getGlueScript();
|
|
120
|
+
})
|
|
121
|
+
.then(runGlue)
|
|
122
|
+
.catch(rej);
|
|
115
123
|
}
|
|
116
124
|
});
|
|
117
125
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@re2tools/re2-wasm-esm",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Google RE2 as WASM - ESM build with browser support (async init)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -31,14 +31,21 @@
|
|
|
31
31
|
"@types/node": "^18.0.0",
|
|
32
32
|
"typescript": "~5.0.0"
|
|
33
33
|
},
|
|
34
|
-
"peerDependencies": {
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"react": ">=16.8.0"
|
|
36
|
+
},
|
|
37
|
+
"peerDependenciesMeta": {
|
|
38
|
+
"react": { "optional": true }
|
|
39
|
+
},
|
|
35
40
|
"keywords": [
|
|
36
41
|
"re2",
|
|
37
42
|
"regex",
|
|
38
43
|
"wasm",
|
|
39
44
|
"esm",
|
|
40
45
|
"browser",
|
|
41
|
-
"redos"
|
|
46
|
+
"redos",
|
|
47
|
+
"react",
|
|
48
|
+
"react16"
|
|
42
49
|
],
|
|
43
50
|
"license": "Apache-2.0",
|
|
44
51
|
"repository": {
|