Domino的Angular Universal:获得'instanceof'的右边不是对象

时间:2019-07-05 13:16:52

标签: javascript angular webpack angular-universal

我正在使用angular Universal和Domino在Express服务器上“伪造” DOM服务器端。一切正常,直到npm cache clean --forc e然后是npm install

我遇到以下错误:

/path/to/app/src/gui/dist/server/server.js:2378
 */(s),2))[0],l=i[1]),this.engine.listen(this.namespaceId,a,s,l,function(e){var t=e._data||-1;o.factory.scheduleListenerCallback(t,n,e)})}return this.delegate.listen(e,t,n)},t}(h);var g=function(e){function t(t,n,r){return e.call(this,t.body,n,r)||this}return Object(r.c)(t,e),t=Object(r.b)([Object(i.Injectable)(),Object(r.e)(0,Object(i.Inject)(l.DOCUMENT)),Object(r.d)("design:paramtypes",[Object,s.AnimationDriver,s["ɵAnimationStyleNormalizer"]])],t)}(s["ɵAnimationEngine"]);function v(){return Object(s["ɵsupportsWebAnimations"])()?new s["ɵWebAnimationsDriver"]:new s["ɵCssKeyframesDriver"]}function y(){return new s["ɵWebAnimationsStyleNormalizer"]}function b(e,t,n){return new f(e,t,n)}var w=new i.InjectionToken("AnimationModuleType"),_=[{provide:a.AnimationBuilder,useClass:u},{provide:s["ɵAnimationStyleNormalizer"],useFactory:y},{provide:s["ɵAnimationEngine"],useClass:g},{provide:i.RendererFactory2

TypeError: Right-hand side of 'instanceof' is not an object
    at bt (/path/to/app/src/gui/dist/server/server.js:2378:180688)
    at yt (/path/to/app/src/gui/dist/server/server.js:2378:180563)
    at ir (/path/to/app/src/gui/dist/server/server.js:2378:203358)
    at We.insertToken (/path/to/app/src/gui/dist/server/server.js:2378:179949)
    at /path/to/app/src/gui/dist/server/server.js:2378:183251
    at kt (/path/to/app/src/gui/dist/server/server.js:2378:183266)
    at Xe (/path/to/app/src/gui/dist/server/server.js:2378:178211)
    at Object.parse (/path/to/app/src/gui/dist/server/server.js:2378:177177)
    at Object.t.createDocument (/path/to/app/src/gui/dist/server/server.js:2833:9750)
    at Object.t.createWindow (/path/to/app/src/gui/dist/server/server.js:2833:10129)

该错误发生在Domino的HTMLParser.js中,行:2177在以下行:

doc._appendChild(root);
stack.push(root);
if (fragmentContext instanceof impl.HTMLTemplateElement) {
  templateInsertionModes.push(in_template_mode);
}

impl.HTMLTemplateElement实际上是空的。

我使用domino声明的server.ts片段是:

// const domino = require('@angular/platform-server/node_modules/domino');
const domino = require('domino');

const template = fs.readFileSync(path.join(process.cwd(), 'dist', 'browser', 'index.html')).toString();
const win = domino.createWindow(template);
global['window'] = win;
global['document'] = win.document;
global['DOMTokenList'] = win.DOMTokenList;
global['Node'] = win.Node;
global['Text'] = win.Text;
global['jQuery'] = {};
// global['$'] = require('jquery');
// global['window.$'] = require('jquery');
global['HTMLElement'] = win.HTMLElement;
global['Materialize'] = win.Materialize;
global['navigator'] = win.navigator;

对这个问题有什么建议吗?

2 个答案:

答案 0 :(得分:1)

我发现问题的根源是由于将“生产”模式设置为webpack。实际上,出于性能原因,我在webpack.server.config.js

中进行了设置
module.exports = {
    mode: 'production',
    ...
}

此模式导致多米诺骨牌出现意外行为。设置mode: 'none'解决了这个问题。

答案 1 :(得分:0)

正如 Radouane 的回答中所提到的,这个问题似乎与 webpack 对生产模式构建的缩小有关。如果您需要生产模式提供的优化,请使用 a fork of domino with a fix。有关更多讨论,请参阅多米诺骨牌上的 issue 146