Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2cf708b

Browse files
author
Elena Hristova
committedAug 8, 2019
chore: restore demo app webpack.configs
1 parent bf9372c commit 2cf708b

File tree

6 files changed

+872
-9
lines changed

6 files changed

+872
-9
lines changed
 

‎demo/AngularApp/.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,4 @@ test-results.xml
1010

1111
app/item/items.component.android.css
1212
app/item/items.component.ios.css
13-
mochawesome-report
14-
15-
webpack.config.js
13+
mochawesome-report

‎demo/AngularApp/webpack.config.js

Lines changed: 320 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,320 @@
1+
const { join, relative, resolve, sep, dirname } = require("path");
2+
3+
const webpack = require("webpack");
4+
const nsWebpack = require("nativescript-dev-webpack");
5+
const nativescriptTarget = require("nativescript-dev-webpack/nativescript-target");
6+
const { nsReplaceBootstrap } = require("nativescript-dev-webpack/transformers/ns-replace-bootstrap");
7+
const { nsReplaceLazyLoader } = require("nativescript-dev-webpack/transformers/ns-replace-lazy-loader");
8+
const { nsSupportHmrNg } = require("nativescript-dev-webpack/transformers/ns-support-hmr-ng");
9+
const { getMainModulePath } = require("nativescript-dev-webpack/utils/ast-utils");
10+
const CleanWebpackPlugin = require("clean-webpack-plugin");
11+
const CopyWebpackPlugin = require("copy-webpack-plugin");
12+
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
13+
const { NativeScriptWorkerPlugin } = require("nativescript-worker-loader/NativeScriptWorkerPlugin");
14+
const TerserPlugin = require("terser-webpack-plugin");
15+
const { getAngularCompilerPlugin } = require("nativescript-dev-webpack/plugins/NativeScriptAngularCompilerPlugin");
16+
const hashSalt = Date.now().toString();
17+
18+
module.exports = env => {
19+
// Add your custom Activities, Services and other Android app components here.
20+
const appComponents = [
21+
"tns-core-modules/ui/frame",
22+
"tns-core-modules/ui/frame/activity",
23+
resolve(__dirname, "app/activity.android.ts")
24+
];
25+
26+
const platform = env && (env.android && "android" || env.ios && "ios");
27+
if (!platform) {
28+
throw new Error("You need to provide a target platform!");
29+
}
30+
31+
const AngularCompilerPlugin = getAngularCompilerPlugin(platform);
32+
const projectRoot = __dirname;
33+
34+
// Default destination inside platforms/<platform>/...
35+
const dist = resolve(projectRoot, nsWebpack.getAppPath(platform, projectRoot));
36+
37+
const {
38+
// The 'appPath' and 'appResourcesPath' values are fetched from
39+
// the nsconfig.json configuration file.
40+
appPath = "src",
41+
appResourcesPath = "App_Resources",
42+
43+
// You can provide the following flags when running 'tns run android|ios'
44+
aot, // --env.aot
45+
snapshot, // --env.snapshot,
46+
production, // --env.production
47+
uglify, // --env.uglify
48+
report, // --env.report
49+
sourceMap, // --env.sourceMap
50+
hiddenSourceMap, // --env.hiddenSourceMap
51+
hmr, // --env.hmr,
52+
unitTesting, // --env.unitTesting
53+
verbose, // --env.verbose
54+
} = env;
55+
56+
const isAnySourceMapEnabled = !!sourceMap || !!hiddenSourceMap;
57+
const externals = nsWebpack.getConvertedExternals(env.externals);
58+
const appFullPath = resolve(projectRoot, appPath);
59+
const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
60+
const tsConfigName = "tsconfig.tns.json";
61+
const entryModule = `${nsWebpack.getEntryModule(appFullPath, platform)}.ts`;
62+
const entryPath = `.${sep}${entryModule}`;
63+
const entries = { bundle: entryPath, application: "./application.android" };
64+
const areCoreModulesExternal = Array.isArray(env.externals) && env.externals.some(e => e.indexOf("tns-core-modules") > -1);
65+
if (platform === "ios" && !areCoreModulesExternal) {
66+
entries["tns_modules/tns-core-modules/inspector_modules"] = "inspector_modules";
67+
};
68+
69+
const ngCompilerTransformers = [];
70+
const additionalLazyModuleResources = [];
71+
if (aot) {
72+
ngCompilerTransformers.push(nsReplaceBootstrap);
73+
}
74+
75+
if (hmr) {
76+
ngCompilerTransformers.push(nsSupportHmrNg);
77+
}
78+
79+
// when "@angular/core" is external, it's not included in the bundles. In this way, it will be used
80+
// directly from node_modules and the Angular modules loader won't be able to resolve the lazy routes
81+
// fixes https://github.com/NativeScript/nativescript-cli/issues/4024
82+
if (env.externals && env.externals.indexOf("@angular/core") > -1) {
83+
const appModuleRelativePath = getMainModulePath(resolve(appFullPath, entryModule), tsConfigName);
84+
if (appModuleRelativePath) {
85+
const appModuleFolderPath = dirname(resolve(appFullPath, appModuleRelativePath));
86+
// include the lazy loader inside app module
87+
ngCompilerTransformers.push(nsReplaceLazyLoader);
88+
// include the new lazy loader path in the allowed ones
89+
additionalLazyModuleResources.push(appModuleFolderPath);
90+
}
91+
}
92+
93+
const ngCompilerPlugin = new AngularCompilerPlugin({
94+
hostReplacementPaths: nsWebpack.getResolver([platform, "tns"]),
95+
platformTransformers: ngCompilerTransformers.map(t => t(() => ngCompilerPlugin, resolve(appFullPath, entryModule), projectRoot)),
96+
mainPath: join(appFullPath, entryModule),
97+
tsConfigPath: join(__dirname, tsConfigName),
98+
skipCodeGeneration: !aot,
99+
sourceMap: !!isAnySourceMapEnabled,
100+
additionalLazyModuleResources: additionalLazyModuleResources
101+
});
102+
103+
let sourceMapFilename = nsWebpack.getSourceMapFilename(hiddenSourceMap, __dirname, dist);
104+
105+
const itemsToClean = [`${dist}/**/*`];
106+
if (platform === "android") {
107+
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "src", "main", "assets", "snapshots")}`);
108+
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "build", "configurations", "nativescript-android-snapshot")}`);
109+
}
110+
111+
nsWebpack.processAppComponents(appComponents, platform);
112+
const config = {
113+
mode: production ? "production" : "development",
114+
context: appFullPath,
115+
externals,
116+
watchOptions: {
117+
ignored: [
118+
appResourcesFullPath,
119+
// Don't watch hidden files
120+
"**/.*",
121+
]
122+
},
123+
target: nativescriptTarget,
124+
entry: entries,
125+
output: {
126+
pathinfo: false,
127+
path: dist,
128+
sourceMapFilename,
129+
libraryTarget: "commonjs2",
130+
filename: "[name].js",
131+
globalObject: "global",
132+
hashSalt
133+
},
134+
resolve: {
135+
extensions: [".ts", ".js", ".scss", ".css"],
136+
// Resolve {N} system modules from tns-core-modules
137+
modules: [
138+
resolve(__dirname, "node_modules/tns-core-modules"),
139+
resolve(__dirname, "node_modules"),
140+
"node_modules/tns-core-modules",
141+
"node_modules",
142+
],
143+
alias: {
144+
'~': appFullPath
145+
},
146+
symlinks: true
147+
},
148+
resolveLoader: {
149+
symlinks: false
150+
},
151+
node: {
152+
// Disable node shims that conflict with NativeScript
153+
"http": false,
154+
"timers": false,
155+
"setImmediate": false,
156+
"fs": "empty",
157+
"__dirname": false,
158+
},
159+
devtool: hiddenSourceMap ? "hidden-source-map" : (sourceMap ? "inline-source-map" : "none"),
160+
optimization: {
161+
runtimeChunk: "single",
162+
splitChunks: {
163+
cacheGroups: {
164+
vendor: {
165+
name: "vendor",
166+
chunks: "all",
167+
test: (module, chunks) => {
168+
const moduleName = module.nameForCondition ? module.nameForCondition() : '';
169+
return /[\\/]node_modules[\\/]/.test(moduleName) ||
170+
appComponents.some(comp => comp === moduleName);
171+
},
172+
enforce: true,
173+
},
174+
}
175+
},
176+
minimize: !!uglify,
177+
minimizer: [
178+
new TerserPlugin({
179+
parallel: true,
180+
cache: true,
181+
sourceMap: isAnySourceMapEnabled,
182+
terserOptions: {
183+
output: {
184+
comments: false,
185+
semicolons: !isAnySourceMapEnabled
186+
},
187+
compress: {
188+
// The Android SBG has problems parsing the output
189+
// when these options are enabled
190+
'collapse_vars': platform !== "android",
191+
sequences: platform !== "android",
192+
}
193+
}
194+
})
195+
],
196+
},
197+
module: {
198+
rules: [
199+
{
200+
include: join(appFullPath, entryPath),
201+
use: [
202+
// Require all Android app components
203+
platform === "android" && {
204+
loader: "nativescript-dev-webpack/android-app-components-loader",
205+
options: { modules: appComponents }
206+
},
207+
208+
{
209+
loader: "nativescript-dev-webpack/bundle-config-loader",
210+
options: {
211+
angular: true,
212+
loadCss: !snapshot, // load the application css if in debug mode
213+
unitTesting,
214+
appFullPath,
215+
projectRoot,
216+
ignoredFiles: nsWebpack.getUserDefinedEntries(entries, platform)
217+
}
218+
},
219+
].filter(loader => !!loader)
220+
},
221+
222+
{ test: /\.html$|\.xml$/, use: "raw-loader" },
223+
224+
// tns-core-modules reads the app.css and its imports using css-loader
225+
{
226+
test: /[\/|\\]app\.css$/,
227+
use: [
228+
"nativescript-dev-webpack/style-hot-loader",
229+
{ loader: "css-loader", options: { url: false } }
230+
]
231+
},
232+
{
233+
test: /[\/|\\]app\.scss$/,
234+
use: [
235+
"nativescript-dev-webpack/style-hot-loader",
236+
{ loader: "css-loader", options: { url: false } },
237+
"sass-loader"
238+
]
239+
},
240+
241+
// Angular components reference css files and their imports using raw-loader
242+
{ test: /\.css$/, exclude: /[\/|\\]app\.css$/, use: "raw-loader" },
243+
{ test: /\.scss$/, exclude: /[\/|\\]app\.scss$/, use: ["raw-loader", "resolve-url-loader", "sass-loader"] },
244+
245+
{
246+
test: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/,
247+
use: [
248+
"nativescript-dev-webpack/moduleid-compat-loader",
249+
"nativescript-dev-webpack/lazy-ngmodule-hot-loader",
250+
"@ngtools/webpack",
251+
]
252+
},
253+
254+
// Mark files inside `@angular/core` as using SystemJS style dynamic imports.
255+
// Removing this will cause deprecation warnings to appear.
256+
{
257+
test: /[\/\\]@angular[\/\\]core[\/\\].+\.js$/,
258+
parser: { system: true },
259+
},
260+
],
261+
},
262+
plugins: [
263+
// Define useful constants like TNS_WEBPACK
264+
new webpack.DefinePlugin({
265+
"global.TNS_WEBPACK": "true",
266+
"process": undefined,
267+
}),
268+
// Remove all files from the out dir.
269+
new CleanWebpackPlugin(itemsToClean, { verbose: !!verbose }),
270+
// Copy assets to out dir. Add your own globs as needed.
271+
new CopyWebpackPlugin([
272+
{ from: { glob: "fonts/**" } },
273+
{ from: { glob: "**/*.jpg" } },
274+
{ from: { glob: "**/*.png" } },
275+
], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),
276+
new nsWebpack.GenerateNativeScriptEntryPointsPlugin("bundle"),
277+
// For instructions on how to set up workers with webpack
278+
// check out https://github.com/nativescript/worker-loader
279+
new NativeScriptWorkerPlugin(),
280+
ngCompilerPlugin,
281+
// Does IPC communication with the {N} CLI to notify events when running in watch mode.
282+
new nsWebpack.WatchStateLoggerPlugin(),
283+
],
284+
};
285+
286+
if (report) {
287+
// Generate report files for bundles content
288+
config.plugins.push(new BundleAnalyzerPlugin({
289+
analyzerMode: "static",
290+
openAnalyzer: false,
291+
generateStatsFile: true,
292+
reportFilename: resolve(projectRoot, "report", `report.html`),
293+
statsFilename: resolve(projectRoot, "report", `stats.json`),
294+
}));
295+
}
296+
297+
if (snapshot) {
298+
config.plugins.push(new nsWebpack.NativeScriptSnapshotPlugin({
299+
chunk: "vendor",
300+
angular: true,
301+
requireModules: [
302+
"reflect-metadata",
303+
"@angular/platform-browser",
304+
"@angular/core",
305+
"@angular/common",
306+
"@angular/router",
307+
"nativescript-angular/platform-static",
308+
"nativescript-angular/router",
309+
],
310+
projectRoot,
311+
webpackConfig: config,
312+
}));
313+
}
314+
315+
if (hmr) {
316+
config.plugins.push(new webpack.HotModuleReplacementPlugin());
317+
}
318+
319+
return config;
320+
};

