在构建生产环境时如何禁用Angular 7代码处理?

时间:2019-03-22 13:17:01

标签: angular webpack build uglifyjs

当我制作Angular 7应用程序的生产版本时,我想禁用代码处理。

现在的问题是,当我尝试在构建后启动应用程序时出现错误:

Error: [$injector:unpr] Unknown provider: e

我认为这是因为注入器在缩小后无法识别提供者名称。我想关掉代码处理可以解决这个问题。但是,在Angular 7中,我无法使用webpack eject来更改默认的webpack配置。我使用@angular-builders/custom-webpack:browser添加自己的webpack配置,以将其合并为默认配置。然后,我尝试设置UglifyJsPlugin的电源关闭设置:

const path = require('path');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const nibStylusPlugin = require('nib');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const CopyWebpackPlugin  = require('copy-webpack-plugin');
const webpack = require('webpack');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

const nodeEnvironment = process.env.NODE_ENV || 'development';
const appConfig = process.env.APP_CONFIG || 'testing';

console.info("[INFO] NodeJS environment in webpack: " + nodeEnvironment);
console.info("[INFO] Used configuration in webpack: " + appConfig);


module.exports = {
  target: 'web',
  context: path.resolve(__dirname, 'ng1App'),
  resolve: {
    extensions: ['.ts', '.js', '.tsx', '.json']
  },
  module: {
    rules: [
      {
        test: /\.pug$/,
        use: [{
          loader: 'ng-cache-loader?module=views&prefix=app:**', // ng-cache-loader doesn't understand new syntax
        }, {
          loader: 'pug-html-loader',
          options: {
            pretty: false,
            exports: false,
            doctype: 'html'
          }
        }]
      }, // end pug
      {
        test: /\.ts$/,
        exclude: [/node_modules/],
        use: [
          {
            loader: 'ng-annotate-loader',
            options: {
              es6: true
            }
          },
          {
            loader: 'ts-loader',
            options: {
              transpileOnly: true
            }
          },
          {
            loader: 'tslint-loader',
            options: {
              emitErrors: false,
              failOnHint: false
            }
          }]
      }, // end typescript
      {
        test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?(\?[0-9]+)?$/,
        use: ['file-loader']
      },
      {
        test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?(\?[0-9]+)?$/,
        use: ['file-loader']
      },
      {
        test: /\.jpe?g$|\.ico$|\.gif$|\.png$|\.wav$|\.mp3$/,
        loader: 'file-loader?name=[name].[ext]'  // <-- retain original file name
      },
    ]
  },
  plugins: [
  new ForkTsCheckerWebpackPlugin({tsconfig: '../tsconfig.json', workers: ForkTsCheckerWebpackPlugin.TWO_CPUS_FREE }),
  new CopyWebpackPlugin([
    { from: `../dist_config/${appConfig}.config.json`, to: 'config.json' },
    { from: 'assets', to: 'assets' },
    { from: 'favicon.ico', to: 'favicon.ico' },
  ]),
  new MiniCssExtractPlugin({
    filename: "[name].css"
  }),

  // provide global variables in each file - this allows to use them directly without importing. Below variables are exported by vendors
  // below code is an example on how to tell webpack to treat some variable as a global, and provide it through code
  new webpack.ProvidePlugin({
    "window.jQuery": "jquery",
    tv4: "tv4"
  }),
  new webpack.LoaderOptionsPlugin({
    options: {
      stylus: {
        use: [nibStylusPlugin()],
        import: ['~nib/lib/nib/index.styl'],
      }
    }
  }),
  // new BundleAnalyzerPlugin({generateStatsFile: true}) // comment out to launch bundle analyzer after the build is finished
],
  optimization: {
    minimizer: [new UglifyJsPlugin({
      uglifyOptions: {
        warnings: false,
        parse: {},
        compress: {},
        mangle: false, // Note `mangle.properties` is `false` by default.
        output: null,
        toplevel: false,
        nameCache: null,
        ie8: false,
        keep_fnames: true,
      },
    })],
    splitChunks: {
      cacheGroups: {
        vendor: {
          chunks: 'all',
          name: 'vendor',
          reuseExistingChunk: true,
          test: /[\\/]node_modules[\\/]/,
          priority: -10
        }
      }
    }
  },
  devServer: {
    contentBase: path.join(__dirname, 'dist_config'),
    proxy: {
      '/config.json': {
        target: 'http://localhost:4200/' + appConfig + '.config.json',
        pathRewrite: {'^/config.json' : ''}
      }
    },
    open: true,
    port: 4200,
    historyApiFallback: true,
    compress: true,
    watchOptions: {
      aggregateTimeout: 200,
      ignored: /node_modules/
    }
  },
  node: {
    global: true,
    process: true,
    crypto: 'empty',
    module: false,
    clearImmediate: false,
    setImmediate: false
  }
};

不幸的是,它没有改变任何东西。有人知道我该如何关闭转换功能?

0 个答案:

没有答案