Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,16 @@ When using the ESM version, Wasm is supported instead:

```js
import { parse, init } from 'cjs-module-lexer';
// init() needs to be called and waited upon, or use initSync() to compile
// Wasm blockingly and synchronously.
// In browsers, initialize the Wasm before parsing.
await init();
const { exports, reexports } = parse(source);
```

`parse` calls `initSync()` on first use if initialization has not completed.
Call `initSync()` directly to choose when the blocking initialization runs.
Browser main threads can restrict synchronous Wasm compilation, so await
`init()` before parsing in browsers.

The Wasm build is around 1.5x faster and without a cold start.

### Grammar
Expand Down
2 changes: 1 addition & 1 deletion src/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const isLE = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;

export function parse (source, name = '@') {
if (!wasm)
throw new Error('Not initialized');
initSync();

const len = source.length + 1;

Expand Down
10 changes: 10 additions & 0 deletions test/_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ async function loadParser () {
suite('Lexer', () => {
suiteSetup(async () => await loadParser());

test('automatic Wasm initialization', async () => {
const lexer = await import('../dist/lexer.mjs?automatic-initialization');

assert.deepStrictEqual(lexer.parse('exports.value = 1'), {
exports: ['value'],
reexports: []
});
lexer.initSync();
});

test('export star failure', () => {
parse(`__exportStar((0));`);
});
Expand Down
Loading