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

Commit

Permalink
feat: allow extending webpack.config.js through env
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rosen-vladimirov committed Jan 16, 2020
1 parent e95287d commit 69ace1e
Show file tree
Hide file tree
Showing 17 changed files with 122 additions and 63 deletions.
15 changes: 7 additions & 8 deletions demo/AngularApp/app/App_Resources/Android/app.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
// compile 'com.android.support:recyclerview-v7:+'
//}

android {
defaultConfig {
android {
defaultConfig {
generatedDensities = []
applicationId = "org.nativescript.AngularApp"
}
aaptOptions {
additionalParameters "--no-version-vectors"
}
}
}
aaptOptions {
additionalParameters "--no-version-vectors"
}
}
5 changes: 3 additions & 2 deletions demo/AngularApp/app/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"android": {
"v8Flags": "--expose_gc"
"v8Flags": "--expose_gc",
"markingMode": "none"
},
"main": "main.js",
"name": "tns-template-hello-world-ng",
"version": "3.3.0"
}
}
13 changes: 13 additions & 0 deletions demo/AngularApp/custom-application-activity.webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const webpackConfig = require("./webpack.config");
const path = require("path");

module.exports = env => {
env = env || {};
env.appComponents = env.appComponents || [];
env.appComponents.push(path.resolve(__dirname, "app/activity.android.ts"));

env.entries = env.entries || {};
env.entries.application = "./application.android";
const config = webpackConfig(env);
return config;
};
3 changes: 3 additions & 0 deletions demo/AngularApp/nsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"webpackConfigPath": "custom-application-activity.webpack.config.js"
}
15 changes: 8 additions & 7 deletions demo/AngularApp/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ const hashSalt = Date.now().toString();

