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 UglifyJsPlugin = require("uglifyjs-webpack-plugin");
|
11 |
|
12 | module.exports = env => {
|
13 |
|
14 | const appComponents = [
|
15 | "tns-core-modules/ui/frame",
|
16 | "tns-core-modules/ui/frame/activity",
|
17 | ];
|
18 |
|
19 | const platform = env && (env.android && "android" || env.ios && "ios");
|
20 | if (!platform) {
|
21 | throw new Error("You need to provide a target platform!");
|
22 | }
|
23 |
|
24 | const platforms = ["ios", "android"];
|
25 | const projectRoot = __dirname;
|
26 |
|
27 |
|
28 | const dist = resolve(projectRoot, nsWebpack.getAppPath(platform, projectRoot));
|
29 | const appResourcesPlatformDir = platform === "android" ? "Android" : "iOS";
|
30 |
|
31 | const {
|
32 |
|
33 |
|
34 |
|
35 | appPath = "app",
|
36 | appResourcesPath = "app/App_Resources",
|
37 |
|
38 |
|
39 | snapshot,
|
40 | uglify,
|
41 | report,
|
42 | sourceMap,
|
43 | hmr,
|
44 | } = env;
|
45 | const externals = nsWebpack.getConvertedExternals(env.externals);
|
46 |
|
47 | const appFullPath = resolve(projectRoot, appPath);
|
48 | const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
|
49 |
|
50 | const entryModule = nsWebpack.getEntryModule(appFullPath);
|
51 | const entryPath = `.${sep}${entryModule}.js`;
|
52 |
|
53 | const config = {
|
54 | mode: uglify ? "production" : "development",
|
55 | context: appFullPath,
|
56 | externals,
|
57 | watchOptions: {
|
58 | ignored: [
|
59 | appResourcesFullPath,
|
60 |
|
61 | "**/.*",
|
62 | ]
|
63 | },
|
64 | target: nativescriptTarget,
|
65 | entry: {
|
66 | bundle: entryPath,
|
67 | },
|
68 | output: {
|
69 | pathinfo: false,
|
70 | path: dist,
|
71 | libraryTarget: "commonjs2",
|
72 | filename: "[name].js",
|
73 | globalObject: "global",
|
74 | },
|
75 | resolve: {
|
76 | extensions: [".js", ".scss", ".css"],
|
77 |
|
78 | modules: [
|
79 | "node_modules/tns-core-modules",
|
80 | "node_modules",
|
81 | ],
|
82 | alias: {
|
83 | '~': appFullPath
|
84 | },
|
85 |
|
86 | symlinks: false
|
87 | },
|
88 | resolveLoader: {
|
89 |
|
90 | symlinks: false
|
91 | },
|
92 | node: {
|
93 |
|
94 | "http": false,
|
95 | "timers": false,
|
96 | "setImmediate": false,
|
97 | "fs": "empty",
|
98 | "__dirname": false,
|
99 | },
|
100 | devtool: sourceMap ? "inline-source-map" : "none",
|
101 | optimization: {
|
102 | splitChunks: {
|
103 | cacheGroups: {
|
104 | vendor: {
|
105 | name: "vendor",
|
106 | chunks: "all",
|
107 | test: (module, chunks) => {
|
108 | const moduleName = module.nameForCondition ? module.nameForCondition() : '';
|
109 | return /[\\/]node_modules[\\/]/.test(moduleName) ||
|
110 | appComponents.some(comp => comp === moduleName);
|
111 |
|
112 | },
|
113 | enforce: true,
|
114 | },
|
115 | }
|
116 | },
|
117 | minimize: !!uglify,
|
118 | minimizer: [
|
119 | new UglifyJsPlugin({
|
120 | parallel: true,
|
121 | cache: true,
|
122 | uglifyOptions: {
|
123 | output: {
|
124 | comments: false,
|
125 | },
|
126 | compress: {
|
127 |
|
128 |
|
129 | 'collapse_vars': platform !== "android",
|
130 | sequences: platform !== "android",
|
131 | }
|
132 | }
|
133 | })
|
134 | ],
|
135 | },
|
136 | module: {
|
137 | rules: [
|
138 | {
|
139 | test: new RegExp(entryPath),
|
140 | use: [
|
141 |
|
142 | platform === "android" && {
|
143 | loader: "nativescript-dev-webpack/android-app-components-loader",
|
144 | options: { modules: appComponents }
|
145 | },
|
146 |
|
147 | {
|
148 | loader: "nativescript-dev-webpack/bundle-config-loader",
|
149 | options: {
|
150 | loadCss: !snapshot,
|
151 | }
|
152 | },
|
153 | ].filter(loader => !!loader)
|
154 | },
|
155 |
|
156 | {
|
157 | test: /-page\.js$/,
|
158 | use: "nativescript-dev-webpack/script-hot-loader"
|
159 | },
|
160 |
|
161 | {
|
162 | test: /\.(css|scss)$/,
|
163 | use: "nativescript-dev-webpack/style-hot-loader"
|
164 | },
|
165 |
|
166 | {
|
167 | test: /\.(html|xml)$/,
|
168 | use: "nativescript-dev-webpack/markup-hot-loader"
|
169 | },
|
170 |
|
171 | { test: /\.(html|xml)$/, use: "nativescript-dev-webpack/xml-namespace-loader"},
|
172 |
|
173 | {
|
174 | test: /\.css$/,
|
175 | use: { loader: "css-loader", options: { minimize: false, url: false } }
|
176 | },
|
177 |
|
178 | {
|
179 | test: /\.scss$/,
|
180 | use: [
|
181 | { loader: "css-loader", options: { minimize: false, url: false } },
|
182 | "sass-loader"
|
183 | ]
|
184 | },
|
185 | ]
|
186 | },
|
187 | plugins: [
|
188 |
|
189 | new webpack.DefinePlugin({
|
190 | "global.TNS_WEBPACK": "true",
|
191 | "process": undefined,
|
192 | }),
|
193 |
|
194 | new CleanWebpackPlugin([ `${dist}/**/*` ]),
|
195 |
|
196 | new CopyWebpackPlugin([
|
197 | {
|
198 | from: `${appResourcesFullPath}/${appResourcesPlatformDir}`,
|
199 | to: `${dist}/App_Resources/${appResourcesPlatformDir}`,
|
200 | context: projectRoot
|
201 | },
|
202 | ]),
|
203 |
|
204 | new CopyWebpackPlugin([
|
205 | { from: { glob: "fonts/**" } },
|
206 | { from: { glob: "**/*.jpg" } },
|
207 | { from: { glob: "**/*.png" } },
|
208 | ], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),
|
209 |
|
210 | new nsWebpack.GenerateBundleStarterPlugin([
|
211 | "./vendor",
|
212 | "./bundle",
|
213 | ]),
|
214 |
|
215 |
|
216 | new NativeScriptWorkerPlugin(),
|
217 | new nsWebpack.PlatformFSPlugin({
|
218 | platform,
|
219 | platforms,
|
220 | }),
|
221 |
|
222 | new nsWebpack.WatchStateLoggerPlugin(),
|
223 | ],
|
224 | };
|
225 |
|
226 | if (report) {
|
227 |
|
228 | config.plugins.push(new BundleAnalyzerPlugin({
|
229 | analyzerMode: "static",
|
230 | openAnalyzer: false,
|
231 | generateStatsFile: true,
|
232 | reportFilename: resolve(projectRoot, "report", `report.html`),
|
233 | statsFilename: resolve(projectRoot, "report", `stats.json`),
|
234 | }));
|
235 | }
|
236 |
|
237 | if (snapshot) {
|
238 | config.plugins.push(new nsWebpack.NativeScriptSnapshotPlugin({
|
239 | chunk: "vendor",
|
240 | requireModules: [
|
241 | "tns-core-modules/bundle-entry-points",
|
242 | ],
|
243 | projectRoot,
|
244 | webpackConfig: config,
|
245 | }));
|
246 | }
|
247 |
|
248 | if (hmr) {
|
249 | config.plugins.push(new webpack.HotModuleReplacementPlugin());
|
250 | }
|
251 |
|
252 |
|
253 | return config;
|
254 | };
|