ESM 入口点
🌐 ESM entry points
"useVfs": true 也支持 "mainFormat": "module"。ESM 主脚本是通过 ESM 加载器从挂载点内部加载的,因此 import.meta.url、import.meta.filename 和 import.meta.dirname 反映了主脚本在虚拟文件系统中的位置,静态和动态导入则会基于打包后的资源进行解析:
import fs from 'node:fs';
import path from 'node:path';
// import.meta.dirname is the root of the virtual file system.
const data = fs.readFileSync(
path.join(import.meta.dirname, 'data/file.txt'));
// Relative and bare specifier imports resolve inside the mount.
import myModule from './lib/mymodule.mjs';
const lazy = await import('./lib/lazy.mjs'); 模块格式检测的工作方式与真实文件系统相同:将打包的 ES 模块命名为 .mjs 扩展名(或将相关的 package.json 文件作为资源提供),这样它们就会被解释为 ESM。
🌐 Module format detection works the same way as on the real file
system: name bundled ES modules with the .mjs extension (or provide the
relevant package.json files as assets) so they are interpreted as ESM.