无法构建应用程序的Angularprod
版本。
IDE 仅在控制台中提供以下消息:
无法分配给引用或变量中的错误!
我只能通过将这些选项添加到构建命令来解决这个问题:
--aot=false --buildOptimizer=false
但是,即使使用这些选项,应用程序在部署后已经在浏览器中崩溃,并出现以下错误:
错误类型错误:无法读取未定义的属性“init”
这可以通过添加一个选项来解决:
--optimization=false
结果,我只能部署应用程序的开发版本,没有优化和其他事情。
当我尝试使用此命令构建应用程序时:
npm run ng build -- --prod
我得到这个日志:
0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Backend\\nodejs\\node.exe',
1 verbose cli 'C:\\Users\\USER_NAME\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli 'run',
1 verbose cli 'ng',
1 verbose cli 'build',
1 verbose cli '--',
1 verbose cli '--prod' ]
2 info using npm@6.10.2
3 info using node@v10.15.3
4 verbose run-script [ 'preng', 'ng', 'postng' ]
5 info lifecycle dont-play-with-gp-web@0.0.2~preng: dont-play-with-gp-web@0.0.2
6 info lifecycle dont-play-with-gp-web@0.0.2~ng: dont-play-with-gp-web@0.0.2
7 verbose lifecycle dont-play-with-gp-web@0.0.2~ng: unsafe-perm in lifecycle true
8 verbose lifecycle dont-play-with-gp-web@0.0.2~ng: PATH: C:\Users\USER_NAME\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;C:\DEV\WebStormProjects\dont-play-with-gp-web\node_modules\.bin;C:\DEV\WebStormProjects\dont-play-with-gp-web\node_modules\.bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Git\cmd;C:\Backend\IntelliJ IDEA 2018.3.5\jre64;C:\Backend\PostgreSQL\10\bin;C:\Backend\nodejs;C:\Backend\WinSCP\;C:\Android\sdk\platform-tools;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Users\USER_NAME\AppData\Local\Microsoft\WindowsApps;
9 verbose lifecycle dont-play-with-gp-web@0.0.2~ng: CWD: C:\DEV\WebStormProjects\dont-play-with-gp-web
10 silly lifecycle dont-play-with-gp-web@0.0.2~ng: Args: [ '/d /s /c', 'ng "build" "--prod"' ]
11 silly lifecycle dont-play-with-gp-web@0.0.2~ng: Returned: code: 1 signal: null
12 info lifecycle dont-play-with-gp-web@0.0.2~ng: Failed to exec ng script
13 verbose stack Error: dont-play-with-gp-web@0.0.2 ng: `ng "build" "--prod"`
13 verbose stack Exit status 1
13 verbose stack at EventEmitter.<anonymous> (C:\Users\USER_NAME\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\index.js:326:16)
13 verbose stack at EventEmitter.emit (events.js:189:13)
13 verbose stack at ChildProcess.<anonymous> (C:\Users\USER_NAME\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack at ChildProcess.emit (events.js:189:13)
13 verbose stack at maybeClose (internal/child_process.js:970:16)
13 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
14 verbose pkgid dont-play-with-gp-web@0.0.2
15 verbose cwd C:\DEV\WebStormProjects\dont-play-with-gp-web
16 verbose Windows_NT 10.0.17134
17 verbose argv "C:\\Backend\\nodejs\\node.exe" "C:\\Users\\USER_NAME\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "run" "ng" "build" "--" "--prod"
18 verbose node v10.15.3
19 verbose npm v6.10.2
20 error code ELIFECYCLE
21 error errno 1
22 error dont-play-with-gp-web@0.0.2 ng: `ng "build" "--prod"`
22 error Exit status 1
23 error Failed at the dont-play-with-gp-web@0.0.2 ng script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]
我尝试更新所有依赖项,但没有帮助。
通话结果ng version
:
Angular CLI: 8.3.4
Node: 10.15.3
OS: win32 x64
Angular: 8.2.6
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.803.4
@angular-devkit/build-angular 0.803.4
@angular-devkit/build-optimizer 0.803.4
@angular-devkit/build-webpack 0.803.4
@angular-devkit/core 8.3.4
@angular-devkit/schematics 8.3.4
@angular/cdk 8.2.0
@angular/cli 8.3.4
@angular/flex-layout 8.0.0-beta.27
@angular/material 8.2.0
@ngtools/webpack 8.3.4
@schematics/angular 8.3.4
@schematics/update 0.803.4
rxjs 6.5.3
typescript 3.5.3
webpack 4.39.2
并且package.json
:
{
"name": "dont-play-with-gp-web",
"version": "0.0.2",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "^8.2.6",
"@angular/cdk": "^8.2.0",
"@angular/common": "~8.2.6",
"@angular/compiler": "~8.2.6",
"@angular/core": "~8.2.6",
"@angular/flex-layout": "8.0.0-beta.27",
"@angular/forms": "~8.2.6",
"@angular/material": "^8.2.0",
"@angular/platform-browser": "~8.2.6",
"@angular/platform-browser-dynamic": "~8.2.6",
"@angular/router": "~8.2.6",
"angular-markdown-editor": "^2.0.2",
"hammerjs": "^2.0.8",
"jquery": "^3.4.1",
"ngx-infinite-scroll": "^8.0.0",
"ngx-markdown": "^8.1.0",
"ngx-material-file-input": "^2.0.0",
"rxjs": "~6.5.3",
"tslib": "^1.9.0",
"zone.js": "^0.9.1"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.803.4",
"@angular/cli": "~8.3.4",
"@angular/compiler-cli": "~8.2.6",
"@angular/language-service": "~8.2.6",
"@types/jasmine": "~3.4.0",
"@types/jasminewd2": "~2.0.3",
"@types/jquery": "^3.3.31",
"@types/node": "~12.7.5",
"codelyzer": "^5.0.0",
"jasmine-core": "~3.4.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~4.3.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage-istanbul-reporter": "~2.1.0",
"karma-jasmine": "~2.0.1",
"karma-jasmine-html-reporter": "^1.4.0",
"protractor": "~5.4.0",
"ts-node": "~8.4.1",
"tslint": "~5.20.0",
"typescript": "^3.5.3"
}
}
在这种情况下该怎么办?如何找到错误的原因以及如何修复它?
如果有帮助,以下是来源:
https://bitbucket.org/mohaxspb/gp-web/commits/tag/ATTEMPT_TO_FIX_PROD_BUILD_WITH_DEPS_UPDATE
UPD: Taki 找到了一种在不破坏 PROD 构建的情况下使用我的指令的方法。但是,这需要在此处编辑编译器源
检查模板中的表达式类型if (localExpr instanceof ReadPropExpr)
,如果再添加一个条件|| localExpr instanceof ReadVarExpr)
,一切正常(PROD 构建、部署和在浏览器中启动)。也可以通过将其添加到消息中来改进显示的错误。JSON.stringify(ast)
一般来说,来自 AST 解析器的异常很令人头疼,因为它们不携带任何有用的东西,除了一条消息:
(这里我们需要一张从电影蜘蛛侠中哭泣的托比马奎尔的照片🙂)
例如,链接器总是在无法组合模板表达式的地方写入,因此使用它要容易得多。
该问题的解决方案之一当然是尝试谷歌,但整个圈子都会关闭每个人都会提供检查指令的事实
ngModel
。第二种选择更硬核。值得进入编译器本身并进行一些调试。在构建过程中使用 Angular
node_modules/@angular/compiler/bundles/compiler.umd.js
,因为该节点只理解 CommonJS(我们不提出 ESM 主题)。接下来,我们进行文本搜索
Cannot assign to a reference or variable!
并找到类_AstToIrVisitor
和方法visitPropertyWrite
。我们看到这条线:我们确信这是一场惨败,在没有任何信息的情况下抛出异常🤨
然后我们让它变得更简单,在我们添加异常之前,函数的参数在
console.log(ast)
哪里,之后解析器会在控制台中给我们一个类的实例:ast
visitPropertyWrite
PropertyWrite
我们明白,我们需要找出属性
translation
和方法调用之间的关系getTranslationForLanguageFromArticle
。在 IDE 中搜索后,我们在 IDE 中找到该行feed.component.html
:这是解析器无法理解的。
最好只在组件中创建一个方法
changeTranslation
并更改其中的属性translation
:好吧,在模板中,进行替换: