模极社社区 - 制造业全产业链交流平台

    • 登录
    • 版块
    • 最新
    • 话题
    • 热门
    • 用户
    • 群组

    ES Module Lexer——模具行业从业者必备知识手册

    社区公告
    1
    1
    1
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • S
      system 2 最后由 编辑

      <h2>ES Module Lexer</h2>

      <p>[![Build Status][actions-image]][actions-url]</p>

      <p>A JS module syntax lexer used in <a href="https://github.com/guybedford/es-module-shims">es-module-shims</a>.</p>

      <p>Outputs the list of exports and locations of import specifiers, including dynamic import and import meta handling.</p>

      <p>Supports new syntax features including import attributes and source phase imports.</p>

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

      <p>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.</p>

      <p>Comprehensively handles the JS language grammar while remaining small and fast. - ~10ms per MB of JS cold and ~5ms per MB of JS warm, <a href="#benchmarks">see benchmarks</a> for more info.</p>

      <blockquote>
      <p><a href="https://github.com/guybedford/es-module-lexer/blob/main/chompfile.toml">Built with</a> <a href="https://chompbuild.com/">Chomp</a></p>
      </blockquote>

      <h4>Usage</h4>

      <pre><code>
      npm install es-module-lexer
      </code></pre>

      <p>See <a href="types/lexer.d.ts">types/lexer.d.ts</a> for the type definitions.</p>

      <p>For use in CommonJS:</p>

      <pre><code>
      const { init, parse } = require('es-module-lexer');

      (async () => {
      // either await init, or call parse asynchronously
      // this is necessary for the Web Assembly boot
      await init;

      const source = 'export var p = 5';
      const [imports, exports] = parse(source);

      // Returns "p"
      source.slice(exports[0].s, exports[0].e);
      // Returns "p"
      source.slice(exports[0].ls, exports[0].le);
      })();
      </code></pre>

      <p>An ES module version is also available:</p>

      <pre><code>
      import { init, parse } from 'es-module-lexer';

      (async () => {
      await init;

      const source = `
      import { name } from 'mod\u1011';
      import json from './json.json' assert { type: 'json' }
      export var p = 5;
      export function q () {

      };
      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);
      

      `;

      const [imports, exports] = parse(source, 'optional-sourcename');

      // Returns "modထ"
      imports[0].n
      // Returns "mod\u1011"
      source.slice(imports[0].s, imports[0].e);
      // "s" = start
      // "e" = end

      // Returns "import { name } from 'mod'"
      source.slice(imports[0].ss, imports[0].se);
      // "ss" = statement start
      // "se" = statement end

      // Returns "{ type: 'json' }"
      source.slice(imports[1].a, imports[1].se);
      // "a" = assert, -1 for no assertion

      // Returns "external"
      source.slice(imports[2].s, imports[2].e);

      // Returns "p"
      source.slice(exports[0].s, exports[0].e);
      // Returns "p"
      source.slice(exports[0].ls, exports[0].le);
      // Returns "q"
      source.slice(exports[1].s, exports[1].e);
      // Returns "q"
      source.slice(exports[1].ls, exports[1].le);
      // Returns "'external name'"
      source.slice(exports[2].s, exports[2].e);
      // Returns -1
      exports[2].ls;
      // Returns -1
      exports[2].le;

      // Import type is provided by t value
      // (1 for static, 2, for dynamic)
      // Returns true
      imports[2].t == 2;

      // Returns "asdf" (only for string literal dynamic imports)
      imports[2].n
      // Returns "import /comment!/ ( 'asdf', { assert: { type: 'json' } })"
      source.slice(imports[3].ss, imports[3].se);
      // Returns "'asdf'"
      source.slice(imports[3].s, imports[3].e);
      // Returns "( 'asdf', { assert: { type: 'json' } })"
      source.slice(


      哪个方案更实用?欢迎投票讨论。
      </code></pre>

      1 条回复 最后回复 回复 引用 0
      • First post
        Last post
      Powered by NodeBB | Contributors