forked from NativeScript/nativescript-dev-webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlazy-ngmodule-hot-loader.js
More file actions
30 lines (25 loc) · 920 Bytes
/
lazy-ngmodule-hot-loader.js
File metadata and controls
30 lines (25 loc) · 920 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const { safeGet } = require("./projectHelpers");
const LAZY_RESOURCE_CONTEXT = "$$_lazy_route_resource";
const HOT_SELF_ACCEPT = "module.hot.accept();";
const HOT_DISPOSE = `
module.hot.dispose(() => {
// Currently the context is needed only for application style modules.
const moduleContext = {};
global.hmrRefresh(moduleContext);
});`;
const HMR_HANDLER = `
if (module.hot) {
${HOT_SELF_ACCEPT}${HOT_DISPOSE}
}
`;
const isLazyLoadedNgModule = resource => {
const issuer = safeGet(resource, "issuer");
const issuerContext = safeGet(issuer, "context");
return issuerContext && issuerContext.endsWith(LAZY_RESOURCE_CONTEXT);
};
module.exports = function (source, map) {
const modifiedSource = isLazyLoadedNgModule(this._module) ?
`${source};${HMR_HANDLER}`:
source;
this.callback(null, modifiedSource, map);
};