javascript - 如何从 Angular 构建中排除文件类型?

标签 javascript angular typescript webpack mocking

在 Angular 9 项目中,我正在使用两种环境:生产环境和模拟环境。

我想要那个:

  • 当我运行 ng build --prod

  • 那么模拟文件不应包含在 js 包中。

我使用扩展名 .mock.ts 来存储模拟文件,并创建 mocks 文件夹,以便更容易过滤它们。

我认为这可以用 webpack 来完成,但我做不到:(

项目结构:

|- app
  |- integration
    |- country
      |- mocks
        get-countries.mock.ts
      country.service.ts
    |- device
      |- mocks
        get-devices.mock.ts
      device.service.ts

这是我在 angular.json 文件中的构建配置:

"architect": {
    "build": {
      "builder": "@angular-devkit/build-angular:browser",
      "options": {
        ...
      },
      "configurations": {
        "production": {
          "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.prod.ts"
            }
          ],
          "optimization": true,
          "outputHashing": "all",
          "sourceMap": false,
          "extractCss": true,
          "namedChunks": false,
          "aot": true,
          "extractLicenses": true,
          "vendorChunk": false,
          "buildOptimizer": true,
        },
        "mock": {
          "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.mock.ts"
            }
          ]
        }
      }
    },
}

这是我的webpack.config.js:

const path = require('path');

module.exports = {
  mode: 'development',
  // make sure the source maps work
  devtool: 'eval-source-map',
  // webpack will transpile TS and JS files
  resolve: {
    extensions: ['.ts', '.js'],
  },
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        exclude: ['/node_modules/', '/src/app/integration'],
        use: [
          {
            loader: 'ts-loader',
            options: {
              transpileOnly: true,
            },
          },
        ],
      },
    ],
  },
};

最佳答案

src 文件夹中的 tsconfig.app.json 文件为此有一个特定的排除部分。如果您的模拟目录位于 src 之外,您必须尝试项目根目录中的主 tsconfig.json。

关于javascript - 如何从 Angular 构建中排除文件类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60565155/

相关文章:

javascript - brfore浏览器关闭时如何通过ajax发布数据

javascript - 如何限制应用于 <body> 的单击事件在正文中的 jquery 数据表上执行

javascript - JS 去除前导零

typescript - 如何在缺少映射键时出现 TypeScript 错误?

javascript - Angular2 - 通过超时函数将变量与 EventEmitter 传递给父组件

javascript - 使用 $_POST 从 HTML 表单向 js 函数传递 2 个值

javascript - Angular 2 : Why do I have to import a Service in a child component if it is already included in the app component?

angular - WebStorm (v2.3.1) : Live reload after file change (html) does not work. SyntaxError: Unexpected token < in JSON at position 0

Angular 9 Material 对话框在关闭时返回未定义

typescript - 带有 Angular2 的 Pouchdb,找不到名称发出