angular - How to only load Zone.js when it is needed -


i'm creating angular 2 application can dynamically injected running website. point of app able modify website content visually.

everything works expected when website in question not run using angular 2. in particular, when original website uses angular 2 , zone.js, application tries load zone.js crashes on error: zone loaded. i'm using webpack build system , tried solve problem splitting build 3 parts: manifest, polyfill, , app. manifest contains webpack manifest, polyfill contains core-js , zone.js, , rest in app. there's fourth chunk called index. 1 placed website , first checks if window.zone defined. if is, manifest , app added. otherwise polyfill.

the problem when polyfill chunk not loaded, modules in chunk missing webpack. when code reaches import app chunk requires either core-js or zone.js (that's think happening), error:

typeerror: cannot read property 'call' of undefined     @ __webpack_require__ (manifest.js:55)     @ object.<anonymous> (app.js:15016)     @ __webpack_require__ (manifest.js:55)     @ object.<anonymous> (app.js:65567)     @ __webpack_require__ (manifest.js:55)     @ object.<anonymous> (app.js:65163)     @ __webpack_require__ (manifest.js:55)     @ object.defineproperty.value (app.js:65137)     @ __webpack_require__ (manifest.js:55)     @ webpackjsonpcallback (manifest.js:26) 

somewhere in manifest file:

/******/   // execute module function /******/   modules[moduleid].call(module.exports, module, module.exports, __webpack_require__); 

question

how configure webpack or angular not import zone.js dependency use 1 registered on window? want able conditionally load core-js , zone.js if it's not yet loaded on website.

update

i modified build (webpack) use single bundle. in bundle, tried using webpack's dynamic imports this:

// import reflect metadata shim import 'core-js/es6/reflect'; import 'core-js/es7/reflect';   // angular , application imports import { platformbrowserdynamic } '@angular/platform-browser-dynamic'; import { appmodule } './app.module'; import { viewencapsulation } '@angular/core';   // load editor in next frame requestanimationframe(() => {      if (window.zone) {         bootstrap();     }     else {         import('zone.js/dist/zone') // <-- problem here             .then(bootstrap);     }  });   // bootstraps editor function bootstrap() {      // add root component dom     const root = document.createelement('efe-root');     document.body.appendchild(root);      // bootstrap angular app     platformbrowserdynamic()         .bootstrapmodule(appmodule, {             defaultencapsulation: viewencapsulation.native         })         .then(() => console.debug('exponea free editor has bootstrapped'));  } 

i'm using typescript , requires definition files. problem zone.js ambient dependency error missing definition files when use this. how solve this?

if don't want load zone.js bundle, remove import polyfills.ts:

import 'core-js/es6/reflect'; import 'core-js/es7/reflect'; import 'zone.js/dist/zone';  <---- remove line 

if want check existence of zone.js in runtime , decide based on result use this:

declare var system; if (!window['zone']) {     system.import('zone.js/dist/zone');  // included angular cli. } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -