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

Commit 2ff43bf

Browse files
authored
feat: move all node modules to the common chunk (#507)
1 parent 88de04b commit 2ff43bf

File tree

3 files changed

+55
-43
lines changed

3 files changed

+55
-43
lines changed

Diff for: templates/webpack.angular.js

+19-15
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ const { NativeScriptWorkerPlugin } = require("nativescript-worker-loader/NativeS
1010
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
1111

1212
module.exports = env => {
13+
// Add your custom Activities, Services and other Android app components here.
14+
const appComponents = [
15+
"tns-core-modules/ui/frame",
16+
"tns-core-modules/ui/frame/activity",
17+
];
18+
1319
const platform = env && (env.android && "android" || env.ios && "ios");
1420
if (!platform) {
1521
throw new Error("You need to provide a target platform!");
@@ -30,13 +36,12 @@ module.exports = env => {
3036
appPath = "app",
3137
appResourcesPath = "app/App_Resources",
3238

33-
// Aot, snapshot, uglify and report can be enabled by providing
34-
// the `--env.snapshot`, `--env.uglify` or `--env.report` flags
35-
// when running 'tns run android|ios'
36-
aot,
37-
snapshot,
38-
uglify,
39-
report,
39+
// You can provide the following flags when running 'tns run android|ios'
40+
production, // --env.production
41+
aot, // --env.aot
42+
snapshot, // --env.snapshot
43+
uglify, // --env.uglify
44+
report, // --env.report
4045
} = env;
4146
const ngToolsWebpackOptions = { tsConfigPath: join(__dirname, "tsconfig.json") };
4247

@@ -50,7 +55,7 @@ module.exports = env => {
5055
const vendorPath = `.${sep}vendor.ts`;
5156

5257
const config = {
53-
mode: "development",
58+
mode: production ? "production" : "development",
5459
context: appFullPath,
5560
watchOptions: {
5661
ignored: [
@@ -104,7 +109,12 @@ module.exports = env => {
104109
common: {
105110
name: "common",
106111
chunks: "all",
107-
test: /vendor/,
112+
test: (module, chunks) => {
113+
const moduleName = module.nameForCondition ? module.nameForCondition() : '';
114+
return /[\\/]node_modules[\\/]/.test(moduleName) ||
115+
appComponents.some(comp => comp === moduleName);
116+
117+
},
108118
enforce: true,
109119
},
110120
}
@@ -199,12 +209,6 @@ module.exports = env => {
199209
};
200210

201211
if (platform === "android") {
202-
// Add your custom Activities, Services and other android app components here.
203-
const appComponents = [
204-
"tns-core-modules/ui/frame",
205-
"tns-core-modules/ui/frame/activity",
206-
];
207-
208212
// Require all Android app components
209213
// in the entry module (bundle.ts) and the vendor module (vendor.ts).
210214
config.module.rules.unshift({

Diff for: templates/webpack.javascript.js

+18-14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ const { NativeScriptWorkerPlugin } = require("nativescript-worker-loader/NativeS
1010
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
1111

1212
module.exports = env => {
13+
// Add your custom Activities, Services and other android app components here.
14+
const appComponents = [
15+
"tns-core-modules/ui/frame",
16+
"tns-core-modules/ui/frame/activity",
17+
];
18+
1319
const platform = env && (env.android && "android" || env.ios && "ios");
1420
if (!platform) {
1521
throw new Error("You need to provide a target platform!");
@@ -30,12 +36,11 @@ module.exports = env => {
3036
appPath = "app",
3137
appResourcesPath = "app/App_Resources",
3238

33-
// Snapshot, uglify and report can be enabled by providing
34-
// the `--env.snapshot`, `--env.uglify` or `--env.report` flags
35-
// when running 'tns run android|ios'
36-
snapshot,
37-
uglify,
38-
report,
39+
// You can provide the following flags when running 'tns run android|ios'
40+
production, // --env.production
41+
snapshot, // --env.snapshot
42+
uglify, // --env.uglify
43+
report, // --env.report
3944
} = env;
4045

4146
const appFullPath = resolve(projectRoot, appPath);
@@ -46,7 +51,7 @@ module.exports = env => {
4651
const vendorPath = `.${sep}vendor.js`;
4752

4853
const config = {
49-
mode: "development",
54+
mode: production ? "production" : "development",
5055
context: appFullPath,
5156
watchOptions: {
5257
ignored: [
@@ -100,7 +105,12 @@ module.exports = env => {
100105
common: {
101106
name: "common",
102107
chunks: "all",
103-
test: /vendor/,
108+
test: (module, chunks) => {
109+
const moduleName = module.nameForCondition ? module.nameForCondition() : '';
110+
return /[\\/]node_modules[\\/]/.test(moduleName) ||
111+
appComponents.some(comp => comp === moduleName);
112+
113+
},
104114
enforce: true,
105115
},
106116
}
@@ -173,12 +183,6 @@ module.exports = env => {
173183
};
174184

175185
if (platform === "android") {
176-
// Add your custom Activities, Services and other android app components here.
177-
const appComponents = [
178-
"tns-core-modules/ui/frame",
179-
"tns-core-modules/ui/frame/activity",
180-
];
181-
182186
// Require all Android app components
183187
// in the entry module (bundle.js) and the vendor module (vendor.js).
184188
config.module.rules.unshift({

Diff for: templates/webpack.typescript.js

+18-14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ const { NativeScriptWorkerPlugin } = require("nativescript-worker-loader/NativeS
1010
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
1111

1212
module.exports = env => {
13+
// Add your custom Activities, Services and other Android app components here.
14+
const appComponents = [
15+
"tns-core-modules/ui/frame",
16+
"tns-core-modules/ui/frame/activity",
17+
];
18+
1319
const platform = env && (env.android && "android" || env.ios && "ios");
1420
if (!platform) {
1521
throw new Error("You need to provide a target platform!");
@@ -30,12 +36,11 @@ module.exports = env => {
3036
appPath = "app",
3137
appResourcesPath = "app/App_Resources",
3238

33-
// Snapshot, uglify and report can be enabled by providing
34-
// the `--env.snapshot`, `--env.uglify` or `--env.report` flags
35-
// when running 'tns run android|ios'
36-
snapshot,
37-
uglify,
38-
report,
39+
// You can provide the following flags when running 'tns run android|ios'
40+
production, // --env.production
41+
snapshot, // --env.snapshot
42+
uglify, // --env.uglify
43+
report, // --env.report
3944
} = env;
4045

4146
const appFullPath = resolve(projectRoot, appPath);
@@ -46,7 +51,7 @@ module.exports = env => {
4651
const vendorPath = `.${sep}vendor.ts`;
4752

4853
const config = {
49-
mode: "development",
54+
mode: production ? "production" : "development",
5055
context: appFullPath,
5156
watchOptions: {
5257
ignored: [
@@ -100,7 +105,12 @@ module.exports = env => {
100105
common: {
101106
name: "common",
102107
chunks: "all",
103-
test: /vendor/,
108+
test: (module, chunks) => {
109+
const moduleName = module.nameForCondition ? module.nameForCondition() : '';
110+
return /[\\/]node_modules[\\/]/.test(moduleName) ||
111+
appComponents.some(comp => comp === moduleName);
112+
113+
},
104114
enforce: true,
105115
},
106116
}
@@ -175,12 +185,6 @@ module.exports = env => {
175185
};
176186

177187
if (platform === "android") {
178-
// Add your custom Activities, Services and other android app components here.
179-
const appComponents = [
180-
"tns-core-modules/ui/frame",
181-
"tns-core-modules/ui/frame/activity",
182-
];
183-
184188
// Require all Android app components
185189
// in the entry module (bundle.ts) and the vendor module (vendor.ts).
186190
config.module.rules.unshift({

0 commit comments

Comments
 (0)