Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Commit af55ec6

Browse files
committed
feat(Angular): apply changes in application styles at runtime with HMR
1 parent eb48234 commit af55ec6

File tree

4 files changed

+81
-35
lines changed

4 files changed

+81
-35
lines changed

Diff for: bundle-config-loader.js

+49-26
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,61 @@ module.exports = function (source) {
22
this.cacheable();
33
const { angular = false, loadCss = true, registerModules = /(root|page)\.(xml|css|js|ts|scss)$/ } = this.query;
44

5+
const hmr = `
6+
if (module.hot) {
7+
const fileSystemModule = require("tns-core-modules/file-system");
8+
const applicationFiles = fileSystemModule.knownFolders.currentApp();
9+
10+
global.__hmrLivesyncBackup = global.__onLiveSync;
11+
global.__onLiveSync = function () {
12+
console.log("HMR: Sync...");
13+
require("nativescript-dev-webpack/hot")(__webpack_require__.h(), (fileName) => applicationFiles.getFile(fileName));
14+
};
15+
16+
global.__hmrRefresh = function({ type, module }) {
17+
global.__hmrNeedReload = true;
18+
setTimeout(() => {
19+
if(global.__hmrNeedReload) {
20+
global.__hmrNeedReload = false;
21+
global.__hmrLivesyncBackup({ type, module });
22+
}
23+
});
24+
}
25+
26+
global.__hmrInitialSync = true; // needed to determine if we are performing initial sync
27+
global.__onLiveSync();
28+
}
29+
`;
30+
31+
const angularHmr = `
32+
if (module.hot) {
33+
const hmrUpdate = require("nativescript-dev-webpack/hmr").hmrUpdate;
34+
35+
global.__hmrLivesyncBackup = global.__onLiveSync;
36+
global.__onLiveSync = function () {
37+
console.log("HMR: Sync...");
38+
hmrUpdate();
39+
};
40+
41+
global.__hmrRefresh = function({ type, module }) {
42+
setTimeout(() => {
43+
global.__hmrLivesyncBackup({ type, module });
44+
});
45+
};
46+
}
47+
`;
48+
549
source = `
650
require("tns-core-modules/bundle-entry-points");
751
${source}
852
`;
953

10-
if (!angular && registerModules) {
11-
const hmr = `
12-
if (module.hot) {
13-
const fileSystemModule = require("tns-core-modules/file-system");
14-
const applicationFiles = fileSystemModule.knownFolders.currentApp();
15-
16-
global.__hmrLivesyncBackup = global.__onLiveSync;
17-
global.__onLiveSync = function () {
18-
console.log("HMR: Sync...");
19-
require("nativescript-dev-webpack/hot")(__webpack_require__.h(), (fileName) => applicationFiles.getFile(fileName));
20-
};
21-
22-
global.__hmrRefresh = function({ type, module }) {
23-
global.__hmrNeedReload = true;
24-
setTimeout(() => {
25-
if(global.__hmrNeedReload) {
26-
global.__hmrNeedReload = false;
27-
global.__hmrLivesyncBackup({ type, module });
28-
}
29-
});
30-
}
31-
32-
global.__hmrInitialSync = true; // needed to determine if we are performing initial sync
33-
global.__onLiveSync();
34-
}
54+
if (angular) {
55+
source = `
56+
${angularHmr}
57+
${source}
3558
`;
36-
59+
} else if (registerModules) {
3760
source = `
3861
${hmr}
3962
const context = require.context("~/", true, ${registerModules});

Diff for: demo/AngularApp/app/main.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
// this import should be first in order to load some required settings (like globals and reflect-metadata)
22
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
3+
import { AppOptions } from "nativescript-angular/platform-common";
34

45
import { AppModule } from "./app.module";
56

7+
let options: AppOptions = {};
8+
9+
if (module["hot"]) {
10+
const hmrUpdate = require("nativescript-dev-webpack/hmr").hmrUpdate;
11+
12+
options.hmrOptions = {
13+
moduleTypeFactory: () => AppModule,
14+
livesyncCallback: (platformReboot) => {
15+
console.log("HMR livesyncCallback: Sync...")
16+
setTimeout(platformReboot, 0);
17+
},
18+
}
19+
20+
hmrUpdate();
21+
22+
// Path to your app module.
23+
// You might have to change it if your module is in a different place.
24+
module["hot"].accept(["./app.module"], global["__hmrRefresh"]);
25+
}
26+
627
// A traditional NativeScript application starts by initializing global objects, setting up global CSS rules, creating, and navigating to the main page.
728
// Angular applications need to take care of their own initialization: modules, components, directives, routes, DI providers.
829
// A NativeScript Angular app needs to make both paradigms work together, so we provide a wrapper platform object, platformNativeScriptDynamic,
930
// that sets up a NativeScript application and can bootstrap the Angular framework.
10-
platformNativeScriptDynamic().bootstrapModule(AppModule);
31+
platformNativeScriptDynamic(options).bootstrapModule(AppModule);

Diff for: demo/AngularApp/webpack.config.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,15 @@ module.exports = env => {
173173
// tns-core-modules reads the app.css and its imports using css-loader
174174
{
175175
test: /[\/|\\]app\.css$/,
176-
use: {
177-
loader: "css-loader",
178-
options: { minimize: false, url: false },
179-
}
176+
use: [
177+
"nativescript-dev-webpack/style-hot-loader",
178+
{ loader: "css-loader", options: { minimize: false, url: false } }
179+
]
180180
},
181181
{
182182
test: /[\/|\\]app\.scss$/,
183183
use: [
184+
"nativescript-dev-webpack/style-hot-loader",
184185
{ loader: "css-loader", options: { minimize: false, url: false } },
185186
"sass-loader"
186187
]

Diff for: templates/webpack.angular.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,15 @@ module.exports = env => {
171171
// tns-core-modules reads the app.css and its imports using css-loader
172172
{
173173
test: /[\/|\\]app\.css$/,
174-
use: {
175-
loader: "css-loader",
176-
options: { minimize: false, url: false },
177-
}
174+
use: [
175+
"nativescript-dev-webpack/style-hot-loader",
176+
{ loader: "css-loader", options: { minimize: false, url: false } }
177+
]
178178
},
179179
{
180180
test: /[\/|\\]app\.scss$/,
181181
use: [
182+
"nativescript-dev-webpack/style-hot-loader",
182183
{ loader: "css-loader", options: { minimize: false, url: false } },
183184
"sass-loader"
184185
]

0 commit comments

Comments
 (0)