• T8_471281
  • 666.4KB
  • zip
  • 0
  • VIP专享
  • 0
  • 2022-05-06 05:32
esm 极其简单,无babel,无捆绑的ECMAScript模块加载器。 esm是世界上最先进的ECMAScript模块加载器。 在Node 6+中支持ECMAScript模块只需要这种快速,可生产且零依赖的加载器。 有关详细信息,请参见发布和! 安装 新项目 运行npm init esm或yarn create esm 。 :light_bulb: 使用-y标志对所有提示回答“是”。 现有项目 运行npm i esm或yarn add esm 。 入门 有两种启用esm 。 为软件包启用esm : 使用esm加载主ES模块并将其导出为CommonJS。 index.js // Set options as a parameter, environment variable, or rc file. require = require ( "esm" ) ( module /*, options*/ ) module . exports = require ( "./main.js" ) main.js // ESM syntax is supported. ex
# esm The brilliantly simple, babel-less, bundle-less ECMAScript module loader. > `esm` is the world’s most advanced ECMAScript module loader. This fast, production ready, zero dependency loader is all you need to support ECMAScript modules in Node 6+. See the release [post]( and [video]( for details! Install --- * __New projects__ Run `npm init esm` or `yarn create esm`. :bulb: Use the `-y` flag to answer “yes” to all prompts. * __Existing projects__ Run `npm i esm` or `yarn add esm`. Getting started --- There are two ways to enable `esm`. 1. Enable `esm` for packages: Use `esm` to load the main ES module and export it as CommonJS. __index.js__ ```js // Set options as a parameter, environment variable, or rc file. require = require("esm")(module/*, options*/) module.exports = require("./main.js") ``` __main.js__ ```js // ESM syntax is supported. export {} ``` :bulb: These files are automagically created with `npm init esm` or `yarn create esm`. 2. Enable `esm` for local runs: ```shell node -r esm main.js ``` :bulb: Omit the filename to enable `esm` in the REPL. Features --- :clap: By default, :100: percent CJS interoperability is enabled so you can get stuff done.<br> :lock: `.mjs` files are limited to basic functionality without support for `esm` options. Out of the box `esm` just works, no configuration necessary, and supports: * Passing all applicable [test262]( compliance tests * [`import`]([`export`]( * [`import.meta`]( * [Dynamic `import`]( * [Live bindings]( * [File URI scheme]( * Node `stdin`, [`--eval`](, [`--print`]( flags * Node [`--check`]( flag _(Node 10+)_ Options --- Specify options with one of the following: * `"esm"` field in `package.json` * CJS/ESM in an `.esmrc.js`, `.esmrc.cjs`, or `.esmrc.mjs` file * [JSON6]( in an `.esmrc` or `.esmrc.json` file * JSON6 or file path in the `ESM_OPTIONS` environment variable * `ESM_DISABLE_CACHE` environment variable <table> <tr> <td colspan="2"><code>{</code></td> </tr> <tr> <td valign="top"><code>"cjs":true</code></td> <td> <p>A boolean or object for toggling CJS features in ESM.</p> <details> <summary>Features</summary> <table> <tr> <td colspan="2"><code>{</code></td> </tr> <tr> <td valign="top"><code>"cache":true</code></td> <td> <p>A boolean for storing ES modules in <code>require.cache</code>.</p> </td> </tr> <tr> <td valign="top"><code>"esModule":true</code></td> <td> <p>A boolean for <code>__esModule</code> interoperability.</p> </td> </tr> <tr> <td valign="top"><code>"extensions":true</code></td> <td> <p>A boolean for respecting <code>require.extensions</code> in ESM.</p> </td> </tr> <tr> <td valign="top"><code>"mutableNamespace":true</code></td> <td> <p>A boolean for mutable <a href="" rel='nofollow' onclick='return false;'>namespace objects</a>.</p> </td> </tr> <tr> <td valign="top"><code>"namedExports":true</code></td> <td> <p>A boolean for <a href="" rel='nofollow' onclick='return false;'>importing named exports</a> of CJS modules.</p> </td> </tr> <tr> <td valign="top"><code>"paths":true</code></td> <td> <p>A boolean for following CJS <a href="" rel='nofollow' onclick='return false;'>path rules</a> in ESM.</p> </td> </tr> <tr> <td valign="top"><code>"vars":true</code></td> <td> <p>A boolean for <code>__dirname</code>, <code>__filename</code>, and <code>require</code> in ESM.</p> </td> </tr> <tr> <td valign="top"><code>"dedefault":false</code></td> <td> <p>A boolean for requiring ES modules without the dangling <code>require().default</code>.</p> </td> </tr> <tr> <td valign="top"><code>"topLevelReturn":false</code></td> <td> <p>A boolean for top-level <code>return</code> support.</p> </td> </tr> <tr> <td colspan="2"><code>}</code></td> </tr> </table> </details> </td> </tr> <tr> <td valign="top"><code>"mainFields":["main"]</code></td> <td> <p>An array of fields checked when importing a package.</p> </td> </tr> <tr> <td valign="top"><code>"mode":"auto"</code></td> <td> <p>A string mode:</p> <ul> <li><code>"auto"</code> detect files with <code>import</code>, <code>import.meta</code>, <code>export</code>,<br><a href="" rel='nofollow' onclick='return false;'><code>"use module"</code></a>, or <code>.mjs</code> as ESM.</li> <li><code>"all"</code> files besides those with <code>"use script"</code> or <code>.cjs</code> are treated as ESM.</li> <li><code>"strict"</code> to treat <strong>only</strong> <code>.mjs</code> files as ESM.</li> </ul> </td> </tr> <tr> <td valign="top"><code>"await":false</code></td> <td> <p>A boolean for <a href="" rel='nofollow' onclick='return false;'>top-level <code>await</code></a> in modules without ESM exports. <em>(Node 10+)</em></p> </td> </tr> <tr> <td valign="top"><code>"force":false</code></td> <td> <p>A boolean to apply these options to all module loads.</p> </td> </tr> <tr> <td valign="top"><code>"wasm":false</code></td> <td> <p>A boolean for <a href="" rel='nofollow' onclick='return false;'>WebAssembly</a> module support. <em>(Node 8+)</em></p> </td> </tr> <tr> <td colspan="2"><code>}</code></td> </tr> </table> DevOpts --- <table> <tr> <td colspan="2"><code>{</code></td> </tr> <tr> <td valign="top"><code>"cache":true</code></td> <td> <p>A boolean for toggling cache creation or a cache directory path.</p> </td> </tr> <tr> <td valign="top"><code>"sourceMap":false</code></td> <td> <p>A boolean for including inline source maps.</p> </td> </tr> <tr> <td colspan="2"><code>}</code></td> </tr> </table> Tips --- ### Bundling * For bundlers like [`browserify`]([`esmify`](, [`parcel-bundler`](, and [`webpack`]( add a `"module"` field to `package.json` pointing to the main ES module. ```json "main": "index.js", "module": "main.js" ``` :bulb: This is automagically done with `npm init esm` or `yarn create esm`. ### Extensions * Enable `esm` for [`wallaby.js`]( following their [integration example]( ### Loading * Load `esm` before loaders/monitors like [`@babel/register`](, [`newrelic`](, [`sqreen`](, and [`ts-node`]( * Load `esm` for [`jasmine`]( using the [`"helpers"`]( fiel