类型错误:无法读取未定义的属性“tapPromise”
我在 Angular 9 项目中使用 Angular Build Optimization 并使用以下代码生成包,但出现错误。我用错误谷歌搜索,但没有找到任何具体的答案。
由于错误表明问题仅在库本身中。我也做了 npm install 和 npm audit fix ,但一次又一次地得到同样的错误。
错误详情
An error occurred during the build:
TypeError: Cannot read property 'tapPromise' of undefined
at compiler.hooks.compilation.tap.compilation (E:CodebloguiBlogAppnode_modulescompression-webpack-plugindistindex.js:269:39)
at SyncHook.eval [as call] (eval at create (E:CodebloguiBlogAppnode_modulestapablelibHookCodeFactory.js:19:10), <anonymous>:25:1)
at SyncHook.lazyCompileHook (E:CodebloguiBlogAppnode_modulestapablelibHook.js:154:20)
at Compiler.newCompilation (E:CodebloguiBlogAppnode_modules@angular-builderscustom-webpacknode_moduleswebpacklibCompiler.js:631:26)
at hooks.beforeCompile.callAsync.err (E:CodebloguiBlogAppnode_modules@angular-builderscustom-webpacknode_moduleswebpacklibCompiler.js:667:29)
at AsyncSeriesHook.eval [as callAsync] (eval at create (E:CodebloguiBlogAppnode_modulestapablelibHookCodeFactory.js:33:10), <anonymous>:6:1)
at AsyncSeriesHook.lazyCompileHook (E:CodebloguiBlogAppnode_modulestapablelibHook.js:154:20)
at Compiler.compile (E:CodebloguiBlogAppnode_modules@angular-builderscustom-webpacknode_moduleswebpacklibCompiler.js:662:28)
at readRecords.err (E:CodebloguiBlogAppnode_modules@angular-builderscustom-webpacknode_moduleswebpacklibCompiler.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
}
}
回答
我有这个问题,但是使用 React。原因是压缩插件版本和Webpack版本不兼容。
在你的情况下,我认为你通过“间接”使用Webpack "@angular-builders/custom-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"。所以我建议你降级到"compression-webpack-plugin": "^6.0.5"哪个取决于"webpack": "^5.3.2".
试试那个版本或更低的版本,看看会发生什么。
学分