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