{"name":"es-module-lexer","version":"2.2.0","keywords":[],"author":{"name":"Guy Bedford"},"license":"MIT","_id":"es-module-lexer@2.2.0","maintainers":[{"name":"guybedford","email":"guybedford@gmail.com"}],"homepage":"https://github.com/guybedford/es-module-lexer#readme","bugs":{"url":"https://github.com/guybedford/es-module-lexer/issues"},"dist":{"shasum":"fe824f12e3f28bde741bb572b5a182786eaf3764","tarball":"http://registry-npm.ywwl.com/es-module-lexer/download/es-module-lexer-2.2.0.tgz","fileCount":8,"integrity":"sha512-3lGxdTXCLfe1MYfTz1y2ksAAUM4NAOP6rPEjxGJVKO7TZ5+tvHCaQWGpC4Y3IXvW3ece0Cz1cIP4FWBxOnGCTQ==","signatures":[{"sig":"MEUCIGHDq7rO4uka1GjGHceiDCWp9P5IRNEC6WWbW9b9n6YaAiEApLYaoxMIxa6Ujqu37dvJe+1F0WnmTLFgsXsMQkqCmY0=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":110608,"size":37296,"noattachment":false,"key":"/es-module-lexer/-/es-module-lexer-2.2.0.tgz"},"main":"dist/lexer.cjs","type":"module","types":"types/lexer.d.ts","module":"dist/lexer.js","exports":{".":{"types":"./types/lexer.d.ts","import":"./dist/lexer.js","module":"./dist/lexer.js","require":"./dist/lexer.cjs"},"./js":{"types":"./types/lexer.d.ts","default":"./dist/lexer.asm.js"}},"gitHead":"1f1949411c9e8b2726bb5f99b77952142833ea5c","scripts":{"test":"npm install -g chomp ; chomp test","build":"npm install -g chomp ; chomp build"},"_npmUser":{"name":"guybedford","email":"guybedford@gmail.com"},"repository":{"url":"git+https://github.com/guybedford/es-module-lexer.git","type":"git"},"_npmVersion":"11.13.0","description":"Lexes ES modules returning their import/export metadata","directories":{"lib":"lib","test":"test"},"_nodeVersion":"26.2.0","_hasShrinkwrap":false,"devDependencies":{"kleur":"^4.1.5","mocha":"^11.7.6","terser":"^5.48.0","@swc/cli":"^0.8.1","@swc/core":"^1.15.41","@babel/cli":"^7.29.7","typescript":"^6.0.3","@babel/core":"^7.29.7","@types/node":"^25.9.3","@babel/plugin-transform-modules-commonjs":"^7.29.7"},"_npmOperationalInternal":{"tmp":"tmp/es-module-lexer_2.2.0_1782709963212_0.28267567798194815","host":"s3://npm-registry-packages-npm-production"},"readme":"# ES Module Lexer\n\n[![Build Status][actions-image]][actions-url]\n\nA JS module syntax lexer used in [es-module-shims](https://github.com/guybedford/es-module-shims).\n\nOutputs the list of exports and locations of import specifiers, including dynamic import and import meta handling.\n\nSupports new syntax features including import attributes and source phase imports.\n\nA very small single JS file (~7KiB gzipped) that includes inlined Web Assembly for very fast source analysis of ECMAScript module syntax only.\n\nFor 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.\n\n_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](#benchmarks) for more info._\n\n> [Built with](https://github.com/guybedford/es-module-lexer/blob/main/chompfile.toml) [Chomp](https://chompbuild.com/)\n\n### Usage\n\n```\nnpm install es-module-lexer\n```\n\nSee [src/lexer.ts](src/lexer.ts) for the type definitions.\n\nFor use in CommonJS:\n\n```js\nconst { init, parse } = require('es-module-lexer');\n\n(async () => {\n  // either await init, or call parse asynchronously\n  // this is necessary for the Web Assembly boot\n  await init;\n\n  const source = 'export var p = 5';\n  const [imports, exports] = parse(source);\n  \n  // Returns \"p\"\n  source.slice(exports[0].s, exports[0].e);\n  // Returns \"p\"\n  source.slice(exports[0].ls, exports[0].le);\n})();\n```\n\nAn ES module version is also available:\n\n```js\nimport { init, parse } from 'es-module-lexer';\n\n(async () => {\n  await init;\n\n  const source = `\n    import { name } from 'mod\\\\u1011';\n    import json from './json.json' with { type: 'json' }\n    export var p = 5;\n    export function q () {\n\n    };\n    export { x as 'external name' } from 'external';\n\n    // Comments provided to demonstrate edge cases\n    import /*comment!*/ (  'asdf', { with: { type: 'json' }});\n    import /*comment!*/.meta.asdf;\n\n    // Source phase imports:\n    import source mod from './mod.wasm';\n    import.source('./mod.wasm');\n  `;\n\n  const [imports, exports] = parse(source, 'optional-sourcename');\n\n  // Returns \"modထ\"\n  imports[0].n\n  // Returns \"mod\\u1011\"\n  source.slice(imports[0].s, imports[0].e);\n  // \"s\" = start\n  // \"e\" = end\n\n  // Returns \"import { name } from 'mod'\"\n  source.slice(imports[0].ss, imports[0].se);\n  // \"ss\" = statement start\n  // \"se\" = statement end\n\n  // Returns \"{ type: 'json' }\"\n  source.slice(imports[1].a, imports[1].se);\n  // \"a\" = attribute start, -1 for no import attributes\n\n  // Parsed import attributes are available in `at`\n  // Returns [['type', 'json']]\n  imports[1].at;\n  // Returns 'json'\n  imports[1].at[0][1];\n\n  // Returns null (no attributes)\n  imports[0].at;\n\n  // Returns \"external\"\n  source.slice(imports[2].s, imports[2].e);\n\n  // Returns \"p\"\n  source.slice(exports[0].s, exports[0].e);\n  // Returns \"p\"\n  source.slice(exports[0].ls, exports[0].le);\n  // Returns \"q\"\n  source.slice(exports[1].s, exports[1].e);\n  // Returns \"q\"\n  source.slice(exports[1].ls, exports[1].le);\n\n  // \"ss\" = export statement start (only the start is tracked, not the end)\n  // Returns \"export\"\n  source.slice(exports[0].ss, exports[0].ss + 6);\n\n  // Returns \"'external name'\"\n  source.slice(exports[2].s, exports[2].e);\n  // Returns -1\n  exports[2].ls;\n  // Returns -1\n  exports[2].le;\n\n  // Import type is provided by `t` value\n  // (1 for static, 2, for dynamic)\n  // Returns true\n  imports[2].t == 2;\n\n  // Returns \"asdf\" (only for string literal dynamic imports)\n  imports[2].n\n  // Returns \"import /*comment!*/ (  'asdf', { with: { type: 'json' } })\"\n  source.slice(imports[3].ss, imports[3].se);\n  // Returns \"'asdf'\"\n  source.slice(imports[3].s, imports[3].e);\n  // Returns \"(  'asdf', { with: { type: 'json' } })\"\n  source.slice(imports[3].d, imports[3].se);\n  // Returns \"{ with: { type: 'json' } }\"\n  source.slice(imports[3].a, imports[3].se - 1);\n\n  // For non-string dynamic import expressions:\n  // - n will be undefined\n  // - a is currently -1 even if there is an import attribute\n  // - e is currently the character before the closing )\n\n  // For nested dynamic imports, the se value of the outer import is -1 as end tracking does not\n  // currently support nested dynamic immports\n\n  // import.meta is indicated by imports[3].d === -2\n  // Returns true\n  imports[4].d === -2;\n  // Returns \"import /*comment!*/.meta\"\n  source.slice(imports[4].s, imports[4].e);\n  // ss and se are the same for import meta\n\n  // Returns \"'./mod.wasm'\"\n  source.slice(imports[5].s, imports[5].e);\n\n  // Import type 4 and 5 for static and dynamic source phase\n  imports[5].t === 4;\n  imports[6].t === 5;\n})();\n```\n\n### CSP asm.js Build\n\nThe default version of the library uses Wasm and (safe) eval usage for performance and a minimal footprint.\n\nNeither of these represent security escalation possibilities since there are no execution string injection vectors, but that can still violate existing CSP policies for applications.\n\nFor a version that works with CSP eval disabled, use the `es-module-lexer/js` build:\n\n```js\nimport { parse } from 'es-module-lexer/js';\n```\n\nInstead of Web Assembly, this uses an asm.js build which is almost as fast as the Wasm version ([see benchmarks below](#benchmarks)).\n\n### Minimal Build\n\nFor size-sensitive embedders, the `es-module-lexer/minimal` build drops certain features to reduce the binary size. This is used for example by [es-module-shims](https://github.com/guybedford/es-module-shims):\n\n```js\nimport { parse } from 'es-module-lexer/minimal';\n```\n\nCompared to the full build:\n\n* `parse` returns a two-element `[imports, exports]` tuple only - the third and fourth facade and `hasModuleSyntax` booleans are dropped.\n* Imports drop the parsed attribute list `at` (the attribute source remains recoverable via the `a` attributes index).\n* Exports drop the statement start `ss`.\n\nAll other fields are identical to the full build. For CSP eval disabled support, the equivalent asm.js build is available as `es-module-lexer/minimal/js`.\n\n### Import Attributes\n\nThe `a` field provides the index of the start of the `{` attributes bracket, or -1 for no attributes.\n\nThe list of attribute key and value pairs are provided on the `at` field (full build only):\n\n```js\nconst [imports] = parse(`\n  import json from './foo.json' with { type: 'json' };\n  import './foo.css' with { type: 'css' };\n  import pkg from 'pkg' with { type: 'json', integrity: 'sha384-...' };\n`);\n\n// Returns [['type', 'json']]\nimports[0].at;\n\n// Returns [['type', 'css']]\nimports[1].at;\n\n// Multiple attributes\n// Returns [['type', 'json'], ['integrity', 'sha384-...']]\nimports[2].at;\n```\n\nThe `at` field is an array of `[key, value]` tuples, or `null` if there are no attributes.\n\nBoth keys and values support escape sequences:\n\n```js\nconst [imports] = parse(`\n  import foo from './foo.js' with { \"custom-key\": \"value\" };\n  import bar from './bar.js' with { \"key\\\\nwith\\\\nnewlines\": \"value\\\\twith\\\\ttabs\" };\n`);\n\n// Quoted keys are unquoted\n// Returns [['custom-key', 'value']]\nimports[0].at;\n\n// Escape sequences are processed\n// Returns [['key\\nwith\\nnewlines', 'value\\twith\\ttabs']]\nimports[1].at;\n```\n\n### Escape Sequences\n\nTo handle escape sequences in specifier strings, the `.n` field of imported specifiers will be provided where possible.\n\nFor dynamic import expressions, this field will be empty if not a valid JS string.\n\n### Facade Detection\n\nFacade modules that only use import / export syntax can be detected via the third return value (full build only):\n\n```js\nconst [,, facade] = parse(`\n  export * from 'external';\n  import * as ns from 'external2';\n  export { a as b } from 'external3';\n  export { ns };\n`);\nfacade === true;\n```\n\n### ESM Detection\n\nModules that uses ESM syntaxes can be detected via the fourth return value (full build only):\n\n```js\nconst [,,, hasModuleSyntax] = parse(`\n  export {}\n`);\nhasModuleSyntax === true;\n```\n\nDynamic imports are ignored since they can be used in Non-ESM files.\n\n```js\nconst [,,, hasModuleSyntax] = parse(`\n  import('./foo.js')\n`);\nhasModuleSyntax === false;\n```\n\n### Environment Support\n\nNode.js 10+, and [all browsers with Web Assembly support](https://caniuse.com/#feat=wasm).\n\n### Grammar Support\n\n* Token state parses all line comments, block comments, strings, template strings, blocks, parens and punctuators.\n* Division operator / regex token ambiguity is handled via backtracking checks against punctuator prefixes, including closing brace or paren backtracking.\n* Always correctly parses valid JS source, but may parse invalid JS source without errors.\n\n### Limitations\n\nThe lexing approach is designed to deal with the full language grammar including RegEx / division operator ambiguity through backtracking and paren / brace tracking.\n\nBecause it lexes rather than fully parses, the analysis is not a validation pass: valid JS source is always analyzed correctly, but some invalid source is accepted without an error rather than rejected. For example `export const = 1` lexes to an empty exports list instead of throwing. Callers that need to reject invalid source should run a validating parser separately.\n\nMultiple exports per declaration (`export var a = 'asdf', q = z`) and renamed destructured exports (`export var { a: b } = asdf`) are detected correctly; earlier versions missed `q` and `b` in these forms.\n\n### Benchmarks\n\nBenchmarks can be run with `npm run bench`.\n\nCurrent results on a standard desktop machine:\n\n#### Wasm Build\n\n```\nModule load time\n> 1ms\nCold Run, All Samples\ntest/samples/*.js (3057 KiB)\n> 13ms\n\nWarm Runs (average of 25 runs)\ntest/samples/angular.js (719 KiB)\n> 1ms\ntest/samples/angular.min.js (188 KiB)\n> 1ms\ntest/samples/d3.js (491 KiB)\n> 2ms\ntest/samples/d3.min.js (274 KiB)\n> 1.04ms\ntest/samples/magic-string.js (34 KiB)\n> 0ms\ntest/samples/magic-string.min.js (20 KiB)\n> 0ms\ntest/samples/rollup.js (902 KiB)\n> 3ms\ntest/samples/rollup.min.js (429 KiB)\n> 2ms\n\nWarm Runs, All Samples (average of 25 runs)\ntest/samples/*.js (3057 KiB)\n> 10m\n```\n\n### JS Build (asm.js)\n\n```\nModule load time\n> 1ms\nCold Run, All Samples\ntest/samples/*.js (3057 KiB)\n> 92ms\n\nWarm Runs (average of 25 runs)\ntest/samples/angular.js (719 KiB)\n> 3.6ms\ntest/samples/angular.min.js (188 KiB)\n> 2ms\ntest/samples/d3.js (491 KiB)\n> 4ms\ntest/samples/d3.min.js (274 KiB)\n> 2.52ms\ntest/samples/magic-string.js (34 KiB)\n> 0ms\ntest/samples/magic-string.min.js (20 KiB)\n> 0ms\ntest/samples/rollup.js (902 KiB)\n> 6ms\ntest/samples/rollup.min.js (429 KiB)\n> 3.2ms\n\nWarm Runs, All Samples (average of 25 runs)\ntest/samples/*.js (3057 KiB)\n> 20.88ms\n```\n\n### Building\n\nThis project uses [Chomp](https://chompbuild.com) for building.\n\nWith Chomp installed, download the WASI SDK 12.0 from https://github.com/WebAssembly/wasi-sdk/releases/tag/wasi-sdk-12.\n\n- [Linux](https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-12/wasi-sdk-12.0-linux.tar.gz)\n- [Windows (MinGW)](https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-12/wasi-sdk-12.0-mingw.tar.gz)\n- [macOS](https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-12/wasi-sdk-12.0-macos.tar.gz)\n\nLocate the WASI-SDK as a sibling folder, or customize the path via the `WASI_PATH` environment variable.\n\nEmscripten emsdk is also assumed to be a sibling folder or via the `EMSDK_PATH` environment variable.\n\nExample setup:\n\n```\ngit clone https://github.com:guybedford/es-module-lexer\ngit clone https://github.com/emscripten-core/emsdk\ncd emsdk\ngit checkout 1.40.1-fastcomp\n./emsdk install 1.40.1-fastcomp\ncd ..\nwget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-12/wasi-sdk-12.0-linux.tar.gz\ngunzip wasi-sdk-12.0-linux.tar.gz\ntar -xf wasi-sdk-12.0-linux.tar\nmv wasi-sdk-12.0-linux.tar wasi-sdk-12.0\ncargo install chompbuild\ncd es-module-lexer\nchomp test\n```\n\nFor the `asm.js` build, git clone `emsdk` from  is assumed to be a sibling folder as well.\n\n### License\n\nMIT\n\n[actions-image]: https://github.com/guybedford/es-module-lexer/actions/workflows/build.yml/badge.svg\n[actions-url]: https://github.com/guybedford/es-module-lexer/actions/workflows/build.yml\n","publish_time":1782709963341,"_cnpm_publish_time":1782709963341,"dist-tags":{"latest":"3.0.5"}}