‎demo/JavaScriptApp/.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
app/main-page.android.css
22
app/main-page.ios.css
3-
mochawesome-report
4-
5-
webpack.config.js
3+
mochawesome-report

‎demo/JavaScriptApp/webpack.config.js

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
const { join, relative, resolve, sep } = require("path");
2+
3+
const webpack = require("webpack");
4+
const nsWebpack = require("nativescript-dev-webpack");
5+
const nativescriptTarget = require("nativescript-dev-webpack/nativescript-target");
6+
const CleanWebpackPlugin = require("clean-webpack-plugin");
7+
const CopyWebpackPlugin = require("copy-webpack-plugin");
8+
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
9+
const { NativeScriptWorkerPlugin } = require("nativescript-worker-loader/NativeScriptWorkerPlugin");
10+
const TerserPlugin = require("terser-webpack-plugin");
11+
const hashSalt = Date.now().toString();
12+
13+
module.exports = env => {
14+
// Add your custom Activities, Services and other android app components here.
15+
const appComponents = [
16+
"tns-core-modules/ui/frame",
17+
"tns-core-modules/ui/frame/activity",
18+
resolve(__dirname, "app/activity.android.js")
19+
];
20+
21+
const platform = env && (env.android && "android" || env.ios && "ios");
22+
if (!platform) {
23+
throw new Error("You need to provide a target platform!");
24+
}
25+
26+
const platforms = ["ios", "android"];
27+
const projectRoot = __dirname;
28+
29+
// Default destination inside platforms/<platform>/...
30+
const dist = resolve(projectRoot, nsWebpack.getAppPath(platform, projectRoot));
31+
32+
const {
33+
// The 'appPath' and 'appResourcesPath' values are fetched from
34+
// the nsconfig.json configuration file.
35+
appPath = "app",
36+
appResourcesPath = "app/App_Resources",
37+
38+
// You can provide the following flags when running 'tns run android|ios'
39+
snapshot, // --env.snapshot
40+
production, // --env.production
41+
uglify, // --env.uglify
42+
report, // --env.report
43+
sourceMap, // --env.sourceMap
44+
hiddenSourceMap, // --env.hiddenSourceMap
45+
hmr, // --env.hmr,
46+
unitTesting, // --env.unitTesting,
47+
verbose, // --env.verbose
48+
} = env;
49+
50+
const isAnySourceMapEnabled = !!sourceMap || !!hiddenSourceMap;
51+
const externals = nsWebpack.getConvertedExternals(env.externals);
52+
const appFullPath = resolve(projectRoot, appPath);
53+
const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
54+
55+
const entryModule = nsWebpack.getEntryModule(appFullPath, platform);
56+
const entryPath = `.${sep}${entryModule}.js`;
57+
const entries = { bundle: entryPath, application: "./application.android" };
58+
const areCoreModulesExternal = Array.isArray(env.externals) && env.externals.some(e => e.indexOf("tns-core-modules") > -1);
59+
if (platform === "ios" && !areCoreModulesExternal) {
60+
entries["tns_modules/tns-core-modules/inspector_modules"] = "inspector_modules";
61+
};
62+
63+
let sourceMapFilename = nsWebpack.getSourceMapFilename(hiddenSourceMap, __dirname, dist);
64+
65+
const itemsToClean = [`${dist}/**/*`];
66+
if (platform === "android") {
67+
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "src", "main", "assets", "snapshots")}`);
68+
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "build", "configurations", "nativescript-android-snapshot")}`);
69+
}
70+
71+
nsWebpack.processAppComponents(appComponents, platform);
72+
const config = {
73+
mode: production ? "production" : "development",
74+
context: appFullPath,
75+
externals,
76+
watchOptions: {
77+
ignored: [
78+
appResourcesFullPath,
79+
// Don't watch hidden files
80+
"**/.*",
81+
]
82+
},
83+
target: nativescriptTarget,
84+
entry: entries,
85+
output: {
86+
pathinfo: false,
87+
path: dist,
88+
sourceMapFilename,
89+
libraryTarget: "commonjs2",
90+
filename: "[name].js",
91+
globalObject: "global",
92+
hashSalt
93+
},
94+
resolve: {
95+
extensions: [".js", ".scss", ".css"],
96+
// Resolve {N} system modules from tns-core-modules
97+
modules: [
98+
"node_modules/tns-core-modules",
99+
"node_modules",
100+
],
101+
alias: {
102+
'~': appFullPath
103+
},
104+
// don't resolve symlinks to symlinked modules
105+
symlinks: true
106+
},
107+
resolveLoader: {
108+
// don't resolve symlinks to symlinked loaders
109+
symlinks: false
110+
},
111+
node: {
112+
// Disable node shims that conflict with NativeScript
113+
"http": false,
114+
"timers": false,
115+
"setImmediate": false,
116+
"fs": "empty",
117+
"__dirname": false,
118+
},
119+
devtool: hiddenSourceMap ? "hidden-source-map" : (sourceMap ? "inline-source-map" : "none"),
120+
optimization: {
121+
runtimeChunk: "single",
122+
splitChunks: {
123+
cacheGroups: {
124+
vendor: {
125+
name: "vendor",
126+
chunks: "all",
127+
test: (module, chunks) => {
128+
const moduleName = module.nameForCondition ? module.nameForCondition() : '';
129+
return /[\\/]node_modules[\\/]/.test(moduleName) ||
130+
appComponents.some(comp => comp === moduleName);
131+
132+
},
133+
enforce: true,
134+
},
135+
}
136+
},
137+
minimize: !!uglify,
138+
minimizer: [
139+
new TerserPlugin({
140+
parallel: true,
141+
cache: true,
142+
sourceMap: isAnySourceMapEnabled,
143+
terserOptions: {
144+
output: {
145+
comments: false,
146+
semicolons: !isAnySourceMapEnabled
147+
},
148+
compress: {
149+
// The Android SBG has problems parsing the output
150+
// when these options are enabled
151+
'collapse_vars': platform !== "android",
152+
sequences: platform !== "android",
153+
}
154+
}
155+
})
156+
],
157+
},
158+
module: {
159+
rules: [
160+
{
161+
include: join(appFullPath, entryPath),
162+
use: [
163+
// Require all Android app components
164+
platform === "android" && {
165+
loader: "nativescript-dev-webpack/android-app-components-loader",
166+
options: { modules: appComponents }
167+
},
168+
169+
{
170+
loader: "nativescript-dev-webpack/bundle-config-loader",
171+
options: {
172+
loadCss: !snapshot, // load the application css if in debug mode
173+
unitTesting,
174+
appFullPath,
175+
projectRoot,
176+
ignoredFiles: nsWebpack.getUserDefinedEntries(entries, platform)
177+
}
178+
},
179+
].filter(loader => !!loader)
180+
},
181+
182+
{
183+
test: /\.(js|css|scss|html|xml)$/,
184+
use: "nativescript-dev-webpack/hmr/hot-loader"
185+
},
186+
187+
{ test: /\.(html|xml)$/, use: "nativescript-dev-webpack/xml-namespace-loader" },
188+
189+
{
190+
test: /\.css$/,
191+
use: { loader: "css-loader", options: { url: false } }
192+
},
193+
194+
{
195+
test: /\.scss$/,
196+
use: [
197+
{ loader: "css-loader", options: { url: false } },
198+
"sass-loader"
199+
]
200+
},
201+
]
202+
},
203+
plugins: [
204+
// Define useful constants like TNS_WEBPACK
205+
new webpack.DefinePlugin({
206+
"global.TNS_WEBPACK": "true",
207+
"process": undefined,
208+
}),
209+
// Remove all files from the out dir.
210+
new CleanWebpackPlugin(itemsToClean, { verbose: !!verbose }),
211+
// Copy assets to out dir. Add your own globs as needed.
212+
new CopyWebpackPlugin([
213+
{ from: { glob: "fonts/**" } },
214+
{ from: { glob: "**/*.jpg" } },
215+
{ from: { glob: "**/*.png" } },
216+
], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),
217+
new nsWebpack.GenerateNativeScriptEntryPointsPlugin("bundle"),
218+
219+
// For instructions on how to set up workers with webpack
220+
// check out https://github.com/nativescript/worker-loader
221+
new NativeScriptWorkerPlugin(),
222+
new nsWebpack.PlatformFSPlugin({
223+
platform,
224+
platforms,
225+
}),
226+
// Does IPC communication with the {N} CLI to notify events when running in watch mode.
227+
new nsWebpack.WatchStateLoggerPlugin()
228+
],
229+
};
230+
231+
if (report) {
232+
// Generate report files for bundles content
233+
config.plugins.push(new BundleAnalyzerPlugin({
234+
analyzerMode: "static",
235+
openAnalyzer: false,
236+
generateStatsFile: true,
237+
reportFilename: resolve(projectRoot, "report", `report.html`),
238+
statsFilename: resolve(projectRoot, "report", `stats.json`),
239+
}));
240+
}
241+
242+
if (snapshot) {
243+
config.plugins.push(new nsWebpack.NativeScriptSnapshotPlugin({
244+
chunk: "vendor",
245+
requireModules: [
246+
"tns-core-modules/bundle-entry-points",
247+
],
248+
projectRoot,
249+
webpackConfig: config,
250+
}));
251+
}
252+
253+
if (hmr) {
254+
config.plugins.push(new webpack.HotModuleReplacementPlugin());
255+
}
256+
257+
258+
return config;
259+
};

