Skip to content

Commit

Permalink
feat(stark-build): upgrade to Angular 12
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
Remove useless babel-loader dependency + remove IE 11 support in development mode

BREAKING CHANGE:
  Due to Angular upgrade, "angular.json" file has to be updated as following:

  1. Edit `projects.<project_name>.architect.build.options`:

  Before:

  ```txt
  {
    // ...
    "projects": {
      "<project_name>": {
        // ...
        "architect": {
          "build": {
            "builder": "@angular-builders/custom-webpack:browser",
            "options": {
              "customWebpackConfig": {
                "path": "./node_modules/@nationalbankbelgium/stark-build/config/webpack-partial.dev.js",
                "mergeStrategies": {
                  "modules.rules": "prepend",
                  "plugins": "prepend",
                  "devServer": "prepend",
                  "replaceDuplicatePlugins": false
                }
              },
              // ...
            },
          }
        }
      }
    }
  }
  ```

  After:

  ```txt
  {
    // ...
    "projects": {
      "<project_name>": {
        // ...
        "architect": {
          "build": {
            "builder": "@angular-builders/custom-webpack:browser",
            "options": {
              "customWebpackConfig": {
                "path": "./node_modules/@nationalbankbelgium/stark-build/config/webpack.config.js"
              },
              // /!\ Add following line
              "indexTransform": "./node_modules/@nationalbankbelgium/stark-build/config/index-html.transform.js",
              // ...
            },
          }
        }
      }
    }
  }
  ```

  2. Edit `projects.<project_name>.architect.build.configurations.<environment>`:

  In Stark 12, there is only one "webpack.config.js" file.
  Thanks to this, this is no longer needed to have specific configurations for other environment.

  You need to remove the following lines in
  `projects.<project_name>.architect.build.configurations.<environment>`:

  Before:

  ```txt
  {
    // ...
    "projects": {
      "<project_name>": {
        // ...
        "architect": {
          "build": {
            "configurations": {
              "production": {
                // Remove all the "customWebpackConfig"
                "customWebpackConfig": {
                  "path": "./node_modules/@nationalbankbelgium/stark-build/config/webpack-partial.prod.js",
                  "mergeStrategies": {
                    "modules.rules": "prepend",
                    "plugins": "prepend",
                    "devServer": "prepend",
                    "replaceDuplicatePlugins": false
                  }
                },
                // ...
              },
              // ...
            },
          }
        }
      }
    }
  }
  ```

  After:

  ```txt
  {
    //...
    "projects": {
      "<project_name>": {
        // ...
        "architect": {
          "build": {
            "configurations": {
              "production": {
                // "customWebpackConfig" is entirely removed
                // ...
              },
              // ...
            },
          }
        }
      }
    }
  }
  ```

  3. Edit `projects.<project_name>.architect.serve.builder`:

  Before:

  ```txt
  {
    //...
    "projects": {
      "<project_name>": {
        // ...
        "architect": {
          "serve": {
            "builder": "@angular-builders/dev-server:generic",
            "options": {
              "browserTarget": "<project_name>:build",
              "port": 3000,
              "open": true
            },
            // ...
          }
        }
      }
    }
  }
  ```

  After:

  ```txt
  {
    //...
    "projects": {
      "<project_name>": {
        // ...
        "architect": {
          "serve": {
            // /!\ Edit following line
            "builder": "@angular-builders/custom-webpack:dev-server",
            "options": {
              "browserTarget": "<project_name>:build",
              "port": 3000,
              "open": true
            },
            // ...
          }
        }
      }
    }
  }
  ```

  4. Edit `projects.<project_name>.architect.test.builder`:

  Add support for stark-testing karma config with command `ng test`

  Before:

  ```txt
  {
    //...
    "projects": {
      "<project_name>": {
        // ...
        "architect": {
          "test": {
            "builder": "@angular-devkit/build-angular:karma",
            "options": {
              "main": "base.spec.ts",
              "karmaConfig": "./karma.conf.js",
              "tsConfig": "tsconfig.spec.json"
            }
          }
        }
      }
    }
  }
  ```

  After:

  ```txt
  {
    //...
    "projects": {
      "<project_name>": {
        // ...
        "architect": {
          "test": {
            // /!\ Edit following line
            "builder": "@angular-builders/custom-webpack:karma",
            "options": {
              "main": "base.spec.ts",
              "karmaConfig": "./karma.conf.js",
              "tsConfig": "tsconfig.spec.json"
            }
          }
        }
      }
    }
  }
  ```

  5. Edit `projects.<project_name>.architect.build.configurations.hmr`:

  Add support for CSS Hot Reloading by setting `extractCss` property to `false` in hmr configuration.

  Before:

  ```txt
  {
    // ...
    "projects": {
      "<project_name>": {
        // ...
        "architect": {
          "build": {
            "configurations": {
              "hmr": {
                "fileReplacements": [
                  {
                    "replace": "src/environments/environment.ts",
                    "with": "src/environments/environment.hmr.ts"
                  }
                ]
              },
              // ...
            },
          }
        }
      }
    }
  }
  ```

  After:

  ```txt
  {
    // ...
    "projects": {
      "<project_name>": {
        // ...
        "architect": {
          "build": {
            "configurations": {
              "hmr": {
                "extractCss": false, // <-- Line to add
                "fileReplacements": [
                  {
                    "replace": "src/environments/environment.ts",
                    "with": "src/environments/environment.hmr.ts"
                  }
                ]
              },
              // ...
            },
          }
        }
      }
    }
  }
  ```

