Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use es-module-lexer to parse ESM #90

Open
timfish opened this issue May 30, 2024 · 0 comments
Open

Use es-module-lexer to parse ESM #90

timfish opened this issue May 30, 2024 · 0 comments

Comments

@timfish
Copy link
Contributor

timfish commented May 30, 2024

@AbhiPrasad suggested we use es-module-lexer father than acorn:

  • It's specifically designed for the task of parsing imports and exports
  • 8x smaller than acorn (4KiB vs 32KiB gzipped)
  • 20x faster than acorn

A very small single JS file (4KiB gzipped) that includes inlined Web Assembly for very fast source analysis of ECMAScript module syntax only.

For an example of the performance, Angular 1 (720KiB) is fully parsed in 5ms, in comparison to the fastest JS parser, Acorn which takes over 100ms.

Comprehensively handles the JS language grammar while remaining small and fast. - ~10ms per MB of JS cold and ~5ms per MB of JS warm, see benchmarks for more info.

I gave it a test and the entire getEsmExports can be replaced with this:

const { init, parse } = require('es-module-lexer')

async function getEsmExports (moduleSource) {
  await init
  const srcString = moduleSource.toString()
  const [imports, exports] = parse(srcString)

  const reexports = imports
    .map(i => [srcString.slice(i.s, i.e), srcString.slice(i.ss, i.se)])
    .filter(([, full]) => full.match(/export\s*\*\s*from/))
    .map(([file]) => `* from ${file}`)

  const exportNames = exports.map(e => e.n)
  return [...exportNames, ...reexports]
}

A couple of tests fail due to missing parser features:
guybedford/es-module-lexer#175
guybedford/es-module-lexer#176

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant