UNPKG

1.99 kBJavaScriptView Raw
1const unitTestingConfigLoader = require("./unit-testing-config-loader");
2
3module.exports = function (source, map) {
4 this.cacheable();
5 const { angular = false, loadCss = true, unitTesting, projectRoot, appFullPath, registerModules = /(root|page)\.(xml|css|js|ts|scss)$/ } = this.query;
6
7 if (unitTesting) {
8 source = unitTestingConfigLoader({ appFullPath, projectRoot, angular, rootPagesRegExp: registerModules });
9 this.callback(null, source);
10 return;
11 }
12
13 const hmr = `
14 if (module.hot) {
15 const hmrUpdate = require("nativescript-dev-webpack/hmr").hmrUpdate;
16 global.__initialHmrUpdate = true;
17 global.__hmrSyncBackup = global.__onLiveSync;
18
19 global.__onLiveSync = function () {
20 hmrUpdate();
21 };
22
23 global.hmrRefresh = function({ type, path } = {}) {
24 if (global.__initialHmrUpdate) {
25 return;
26 }
27
28 setTimeout(() => {
29 global.__hmrSyncBackup({ type, path });
30 });
31 };
32
33 hmrUpdate().then(() => {
34 global.__initialHmrUpdate = false;
35 })
36 }
37 `;
38
39 source = `
40 require("tns-core-modules/bundle-entry-points");
41 ${source}
42 `;
43
44 if (angular) {
45 source = `
46 ${hmr}
47 ${source}
48 `;
49 } else if (registerModules) {
50 source = `
51 ${hmr}
52 const context = require.context("~/", true, ${registerModules});
53 global.registerWebpackModules(context);
54 ${source}
55 `;
56 }
57
58 if (loadCss) {
59 source = `
60 require("${
61 angular ?
62 'nativescript-dev-webpack/load-application-css-angular' :
63 'nativescript-dev-webpack/load-application-css-regular'
64 }")();
65 ${source}
66 `;
67 }
68
69 this.callback(null, source, map);
70};