<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[ES Module Lexer——模具行业从业者必备知识手册]]></title><description><![CDATA[<p dir="auto">&lt;h2&gt;ES Module Lexer&lt;/h2&gt;</p>
<p dir="auto">&lt;p&gt;[![Build Status][actions-image&rsqb;&rsqb;[actions-url]&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;A JS module syntax lexer used in &lt;a href="<a href="https://github.com/guybedford/es-module-shims" rel="nofollow ugc">https://github.com/guybedford/es-module-shims</a>"&gt;es-module-shims&lt;/a&gt;.&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;Outputs the list of exports and locations of import specifiers, including dynamic import and import meta handling.&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;Supports new syntax features including import attributes and source phase imports.&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;A very small single JS file (4KiB gzipped) that includes inlined Web Assembly for very fast source analysis of ECMAScript module syntax only.&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;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.&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;<em>Comprehensively handles the JS language grammar while remaining small and fast. - ~10ms per MB of JS cold and ~5ms per MB of JS warm, &lt;a href="#benchmarks"&gt;see benchmarks&lt;/a&gt; for more info.</em>&lt;/p&gt;</p>
<p dir="auto">&lt;blockquote&gt;<br />
&lt;p&gt;&lt;a href="<a href="https://github.com/guybedford/es-module-lexer/blob/main/chompfile.toml" rel="nofollow ugc">https://github.com/guybedford/es-module-lexer/blob/main/chompfile.toml</a>"&gt;Built with&lt;/a&gt; &lt;a href="<a href="https://chompbuild.com/" rel="nofollow ugc">https://chompbuild.com/</a>"&gt;Chomp&lt;/a&gt;&lt;/p&gt;<br />
&lt;/blockquote&gt;</p>
<p dir="auto">&lt;h4&gt;Usage&lt;/h4&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
npm install es-module-lexer<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;p&gt;See &lt;a href="types/lexer.d.ts"&gt;types/lexer.d.ts&lt;/a&gt; for the type definitions.&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;For use in CommonJS:&lt;/p&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
const { init, parse } = require('es-module-lexer');</p>
<p dir="auto">(async () =&gt; {<br />
// either await init, or call parse asynchronously<br />
// this is necessary for the Web Assembly boot<br />
await init;</p>
<p dir="auto">const source = 'export var p = 5';<br />
const [imports, exports] = parse(source);</p>
<p dir="auto">// Returns "p"<br />
source.slice(exports[0].s, exports[0].e);<br />
// Returns "p"<br />
source.slice(exports[0].ls, exports[0].le);<br />
})();<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;p&gt;An ES module version is also available:&lt;/p&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
import { init, parse } from 'es-module-lexer';</p>
<p dir="auto">(async () =&gt; {<br />
await init;</p>
<p dir="auto">const source = `<br />
import { name } from 'mod\u1011';<br />
import json from './json.json' assert { type: 'json' }<br />
export var p = 5;<br />
export function q () {</p>
<pre><code>};
export { x as 'external name' } from 'external';

// Comments provided to demonstrate edge cases
import /*comment!*/ (  'asdf', { assert: { type: 'json' }});
import /*comment!*/.meta.asdf;

// Source phase imports:
import source mod from './mod.wasm';
import.source('./mod.wasm);
</code></pre>
<p dir="auto">`;</p>
<p dir="auto">const [imports, exports] = parse(source, 'optional-sourcename');</p>
<p dir="auto">// Returns "modထ"<br />
imports[0].n<br />
// Returns "mod\u1011"<br />
source.slice(imports[0].s, imports[0].e);<br />
// "s" = start<br />
// "e" = end</p>
<p dir="auto">// Returns "import { name } from 'mod'"<br />
source.slice(imports[0].ss, imports[0].se);<br />
// "ss" = statement start<br />
// "se" = statement end</p>
<p dir="auto">// Returns "{ type: 'json' }"<br />
source.slice(imports[1].a, imports[1].se);<br />
// "a" = assert, -1 for no assertion</p>
<p dir="auto">// Returns "external"<br />
source.slice(imports[2].s, imports[2].e);</p>
<p dir="auto">// Returns "p"<br />
source.slice(exports[0].s, exports[0].e);<br />
// Returns "p"<br />
source.slice(exports[0].ls, exports[0].le);<br />
// Returns "q"<br />
source.slice(exports[1].s, exports[1].e);<br />
// Returns "q"<br />
source.slice(exports[1].ls, exports[1].le);<br />
// Returns "'external name'"<br />
source.slice(exports[2].s, exports[2].e);<br />
// Returns -1<br />
exports[2].ls;<br />
// Returns -1<br />
exports[2].le;</p>
<p dir="auto">// Import type is provided by <code>t</code> value<br />
// (1 for static, 2, for dynamic)<br />
// Returns true<br />
imports[2].t == 2;</p>
<p dir="auto">// Returns "asdf" (only for string literal dynamic imports)<br />
imports[2].n<br />
// Returns "import /<em>comment!</em>/ (  'asdf', { assert: { type: 'json' } })"<br />
source.slice(imports[3].ss, imports[3].se);<br />
// Returns "'asdf'"<br />
source.slice(imports[3].s, imports[3].e);<br />
// Returns "(  'asdf', { assert: { type: 'json' } })"<br />
source.slice(</p>
<hr />
<p dir="auto">哪个方案更实用？欢迎投票讨论。<br />
&lt;/code&gt;&lt;/pre&gt;</p>
]]></description><link>https://forum.mj7.cn/topic/7200/es-module-lexer-模具行业从业者必备知识手册</link><generator>RSS for Node</generator><lastBuildDate>Fri, 02 Oct 2026 09:34:28 GMT</lastBuildDate><atom:link href="https://forum.mj7.cn/topic/7200.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 19 Apr 2026 21:53:46 GMT</pubDate><ttl>60</ttl></channel></rss>