oxide-wasm 0.1.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/LICENSE +21 -0
- package/README.md +36 -0
- package/index.d.ts +7 -0
- package/index.mjs +39 -0
- package/package.json +27 -0
- package/pkg/oxide_wasm.js +229 -0
- package/pkg/oxide_wasm_bg.wasm +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Je Xia <i@jex.me>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# oxide-wasm
|
|
2
|
+
|
|
3
|
+
A wasm build for [tailwindcss-oxide](https://github.com/tailwindlabs/tailwindcss/tree/main/crates/oxide).
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
import init, { extract } from "https://esm.sh/oxide-wasm";
|
|
9
|
+
|
|
10
|
+
// load oxide-wasm_bg.wasm
|
|
11
|
+
await init();
|
|
12
|
+
|
|
13
|
+
const code = `
|
|
14
|
+
<h1 class="text-3xl font-bold underline">
|
|
15
|
+
Hello world!
|
|
16
|
+
</h1>
|
|
17
|
+
`;
|
|
18
|
+
console.log(extract(code));
|
|
19
|
+
// [
|
|
20
|
+
// "class",
|
|
21
|
+
// "text-3xl",
|
|
22
|
+
// "font-bold",
|
|
23
|
+
// "underline"
|
|
24
|
+
// ]
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Development Setup
|
|
28
|
+
|
|
29
|
+
You will need [rust](https://www.rust-lang.org/tools/install) 1.60+ and
|
|
30
|
+
[wasm-pack](https://rustwasm.github.io/wasm-pack/installer/).
|
|
31
|
+
|
|
32
|
+
## Build
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
wasm-pack build --target web --no-pack --release
|
|
36
|
+
```
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
type SyncInitInput = BufferSource | WebAssembly.Module;
|
|
2
|
+
type InitInput = RequestInfo | URL | Response | SyncInitInput;
|
|
3
|
+
|
|
4
|
+
export function initSync(module: SyncInitInput): void;
|
|
5
|
+
export function init(module_or_path?: InitInput | Promise<InitInput>): Promise<void>;
|
|
6
|
+
export function extract(input: string): string[];
|
|
7
|
+
export default init;
|
package/index.mjs
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import initWasm, { extract as wasmExtract, initSync as initWasmSync } from "./pkg/oxide_wasm.js";
|
|
2
|
+
|
|
3
|
+
export function extract(input) {
|
|
4
|
+
if (typeof input !== "string") {
|
|
5
|
+
throw new TypeError("input must be a non-empty string");
|
|
6
|
+
}
|
|
7
|
+
return wasmExtract(input);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function initSync(module) {
|
|
11
|
+
return initWasmSync({ module });
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function init(module_or_path) {
|
|
15
|
+
const importUrl = import.meta.url;
|
|
16
|
+
if (!module_or_path && importUrl.startsWith("file://")) {
|
|
17
|
+
const { pathname: filename } = new URL("./pkg/oxide_wasm_bg.wasm", importUrl);
|
|
18
|
+
let wasmBytes;
|
|
19
|
+
if (globalThis.Deno) {
|
|
20
|
+
wasmBytes = await Deno.readFile(filename);
|
|
21
|
+
} else if (globalThis.Bun) {
|
|
22
|
+
wasmBytes = await Bun.file(filename).arrayBuffer();
|
|
23
|
+
} else {
|
|
24
|
+
const moduleSpecifier = `node:fs/promises`;
|
|
25
|
+
const fsPromise = await import(moduleSpecifier); // <- use variable to skip deno-lsp analyzing
|
|
26
|
+
wasmBytes = await fsPromise.readFile(filename);
|
|
27
|
+
}
|
|
28
|
+
initWasmSync({ module: wasmBytes });
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const esmshBaseUrl = "https://esm.sh/@esm.sh/oxide-wasm@";
|
|
32
|
+
if (!module_or_path && importUrl.startsWith(esmshBaseUrl)) {
|
|
33
|
+
const version = importUrl.slice(esmshBaseUrl.length).split("/", 1)[0];
|
|
34
|
+
module_or_path = esmshBaseUrl + version + "/pkg/oxide_wasm_bg.wasm";
|
|
35
|
+
}
|
|
36
|
+
return initWasm(module_or_path ? { module_or_path } : undefined);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export default init;
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "oxide-wasm",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A wasm build of the tailwindcss oxide",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./index.mjs",
|
|
7
|
+
"module": "./index.mjs",
|
|
8
|
+
"types": "./index.d.ts",
|
|
9
|
+
"sideEffects": false,
|
|
10
|
+
"files": [
|
|
11
|
+
"./index.mjs",
|
|
12
|
+
"./index.d.ts",
|
|
13
|
+
"./pkg/oxide_wasm.js",
|
|
14
|
+
"./pkg/oxide_wasm_bg.wasm"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"prepublishOnly": "npm run test && npm run test",
|
|
18
|
+
"build": "wasm-pack build --target web --no-pack --release",
|
|
19
|
+
"test": "npm run build && node test.mjs",
|
|
20
|
+
"fmt": "cargo fmt --all"
|
|
21
|
+
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/esm-dev/oxide.git"
|
|
25
|
+
},
|
|
26
|
+
"license": "MIT"
|
|
27
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/* @ts-self-types="./oxide_wasm.d.ts" */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param {string} input
|
|
5
|
+
* @returns {any[]}
|
|
6
|
+
*/
|
|
7
|
+
export function extract(input) {
|
|
8
|
+
const ptr0 = passStringToWasm0(input, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
9
|
+
const len0 = WASM_VECTOR_LEN;
|
|
10
|
+
const ret = wasm.extract(ptr0, len0);
|
|
11
|
+
var v2 = getArrayJsValueFromWasm0(ret[0], ret[1]).slice();
|
|
12
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 4, 4);
|
|
13
|
+
return v2;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function __wbg_get_imports() {
|
|
17
|
+
const import0 = {
|
|
18
|
+
__proto__: null,
|
|
19
|
+
__wbindgen_cast_0000000000000001: function(arg0, arg1) {
|
|
20
|
+
// Cast intrinsic for `Ref(String) -> Externref`.
|
|
21
|
+
const ret = getStringFromWasm0(arg0, arg1);
|
|
22
|
+
return ret;
|
|
23
|
+
},
|
|
24
|
+
__wbindgen_init_externref_table: function() {
|
|
25
|
+
const table = wasm.__wbindgen_externrefs;
|
|
26
|
+
const offset = table.grow(4);
|
|
27
|
+
table.set(0, undefined);
|
|
28
|
+
table.set(offset + 0, undefined);
|
|
29
|
+
table.set(offset + 1, null);
|
|
30
|
+
table.set(offset + 2, true);
|
|
31
|
+
table.set(offset + 3, false);
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
return {
|
|
35
|
+
__proto__: null,
|
|
36
|
+
"./oxide_wasm_bg.js": import0,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function getArrayJsValueFromWasm0(ptr, len) {
|
|
41
|
+
ptr = ptr >>> 0;
|
|
42
|
+
const mem = getDataViewMemory0();
|
|
43
|
+
const result = [];
|
|
44
|
+
for (let i = ptr; i < ptr + 4 * len; i += 4) {
|
|
45
|
+
result.push(wasm.__wbindgen_externrefs.get(mem.getUint32(i, true)));
|
|
46
|
+
}
|
|
47
|
+
wasm.__externref_drop_slice(ptr, len);
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
let cachedDataViewMemory0 = null;
|
|
52
|
+
function getDataViewMemory0() {
|
|
53
|
+
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
|
|
54
|
+
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
|
|
55
|
+
}
|
|
56
|
+
return cachedDataViewMemory0;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function getStringFromWasm0(ptr, len) {
|
|
60
|
+
ptr = ptr >>> 0;
|
|
61
|
+
return decodeText(ptr, len);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
let cachedUint8ArrayMemory0 = null;
|
|
65
|
+
function getUint8ArrayMemory0() {
|
|
66
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
67
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
68
|
+
}
|
|
69
|
+
return cachedUint8ArrayMemory0;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
73
|
+
if (realloc === undefined) {
|
|
74
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
75
|
+
const ptr = malloc(buf.length, 1) >>> 0;
|
|
76
|
+
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
|
|
77
|
+
WASM_VECTOR_LEN = buf.length;
|
|
78
|
+
return ptr;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
let len = arg.length;
|
|
82
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
83
|
+
|
|
84
|
+
const mem = getUint8ArrayMemory0();
|
|
85
|
+
|
|
86
|
+
let offset = 0;
|
|
87
|
+
|
|
88
|
+
for (; offset < len; offset++) {
|
|
89
|
+
const code = arg.charCodeAt(offset);
|
|
90
|
+
if (code > 0x7F) break;
|
|
91
|
+
mem[ptr + offset] = code;
|
|
92
|
+
}
|
|
93
|
+
if (offset !== len) {
|
|
94
|
+
if (offset !== 0) {
|
|
95
|
+
arg = arg.slice(offset);
|
|
96
|
+
}
|
|
97
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
98
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
99
|
+
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
100
|
+
|
|
101
|
+
offset += ret.written;
|
|
102
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
WASM_VECTOR_LEN = offset;
|
|
106
|
+
return ptr;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
110
|
+
cachedTextDecoder.decode();
|
|
111
|
+
const MAX_SAFARI_DECODE_BYTES = 2146435072;
|
|
112
|
+
let numBytesDecoded = 0;
|
|
113
|
+
function decodeText(ptr, len) {
|
|
114
|
+
numBytesDecoded += len;
|
|
115
|
+
if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
|
|
116
|
+
cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
117
|
+
cachedTextDecoder.decode();
|
|
118
|
+
numBytesDecoded = len;
|
|
119
|
+
}
|
|
120
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const cachedTextEncoder = new TextEncoder();
|
|
124
|
+
|
|
125
|
+
if (!('encodeInto' in cachedTextEncoder)) {
|
|
126
|
+
cachedTextEncoder.encodeInto = function (arg, view) {
|
|
127
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
128
|
+
view.set(buf);
|
|
129
|
+
return {
|
|
130
|
+
read: arg.length,
|
|
131
|
+
written: buf.length
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
let WASM_VECTOR_LEN = 0;
|
|
137
|
+
|
|
138
|
+
let wasmModule, wasm;
|
|
139
|
+
function __wbg_finalize_init(instance, module) {
|
|
140
|
+
wasm = instance.exports;
|
|
141
|
+
wasmModule = module;
|
|
142
|
+
cachedDataViewMemory0 = null;
|
|
143
|
+
cachedUint8ArrayMemory0 = null;
|
|
144
|
+
wasm.__wbindgen_start();
|
|
145
|
+
return wasm;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async function __wbg_load(module, imports) {
|
|
149
|
+
if (typeof Response === 'function' && module instanceof Response) {
|
|
150
|
+
if (typeof WebAssembly.instantiateStreaming === 'function') {
|
|
151
|
+
try {
|
|
152
|
+
return await WebAssembly.instantiateStreaming(module, imports);
|
|
153
|
+
} catch (e) {
|
|
154
|
+
const validResponse = module.ok && expectedResponseType(module.type);
|
|
155
|
+
|
|
156
|
+
if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
|
|
157
|
+
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
|
|
158
|
+
|
|
159
|
+
} else { throw e; }
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const bytes = await module.arrayBuffer();
|
|
164
|
+
return await WebAssembly.instantiate(bytes, imports);
|
|
165
|
+
} else {
|
|
166
|
+
const instance = await WebAssembly.instantiate(module, imports);
|
|
167
|
+
|
|
168
|
+
if (instance instanceof WebAssembly.Instance) {
|
|
169
|
+
return { instance, module };
|
|
170
|
+
} else {
|
|
171
|
+
return instance;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function expectedResponseType(type) {
|
|
176
|
+
switch (type) {
|
|
177
|
+
case 'basic': case 'cors': case 'default': return true;
|
|
178
|
+
}
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function initSync(module) {
|
|
184
|
+
if (wasm !== undefined) return wasm;
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
if (module !== undefined) {
|
|
188
|
+
if (Object.getPrototypeOf(module) === Object.prototype) {
|
|
189
|
+
({module} = module)
|
|
190
|
+
} else {
|
|
191
|
+
console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const imports = __wbg_get_imports();
|
|
196
|
+
if (!(module instanceof WebAssembly.Module)) {
|
|
197
|
+
module = new WebAssembly.Module(module);
|
|
198
|
+
}
|
|
199
|
+
const instance = new WebAssembly.Instance(module, imports);
|
|
200
|
+
return __wbg_finalize_init(instance, module);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
async function __wbg_init(module_or_path) {
|
|
204
|
+
if (wasm !== undefined) return wasm;
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
if (module_or_path !== undefined) {
|
|
208
|
+
if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
|
|
209
|
+
({module_or_path} = module_or_path)
|
|
210
|
+
} else {
|
|
211
|
+
console.warn('using deprecated parameters for the initialization function; pass a single object instead')
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (module_or_path === undefined) {
|
|
216
|
+
module_or_path = new URL('oxide_wasm_bg.wasm', import.meta.url);
|
|
217
|
+
}
|
|
218
|
+
const imports = __wbg_get_imports();
|
|
219
|
+
|
|
220
|
+
if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
|
|
221
|
+
module_or_path = fetch(module_or_path);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const { instance, module } = await __wbg_load(await module_or_path, imports);
|
|
225
|
+
|
|
226
|
+
return __wbg_finalize_init(instance, module);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export { initSync, __wbg_init as default };
|
|
Binary file
|