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

Commit 075ffb9

Browse files
committed
chore(demos): update webpack config files
1 parent 5cb8da1 commit 075ffb9

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

demo/AngularApp/webpack.config.js

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
const { join, relative, resolve, sep } = require("path");
1+
const { join, relative, resolve, sep, dirname } = require("path");
22

33
const webpack = require("webpack");
44
const nsWebpack = require("nativescript-dev-webpack");
55
const nativescriptTarget = require("nativescript-dev-webpack/nativescript-target");
66
const { nsReplaceBootstrap } = require("nativescript-dev-webpack/transformers/ns-replace-bootstrap");
7+
const { nsReplaceLazyLoader } = require("nativescript-dev-webpack/transformers/ns-replace-lazy-loader");
8+
const { getMainModulePath } = require("nativescript-dev-webpack/utils/ast-utils");
79
const CleanWebpackPlugin = require("clean-webpack-plugin");
810
const CopyWebpackPlugin = require("copy-webpack-plugin");
911
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
@@ -34,8 +36,8 @@ module.exports = env => {
3436
// The 'appPath' and 'appResourcesPath' values are fetched from
3537
// the nsconfig.json configuration file
3638
// when bundling with `tns run android|ios --bundle`.
37-
appPath = "app",
38-
appResourcesPath = "app/App_Resources",
39+
appPath = "src",
40+
appResourcesPath = "App_Resources",
3941

4042
// You can provide the following flags when running 'tns run android|ios'
4143
aot, // --env.aot
@@ -45,7 +47,8 @@ module.exports = env => {
4547
sourceMap, // --env.sourceMap
4648
hmr, // --env.hmr,
4749
} = env;
48-
const externals = (env.externals || []).map((e) => { // --env.externals
50+
env.externals = env.externals || [];
51+
const externals = (env.externals).map((e) => { // --env.externals
4952
return new RegExp(e + ".*");
5053
});
5154

@@ -54,14 +57,34 @@ module.exports = env => {
5457

5558
const entryModule = `${nsWebpack.getEntryModule(appFullPath)}.ts`;
5659
const entryPath = `.${sep}${entryModule}`;
60+
const ngCompilerTransformers = [];
61+
const additionalLazyModuleResources = [];
62+
if (aot) {
63+
ngCompilerTransformers.push(nsReplaceBootstrap);
64+
}
65+
66+
// when "@angular/core" is external, it's not included in the bundles. In this way, it will be used
67+
// directly from node_modules and the Angular modules loader won't be able to resolve the lazy routes
68+
// fixes https://github.com/NativeScript/nativescript-cli/issues/4024
69+
if (env.externals.indexOf("@angular/core") > -1) {
70+
const appModuleRelativePath = getMainModulePath(resolve(appFullPath, entryModule));
71+
if (appModuleRelativePath) {
72+
const appModuleFolderPath = dirname(resolve(appFullPath, appModuleRelativePath));
73+
// include the lazy loader inside app module
74+
ngCompilerTransformers.push(nsReplaceLazyLoader);
75+
// include the new lazy loader path in the allowed ones
76+
additionalLazyModuleResources.push(appModuleFolderPath);
77+
}
78+
}
5779

5880
const ngCompilerPlugin = new AngularCompilerPlugin({
5981
hostReplacementPaths: nsWebpack.getResolver([platform, "tns"]),
60-
platformTransformers: aot ? [nsReplaceBootstrap(() => ngCompilerPlugin)] : null,
82+
platformTransformers: ngCompilerTransformers.map(t => t(() => ngCompilerPlugin)),
6183
mainPath: resolve(appPath, entryModule),
6284
tsConfigPath: join(__dirname, "tsconfig.tns.json"),
6385
skipCodeGeneration: !aot,
6486
sourceMap: !!sourceMap,
87+
additionalLazyModuleResources: additionalLazyModuleResources
6588
});
6689

6790
const config = {
@@ -78,7 +101,6 @@ module.exports = env => {
78101
target: nativescriptTarget,
79102
entry: {
80103
bundle: entryPath,
81-
application: "./application.android",
82104
},
83105
output: {
84106
pathinfo: false,
@@ -195,6 +217,7 @@ module.exports = env => {
195217
test: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/,
196218
use: [
197219
"nativescript-dev-webpack/moduleid-compat-loader",
220+
"nativescript-dev-webpack/lazy-ngmodule-hot-loader",
198221
"@ngtools/webpack",
199222
]
200223
},
@@ -225,9 +248,9 @@ module.exports = env => {
225248
]),
226249
// Copy assets to out dir. Add your own globs as needed.
227250
new CopyWebpackPlugin([
228-
{ from: "fonts/**" },
229-
{ from: "**/*.jpg" },
230-
{ from: "**/*.png" },
251+
{ from: { glob: "fonts/**" } },
252+
{ from: { glob: "**/*.jpg" } },
253+
{ from: { glob: "**/*.png" } },
231254
], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),
232255
// Generate a bundle starter script and activate it in package.json
233256
new nsWebpack.GenerateBundleStarterPlugin([

demo/JavaScriptApp/webpack.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,9 @@ module.exports = env => {
206206
]),
207207
// Copy assets to out dir. Add your own globs as needed.
208208
new CopyWebpackPlugin([
209-
{ from: "fonts/**" },
210-
{ from: "**/*.jpg" },
211-
{ from: "**/*.png" },
209+
{ from: { glob: "fonts/**" } },
210+
{ from: { glob: "**/*.jpg" } },
211+
{ from: { glob: "**/*.png" } },
212212
], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),
213213
// Generate a bundle starter script and activate it in package.json
214214
new nsWebpack.GenerateBundleStarterPlugin([

demo/TypeScriptApp/webpack.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,9 @@ module.exports = env => {
220220
]),
221221
// Copy assets to out dir. Add your own globs as needed.
222222
new CopyWebpackPlugin([
223-
{ from: "fonts/**" },
224-
{ from: "**/*.jpg" },
225-
{ from: "**/*.png" },
223+
{ from: { glob: "fonts/**" } },
224+
{ from: { glob: "**/*.jpg" } },
225+
{ from: { glob: "**/*.png" } },
226226
], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),
227227
// Generate a bundle starter script and activate it in package.json
228228
new nsWebpack.GenerateBundleStarterPlugin([

0 commit comments

Comments
 (0)