|
| 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 | + |
| 10 | + |
| 11 | +const mainSheet = `app.css`; |
| 12 | + |
| 13 | +module.exports = env => { |
| 14 | + const platform = getPlatform(env); |
| 15 | + |
| 16 | + // Default destination inside platforms/<platform>/... |
| 17 | + const path = resolve(nsWebpack.getAppPath(platform)); |
| 18 | + |
| 19 | + const entry = { |
| 20 | + // Discover entry module from package.json |
| 21 | + bundle: `./${nsWebpack.getEntryModule()}`, |
| 22 | + |
| 23 | + // Vendor entry with third-party libraries |
| 24 | + vendor: `./vendor`, |
| 25 | + |
| 26 | + // Entry for stylesheet with global application styles |
| 27 | + [mainSheet]: `./${mainSheet}`, |
| 28 | + }; |
| 29 | + |
| 30 | + const rules = getRules(); |
| 31 | + const plugins = getPlugins(platform, env); |
| 32 | + const extensions = getExtensions(platform); |
| 33 | + |
| 34 | + const config = { |
| 35 | + context: resolve("./app"), |
| 36 | + target: nativescriptTarget, |
| 37 | + entry, |
| 38 | + output: { |
| 39 | + pathinfo: true, |
| 40 | + path, |
| 41 | + libraryTarget: "commonjs2", |
| 42 | + filename: "[name].js", |
| 43 | + }, |
| 44 | + resolve: { |
| 45 | + extensions, |
| 46 | + |
| 47 | + // Resolve {N} system modules from tns-core-modules |
| 48 | + modules: [ |
| 49 | + "node_modules/tns-core-modules", |
| 50 | + "node_modules", |
| 51 | + ], |
| 52 | + |
| 53 | + alias: { |
| 54 | + '~': resolve("./app") |
| 55 | + }, |
| 56 | + }, |
| 57 | + node: { |
| 58 | + // Disable node shims that conflict with NativeScript |
| 59 | + "http": false, |
| 60 | + "timers": false, |
| 61 | + "setImmediate": false, |
| 62 | + "fs": "empty", |
| 63 | + }, |
| 64 | + module: { rules }, |
| 65 | + plugins, |
| 66 | + }; |
| 67 | + |
| 68 | + if (env.snapshot) { |
| 69 | + plugins.push(new nsWebpack.NativeScriptSnapshotPlugin({ |
| 70 | + chunk: "vendor", |
| 71 | + projectRoot: __dirname, |
| 72 | + webpackConfig: config, |
| 73 | + targetArchs: ["arm", "arm64", "ia32"], |
| 74 | + tnsJavaClassesOptions: { packages: ["tns-core-modules" ] }, |
| 75 | + useLibs: false |
| 76 | + })); |
| 77 | + } |
| 78 | + |
| 79 | + return config; |
| 80 | +}; |
| 81 | + |
| 82 | + |
| 83 | +function getPlatform(env) { |
| 84 | + return env.android ? "android" : |
| 85 | + env.ios ? "ios" : |
| 86 | + () => { throw new Error("You need to provide a target platform!") }; |
| 87 | +} |
| 88 | + |
| 89 | +function getRules() { |
| 90 | + return [ |
| 91 | + { |
| 92 | + test: /\.html$|\.xml$/, |
| 93 | + use: [ |
| 94 | + "raw-loader", |
| 95 | + ] |
| 96 | + }, |
| 97 | + // Root stylesheet gets extracted with bundled dependencies |
| 98 | + { |
| 99 | + test: new RegExp(mainSheet), |
| 100 | + use: ExtractTextPlugin.extract([ |
| 101 | + { |
| 102 | + loader: "resolve-url-loader", |
| 103 | + options: { silent: true }, |
| 104 | + }, |
| 105 | + { |
| 106 | + loader: "nativescript-css-loader", |
| 107 | + options: { minimize: false } |
| 108 | + }, |
| 109 | + "nativescript-dev-webpack/platform-css-loader", |
| 110 | + ]), |
| 111 | + }, |
| 112 | + // Other CSS files get bundled using the raw loader |
| 113 | + { |
| 114 | + test: /\.css$/, |
| 115 | + exclude: new RegExp(mainSheet), |
| 116 | + use: [ |
| 117 | + "raw-loader", |
| 118 | + ] |
| 119 | + }, |
| 120 | + // SASS support |
| 121 | + { |
| 122 | + test: /\.scss$/, |
| 123 | + use: [ |
| 124 | + "raw-loader", |
| 125 | + "resolve-url-loader", |
| 126 | + "sass-loader", |
| 127 | + ] |
| 128 | + }, |
| 129 | + |
| 130 | + |
| 131 | + // Compile TypeScript files, replace templateUrl and styleUrls. |
| 132 | + { |
| 133 | + test: /\.ts$/, |
| 134 | + loaders: [ |
| 135 | + "awesome-typescript-loader", |
| 136 | + ] |
| 137 | + } |
| 138 | + |
| 139 | + ]; |
| 140 | +} |
| 141 | + |
| 142 | +function getPlugins(platform, env) { |
| 143 | + let plugins = [ |
| 144 | + new ExtractTextPlugin(mainSheet), |
| 145 | + |
| 146 | + // Vendor libs go to the vendor.js chunk |
| 147 | + new webpack.optimize.CommonsChunkPlugin({ |
| 148 | + name: ["vendor"], |
| 149 | + }), |
| 150 | + |
| 151 | + // Define useful constants like TNS_WEBPACK |
| 152 | + new webpack.DefinePlugin({ |
| 153 | + "global.TNS_WEBPACK": "true", |
| 154 | + }), |
| 155 | + |
| 156 | + // Copy assets to out dir. Add your own globs as needed. |
| 157 | + new CopyWebpackPlugin([ |
| 158 | + { from: mainSheet }, |
| 159 | + { from: "**/*.css" }, |
| 160 | + { from: "fonts/**" }, |
| 161 | + { from: "**/*.jpg" }, |
| 162 | + { from: "**/*.png" }, |
| 163 | + { from: "**/*.xml" }, |
| 164 | + ], { ignore: ["App_Resources/**"] }), |
| 165 | + |
| 166 | + // Generate a bundle starter script and activate it in package.json |
| 167 | + new nsWebpack.GenerateBundleStarterPlugin([ |
| 168 | + "./vendor", |
| 169 | + "./bundle", |
| 170 | + ]), |
| 171 | + |
| 172 | + // Generate report files for bundles content |
| 173 | + new BundleAnalyzerPlugin({ |
| 174 | + analyzerMode: "static", |
| 175 | + openAnalyzer: false, |
| 176 | + generateStatsFile: true, |
| 177 | + reportFilename: join(__dirname, "report", `report.html`), |
| 178 | + statsFilename: join(__dirname, "report", `stats.json`), |
| 179 | + }), |
| 180 | + ]; |
| 181 | + |
| 182 | + if (env.uglify) { |
| 183 | + plugins.push(new webpack.LoaderOptionsPlugin({ minimize: true })); |
| 184 | + |
| 185 | + // Work around an Android issue by setting compress = false |
| 186 | + const compress = platform !== "android"; |
| 187 | + plugins.push(new webpack.optimize.UglifyJsPlugin({ |
| 188 | + mangle: { except: nsWebpack.uglifyMangleExcludes }, |
| 189 | + compress, |
| 190 | + })); |
| 191 | + } |
| 192 | + |
| 193 | + return plugins; |
| 194 | +} |
| 195 | + |
| 196 | +// Resolve platform-specific modules like module.android.js |
| 197 | +function getExtensions(platform) { |
| 198 | + return Object.freeze([ |
| 199 | + `.${platform}.ts`, |
| 200 | + `.${platform}.js`, |
| 201 | + ".ts", |
| 202 | + ".js", |
| 203 | + ".css", |
| 204 | + `.${platform}.css`, |
| 205 | + ]); |
| 206 | +} |
0 commit comments