module.exports = env => {
// Add your custom Activities, Services and other Android app components here.
const appComponents = [
const appComponents = env.appComponents || [];
appComponents.push(...[
"tns-core-modules/ui/frame",
"tns-core-modules/ui/frame/activity",
resolve(__dirname, "app/activity.android.ts")
];
]);

const platform = env && (env.android && "android" || env.ios && "ios");
if (!platform) {
Expand Down Expand Up @@ -66,9 +66,8 @@ module.exports = env => {
const hasRootLevelScopedModules = nsWebpack.hasRootLevelScopedModules({ projectDir: projectRoot });
const hasRootLevelScopedAngular = nsWebpack.hasRootLevelScopedAngular({ projectDir: projectRoot });
let coreModulesPackageName = "tns-core-modules";
const alias = {
'~': appFullPath
};
const alias = env.alias || {};
alias['~'] = appFullPath;

const compilerOptions = getCompilerOptionsFromTSConfig(tsConfigPath);
if (hasRootLevelScopedModules) {
Expand All @@ -85,7 +84,9 @@ module.exports = env => {
const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
const entryModule = `${nsWebpack.getEntryModule(appFullPath, platform)}.ts`;
const entryPath = `.${sep}${entryModule}`;
const entries = { bundle: entryPath, application: "./application.android" };
const entries = env.entries || {};
entries.bundle = entryPath;

const areCoreModulesExternal = Array.isArray(env.externals) && env.externals.some(e => e.indexOf("tns-core-modules") > -1);
if (platform === "ios" && !areCoreModulesExternal) {
entries["tns_modules/tns-core-modules/inspector_modules"] = "inspector_modules";
Expand Down
5 changes: 3 additions & 2 deletions demo/JavaScriptApp/app/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"android": {
"v8Flags": "--expose_gc"
"v8Flags": "--expose_gc",
"markingMode": "none"
},
"main": "app.js",
"name": "tns-template-hello-world",
"version": "3.3.0"
}
}
13 changes: 13 additions & 0 deletions demo/JavaScriptApp/custom-application-activity.webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const webpackConfig = require("./webpack.config");
const path = require("path");

module.exports = env => {
env = env || {};
env.appComponents = env.appComponents || [];
env.appComponents.push(path.resolve(__dirname, "app/activity.android.js"));

env.entries = env.entries || {};
env.entries.application = "./application.android";
const config = webpackConfig(env);
return config;
};
3 changes: 3 additions & 0 deletions demo/JavaScriptApp/nsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"webpackConfigPath": "./custom-application-activity.webpack.config.js"
}
20 changes: 12 additions & 8 deletions demo/JavaScriptApp/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ const hashSalt = Date.now().toString();

module.exports = env => {
// Add your custom Activities, Services and other android app components here.
const appComponents = [
const appComponents = env.appComponents || [];
appComponents.push(...[
"tns-core-modules/ui/frame",
"tns-core-modules/ui/frame/activity",
resolve(__dirname, "app/activity.android.js")
];
]);

const platform = env && (env.android && "android" || env.ios && "ios");
if (!platform) {
Expand Down Expand Up @@ -56,9 +56,8 @@ module.exports = env => {
const appFullPath = resolve(projectRoot, appPath);
const hasRootLevelScopedModules = nsWebpack.hasRootLevelScopedModules({ projectDir: projectRoot });
let coreModulesPackageName = "tns-core-modules";
const alias = {
'~': appFullPath
};
const alias = env.alias || {};
alias['~'] = appFullPath;

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

const entryModule = nsWebpack.getEntryModule(appFullPath, platform);
const entryPath = `.${sep}${entryModule}.js`;
const entries = { bundle: entryPath, application: "./application.android" };
const entries = env.entries || {};
entries.bundle = entryPath;

const areCoreModulesExternal = Array.isArray(env.externals) && env.externals.some(e => e.indexOf("tns-core-modules") > -1);
if (platform === "ios" && !areCoreModulesExternal) {
entries["tns_modules/tns-core-modules/inspector_modules"] = "inspector_modules";
Expand All @@ -82,6 +83,7 @@ module.exports = env => {
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "build", "configurations", "nativescript-android-snapshot")}`);
}


nsWebpack.processAppComponents(appComponents, platform);
const config = {
mode: production ? "production" : "development",
Expand Down Expand Up @@ -109,6 +111,8 @@ module.exports = env => {
extensions: [".js", ".scss", ".css"],
// Resolve {N} system modules from tns-core-modules
modules: [
resolve(__dirname, `node_modules/${coreModulesPackageName}`),
resolve(__dirname, "node_modules"),
`node_modules/${coreModulesPackageName}`,
"node_modules",
],
Expand Down Expand Up @@ -272,4 +276,4 @@ module.exports = env => {


return config;
};
};
5 changes: 3 additions & 2 deletions demo/TypeScriptApp/app/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"android": {
"v8Flags": "--expose_gc"
"v8Flags": "--expose_gc",
"markingMode": "none"
},
"main": "app.js",
"name": "tns-template-hello-world-ts",
"version": "3.3.0"
}
}
13 changes: 13 additions & 0 deletions demo/TypeScriptApp/custom-application-activity.webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const webpackConfig = require("./webpack.config");
const path = require("path");

module.exports = env => {
env = env || {};
env.appComponents = env.appComponents || [];
env.appComponents.push(path.resolve(__dirname, "app/activity.android.ts"));

env.entries = env.entries || {};
env.entries.application = "./application.android";
const config = webpackConfig(env);
return config;
};
2 changes: 1 addition & 1 deletion demo/TypeScriptApp/nsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"useLegacyWorkflow": false
"webpackConfigPath": "./custom-application-activity.webpack.config.js"
}
14 changes: 7 additions & 7 deletions demo/TypeScriptApp/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ const hashSalt = Date.now().toString();

module.exports = env => {
// Add your custom Activities, Services and other Android app components here.
const appComponents = [
const appComponents = env.appComponents || [];
appComponents.push(...[
"tns-core-modules/ui/frame",
"tns-core-modules/ui/frame/activity",
resolve(__dirname, "app/activity.android.ts")
];
]);

const platform = env && (env.android && "android" || env.ios && "ios");
if (!platform) {
Expand Down Expand Up @@ -59,9 +59,8 @@ module.exports = env => {
const appFullPath = resolve(projectRoot, appPath);
const hasRootLevelScopedModules = nsWebpack.hasRootLevelScopedModules({ projectDir: projectRoot });
let coreModulesPackageName = "tns-core-modules";
const alias = {
'~': appFullPath
};
const alias = env.alias || {};
alias['~'] = appFullPath;

if (hasRootLevelScopedModules) {
coreModulesPackageName = "@nativescript/core";
Expand All @@ -71,7 +70,8 @@ module.exports = env => {

const entryModule = nsWebpack.getEntryModule(appFullPath, platform);
const entryPath = `.${sep}${entryModule}.ts`;
const entries = { bundle: entryPath, application: "./application.android" };
const entries = env.entries || {};
entries.bundle = entryPath;

const tsConfigPath = resolve(projectRoot, "tsconfig.tns.json");

Expand Down
14 changes: 8 additions & 6 deletions templates/webpack.angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ const hashSalt = Date.now().toString();

module.exports = env => {
// Add your custom Activities, Services and other Android app components here.
const appComponents = [
const appComponents = env.appComponents || [];
appComponents.push(...[
"tns-core-modules/ui/frame",
"tns-core-modules/ui/frame/activity",
];
]);

const platform = env && (env.android && "android" || env.ios && "ios");
if (!platform) {
Expand Down Expand Up @@ -65,9 +66,8 @@ module.exports = env => {
const hasRootLevelScopedModules = nsWebpack.hasRootLevelScopedModules({ projectDir: projectRoot });
const hasRootLevelScopedAngular = nsWebpack.hasRootLevelScopedAngular({ projectDir: projectRoot });
let coreModulesPackageName = "tns-core-modules";
const alias = {
'~': appFullPath
};
const alias = env.alias || {};
alias['~'] = appFullPath;

const compilerOptions = getCompilerOptionsFromTSConfig(tsConfigPath);
if (hasRootLevelScopedModules) {
Expand All @@ -84,7 +84,9 @@ module.exports = env => {
const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
const entryModule = `${nsWebpack.getEntryModule(appFullPath, platform)}.ts`;
const entryPath = `.${sep}${entryModule}`;
const entries = { bundle: entryPath };
const entries = env.entries || {};
entries.bundle = entryPath;

const areCoreModulesExternal = Array.isArray(env.externals) && env.externals.some(e => e.indexOf("tns-core-modules") > -1);
if (platform === "ios" && !areCoreModulesExternal) {
entries["tns_modules/tns-core-modules/inspector_modules"] = "inspector_modules";
Expand Down
14 changes: 8 additions & 6 deletions templates/webpack.javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ const hashSalt = Date.now().toString();

module.exports = env => {
// Add your custom Activities, Services and other android app components here.
const appComponents = [
const appComponents = env.appComponents || [];
appComponents.push(...[
"tns-core-modules/ui/frame",
"tns-core-modules/ui/frame/activity",
];
]);

const platform = env && (env.android && "android" || env.ios && "ios");
if (!platform) {
Expand Down Expand Up @@ -55,9 +56,8 @@ module.exports = env => {
const appFullPath = resolve(projectRoot, appPath);
const hasRootLevelScopedModules = nsWebpack.hasRootLevelScopedModules({ projectDir: projectRoot });
let coreModulesPackageName = "tns-core-modules";
const alias = {
'~': appFullPath
};
const alias = env.alias || {};
alias['~'] = appFullPath;

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

const entryModule = nsWebpack.getEntryModule(appFullPath, platform);
const entryPath = `.${sep}${entryModule}.js`;
const entries = { bundle: entryPath };
const entries = env.entries || {};
entries.bundle = entryPath;

const areCoreModulesExternal = Array.isArray(env.externals) && env.externals.some(e => e.indexOf("tns-core-modules") > -1);
if (platform === "ios" && !areCoreModulesExternal) {
entries["tns_modules/tns-core-modules/inspector_modules"] = "inspector_modules";
Expand Down
13 changes: 7 additions & 6 deletions templates/webpack.typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ const hashSalt = Date.now().toString();

module.exports = env => {
// Add your custom Activities, Services and other Android app components here.
const appComponents = [
const appComponents = env.appComponents || [];
appComponents.push(...[
"tns-core-modules/ui/frame",
"tns-core-modules/ui/frame/activity",
];
]);

const platform = env && (env.android && "android" || env.ios && "ios");
if (!platform) {
Expand Down Expand Up @@ -58,9 +59,8 @@ module.exports = env => {
const appFullPath = resolve(projectRoot, appPath);
const hasRootLevelScopedModules = nsWebpack.hasRootLevelScopedModules({ projectDir: projectRoot });
let coreModulesPackageName = "tns-core-modules";
const alias = {
'~': appFullPath
};
const alias = env.alias || {};
alias['~'] = appFullPath;

if (hasRootLevelScopedModules) {
coreModulesPackageName = "@nativescript/core";
Expand All @@ -70,7 +70,8 @@ module.exports = env => {

const entryModule = nsWebpack.getEntryModule(appFullPath, platform);
const entryPath = `.${sep}${entryModule}.ts`;
const entries = { bundle: entryPath };
const entries = env.entries || {};
entries.bundle = entryPath;

const tsConfigPath = resolve(projectRoot, "tsconfig.tns.json");

Expand Down
Loading

0 comments on commit 69ace1e

Please sign in to comment.