‎demo/TypeScriptApp/.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@ e2e/**/*.js
44
app/app.android.css
55
app/app.ios.css
66
app/main-page.android.css
7-
app/main-page.ios.css
8-
9-
webpack.config.js
7+
app/main-page.ios.css

‎demo/TypeScriptApp/webpack.config.js

Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
const { join, relative, resolve, sep } = require("path");
2+
3+
const webpack = require("webpack");
4+
const nsWebpack = require("nativescript-dev-webpack");
5+
const nativescriptTarget = require("nativescript-dev-webpack/nativescript-target");
6+
const CleanWebpackPlugin = require("clean-webpack-plugin");
7+
const CopyWebpackPlugin = require("copy-webpack-plugin");
8+
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
9+
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
10+
const { NativeScriptWorkerPlugin } = require("nativescript-worker-loader/NativeScriptWorkerPlugin");
11+
const TerserPlugin = require("terser-webpack-plugin");
12+
const hashSalt = Date.now().toString();
13+
14+
module.exports = env => {
15+
// Add your custom Activities, Services and other Android app components here.
16+
const appComponents = [
17+
"tns-core-modules/ui/frame",
18+
"tns-core-modules/ui/frame/activity",
19+
resolve(__dirname, "app/activity.android.ts")
20+
];
21+
22+
const platform = env && (env.android && "android" || env.ios && "ios");
23+
if (!platform) {
24+
throw new Error("You need to provide a target platform!");
25+
}
26+
27+
const platforms = ["ios", "android"];
28+
const projectRoot = __dirname;
29+
30+
// Default destination inside platforms/<platform>/...
31+
const dist = resolve(projectRoot, nsWebpack.getAppPath(platform, projectRoot));
32+
33+
const {
34+
// The 'appPath' and 'appResourcesPath' values are fetched from
35+
// the nsconfig.json configuration file.
36+
appPath = "app",
37+
appResourcesPath = "app/App_Resources",
38+
39+
// You can provide the following flags when running 'tns run android|ios'
40+
snapshot, // --env.snapshot
41+
production, // --env.production
42+
uglify, // --env.uglify
43+
report, // --env.report
44+
sourceMap, // --env.sourceMap
45+
hiddenSourceMap, // --env.hiddenSourceMap
46+
hmr, // --env.hmr,
47+
unitTesting, // --env.unitTesting,
48+
verbose, // --env.verbose
49+
} = env;
50+
const isAnySourceMapEnabled = !!sourceMap || !!hiddenSourceMap;
51+
const externals = nsWebpack.getConvertedExternals(env.externals);
52+
53+
const appFullPath = resolve(projectRoot, appPath);
54+
const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
55+
56+
const entryModule = nsWebpack.getEntryModule(appFullPath, platform);
57+
const entryPath = `.${sep}${entryModule}.ts`;
58+
const entries = { bundle: entryPath, application: "./application.android" };
59+
60+
const tsConfigPath = resolve(projectRoot, "tsconfig.tns.json");
61+
62+
const areCoreModulesExternal = Array.isArray(env.externals) && env.externals.some(e => e.indexOf("tns-core-modules") > -1);
63+
if (platform === "ios" && !areCoreModulesExternal) {
64+
entries["tns_modules/tns-core-modules/inspector_modules"] = "inspector_modules";
65+
};
66+
67+
let sourceMapFilename = nsWebpack.getSourceMapFilename(hiddenSourceMap, __dirname, dist);
68+
69+
const itemsToClean = [`${dist}/**/*`];
70+
if (platform === "android") {
71+
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "src", "main", "assets", "snapshots")}`);
72+
itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "build", "configurations", "nativescript-android-snapshot")}`);
73+
}
74+
75+
nsWebpack.processAppComponents(appComponents, platform);
76+
const config = {
77+
mode: production ? "production" : "development",
78+
context: appFullPath,
79+
externals,
80+
watchOptions: {
81+
ignored: [
82+
appResourcesFullPath,
83+
// Don't watch hidden files
84+
"**/.*",
85+
]
86+
},
87+
target: nativescriptTarget,
88+
entry: entries,
89+
output: {
90+
pathinfo: false,
91+
path: dist,
92+
sourceMapFilename,
93+
libraryTarget: "commonjs2",
94+
filename: "[name].js",
95+
globalObject: "global",
96+
hashSalt
97+
},
98+
resolve: {
99+
extensions: [".ts", ".js", ".scss", ".css"],
100+
// Resolve {N} system modules from tns-core-modules
101+
modules: [
102+
resolve(__dirname, "node_modules/tns-core-modules"),
103+
resolve(__dirname, "node_modules"),
104+
"node_modules/tns-core-modules",
105+
"node_modules",
106+
],
107+
alias: {
108+
'~': appFullPath
109+
},
110+
// resolve symlinks to symlinked modules
111+
symlinks: true
112+
},
113+
resolveLoader: {
114+
// don't resolve symlinks to symlinked loaders
115+
symlinks: false
116+
},
117+
node: {
118+
// Disable node shims that conflict with NativeScript
119+
"http": false,
120+
"timers": false,
121+
"setImmediate": false,
122+
"fs": "empty",
123+
"__dirname": false,
124+
},
125+
devtool: hiddenSourceMap ? "hidden-source-map" : (sourceMap ? "inline-source-map" : "none"),
126+
optimization: {
127+
runtimeChunk: "single",
128+
splitChunks: {
129+
cacheGroups: {
130+
vendor: {
131+
name: "vendor",
132+
chunks: "all",
133+
test: (module, chunks) => {
134+
const moduleName = module.nameForCondition ? module.nameForCondition() : '';
135+
return /[\\/]node_modules[\\/]/.test(moduleName) ||
136+
appComponents.some(comp => comp === moduleName);
137+
138+
},
139+
enforce: true,
140+
},
141+
}
142+
},
143+
minimize: !!uglify,
144+
minimizer: [
145+
new TerserPlugin({
146+
parallel: true,
147+
cache: true,
148+
sourceMap: isAnySourceMapEnabled,
149+
terserOptions: {
150+
output: {
151+
comments: false,
152+
semicolons: !isAnySourceMapEnabled
153+
},
154+
compress: {
155+
// The Android SBG has problems parsing the output
156+
// when these options are enabled
157+
'collapse_vars': platform !== "android",
158+
sequences: platform !== "android",
159+
}
160+
}
161+
})
162+
],
163+
},
164+
module: {
165+
rules: [
166+
{
167+
include: join(appFullPath, entryPath),
168+
use: [
169+
// Require all Android app components
170+
platform === "android" && {
171+
loader: "nativescript-dev-webpack/android-app-components-loader",
172+
options: { modules: appComponents }
173+
},
174+
175+
{
176+
loader: "nativescript-dev-webpack/bundle-config-loader",
177+
options: {
178+
loadCss: !snapshot, // load the application css if in debug mode
179+
unitTesting,
180+
appFullPath,
181+
projectRoot,
182+
ignoredFiles: nsWebpack.getUserDefinedEntries(entries, platform)
183+
}
184+
},
185+
].filter(loader => !!loader)
186+
},
187+
188+
{
189+
test: /\.(ts|css|scss|html|xml)$/,
190+
use: "nativescript-dev-webpack/hmr/hot-loader"
191+
},
192+
193+
{ test: /\.(html|xml)$/, use: "nativescript-dev-webpack/xml-namespace-loader" },
194+
195+
{
196+
test: /\.css$/,
197+
use: { loader: "css-loader", options: { url: false } }
198+
},
199+
200+
{
201+
test: /\.scss$/,
202+
use: [
203+
{ loader: "css-loader", options: { url: false } },
204+
"sass-loader"
205+
]
206+
},
207+
208+
{
209+
test: /\.ts$/,
210+
use: {
211+
loader: "ts-loader",
212+
options: {
213+
configFile: tsConfigPath,
214+
// https://github.com/TypeStrong/ts-loader/blob/ea2fcf925ec158d0a536d1e766adfec6567f5fb4/README.md#faster-builds
215+
// https://github.com/TypeStrong/ts-loader/blob/ea2fcf925ec158d0a536d1e766adfec6567f5fb4/README.md#hot-module-replacement
216+
transpileOnly: true,
217+
allowTsInNodeModules: true,
218+
compilerOptions: {
219+
sourceMap: isAnySourceMapEnabled,
220+
declaration: false
221+
}
222+
},
223+
}
224+
},
225+
]
226+
},
227+
plugins: [
228+
// Define useful constants like TNS_WEBPACK
229+
new webpack.DefinePlugin({
230+
"global.TNS_WEBPACK": "true",
231+
"process": undefined,
232+
}),
233+
// Remove all files from the out dir.
234+
new CleanWebpackPlugin(itemsToClean, { verbose: !!verbose }),
235+
// Copy assets to out dir. Add your own globs as needed.
236+
new CopyWebpackPlugin([
237+
{ from: { glob: "fonts/**" } },
238+
{ from: { glob: "**/*.jpg" } },
239+
{ from: { glob: "**/*.png" } },
240+
], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),
241+
new nsWebpack.GenerateNativeScriptEntryPointsPlugin("bundle"),
242+
// For instructions on how to set up workers with webpack
243+
// check out https://github.com/nativescript/worker-loader
244+
new NativeScriptWorkerPlugin(),
245+
new nsWebpack.PlatformFSPlugin({
246+
platform,
247+
platforms,
248+
}),
249+
// Does IPC communication with the {N} CLI to notify events when running in watch mode.
250+
new nsWebpack.WatchStateLoggerPlugin(),
251+
// https://github.com/TypeStrong/ts-loader/blob/ea2fcf925ec158d0a536d1e766adfec6567f5fb4/README.md#faster-builds
252+
// https://github.com/TypeStrong/ts-loader/blob/ea2fcf925ec158d0a536d1e766adfec6567f5fb4/README.md#hot-module-replacement
253+
new ForkTsCheckerWebpackPlugin({
254+
tsconfig: tsConfigPath,
255+
async: false,
256+
useTypescriptIncrementalApi: true,
257+
memoryLimit: 4096
258+
})
259+
],
260+
};
261+
262+
if (report) {
263+
// Generate report files for bundles content
264+
config.plugins.push(new BundleAnalyzerPlugin({
265+
analyzerMode: "static",
266+
openAnalyzer: false,
267+
generateStatsFile: true,
268+
reportFilename: resolve(projectRoot, "report", `report.html`),
269+
statsFilename: resolve(projectRoot, "report", `stats.json`),
270+
}));
271+
}
272+
273+
if (snapshot) {
274+
config.plugins.push(new nsWebpack.NativeScriptSnapshotPlugin({
275+
chunk: "vendor",
276+
requireModules: [
277+
"tns-core-modules/bundle-entry-points",
278+
],
279+
projectRoot,
280+
webpackConfig: config,
281+
}));
282+
}
283+
284+
if (hmr) {
285+
config.plugins.push(new webpack.HotModuleReplacementPlugin());
286+
}
287+
288+
289+
return config;
290+
};

0 commit comments

Comments
 (0)
This repository has been archived.