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

Commit 69ace1e

Browse files
feat: allow extending webpack.config.js through env
In case you need to extend the webpack.config.js, currently you cannot extend everything and you need to write a lot of custom logic. This PR adds the possibility to extend the appComponents, entries and alias through `env`. Also update demo applications to use latest CLI feature for extending webpack.config.js
1 parent e95287d commit 69ace1e

17 files changed

Lines changed: 122 additions & 63 deletions

‎demo/AngularApp/app/App_Resources/Android/app.gradle‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
// compile 'com.android.support:recyclerview-v7:+'
66
//}
77

8-
android {
9-
defaultConfig {
8+
android {
9+
defaultConfig {
1010
generatedDensities = []
11-
applicationId ="org.nativescript.AngularApp"
12-
}
13-
aaptOptions {
14-
additionalParameters "--no-version-vectors"
15-
}
16-
}
11+
}
12+
aaptOptions {
13+
additionalParameters "--no-version-vectors"
14+
}
15+
}

‎demo/AngularApp/app/package.json‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"android": {
3-
"v8Flags": "--expose_gc"
3+
"v8Flags": "--expose_gc",
4+
"markingMode": "none"
45
},
56
"main": "main.js",
67
"name": "tns-template-hello-world-ng",
78
"version": "3.3.0"
8-
}
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
constwebpackConfig=require("./webpack.config");
2+
constpath=require("path");
3+
4+
module.exports=env=>{
5+
env=env||{};
6+
env.appComponents=env.appComponents||[];
7+
env.appComponents.push(path.resolve(__dirname,"app/activity.android.ts"));
8+
9+
env.entries=env.entries||{};
10+
env.entries.application="./application.android";
11+
constconfig=webpackConfig(env);
12+
returnconfig;
13+
};

‎demo/AngularApp/nsconfig.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"webpackConfigPath": "custom-application-activity.webpack.config.js"
3+
}

‎demo/AngularApp/webpack.config.js‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ const hashSalt = Date.now().toString();
1818

