Migrate to Angular 9 with webpack from Angular 5

Viewed 2655

This is not a question i just want to share my experience!

  1. You need to upgrade your dependencies in package.json

-all angular dependencies to version 9 -add this dependencies:

"@angular-devkit/build-angular": "^0.900.4",
"@angular-builders/custom-webpack": "^9.0.0",
"raw-loader": "^4.0.0",
"sass-loader": "^8.0.2",
"ts-loader": "^6.2.1",
"typescript": "~3.7.5",
"webpack": "^4.41.6",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.10.3",
"angular2-template-loader": "^0.6.2",
"css-loader": "^3.4.2",
"html-loader": "^0.5.5",
"html-webpack-plugin": "3.2.0",
"mini-css-extract-plugin": "^0.9.0",
"to-string-loader": "^1.1.6"

2.Create webpack.config.js (this is mine)

const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const path = require('path');
const  isDevelopment = process.env.WEBPACK_MODE !== 'production'
module.exports = {
  entry: './src/main.ts',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'app.bundle.js'
  },
  resolve:{
    extensions: ['.ts','.js','.scss']
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: ['ts-loader', 'angular2-template-loader']
      },
      {
        test: /\.html$/,
        use: 'html-loader'
      },
      {
        test: /\.scss$/,
        exclude: [/node_modules/, /\.global\.scss$/],
        use: ["to-string-loader", "css-loader", "sass-loader"]
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({template: './src/index.html'}),
    new MiniCssExtractPlugin({
            filename: isDevelopment ? '[name].css' : '[name].[hash].css',
        chunkFilename: isDevelopment ? '[id].css' : '[id].[hash].css'
     })
  ]
}

3.Change build script in package json to used webpack ex:

"start": "webpack-dev-server --mode development --open",
 "build": "webpack --mode development",
 "build_prod": "webpack --mode production",
 "watch": "webpack-dev-server --hot --inline --progress --colors --watch-poll"

4.Change your angular.json (if you have angular-cli you need to migrate first)

"build": {
      "builder": "@angular-builders/custom-webpack:browser",
      "options": {
        "customWebpackConfig": {
          "path": "./webpack.config.js",
          "replaceDuplicatePlugins": true
        }
      },

5.Import polyfills in main.ts

1 Answers

You need to upgrade your dependencies in package.json -all angular dependencies to version 9 -add these dependencies:

"@angular-devkit/build-angular": "^0.900.4",
"@angular-builders/custom-webpack": "^9.0.0",
"raw-loader": "^4.0.0",
"sass-loader": "^8.0.2",
"ts-loader": "^6.2.1",
"typescript": "~3.7.5",
"webpack": "^4.41.6",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.10.3",
"angular2-template-loader": "^0.6.2",
"css-loader": "^3.4.2",
"html-loader": "^0.5.5",
"html-webpack-plugin": "3.2.0",
"mini-css-extract-plugin": "^0.9.0",
"to-string-loader": "^1.1.6"

2.Create webpack.config.js (this is mine)

const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const path = require('path');
const  isDevelopment = process.env.WEBPACK_MODE !== 'production'
module.exports = {
  entry: './src/main.ts',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'app.bundle.js'
  },
  resolve:{
    extensions: ['.ts','.js','.scss']
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: ['ts-loader', 'angular2-template-loader']
      },
      {
        test: /\.html$/,
        use: 'html-loader'
      },
      {
        test: /\.scss$/,
        exclude: [/node_modules/, /\.global\.scss$/],
        use: ["to-string-loader", "css-loader", "sass-loader"]
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({template: './src/index.html'}),
    new MiniCssExtractPlugin({
            filename: isDevelopment ? '[name].css' : '[name].[hash].css',
        chunkFilename: isDevelopment ? '[id].css' : '[id].[hash].css'
     })
  ]
}
  1. Change build script in package JSON to used webpack ex:

    "start": "webpack-dev-server --mode development --open", "build": "webpack --mode development", "build_prod": "webpack --mode production", "watch": "webpack-dev-server --hot --inline --progress --colors --watch-poll"

  2. Change your angular.json (if you have angular-cli you need to migrate first)

    "build": { "builder": "@angular-builders/custom-webpack:browser", "options": { "customWebpackConfig": { "path": "./webpack.config.js", "replaceDuplicatePlugins": true } },

  3. Import polyfills in main.ts

Related