跳至正文
Wener Notes

Bundle FAQ

约 2 分钟阅读
标签:FAQ
TypeScript
// for tree-shaking

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

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

.js 文件后缀

text
✘ [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
json
{
  "exports": {
    "./src/*": {
      "types": "./src/*",
      "typescript": "./src/*",
      "import": "./dist/out/*.js"
    }
  }
}
  1. 通过直接 import 加后缀解决
TypeScript
import { GeneralResponseDto } from 'server/src/common/dto/index.ts';
tsconfig.json
{
  "compilerOptions": {
    "allowImportingTsExtensions": true
  }
}
  1. sed
bash
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

关联信息

反向链接、本文链接的其他页面和外部资料。

反向链接

  • swc
    笔记 · js extension · js extension

    swc-project/swc · Speedy Web Compiler · Rust-based platform for the Web · 开发者被 vercel 收编 · 早期定位是 compiler - bundling & minifition not ready

站内链接

  • NextJS FAQ
    笔记 · 库 import 必须要有 .js 后缀 · js extension

    https://github.com/vercel/next.js/blob/canary/packages/next/src/lib/server-external-packages.json · react router 会导致页面整个刷新 #49479

参考资料

GitHub5 条
最近更新commit 906e816编辑