1919
module.exports=env=>{
2020
// Add your custom Activities, Services and other Android app components here.
21-
constappComponents=[
21+
constappComponents=env.appComponents||[];
22+
appComponents.push(...[
2223
"tns-core-modules/ui/frame",
2324
"tns-core-modules/ui/frame/activity",
24-
resolve(__dirname,"app/activity.android.ts")
25-
];
25+
]);
2626

2727
constplatform=env&&(env.android&&"android"||env.ios&&"ios");
2828
if(!platform){
@@ -66,9 +66,8 @@ module.exports = env => {
6666
consthasRootLevelScopedModules=nsWebpack.hasRootLevelScopedModules({projectDir: projectRoot});
6767
consthasRootLevelScopedAngular=nsWebpack.hasRootLevelScopedAngular({projectDir: projectRoot});
6868
letcoreModulesPackageName="tns-core-modules";
69-
constalias={
70-
'~': appFullPath
71-
};
69+
constalias=env.alias||{};
70+
alias['~']=appFullPath;
7271

7372
constcompilerOptions=getCompilerOptionsFromTSConfig(tsConfigPath);
7473
if(hasRootLevelScopedModules){
@@ -85,7 +84,9 @@ module.exports = env => {
8584
constappResourcesFullPath=resolve(projectRoot,appResourcesPath);
8685
constentryModule=`${nsWebpack.getEntryModule(appFullPath,platform)}.ts`;
8786
constentryPath=`.${sep}${entryModule}`;
88-
constentries={bundle: entryPath,application: "./application.android"};
87+
constentries=env.entries||{};
88+
entries.bundle=entryPath;
89+
8990
constareCoreModulesExternal=Array.isArray(env.externals)&&env.externals.some(e=>e.indexOf("tns-core-modules")>-1);
9091
if(platform==="ios"&&!areCoreModulesExternal){
9192
entries["tns_modules/tns-core-modules/inspector_modules"]="inspector_modules";
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"android": {
3-
"v8Flags": "--expose_gc"
3+
"v8Flags": "--expose_gc",
4+
"markingMode": "none"
45
},
56
"main": "app.js",
67
"name": "tns-template-hello-world",
78
"version": "3.3.0"
8-
}
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
constwebpackConfig=require("./webpack.config");
2+
constpath=require("path");
3+
4+
module.exports=env=>{
5+
env=env||{};
6+
env.appComponents=env.appComponents||[];
7+
env.appComponents.push(path.resolve(__dirname,"app/activity.android.js"));
8+
9+
env.entries=env.entries||{};
10+
env.entries.application="./application.android";
11+
constconfig=webpackConfig(env);
12+
returnconfig;
13+
};

‎demo/JavaScriptApp/nsconfig.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"webpackConfigPath": "./custom-application-activity.webpack.config.js"
3+
}

‎demo/JavaScriptApp/webpack.config.js‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ const hashSalt = Date.now().toString();
1212

1313
module.exports=env=>{
1414
// Add your custom Activities, Services and other android app components here.
15-
constappComponents=[
15+
constappComponents=env.appComponents||[];
16+
appComponents.push(...[
1617
"tns-core-modules/ui/frame",
1718
"tns-core-modules/ui/frame/activity",
18-
resolve(__dirname,"app/activity.android.js")
19-
];
19+
]);
2020

2121
constplatform=env&&(env.android&&"android"||env.ios&&"ios");
2222
if(!platform){
@@ -56,9 +56,8 @@ module.exports = env => {
5656
constappFullPath=resolve(projectRoot,appPath);
5757
consthasRootLevelScopedModules=nsWebpack.hasRootLevelScopedModules({projectDir: projectRoot});
5858
letcoreModulesPackageName="tns-core-modules";
59-
constalias={
60-
'~': appFullPath
61-
};
59+
constalias=env.alias||{};
60+
alias['~']=appFullPath;
6261

6362
if(hasRootLevelScopedModules){
6463
coreModulesPackageName="@nativescript/core";
@@ -68,7 +67,9 @@ module.exports = env => {
6867

6968
constentryModule=nsWebpack.getEntryModule(appFullPath,platform);
7069
constentryPath=`.${sep}${entryModule}.js`;
71-
constentries={bundle: entryPath,application: "./application.android"};
70+
constentries=env.entries||{};
71+
entries.bundle=entryPath;
72+
7273
constareCoreModulesExternal=Array.isArray(env.externals)&&env.externals.some(e=>e.indexOf("tns-core-modules")>-1);
7374
if(platform==="ios"&&!areCoreModulesExternal){
7475
entries["tns_modules/tns-core-modules/inspector_modules"]="inspector_modules";
@@ -82,6 +83,7 @@ module.exports = env => {
8283
itemsToClean.push(`${join(projectRoot,"platforms","android","app","build","configurations","nativescript-android-snapshot")}`);
8384
}
8485

86+
8587
nsWebpack.processAppComponents(appComponents,platform);
8688
constconfig={
8789
mode: production ? "production" : "development",
@@ -109,6 +111,8 @@ module.exports = env => {
109111
extensions: [".js",".scss",".css"],
110112
// Resolve {N} system modules from tns-core-modules
111113
modules: [
114+
resolve(__dirname,`node_modules/${coreModulesPackageName}`),
115+
resolve(__dirname,"node_modules"),
112116
`node_modules/${coreModulesPackageName}`,
113117
"node_modules",
114118
],
@@ -272,4 +276,4 @@ module.exports = env => {
272276

273277

274278
returnconfig;
275-
};
279+
};
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"android": {
3-
"v8Flags": "--expose_gc"
3+
"v8Flags": "--expose_gc",
4+
"markingMode": "none"
45
},
56
"main": "app.js",
67
"name": "tns-template-hello-world-ts",
78
"version": "3.3.0"
8-
}
9+
}

0 commit comments

Comments
 (0)