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

Commit f9bbea9

Browse files
committed
feat: load bundle config and app css at build time
This will remove the need of a default bundle-config.js file in the app templates.
1 parent d02bd91 commit f9bbea9

File tree

13 files changed

+102
-64
lines changed

13 files changed

+102
-64
lines changed

Diff for: bundle-config-loader.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module.exports = function(source) {
2+
this.cacheable();
3+
const { registerPages, loadCss } = this.query;
4+
5+
if (registerPages) {
6+
source = `
7+
require("nativescript-dev-webpack/register-modules");
8+
${source}
9+
`;
10+
}
11+
12+
if (loadCss) {
13+
source = `
14+
require("nativescript-dev-webpack/load-application-css");
15+
${source}
16+
`;
17+
}
18+
19+
this.callback(null, source);
20+
};
21+

Diff for: demo/JavaScriptApp/app/app.js

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ You can use this file to perform app-level initialization, but the primary
44
purpose of the file is to pass control to the app’s first module.
55
*/
66

7-
require("./bundle-config");
87
var application = require("application");
98

109
application.start({ moduleName: "main-page" });

Diff for: demo/JavaScriptApp/app/bundle-config.js

-9
This file was deleted.

Diff for: demo/TypeScriptApp/app/app.ts

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ You can use this file to perform app-level initialization, but the primary
44
purpose of the file is to pass control to the app’s first module.
55
*/
66

7-
import "./bundle-config";
87
import * as app from 'application';
98

109
app.start({ moduleName: 'main-page' });

Diff for: demo/TypeScriptApp/app/bundle-config.ts

-9
This file was deleted.

Diff for: demo/TypeScriptApp/tsconfig.json

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
"*": [
1616
"./node_modules/tns-core-modules/*",
1717
"./node_modules/*"
18+
],
19+
"~/*": [
20+
"app/*"
1821
]
1922
}
2023
},

Diff for: load-application-css.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const application = require("application");
2+
require("ui/styling/style-scope");
3+
const appCssContext = require.context("~/", false, /^\.\/app\.(css|scss|less|sass)$/);
4+
global.registerWebpackModules(appCssContext);
5+
application.loadAppCss();
6+

