|
| 1 | +const { resolve, join } = 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 CopyWebpackPlugin = require("copy-webpack-plugin"); |
| 7 | +const ExtractTextPlugin = require("extract-text-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 | + const platform = env && (env.android && "android" || env.ios && "ios"); |
| 14 | + if (!platform) { |
| 15 | + throw new Error("You need to provide a target platform!"); |
| 16 | + } |
| 17 | + const platforms = ["ios", "android"]; |
| 18 | + const { snapshot, uglify, report, aot } = env; |
| 19 | + const ngToolsWebpackOptions = { tsConfigPath: "tsconfig.json" }; |
| 20 | + |
| 21 | + const config = { |
| 22 | + context: resolve("./app"), |
| 23 | + target: nativescriptTarget, |
| 24 | + entry: { |
| 25 | + bundle: aot ? "./main.aot.ts" : "./main.ts", |
| 26 | + vendor: "./vendor", |
| 27 | + }, |
| 28 | + output: { |
| 29 | + pathinfo: true, |
| 30 | + // Default destination inside platforms/<platform>/... |
| 31 | + path: resolve(nsWebpack.getAppPath(platform)), |
| 32 | + libraryTarget: "commonjs2", |
| 33 | + filename: "[name].js", |
| 34 | + }, |
| 35 | + resolve: { |
| 36 | + extensions: [".ts", ".js", ".scss", ".css"], |
| 37 | + // Resolve {N} system modules from tns-core-modules |
| 38 | + modules: [ |
| 39 | + "node_modules/tns-core-modules", |
| 40 | + "node_modules", |
| 41 | + ], |
| 42 | + alias: { |
| 43 | + '~': resolve("./app") |
| 44 | + }, |
| 45 | + // don't resolve symlinks to symlinked modules |
| 46 | + symlinks: false |
| 47 | + }, |
| 48 | + resolveLoader: { |
| 49 | + // don't resolve symlinks to symlinked loaders |
| 50 | + symlinks: false |
| 51 | + }, |
| 52 | + node: { |
| 53 | + // Disable node shims that conflict with NativeScript |
| 54 | + "http": false, |
| 55 | + "timers": false, |
| 56 | + "setImmediate": false, |
| 57 | + "fs": "empty", |
| 58 | + }, |
| 59 | + module: { |
| 60 | + rules: [ |
| 61 | + { test: /\.html$|\.xml$/, use: "raw-loader" }, |
| 62 | + |
| 63 | + // tns-core-modules reads the app.css and its imports using css-loader |
| 64 | + { |
| 65 | + test: /[\/|\\]app\.css$/, |
| 66 | + use: { |
| 67 | + loader: "css-loader", |
| 68 | + options: { minimize: false, url: false }, |
| 69 | + } |
| 70 | + }, |
| 71 | + { |
| 72 | + test: /[\/|\\]app\.scss$/, |
| 73 | + use: [ |
| 74 | + { loader: "css-loader", options: { minimize: false, url: false } }, |
| 75 | + "sass-loader" |
| 76 | + ] |
| 77 | + }, |
| 78 | + |
| 79 | + // Angular components reference css files and their imports using raw-loader |
| 80 | + { test: /\.css$/, exclude: /[\/|\\]app\.css$/, use: "raw-loader" }, |
| 81 | + { test: /\.scss$/, exclude: /[\/|\\]app\.scss$/, use: ["raw-loader", "resolve-url-loader", "sass-loader"] }, |
| 82 | + |
| 83 | + // Compile TypeScript files with ahead-of-time compiler. |
| 84 | + { test: /.ts$/, use: [ |
| 85 | + "nativescript-dev-webpack/moduleid-compat-loader", |
| 86 | + { loader: "@ngtools/webpack", options: ngToolsWebpackOptions }, |
| 87 | + ]}, |
| 88 | + ], |
| 89 | + }, |
| 90 | + plugins: [ |
| 91 | + // Vendor libs go to the vendor.js chunk |
| 92 | + new webpack.optimize.CommonsChunkPlugin({ |
| 93 | + name: ["vendor"], |
| 94 | + }), |
| 95 | + // Define useful constants like TNS_WEBPACK |
| 96 | + new webpack.DefinePlugin({ |
| 97 | + "global.TNS_WEBPACK": "true", |
| 98 | + }), |
| 99 | + // Copy assets to out dir. Add your own globs as needed. |
| 100 | + new CopyWebpackPlugin([ |
| 101 | + { from: "App_Resources/**" }, |
| 102 | + { from: "fonts/**" }, |
| 103 | + { from: "**/*.jpg" }, |
| 104 | + { from: "**/*.png" }, |
| 105 | + { from: "**/*.xml" }, |
| 106 | + ]), |
| 107 | + // Generate a bundle starter script and activate it in package.json |
| 108 | + new nsWebpack.GenerateBundleStarterPlugin([ |
| 109 | + "./vendor", |
| 110 | + "./bundle", |
| 111 | + ]), |
| 112 | + // Support for web workers since v3.2 |
| 113 | + new NativeScriptWorkerPlugin(), |
| 114 | + // AngularCompilerPlugin with augmented NativeScript filesystem to handle platform specific resource resolution. |
| 115 | + new nsWebpack.NativeScriptAngularCompilerPlugin( |
| 116 | + Object.assign({ |
| 117 | + entryModule: resolve(__dirname, "app/app.module#AppModule"), |
| 118 | + skipCodeGeneration: !aot, |
| 119 | + platformOptions: { |
| 120 | + platform, |
| 121 | + platforms, |
| 122 | + // ignore: ["App_Resources"] |
| 123 | + }, |
| 124 | + }, ngToolsWebpackOptions) |
| 125 | + ), |
| 126 | + // Does IPC communication with the {N} CLI to notify events when running in watch mode. |
| 127 | + new nsWebpack.WatchStateLoggerPlugin(), |
| 128 | + ], |
| 129 | + }; |
| 130 | + if (report) { |
| 131 | + // Generate report files for bundles content |
| 132 | + config.plugins.push(new BundleAnalyzerPlugin({ |
| 133 | + analyzerMode: "static", |
| 134 | + openAnalyzer: false, |
| 135 | + generateStatsFile: true, |
| 136 | + reportFilename: join(__dirname, "report", `report.html`), |
| 137 | + statsFilename: join(__dirname, "report", `stats.json`), |
| 138 | + })); |
| 139 | + } |
| 140 | + if (snapshot) { |
| 141 | + config.plugins.push(new nsWebpack.NativeScriptSnapshotPlugin({ |
| 142 | + chunk: "vendor", |
| 143 | + projectRoot: __dirname, |
| 144 | + webpackConfig: config, |
| 145 | + targetArchs: ["arm", "arm64", "ia32"], |
| 146 | + tnsJavaClassesOptions: { packages: ["tns-core-modules" ] }, |
| 147 | + useLibs: false |
| 148 | + })); |
| 149 | + } |
| 150 | + if (uglify) { |
| 151 | + config.plugins.push(new webpack.LoaderOptionsPlugin({ minimize: true })); |
| 152 | + |
| 153 | + // Work around an Android issue by setting compress = false |
| 154 | + const compress = platform !== "android"; |
| 155 | + config.plugins.push(new UglifyJsPlugin({ |
| 156 | + uglifyOptions: { |
| 157 | + mangle: { reserved: nsWebpack.uglifyMangleExcludes }, |
| 158 | + compress, |
| 159 | + } |
| 160 | + })); |
| 161 | + } |
| 162 | + return config; |
| 163 | +}; |
0 commit comments