我試圖使用prerender-node和一個私有預渲染服務器來預渲染一個Angular2/Express應用程序。如果我嘗試目標ES5在我tsconfig.json,預渲染服務器將引發此錯誤:使用Angular 2/Typescript/Webpack預渲染es6錯誤
ReferenceError: Can't find variable: Map
undefined:1521 in KeyRegistry
:1540
:7
如果我嘗試目標ES6(包括files
陣列中node_modules/typescript/lib/lib.es6.d.ts
),預渲染服務器將引發此錯誤:
SyntaxError: Unexpected token 'const'
http://localhost:3000/app.js:50 in eval
http://localhost:3000/app.js:50
http://localhost:3000/app.js:20 in __webpack_require__
http://localhost:3000/app.js:40
我猜我需要包括某種填充工具,以便爲這個工作,但我沒有第一想法是什麼,包括或者包含它。
這裏是萬一我的WebPack的配置,可以幫助:
var webpack = require('webpack');
var path = require('path');
var rootDir = path.resolve();
module.exports =
{
target: 'node',
entry: rootDir + "/dist/client/app/bootstrap.js",
output: {
path: rootDir + "/dist/client", publicPath: '/', filename: "app.js",
pathinfo: true
},
devtool: 'eval',
resolve: {
root: rootDir + "/dist/client/app",
extensions: ['', '.js']
},
module: {
loaders: [
{
test: /\.ts/, loaders: ['ts-loader'], exclude: /node_modules/
}
]
}
};
而如果我的客戶tsconfig幫助:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": false,
"outDir": "../../dist/client",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": true
},
"files": ["app/bootstrap.ts", "app/vendor.ts", "app/Window.ts", "tests/index.ts", "../../node_modules/typescript/lib/lib.es6.d.ts"]
}
更新
如果我改變我的WebPack配置目標web
而不是node
,並在預渲染服務器中註釋掉server.use(prerender.removeScriptTags());
,該請求每次都會觸發預渲染服務器,並且n錯誤被拋出,但沒有任何東西被放棄。但似乎比以前更接近,所以我想我會更新。
更新
Prerender似乎沒有執行任何Angular代碼。如果我在我的index.html頭部的腳本標籤設置window.prerenderReady = false
,然後嘗試將其設置爲true時再我的根組件實例化時,預渲染服務器超時:
import { Component, OnInit } from '@angular/core'
@Component({
selector: 'my-app',
template: `
<div id="main"> all the things </div>
`
})
export class AppComponent implements OnInit
{
constructor(){}
ngOnInit()
{
// Prerender never executes this code before it times out
window.prerenderReady = true;
console.info('Prerender Ready');
}
}
看起來像那些JavaScript錯誤是防止PhantomJS(底層渲染引擎)失敗並停止渲染頁面。你有沒有爲Map添加一個polyfill? 'removeScriptTags'僅在頁面完成渲染後刪除腳本標記,因此不應影響任何與渲染有關的操作。 –
在我的webpack配置中將'web'而不是'node'定向到預渲染es6錯誤。 prerender服務器返回200,但不執行Angular代碼。 – MyCompassSpins
是否有任何其他細節我可以提供,可能有助於得到一個答案呢? – MyCompassSpins