Diff for: plugins/NativeScriptSnapshotPlugin/index.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,17 @@ exports.NativeScriptSnapshotPlugin = (function() {
3434
}
3535

3636
NativeScriptSnapshotPlugin.ensureSnapshotModuleEntry = function(options) {
37-
const { webpackConfig, requireModules, chunks, projectRoot } = options;
37+
const { webpackConfig, requireModules, chunks, projectRoot, includeApplicationCss } = options;
3838

3939
const androidProjectPath = getAndroidProjectPath({ projectRoot: projectRoot });
4040
const snapshotEntryPath = join(androidProjectPath, SNAPSHOT_ENTRY_MODULE);
41-
const snapshotEntryContent = requireModules.map(mod => `require('${mod}')`).join(";");
41+
42+
let snapshotEntryContent = "";
43+
if (includeApplicationCss) {
44+
snapshotEntryContent += `require("nativescript-dev-webpack/load-application-css");`;
45+
}
46+
snapshotEntryContent += requireModules.map(mod => `require('${mod}')`).join(";");
47+
4248
writeFileSync(snapshotEntryPath, snapshotEntryContent, { encoding: "utf8" });
4349

4450
// add the module to the entry points to make sure it's content is evaluated

Diff for: plugins/NativeScriptSnapshotPlugin/options.json

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
"requireModules": {
4343
"type": "array",
4444
"default": []
45+
},
46+
"includeApplicationCss": {
47+
"type": "boolean",
48+
"default": true
4549
}
4650
},
4751
"required": [

Diff for: register-modules.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require("tns-core-modules/bundle-entry-points");
2+
const context = require.context("~/", true, /(root|page|fragment)\.(xml|css|js|ts|scss)$/);
3+
global.registerWebpackModules(context);
4+

Diff for: templates/webpack.angular.js

+18-14
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,24 @@ module.exports = env => {
127127
},
128128
module: {
129129
rules: [
130+
{
131+
test: new RegExp(entryPath),
132+
use: [
133+
// Require all Android app components
134+
platform === "android" && {
135+
loader: "nativescript-dev-webpack/android-app-components-loader",
136+
options: { modules: appComponents }
137+
},
138+
139+
{
140+
loader: "nativescript-dev-webpack/bundle-config-loader",
141+
options: {
142+
loadCss: !snapshot, // load the application css if in debug mode
143+
}
144+
},
145+
].filter(loader => !!loader)
146+
},
147+
130148
{ test: /\.html$|\.xml$/, use: "raw-loader" },
131149

132150
// tns-core-modules reads the app.css and its imports using css-loader
@@ -203,18 +221,6 @@ module.exports = env => {
203221
],
204222
};
205223

206-
if (platform === "android") {
207-
// Require all Android app components
208-
// in the entry module
209-
config.module.rules.unshift({
210-
test: new RegExp(entryPath),
211-
use: {
212-
loader: "nativescript-dev-webpack/android-app-components-loader",
213-
options: { modules: appComponents }
214-
}
215-
});
216-
}
217-
218224
if (report) {
219225
// Generate report files for bundles content
220226
config.plugins.push(new BundleAnalyzerPlugin({
@@ -240,8 +246,6 @@ module.exports = env => {
240246
],
241247
projectRoot,
242248
webpackConfig: config,
243-
targetArchs: ["arm", "arm64", "ia32"],
244-
useLibs: false
245249
}));
246250
}
247251

Diff for: templates/webpack.javascript.js

+19-14
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,25 @@ module.exports = env => {
124124
},
125125
module: {
126126
rules: [
127+
{
128+
test: new RegExp(entryPath),
129+
use: [
130+
// Require all Android app components
131+
platform === "android" && {
132+
loader: "nativescript-dev-webpack/android-app-components-loader",
133+
options: { modules: appComponents }
134+
},
135+
136+
{
137+
loader: "nativescript-dev-webpack/bundle-config-loader",
138+
options: {
139+
registerPages: true, // applicable only for non-angular apps
140+
loadCss: !snapshot, // load the application css if in debug mode
141+
}
142+
},
143+
].filter(loader => !!loader)
144+
},
145+
127146
{ test: /\.(html|xml)$/, use: "raw-loader" },
128147

129148
{
@@ -178,18 +197,6 @@ module.exports = env => {
178197
],
179198
};
180199

181-
if (platform === "android") {
182-
// Require all Android app components
183-
// in the entry module
184-
config.module.rules.unshift({
185-
test: new RegExp(entryPath),
186-
use: {
187-
loader: "nativescript-dev-webpack/android-app-components-loader",
188-
options: { modules: appComponents }
189-
}
190-
});
191-
}
192-
193200
if (report) {
194201
// Generate report files for bundles content
195202
config.plugins.push(new BundleAnalyzerPlugin({
@@ -209,8 +216,6 @@ module.exports = env => {
209216
],
210217
projectRoot,
211218
webpackConfig: config,
212-
targetArchs: ["arm", "arm64", "ia32"],
213-
useLibs: false
214219
}));
215220
}
216221

Diff for: templates/webpack.typescript.js

+19-14
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,25 @@ module.exports = env => {
124124
},
125125
module: {
126126
rules: [
127+
{
128+
test: new RegExp(entryPath),
129+
use: [
130+
// Require all Android app components
131+
platform === "android" && {
132+
loader: "nativescript-dev-webpack/android-app-components-loader",
133+
options: { modules: appComponents }
134+
},
135+
136+
{
137+
loader: "nativescript-dev-webpack/bundle-config-loader",
138+
options: {
139+
registerPages: true, // applicable only for non-angular apps
140+
loadCss: !snapshot, // load the application css if in debug mode
141+
}
142+
},
143+
].filter(loader => !!loader)
144+
},
145+
127146
{ test: /\.(html|xml)$/, use: "raw-loader" },
128147

129148
{
@@ -180,18 +199,6 @@ module.exports = env => {
180199
],
181200
};
182201

183-
if (platform === "android") {
184-
// Require all Android app components
185-
// in the entry module
186-
config.module.rules.unshift({
187-
test: new RegExp(entryPath),
188-
use: {
189-
loader: "nativescript-dev-webpack/android-app-components-loader",
190-
options: { modules: appComponents }
191-
}
192-
});
193-
}
194-
195202
if (report) {
196203
// Generate report files for bundles content
197204
config.plugins.push(new BundleAnalyzerPlugin({
@@ -211,8 +218,6 @@ module.exports = env => {
211218
],
212219
projectRoot,
213220
webpackConfig: config,
214-
targetArchs: ["arm", "arm64", "ia32"],
215-
useLibs: false
216221
}));
217222
}
218223

0 commit comments

Comments
 (0)