Skip to content

npm 配置

weapp-vite 会自动把 dependencies 里的依赖构建成 miniprogram_npm/,而把 devDependencies 视为“仅构建期依赖”,直接内联进产物。

依赖分类规则(默认)

jsonc
{
  "dependencies": {
    "lodash": "^4.17.21"
  },
  "devDependencies": {
    "lodash-es": "^4.17.21"
  }
}
  • 引入 lodash ⇒ 产物保留 require('lodash'),并生成 miniprogram_npm/lodash
  • 引入 lodash-es ⇒ 相关实现会被打包并内联到页面/组件脚本。

TIP

建议团队统一约定:运行时依赖放 dependencies,构建期依赖放 devDependencies

weapp.npm

  • 类型
    ts
    {
      enable?: boolean
      cache?: boolean
      buildOptions?: (options: NpmBuildOptions, meta: { name: string; entry: InputOption }) => NpmBuildOptions | undefined
    }
  • 默认值{ enable: true, cache: true }
ts
import { defineConfig } from 'weapp-vite/config'

export default defineConfig({
  weapp: {
    npm: {
      enable: true,
      cache: true,
      buildOptions(options, { name }) {
        if (name === 'lodash') {
          return {
            ...options,
            build: {
              ...options.build,
              target: 'es2018',
            },
          }
        }
        return options
      },
    },
  },
})

字段说明:

  • enable:关闭后 不会自动构建 miniprogram_npm。如果你的代码仍保留 require('pkg'),需要自行处理(如 devtools 构建 npm)。
  • cache:是否启用 npm 构建缓存(缓存目录:node_modules/weapp-vite/.cache/)。
  • buildOptions:为单个包覆写 Vite 库模式构建参数。

手动构建命令

如需在命令行触发开发者工具的 npm 构建:

json
{
  "scripts": {
    "build:npm": "weapp-vite build:npm",
    "npm": "weapp-vite npm"
  }
}

NOTE

该命令依赖 weapp-ide-cli,请确保微信开发者工具已开启“服务端口”。

常见问题

  • npm 构建内容未更新:尝试将 cache 设为 false,或清理 node_modules/weapp-vite/.cache/
  • 分包体积过大:结合 weapp.subPackages.*.dependencies 裁剪独立分包依赖。

下一步:需要分包能力?请前往 分包配置

Released under the MIT License.