UNPKG

920 BJavaScriptView Raw
1const { safeGet } = require("./projectHelpers");
2
3const LAZY_RESOURCE_CONTEXT = "$$_lazy_route_resource";
4const HOT_SELF_ACCEPT = "module.hot.accept();";
5const HOT_DISPOSE = `
6 module.hot.dispose(() => {
7 // Currently the context is needed only for application style modules.
8 const moduleContext = {};
9 global.hmrRefresh(moduleContext);
10 });`;
11const HMR_HANDLER = `
12 if (module.hot) {
13 ${HOT_SELF_ACCEPT}${HOT_DISPOSE}
14 }
15`;
16
17const isLazyLoadedNgModule = resource => {
18 const issuer = safeGet(resource, "issuer");
19 const issuerContext = safeGet(issuer, "context");
20
21 return issuerContext && issuerContext.endsWith(LAZY_RESOURCE_CONTEXT);
22};
23
24module.exports = function (source, map) {
25 const modifiedSource = isLazyLoadedNgModule(this._module) ?
26 `${source};${HMR_HANDLER}`:
27 source;
28
29 this.callback(null, modifiedSource, map);
30};