@esm.sh/cjs-module-lexer 1.0.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 ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020-2024 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,162 @@
1
+ # @esm.sh/cjs-module-lexer
2
+
3
+ A lexer for detecting the `module.exports` of a CJS module, written in Rust and compiled to WebAssembly.
4
+
5
+ ## Usage
6
+
7
+ `@esm.sh/cjs-module-lexer` currently only supports Node.js environment. You can install it via npm CLI:
8
+
9
+ ```bash
10
+ npm i @esm.sh/cjs-module-lexer
11
+ ```
12
+
13
+ `@esm.sh/cjs-module-lexer` provides a `parse` function that detects the `module.exports` of a commonjs module. The function returns an object with two properties: `exports` and `reexports`. The `exports` property is an array of the exported names, and the `reexports` property is an array of the reexported modules.
14
+
15
+ ```js
16
+ const { parse } = require("@esm.sh/cjs-module-lexer");
17
+
18
+ // named exports by assignment
19
+ // exports: ["a", "b", "c", "__esModule", "foo"]
20
+ const { exports } = parse("index.cjs", `
21
+ exports.a = "a";
22
+ module.exports.b = "b";
23
+ Object.defineProperty(exports, "c", { value: 1 });
24
+ Object.defineProperty(module.exports, "__esModule", { value: true })
25
+ const key = "foo"
26
+ Object.defineProperty(exports, key, { value: "e" });
27
+ `);
28
+
29
+ // reexports
30
+ // reexports: ["./lib"]
31
+ const { reexports } = parse("index.cjs", `
32
+ module.exports = require("./lib");
33
+ `);
34
+
35
+ // object exports(spread syntax supported)
36
+ // exports: ["foo", "baz"]
37
+ // reexports: ["./lib"]
38
+ const { exports, reexports } = parse("index.cjs", `
39
+ const foo = "bar"
40
+ const obj = { baz: 123 }
41
+ module.exports = { foo, ...obj, ...require("./lib") };
42
+ `);
43
+
44
+ // if expression
45
+ // exports: ["foo", "cjs"]
46
+ const { exports } = parse("index.cjs", `
47
+ module.exports.a = "a";
48
+ if (true) {
49
+ exports.foo = "bar";
50
+ }
51
+ const mtype = "cjs";
52
+ if (mtype === "cjs") {
53
+ exports.cjs = true;
54
+ } else {
55
+ exports.esm = true;
56
+ }
57
+ if (false) {
58
+ exports.unreachable = true;
59
+ }
60
+ `);
61
+
62
+ // condition exports by checking if `process.env.NODE_ENV` equals to `nodeEnv` option
63
+ // reexports: ["./index.development.js"]
64
+ const { reexports } = parse("index.cjs", `
65
+ if (process.env.NODE_ENV === "development") {
66
+ module.exports = require("./index.development.js")
67
+ } else {
68
+ module.exports = require("./index.production.js")
69
+ }
70
+ `, { nodeEnv: "development" });
71
+
72
+ // block&IIFE
73
+ // exports: ["foo", "baz", "__esModule"]
74
+ const { exports } = parse("index.cjs", `
75
+ {
76
+ exports.foo = 'bar'
77
+ }
78
+ (function () {
79
+ exports.baz = 'qux'
80
+ if (true) {
81
+ return
82
+ }
83
+ exports.unreachable = true
84
+ })();
85
+ exports.__esModule = true
86
+ `);
87
+
88
+ // UMD format
89
+ // exports: ["foo"]
90
+ const { exports } = parse("index.cjs", `
91
+ (function (global, factory) {
92
+ typeof exports === "object" && typeof module !== "undefined" ? factory(exports) :
93
+ typeof define === "function" && define.amd ? define(["exports"], factory) :
94
+ (factory((global.MMDParser = global.MMDParser || {})));
95
+ }(this, function (exports) {
96
+ exports.foo = "bar";
97
+ }))
98
+ `);
99
+
100
+ // exports by calling a function
101
+ // exports: ["foo"]
102
+ const { exports } = parse("index.cjs", `
103
+ function module() {
104
+ return { foo: "bar" }
105
+ }
106
+ module.exports = module()
107
+ `);
108
+
109
+ // annotated export names for ESM import
110
+ // exports: ["foo", "bar"]
111
+ const { exports } = parse("lib.cjs", `
112
+ 0 && (module.exports = {
113
+ foo,
114
+ bar,
115
+ })
116
+ `);
117
+
118
+ // call reexports
119
+ // reexports: ["./lib()"]
120
+ const { reexports } = parse("index.cjs", `
121
+ module.exports = require("./lib")()
122
+ `);
123
+ // apply call reexports
124
+ // exports: ["foo"]
125
+ const { exports } = parse("lib.cjs", `
126
+ module.exports = function() {
127
+ return { foo: "bar" }
128
+ }
129
+ `, { callMode: true });
130
+ ```
131
+
132
+ The `parse` function has the following types definition:
133
+
134
+ ```ts
135
+ export function parse(
136
+ specifier: string,
137
+ code: string,
138
+ options? {
139
+ nodeEnv?: 'development' | 'production',
140
+ callMode?: boolean,
141
+ }
142
+ ): {
143
+ exports: string[],
144
+ reexports: string[],
145
+ };
146
+ ```
147
+
148
+ ## Development Setup
149
+
150
+ You will need [rust](https://www.rust-lang.org/tools/install) 1.56+ and [wasm-pack](https://rustwasm.github.io/wasm-pack/installer/).
151
+
152
+ ## Build
153
+
154
+ ```bash
155
+ wasm-pack build --target nodejs
156
+ ```
157
+
158
+ ## Run tests
159
+
160
+ ```bash
161
+ cargo test --all
162
+ ```
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@esm.sh/cjs-module-lexer",
3
+ "description": "A WASM module to parse the `module.exports` of a commonjs module.",
4
+ "version": "1.0.0",
5
+ "main": "pkg/cjs_module_lexer.js",
6
+ "types": "pkg/cjs_module_lexer.d.ts",
7
+ "scripts": {
8
+ "prepublishOnly": "wasm-pack build --target nodejs"
9
+ },
10
+ "files": [
11
+ "pkg/cjs_module_lexer_bg.wasm",
12
+ "pkg/cjs_module_lexer.js",
13
+ "pkg/cjs_module_lexer.d.ts"
14
+ ],
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/esm-dev/esm-cjs-lexer"
18
+ },
19
+ "license": "MIT"
20
+ }
@@ -0,0 +1,9 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ /**
4
+ * @param {string} specifier
5
+ * @param {string} code
6
+ * @param {any} options
7
+ * @returns {any}
8
+ */
9
+ export function parse(specifier: string, code: string, options: any): any;
@@ -0,0 +1,363 @@
1
+
2
+ let imports = {};
3
+ imports['__wbindgen_placeholder__'] = module.exports;
4
+ let wasm;
5
+ const { TextEncoder, TextDecoder } = require(`util`);
6
+
7
+ const heap = new Array(128).fill(undefined);
8
+
9
+ heap.push(undefined, null, true, false);
10
+
11
+ function getObject(idx) { return heap[idx]; }
12
+
13
+ let heap_next = heap.length;
14
+
15
+ function dropObject(idx) {
16
+ if (idx < 132) return;
17
+ heap[idx] = heap_next;
18
+ heap_next = idx;
19
+ }
20
+
21
+ function takeObject(idx) {
22
+ const ret = getObject(idx);
23
+ dropObject(idx);
24
+ return ret;
25
+ }
26
+
27
+ let WASM_VECTOR_LEN = 0;
28
+
29
+ let cachedUint8ArrayMemory0 = null;
30
+
31
+ function getUint8ArrayMemory0() {
32
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
33
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
34
+ }
35
+ return cachedUint8ArrayMemory0;
36
+ }
37
+
38
+ let cachedTextEncoder = new TextEncoder('utf-8');
39
+
40
+ const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
41
+ ? function (arg, view) {
42
+ return cachedTextEncoder.encodeInto(arg, view);
43
+ }
44
+ : function (arg, view) {
45
+ const buf = cachedTextEncoder.encode(arg);
46
+ view.set(buf);
47
+ return {
48
+ read: arg.length,
49
+ written: buf.length
50
+ };
51
+ });
52
+
53
+ function passStringToWasm0(arg, malloc, realloc) {
54
+
55
+ if (realloc === undefined) {
56
+ const buf = cachedTextEncoder.encode(arg);
57
+ const ptr = malloc(buf.length, 1) >>> 0;
58
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
59
+ WASM_VECTOR_LEN = buf.length;
60
+ return ptr;
61
+ }
62
+
63
+ let len = arg.length;
64
+ let ptr = malloc(len, 1) >>> 0;
65
+
66
+ const mem = getUint8ArrayMemory0();
67
+
68
+ let offset = 0;
69
+
70
+ for (; offset < len; offset++) {
71
+ const code = arg.charCodeAt(offset);
72
+ if (code > 0x7F) break;
73
+ mem[ptr + offset] = code;
74
+ }
75
+
76
+ if (offset !== len) {
77
+ if (offset !== 0) {
78
+ arg = arg.slice(offset);
79
+ }
80
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
81
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
82
+ const ret = encodeString(arg, view);
83
+
84
+ offset += ret.written;
85
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
86
+ }
87
+
88
+ WASM_VECTOR_LEN = offset;
89
+ return ptr;
90
+ }
91
+
92
+ function isLikeNone(x) {
93
+ return x === undefined || x === null;
94
+ }
95
+
96
+ let cachedDataViewMemory0 = null;
97
+
98
+ function getDataViewMemory0() {
99
+ if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
100
+ cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
101
+ }
102
+ return cachedDataViewMemory0;
103
+ }
104
+
105
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
106
+
107
+ cachedTextDecoder.decode();
108
+
109
+ function getStringFromWasm0(ptr, len) {
110
+ ptr = ptr >>> 0;
111
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
112
+ }
113
+
114
+ function addHeapObject(obj) {
115
+ if (heap_next === heap.length) heap.push(heap.length + 1);
116
+ const idx = heap_next;
117
+ heap_next = heap[idx];
118
+
119
+ heap[idx] = obj;
120
+ return idx;
121
+ }
122
+
123
+ function debugString(val) {
124
+ // primitive types
125
+ const type = typeof val;
126
+ if (type == 'number' || type == 'boolean' || val == null) {
127
+ return `${val}`;
128
+ }
129
+ if (type == 'string') {
130
+ return `"${val}"`;
131
+ }
132
+ if (type == 'symbol') {
133
+ const description = val.description;
134
+ if (description == null) {
135
+ return 'Symbol';
136
+ } else {
137
+ return `Symbol(${description})`;
138
+ }
139
+ }
140
+ if (type == 'function') {
141
+ const name = val.name;
142
+ if (typeof name == 'string' && name.length > 0) {
143
+ return `Function(${name})`;
144
+ } else {
145
+ return 'Function';
146
+ }
147
+ }
148
+ // objects
149
+ if (Array.isArray(val)) {
150
+ const length = val.length;
151
+ let debug = '[';
152
+ if (length > 0) {
153
+ debug += debugString(val[0]);
154
+ }
155
+ for(let i = 1; i < length; i++) {
156
+ debug += ', ' + debugString(val[i]);
157
+ }
158
+ debug += ']';
159
+ return debug;
160
+ }
161
+ // Test for built-in
162
+ const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val));
163
+ let className;
164
+ if (builtInMatches.length > 1) {
165
+ className = builtInMatches[1];
166
+ } else {
167
+ // Failed to match the standard '[object ClassName]'
168
+ return toString.call(val);
169
+ }
170
+ if (className == 'Object') {
171
+ // we're a user defined class or Object
172
+ // JSON.stringify avoids problems with cycles, and is generally much
173
+ // easier than looping through ownProperties of `val`.
174
+ try {
175
+ return 'Object(' + JSON.stringify(val) + ')';
176
+ } catch (_) {
177
+ return 'Object';
178
+ }
179
+ }
180
+ // errors
181
+ if (val instanceof Error) {
182
+ return `${val.name}: ${val.message}\n${val.stack}`;
183
+ }
184
+ // TODO we could test for more things here, like `Set`s and `Map`s.
185
+ return className;
186
+ }
187
+ /**
188
+ * @param {string} specifier
189
+ * @param {string} code
190
+ * @param {any} options
191
+ * @returns {any}
192
+ */
193
+ module.exports.parse = function(specifier, code, options) {
194
+ try {
195
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
196
+ const ptr0 = passStringToWasm0(specifier, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
197
+ const len0 = WASM_VECTOR_LEN;
198
+ const ptr1 = passStringToWasm0(code, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
199
+ const len1 = WASM_VECTOR_LEN;
200
+ wasm.parse(retptr, ptr0, len0, ptr1, len1, addHeapObject(options));
201
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
202
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
203
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
204
+ if (r2) {
205
+ throw takeObject(r1);
206
+ }
207
+ return takeObject(r0);
208
+ } finally {
209
+ wasm.__wbindgen_add_to_stack_pointer(16);
210
+ }
211
+ };
212
+
213
+ module.exports.__wbindgen_object_drop_ref = function(arg0) {
214
+ takeObject(arg0);
215
+ };
216
+
217
+ module.exports.__wbindgen_is_undefined = function(arg0) {
218
+ const ret = getObject(arg0) === undefined;
219
+ return ret;
220
+ };
221
+
222
+ module.exports.__wbindgen_in = function(arg0, arg1) {
223
+ const ret = getObject(arg0) in getObject(arg1);
224
+ return ret;
225
+ };
226
+
227
+ module.exports.__wbindgen_boolean_get = function(arg0) {
228
+ const v = getObject(arg0);
229
+ const ret = typeof(v) === 'boolean' ? (v ? 1 : 0) : 2;
230
+ return ret;
231
+ };
232
+
233
+ module.exports.__wbindgen_string_get = function(arg0, arg1) {
234
+ const obj = getObject(arg1);
235
+ const ret = typeof(obj) === 'string' ? obj : undefined;
236
+ var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
237
+ var len1 = WASM_VECTOR_LEN;
238
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
239
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
240
+ };
241
+
242
+ module.exports.__wbindgen_is_object = function(arg0) {
243
+ const val = getObject(arg0);
244
+ const ret = typeof(val) === 'object' && val !== null;
245
+ return ret;
246
+ };
247
+
248
+ module.exports.__wbindgen_error_new = function(arg0, arg1) {
249
+ const ret = new Error(getStringFromWasm0(arg0, arg1));
250
+ return addHeapObject(ret);
251
+ };
252
+
253
+ module.exports.__wbindgen_object_clone_ref = function(arg0) {
254
+ const ret = getObject(arg0);
255
+ return addHeapObject(ret);
256
+ };
257
+
258
+ module.exports.__wbindgen_jsval_loose_eq = function(arg0, arg1) {
259
+ const ret = getObject(arg0) == getObject(arg1);
260
+ return ret;
261
+ };
262
+
263
+ module.exports.__wbindgen_number_get = function(arg0, arg1) {
264
+ const obj = getObject(arg1);
265
+ const ret = typeof(obj) === 'number' ? obj : undefined;
266
+ getDataViewMemory0().setFloat64(arg0 + 8 * 1, isLikeNone(ret) ? 0 : ret, true);
267
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, !isLikeNone(ret), true);
268
+ };
269
+
270
+ module.exports.__wbindgen_string_new = function(arg0, arg1) {
271
+ const ret = getStringFromWasm0(arg0, arg1);
272
+ return addHeapObject(ret);
273
+ };
274
+
275
+ module.exports.__wbg_getwithrefkey_edc2c8960f0f1191 = function(arg0, arg1) {
276
+ const ret = getObject(arg0)[getObject(arg1)];
277
+ return addHeapObject(ret);
278
+ };
279
+
280
+ module.exports.__wbg_set_f975102236d3c502 = function(arg0, arg1, arg2) {
281
+ getObject(arg0)[takeObject(arg1)] = takeObject(arg2);
282
+ };
283
+
284
+ module.exports.__wbg_new_16b304a2cfa7ff4a = function() {
285
+ const ret = new Array();
286
+ return addHeapObject(ret);
287
+ };
288
+
289
+ module.exports.__wbg_new_72fb9a18b5ae2624 = function() {
290
+ const ret = new Object();
291
+ return addHeapObject(ret);
292
+ };
293
+
294
+ module.exports.__wbg_set_d4638f722068f043 = function(arg0, arg1, arg2) {
295
+ getObject(arg0)[arg1 >>> 0] = takeObject(arg2);
296
+ };
297
+
298
+ module.exports.__wbg_instanceof_ArrayBuffer_836825be07d4c9d2 = function(arg0) {
299
+ let result;
300
+ try {
301
+ result = getObject(arg0) instanceof ArrayBuffer;
302
+ } catch (_) {
303
+ result = false;
304
+ }
305
+ const ret = result;
306
+ return ret;
307
+ };
308
+
309
+ module.exports.__wbg_buffer_12d079cc21e14bdb = function(arg0) {
310
+ const ret = getObject(arg0).buffer;
311
+ return addHeapObject(ret);
312
+ };
313
+
314
+ module.exports.__wbg_new_63b92bc8671ed464 = function(arg0) {
315
+ const ret = new Uint8Array(getObject(arg0));
316
+ return addHeapObject(ret);
317
+ };
318
+
319
+ module.exports.__wbg_instanceof_Uint8Array_2b3bbecd033d19f6 = function(arg0) {
320
+ let result;
321
+ try {
322
+ result = getObject(arg0) instanceof Uint8Array;
323
+ } catch (_) {
324
+ result = false;
325
+ }
326
+ const ret = result;
327
+ return ret;
328
+ };
329
+
330
+ module.exports.__wbg_length_c20a40f15020d68a = function(arg0) {
331
+ const ret = getObject(arg0).length;
332
+ return ret;
333
+ };
334
+
335
+ module.exports.__wbg_set_a47bac70306a19a7 = function(arg0, arg1, arg2) {
336
+ getObject(arg0).set(getObject(arg1), arg2 >>> 0);
337
+ };
338
+
339
+ module.exports.__wbindgen_debug_string = function(arg0, arg1) {
340
+ const ret = debugString(getObject(arg1));
341
+ const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
342
+ const len1 = WASM_VECTOR_LEN;
343
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
344
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
345
+ };
346
+
347
+ module.exports.__wbindgen_throw = function(arg0, arg1) {
348
+ throw new Error(getStringFromWasm0(arg0, arg1));
349
+ };
350
+
351
+ module.exports.__wbindgen_memory = function() {
352
+ const ret = wasm.memory;
353
+ return addHeapObject(ret);
354
+ };
355
+
356
+ const path = require('path').join(__dirname, 'cjs_module_lexer_bg.wasm');
357
+ const bytes = require('fs').readFileSync(path);
358
+
359
+ const wasmModule = new WebAssembly.Module(bytes);
360
+ const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
361
+ wasm = wasmInstance.exports;
362
+ module.exports.__wasm = wasm;
363
+
Binary file