BREAKING CHANGE:
  Adapt "src/index.html" file

  1. Adapt stark variables usage
  As `htmlWebpackPlugin` is no longer supported by Angular CLI, the options related to this plugin
  have been changed.
  Instead of using `htmlWebpackPlugin`, you need to use `starkOptions` like this:

  Before:

  ```html
  <%= htmlWebpackPlugin.options.starkAppMetadata.name %>
  <!-- or -->
  <%= htmlWebpackPlugin.options.starkAppConfig.defaultLanguage %>
  <!-- or -->
  <%= htmlWebpackPlugin.options.metadata.TITLE %>
  ```

  After:

  ```html
  <%= starkOptions.starkAppMetadata.name %>
  <!-- or -->
  <%= starkOptions.starkAppConfig.defaultLanguage %>
  <!-- or -->
  <%= starkOptions.metadata.TITLE %>
  ```

  Thanks to the following search & replace:

  - search: `htmlWebpackPlugin.options.`
  - replace: `starkOptions.`

  It should be easy to adapt the index.html file.

  2. Remove obsolete code related to webpack-dev-server

  Remove the following piece of code in "src/index.html"

  ```html
  <!-- move the block of webpack dev server to the <head> section and change the IF conditions -->
  <% if (starkOptions.starkAppMetadata.IS_DEV_SERVER && starkOptions.starkAppMetadata.HMR !== true) { %>
  <!-- Webpack Dev Server reload -->
  <script src="/webpack-dev-server.js"></script>
  <% } %>
  ```

BREAKING CHANGE:
  Adapt "package.json" file. Remove scripts with MONITOR

  Due to Angular upgrade, webpack-monitor stopped working.
  Since the package was no longer maintained (4 years),
  we decided to remove the support from `stark-build`.

  The following scripts should be removed from "package.json" file:

  ```json
  {
    "scripts": {
      "build:dev:monitor": "npx mkdirp reports && cross-env MONITOR=1 npm run build:dev",
      "server:dev:monitor": "npm run clean:dist && cross-env MONITOR=1 npm run ng -- serve",
      "start:monitor": "npx mkdirp reports && cross-env MONITOR=1 npm run server:dev"
    }
  }
  ```
  • Loading branch information
SuperITMan committed Aug 16, 2021
1 parent 4c4adb5 commit 44502de
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 57 deletions.
6 changes: 0 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
"@uirouter/core": "^6.0.8",
"@uirouter/rx": "~0.6.0",
"angular2-text-mask": "^9.0.0",
"babel-loader": "^8.0.5",
"cerialize": "^0.1.18",
"class-validator": "~0.13.1",
"codelyzer": "^5.0.0",
Expand Down
56 changes: 15 additions & 41 deletions packages/stark-build/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const fs = require("fs");
* Webpack Plugins
*/
const DefinePlugin = require("webpack/lib/DefinePlugin");
const MomentLocalesPlugin = require('moment-locales-webpack-plugin');
const MomentLocalesPlugin = require("moment-locales-webpack-plugin");
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
const StylelintPlugin = require("stylelint-webpack-plugin");
const webpackMerge = require("webpack-merge");
const webpackMerge = require("webpack-merge").merge;
const METADATA = require("./webpack-metadata").METADATA;

