Webpack, Typescript and Angular2 with Ahead Of Time (AOT) compilation?

后端 未结 2 2065
太阳男子
太阳男子 2020-12-25 14:53

The latest release of Angular2 allows for Ahead of time (AOT) compilation, using this code in your app.bootstrap.ts file:

// The browser platform without a co         


        
2条回答
  •  無奈伤痛
    2020-12-25 15:58

    tsconfig.json

        {
          "compilerOptions": {
            "target": "es5",
            "module": "es2015",
            "moduleResolution": "node",
            "sourceMap": true,
            "emitDecoratorMetadata": true,
            "experimentalDecorators": true,
            "lib": ["es2015", "dom"],
            "noImplicitAny": true,
            "suppressImplicitAnyIndexErrors": true,
            "typeRoots": [
              "./node_modules/@types/"
            ]
          },
          "angularCompilerOptions": {
            "genDir": "aot",
            "skipMetadataEmit" : true
          }
        }
    

    config/webpack-aot.config.js

    /**
     * webpack2 config file for ng2 AOT compile
     * location : config/webpack.aot.js
     */
    var webpack = require('webpack');
    var HtmlWebpackPlugin = require('html-webpack-plugin');
    var ExtractTextPlugin = require('extract-text-webpack-plugin');
    var helpers = require('./helpers');
    
    const ngToolsWebpack = require('@ngtools/webpack');
    const ENV = process.env.NODE_ENV = process.env.ENV = 'production';
    console.log(helpers.root('src', 'app', 'app.module#AppModule'));
    module.exports = {
      devtool: 'source-map',
      entry: {
        'polyfills': './src/polyfills.ts',
        'app': './src/main.ts'
      },
      resolve: {
        extensions: ['*', '.ts', '.js']
      },
      output: {
        path: helpers.root('dist'),
        publicPath: '/',
        filename: '[name].[hash].js',
        chunkFilename: '[id].[hash].chunk.js'
      },
    
      module: {
        rules: [
          {
            test: /\.ts$/,
            loader: '@ngtools/webpack'
          },
          {
            test: /\.html$/,
            loader: 'html-loader'
          },
          {
            test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
            include: helpers.root('public', 'images'),
            loader: 'file-loader',
            options: {
              //name: '/assets/[name].[hash].[ext]'  <-file-loaderError
              name: 'assets/[name].[ext]'
            }
          },
          {
            test: /\.css$/,
            exclude: helpers.root('src', 'app'),
            loader: ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader' })
          },
          {
            test: /\.css$/,
            include: helpers.root('src', 'app'),
            loader: 'raw-loader'
          },
          {
            test: /\.scss$/,
            exclude: /node_modules/,
            loaders: ['raw-loader', 'sass-loader']
          },
          {
            test: /\.sass$/,
            exclude: /node_modules/,
            loaders: ['raw-loader', 'sass-loader']
          }
        ]
      },
    
      plugins: [
        // If you want to use jquery in ng2 uncomment this
        /*
        new webpack.ProvidePlugin({
          $: "jquery",
          jQuery: "jquery"
        }),*/
        new ngToolsWebpack.AotPlugin({
          tsConfigPath: helpers.root('tsconfig-aot.json'),
          basePath: helpers.root(''),
          entryModule: helpers.root('src', 'app', 'app.module#AppModule'),
          mainPath: helpers.root('src', 'main.ts')
        }),
        new webpack.optimize.CommonsChunkPlugin({
          name: ['app', 'polyfills']
        }),
        new webpack.LoaderOptionsPlugin({
          minimize: true,
          options: {
            htmlLoader: {
              minimize: false
            }
          }
        }),
        new HtmlWebpackPlugin({
          template: 'public/index.html'
        }),
        new webpack.optimize.UglifyJsPlugin({
          compress: {
              warnings: false,
              drop_console: true
          },
          output: {
              comments: false
          }
        }),
        new ExtractTextPlugin('[name].[hash].css'),
        new webpack.DefinePlugin({
          'process.env': {
            'ENV': JSON.stringify(ENV)
          }
        })
      ]
    };
    

    I used this webpack config and I did AOT compile with angular2 lazy loading. You can see example app for AOT / JIT compile with angular2 lazy load for production mode and dev mode in this git.

    angular2-webpack2-aot

提交回复
热议问题