示例工程 21-commonjs-snapshot

21-commonjs-snapshot/01-cjs-shim/README.md

markdown · 0.7 KB

运行:21 · cjs-shim

点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。

# 01 · cjs-shim
 
**演示**:`export =` + `import = require` 编译成同步 `require`,用最小 CJS loader 在 VM 里真跑。
**运行**:无浏览器输出;测试会读取 `dist-cjs/` 并执行。
**类型检查**:`node node_modules/typescript/bin/tsc -p examples/21-commonjs-snapshot/01-cjs-shim/tsconfig.types.json`
**预期类型错误**(`demo/esm-in-cjs.ts`,由 `tsconfig.fail.json` 触发):
```text
demo/esm-in-cjs.ts(3,1): error TS1287: A top-level 'export' modifier cannot be used on value declarations in a CommonJS module when 'verbatimModuleSyntax' is enabled.
```
**注意**:`tsconfig.cjs.json` 用 `module: "commonjs"` + `moduleResolution: "bundler"`,并关闭 `verbatimModuleSyntax`。