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