Skip to main content

Bundle FAQ

// for tree-shaking

// 只需要在定义时
/* @__NO_SIDE_EFFECTS__ */

// 所有调用都需要标记
/* @__PURE__ */

.js 文件后缀

✘ [ERROR] Could not resolve "server/src/app/app.run"

dist/out/apps/ve-contract-server/main.js:8:31:
8 │ import { runApplication } from "server/src/app/app.run";
╵ ~~~~~~~~~~~~~~~~~~~~~~~~

The module "./dist/out/app/app.run" was not found on the file system:

node_modules/server/package.json:17:16:
17 │ "import": "./dist/out/*"
╵ ~~~~~~~~~~~~~~

Import from "server/src/app/app.run.js" to get the file
"node_modules/server/dist/out/app/app.run.js":

dist/out/apps/ve-contract-server/main.js:8:54:
8 │ import { runApplication } from "server/src/app/app.run";
│ ^
╵ .js

You can mark the path "server/src/app/app.run" as external to exclude it from the bundle, which
will remove this error.

解决办法

  1. 可以通过 exports 添加 - 不支持自动 index.ts
{
"exports": {
"./src/*": {
"types": "./src/*",
"typescript": "./src/*",
"import": "./dist/out/*.js"
}
}
}
  1. 通过直接 import 加后缀解决
import { GeneralResponseDto } from 'server/src/common/dto/index.ts';
tsconfig.json
{
"compilerOptions": {
"allowImportingTsExtensions": true
}
}
  1. sed
pnpm swc ./src -d ./lib
# for `'` & `"`
sed -E -i -r 's#(\bfrom\s+"\.[^"]+)";#\1.js";#g' ./lib/**/*.js
sed -E -i -r "s#(\bfrom\s+'\.[^']+)';#\1.js';#g" ./lib/**/*.js