我在Angular 9项目中使用了Angular Build Optimization,并使用了下面的代码来生成包,但得到了下面的错误。我在谷歌上搜索了这个错误,但没有找到任何具体的答案。
如错误所示,问题只存在于库本身。我也做了npm安装和npm审计修复,但一次又一次地得到相同的错误。
错误详细信息
An error occurred during the build:
TypeError: Cannot read property 'tapPromise' of undefined
at compiler.hooks.compilation.tap.compilation (E:\Code\blogui\BlogApp\node_modules\compression-webpack-plugin\dist\index.js:269:39)
at SyncHook.eval [as call] (eval at create (E:\Code\blogui\BlogApp\node_modules\tapable\lib\HookCodeFactory.js:19:10), <anonymous>:25:1)
at SyncHook.lazyCompileHook (E:\Code\blogui\BlogApp\node_modules\tapable\lib\Hook.js:154:20)
at Compiler.newCompilation (E:\Code\blogui\BlogApp\node_modules\@angular-builders\custom-webpack\node_modules\webpack\lib\Compiler.js:631:26)
at hooks.beforeCompile.callAsync.err (E:\Code\blogui\BlogApp\node_modules\@angular-builders\custom-webpack\node_modules\webpack\lib\Compiler.js:667:29)
at AsyncSeriesHook.eval [as callAsync] (eval at create (E:\Code\blogui\BlogApp\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:6:1)
at AsyncSeriesHook.lazyCompileHook (E:\Code\blogui\BlogApp\node_modules\tapable\lib\Hook.js:154:20)
at Compiler.compile (E:\Code\blogui\BlogApp\node_modules\@angular-builders\custom-webpack\node_modules\webpack\lib\Compiler.js:662:28)
at readRecords.err (E:\Code\blogui\BlogApp\node_modules\@angular-builders\custom-webpack\node_modules\webpack\lib\Compiler.js:321:11)
angular.json文件
"build": {
"builder": "@angular-builders/custom-webpack:browser",
"options": {
"outputPath": "dist/BlogApp",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.app.json",
"aot": true,
"customWebpackConfig": {
"path": "**src/custom-webpack.config.js**"
},
"assets": [
"src/favicon.ico",
"src/assets",
"src/sitemap.xml",
"src/robots.txt"
],
"styles": [
"src/styles.scss"
],
"scripts": ["./node_modules/jquery/dist/jquery.min.js"]
},
"configurations": {
"production": {
"fileReplacements": [{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "6kb",
"maximumError": "10kb"
}
]
}
}
},
custom-webpack.config.js文件
const CompressionPlugin = require(`compression-webpack-plugin`);
const BrotliPlugin = require(`brotli-webpack-plugin`);
const path = require(`path`);
module.exports = {
plugins: [
new BrotliPlugin({
asset: '[fileWithoutExt].[ext].br',
test: /\.(js|css|html|svg|txt|eot|otf|ttf|gif)$/
}),
new CompressionPlugin({
test: /\.(js|css|html|svg|txt|eot|otf|ttf|gif)$/,
filename(info) {
let opFile = info.path.split('.'),
opFileType = opFile.pop(),
opFileName = opFile.join('.');
return `${opFileName}.${opFileType}.gzip`;
}
})
],
}
package.json文件
{
"name": "blog-app",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular-builders/custom-webpack": "^10.0.1",
"@angular/animations": "~9.1.6",
"@angular/common": "~9.1.6",
"@angular/compiler": "~9.1.6",
"@angular/core": "~9.1.6",
"@angular/forms": "~9.1.6",
"@angular/platform-browser": "~9.1.6",
"@angular/platform-browser-dynamic": "~9.1.6",
"@angular/router": "~9.1.6",
"@syncfusion/ej2-angular-richtexteditor": "^18.2.56",
"aws-sdk": "^2.797.0",
"express-static-gzip": "^2.1.0",
"jquery": "^3.5.1",
"ng-lazyload-image": "^9.1.0",
"ngx-spinner": "^10.0.1",
"rxjs": "~6.5.4",
"schema-utils": "^3.0.0",
"tslib": "^1.10.0",
"web-animations-js": "^2.3.2",
"zone.js": "~0.10.2"
},
"devDependencies": {
"@angular-devkit/build-angular": "^0.1100.3",
"@angular/cli": "~9.1.5",
"@angular/compiler-cli": "~9.1.6",
"@types/jasmine": "~3.5.0",
"@types/jasminewd2": "~2.0.3",
"@types/node": "^12.19.6",
"brotli-webpack-plugin": "^1.0.0",
"codelyzer": "^5.1.2",
"compression-webpack-plugin": "^7.0.0",
"jasmine-core": "~3.5.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~5.0.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage-istanbul-reporter": "~2.1.0",
"karma-jasmine": "~3.0.1",
"karma-jasmine-html-reporter": "^1.4.2",
"protractor": "^7.0.0",
"ts-node": "~8.3.0",
"tslint": "~6.1.0",
"typescript": "~3.8.3"
}
}
tsconfig.app.json
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/app",
"types": ["node"]
},
"files": [
"src/main.ts",
"src/polyfills.ts"
],
"include": [
"src/**/*.d.ts"
],
"angularCompilerOptions": {
"enableIvy": false
}
}
发布于 2020-12-09 09:36:57
我也遇到过这个问题,不过是React。造成这个问题的原因是压缩插件版本和Webpack版本不兼容。
在你的例子中,我认为你通过"@angular-builders/custom-webpack"
“间接”使用了Webpack,因为我在你的package.json
中看不到它
你的Webpack有没有可能是这样的呢?
"@angular-builders/custom-webpack": "^10.0.1"
-> "webpack-merge": "^4.2.2"
-> "webpack": "^5.3.2"
不管怎样,"compression-webpack-plugin": "^7.0.0"
需要"webpack": "^5.9.0"
。所以我建议你降级到依赖于"webpack": "^5.3.2"
的"compression-webpack-plugin": "^6.0.5"
。
尝试这个版本或更低的版本,看看会发生什么。
发布于 2021-08-03 10:31:56
我在使用REACT进行开发时遇到了这个问题。我使用:
npm audit fix --force
它改变了两件事:
压缩-webpack-从^2.0.0到^8.0.1的插件
webpack-开发-服务器从^3.11.0到^0.64.2
该命令中断了我的开发系统,它按以下顺序抛出了错误:
然后,我将webpack-dev-
Error: Cannot find module 'webpack/bin/config-optimist'
恢复到^3.11.0,错误是modified.TypeError: Cannot read property 'tapPromise' of undefined
,然后我将压缩-webpack-插件恢复到^2.0.0它解决了问题。
https://stackoverflow.com/questions/65156116
复制相似问题