问题
目前使用Angular13打出来的包,在index.html中直接对 "type="module"")属性,Chrome从61版本才支持,导致目前打出的包在Chrome60上无法使用,是空白的页面。
解决方案
1、下载webpack自定义配置的插件
npm i @angular-builders/custom-webpack@13.1.0 -D
2、在angular.json中配置插件
下载了@angular-builders/custom-webpack后,需要在angular.json中配置此插件才能使用
"architect": { ... "build": { "builder": "@angular-builders/custom-webpack:browser", // 修改builder,原来为@angular-devkit/build-angular:browser "options": { "customWebpackConfig": { // 新增配置,指向webpack的自定义配置文件webpack-partial.conf.js "path": "webpack-partial.conf.js" }, "indexTransform": "index-html-transform.ts", // 新增配置,用于替换index.html文件内容 ... }, "serve": { "builder": "@angular-builders/custom-webpack:dev-server", // 修改builder,原来为@angular-devkit/build-angular:dev-server ... }, ...}
3、使用webpack自定义配置的插件,覆盖scriptType
angular13的默认配置scriptType='module',需要改为'text/javascript'或者是false。
在angular.json所在目录下,新增文件webpack-partial.conf.js,内容如下:
module.exports = (config) => { config.output.scriptType = 'text/javascript'; return config;};
4、替换index.html内容
在index.html文件中,angular13对
在angular.json所在目录下,新增文件index-html-transform.ts,内容如下:
import { TargetOptions } from '@angular-builders/custom-webpack';module.exports = (targetOptions: TargetOptions, indexHtml: string) => { const index = indexHtml.replace(/type="module"/g, 'defer'); return index;};
参考:
developer.mozilla.org/zh-CN/docs/…