@nrwl/node:build:如何更改输出文件名?

Chr*_*nen 5 node.js angular nrwl nrwl-nx

我正在使用 Angular CLI 和 Node 后端的 nx monorepo 工作。在 angular.json 中,我使用自定义 webpack 配置来更改包的文件名。调用ng build app(调用 @nrwl/node:build)工作正常,并将文件与指定名称捆绑在一起。

问题是:调用ng serve app会出现错误Cannot find module <path-to-outdir>/main.js。查看代码似乎文件名“main.js”是硬编码的!

如何以 @nrwl/node:execute 知道该文件名的方式更改输出文件名,或者是否有任何方法可以配置构建/执行以采用自定义文件名?

谢谢!

我的 angular.json 看起来像:

"service": {
  ...
  "architect": {
    "build": {
      "builder": "@nrwl/node:build",
      "options": {
        "webpackConfig": "apps/service/webpack.config.js",
        "outputPath": "dist/apps/service",
        "filename": "my-main-filename.js", // Note: webpack.config.ts reads this
        "main": "apps/service/src/my-main-filename.ts",
        ...
        }
    },
    "serve": {
          "builder": "@nrwl/node:execute",
          "options": {
            "buildTarget": "service:build"
          },
    }
...
Run Code Online (Sandbox Code Playgroud)

和 webpack.config.js

module.exports = (config, context) => {
  // Extract output path from context
  const {
    options: { outputPath, filename },
  } = context;

  config.output = {
    filename: filename,
    path: outputPath
  }
  return config;
};
Run Code Online (Sandbox Code Playgroud)

@nrwl/node的版本是9.2.1

Has*_*P A 2

您不必在 webpack.config 上指定任何内容。如果使用“executor”,可以直接在project.json中添加:“@nrwl/web:webpack”(https://nx.dev/packages/web/executors/webpack

  1. 如果不可用,请在 package.json 中添加@nrwl/workspace(对我来说,在设置项目时默认情况下会出现)
  2. 在project.json/angular.json中
"build": {
 "executor": "@nrwl/webpack:webpack",
 "outputs": ["{options.outputPath}"],
 "defaultConfiguration": "production",
 "options": {
   ...
   "outputPath": "dist/apps/service",
   "outputFileName": "my-main-filename.js",
   ...
 },
 "configurations": {
   "development": {
     "outputPath": "new path for development if needed"
   },
   "production": {}
 }
}
}
Run Code Online (Sandbox Code Playgroud)