const fixedTSLintConfig = buildUtils.getFixedTSLintConfig(
Expand Down Expand Up @@ -79,7 +79,6 @@ module.exports = (config, options) => {
chunkModules: true,
chunkOrigins: true,
reasons: true,
maxModules: Infinity, // examine all modules (ModuleConcatenationPlugin debugging)
optimizationBailout: true // display bailout reasons (ModuleConcatenationPlugin debugging)
}
: {}
Expand Down Expand Up @@ -110,35 +109,7 @@ module.exports = (config, options) => {
}
],
exclude: [helpers.root("node_modules")]
},

/**
* Prevent any external library from breaking support for Internet Explorer 11 (see https://github.com/NationalBankBelgium/stark/issues/900)
* Therefore, only certain libraries in 'node_modules' (except the biggest ones and the ones from NBB) are transpiled to ES5 with Babel
* reference: https://github.com/babel/babel-loader
*/
...(METADATA.ENV === "development"
? [
{
test: /node_modules.*\.js$/,
exclude: /node_modules.*(@angular|@mdi|@ng-idle|@nationalbankbelgium|@ngrx|@ngx-translate|@uirouter|cerialize|class-validator|core-js|google-libphonenumber|ibantools|lodash|prettier|rxjs)/,
use: {
loader: "babel-loader",
options: {
presets: [
[
"@babel/preset-env",
{
// Environments you support/target. See https://babeljs.io/docs/en/babel-preset-env#targets
targets: { ie: "11" }
}
]
]
}
}
}
]
: [])
}
]
},

Expand All @@ -164,7 +135,6 @@ module.exports = (config, options) => {
AOT: METADATA.AOT, // TODO: is this needed?
"process.env": {
ENV: JSON.stringify(METADATA.ENV),
NODE_ENV: JSON.stringify(METADATA.ENV),
HMR: METADATA.HMR
}
}),
Expand Down Expand Up @@ -200,13 +170,17 @@ module.exports = (config, options) => {
* Description: Lints the stylesheets loaded in the app (pcss, scss, css, sass)
* See: https://github.com/webpack-contrib/stylelint-webpack-plugin
*/
...(fs.existsSync(helpers.root(".stylelintrc")) ? [new StylelintPlugin({
configFile: ".stylelintrc",
emitErrors: true,
emitWarning: true,
failOnError: false,
files: ["src/**/*.?(pc|sc|c|sa)ss"] // pcss|scss|css|sass
})] : []),
...(fs.existsSync(helpers.root(".stylelintrc"))
? [
new StylelintPlugin({
configFile: ".stylelintrc",
emitErrors: true,
emitWarning: true,
failOnError: false,
files: ["src/**/*.?(pc|sc|c|sa)ss"] // pcss|scss|css|sass
})
]
: [])
],

/**
Expand All @@ -222,7 +196,7 @@ module.exports = (config, options) => {
devServer: {
// See: https://webpack.js.org/configuration/dev-server/#devserverwritetodisk-
writeToDisk: true,

compress: true,

// HTML5 History API support: no need for # in URLs
Expand Down
17 changes: 8 additions & 9 deletions packages/stark-build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
},
"types": "typings/index.d.ts",
"dependencies": {
"@angular-builders/custom-webpack": "^8.3.0",
"@angular-devkit/build-angular": ">= 0.800.0 < 0.900.0",
"@types/webpack": "^4.4.24",
"babel-loader": "^8.0.5",
"@angular-builders/custom-webpack": "^12.1.0",
"@angular-devkit/build-angular": "^12.1.4",
"@types/webpack": "^5.28.0",
"codelyzer": "^5.0.0",
"moment-locales-webpack-plugin": "^1.2.0",
"prettier": "~2.3.2",
Expand All @@ -36,11 +35,11 @@
"webpack-bundle-analyzer": "^4.4.2"
},
"peerDependencies": {
"@angular/cli": "^8.2.0",
"@angular/compiler": "^8.2.0",
"@angular/compiler-cli": "^8.2.0",
"@angular/platform-browser": "^8.2.0",
"@angular/platform-browser-dynamic": "^8.2.0"
"@angular/cli": "^12.1.0",
"@angular/compiler": "^12.1.0",
"@angular/compiler-cli": "^12.1.0",
"@angular/platform-browser": "^12.1.0",
"@angular/platform-browser-dynamic": "^12.1.0"
},
"scripts": {
"clean": "npx rimraf dist",
Expand Down

0 comments on commit 44502de

Please sign in to comment.