|
| 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 | + |
| 9 | +const { AotPlugin } = require("@ngtools/webpack"); |
| 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 | + return { |
| 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 | + node: { |
| 54 | + // Disable node shims that conflict with NativeScript |
| 55 | + "http": false, |
| 56 | + "timers": false, |
| 57 | + "setImmediate": false, |
| 58 | + "fs": "empty", |
| 59 | + }, |
| 60 | + module: { rules }, |
| 61 | + plugins, |
| 62 | + }; |
| 63 | +}; |
| 64 | + |
| 65 | + |
| 66 | +function getPlatform(env) { |
| 67 | + return env.android ? "android" : |
| 68 | + env.ios ? "ios" : |
| 69 | + () => { throw new Error("You need to provide a target platform!") }; |
| 70 | +} |
| 71 | + |
| 72 | +function getRules() { |
| 73 | + return [ |
| 74 | + { |
| 75 | + test: /\.html$|\.xml$/, |
| 76 | + use: [ |
| 77 | + "raw-loader", |
| 78 | + ] |
| 79 | + }, |
| 80 | + // Root stylesheet gets extracted with bundled dependencies |
| 81 | + { |
| 82 | + test: new RegExp(mainSheet), |
| 83 | + use: ExtractTextPlugin.extract([ |
| 84 | + { |
| 85 | + loader: "resolve-url-loader", |
| 86 | + options: { silent: true }, |
| 87 | + }, |
| 88 | + "nativescript-css-loader", |
| 89 | + "nativescript-dev-webpack/platform-css-loader", |
| 90 | + ]), |
| 91 | + }, |
| 92 | + // Other CSS files get bundled using the raw loader |
| 93 | + { |
| 94 | + test: /\.css$/, |
| 95 | + exclude: new RegExp(mainSheet), |
| 96 | + use: [ |
| 97 | + "raw-loader", |
| 98 | + ] |
| 99 | + }, |
| 100 | + // SASS support |
| 101 | + { |
| 102 | + test: /\.scss$/, |
| 103 | + use: [ |
| 104 | + "raw-loader", |
| 105 | + "resolve-url-loader", |
| 106 | + "sass-loader", |
| 107 | + ] |
| 108 | + }, |
| 109 | + |
| 110 | + |
| 111 | + // Compile TypeScript files with ahead-of-time compiler. |
| 112 | + { |
| 113 | + test: /\.ts$/, |
| 114 | + loaders: [ |
| 115 | + "nativescript-dev-webpack/tns-aot-loader", |
| 116 | + "@ngtools/webpack", |
| 117 | + ] |
| 118 | + } |
| 119 | + |
| 120 | + ]; |
| 121 | +} |
| 122 | + |
| 123 | +function getPlugins(platform, env) { |
| 124 | + let plugins = [ |
| 125 | + new ExtractTextPlugin(mainSheet), |
| 126 | + |
| 127 | + // Vendor libs go to the vendor.js chunk |
| 128 | + new webpack.optimize.CommonsChunkPlugin({ |
| 129 | + name: ["vendor"], |
| 130 | + }), |
| 131 | + |
| 132 | + // Define useful constants like TNS_WEBPACK |
| 133 | + new webpack.DefinePlugin({ |
| 134 | + "global.TNS_WEBPACK": "true", |
| 135 | + }), |
| 136 | + |
| 137 | + // Copy assets to out dir. Add your own globs as needed. |
| 138 | + new CopyWebpackPlugin([ |
| 139 | + { from: mainSheet }, |
| 140 | + { from: "css/**" }, |
| 141 | + { from: "fonts/**" }, |
| 142 | + { from: "**/*.jpg" }, |
| 143 | + { from: "**/*.png" }, |
| 144 | + { from: "**/*.xml" }, |
| 145 | + ], { ignore: ["App_Resources/**"] }), |
| 146 | + |
| 147 | + // Generate a bundle starter script and activate it in package.json |
| 148 | + new nsWebpack.GenerateBundleStarterPlugin([ |
| 149 | + "./vendor", |
| 150 | + "./bundle", |
| 151 | + ]), |
| 152 | + |
| 153 | + // Angular AOT compiler |
| 154 | + new AotPlugin({ |
| 155 | + tsConfigPath: "tsconfig.aot.json", |
| 156 | + entryModule: resolve(__dirname, "app/app.module#AppModule"), |
| 157 | + typeChecking: false |
| 158 | + }), |
| 159 | + |
| 160 | + // Resolve .ios.css and .android.css component stylesheets |
| 161 | + new nsWebpack.StyleUrlResolvePlugin({platform}), |
| 162 | + |
| 163 | + ]; |
| 164 | + |
| 165 | + if (env.uglify) { |
| 166 | + plugins.push(new webpack.LoaderOptionsPlugin({ minimize: true })); |
| 167 | + |
| 168 | + // Work around an Android issue by setting compress = false |
| 169 | + const compress = platform !== "android"; |
| 170 | + plugins.push(new webpack.optimize.UglifyJsPlugin({ |
| 171 | + mangle: { except: nsWebpack.uglifyMangleExcludes }, |
| 172 | + compress, |
| 173 | + })); |
| 174 | + } |
| 175 | + |
| 176 | + return plugins; |
| 177 | +} |
| 178 | + |
| 179 | +// Resolve platform-specific modules like module.android.js |
| 180 | +function getExtensions(platform) { |
| 181 | + return Object.freeze([ |
| 182 | + `.${platform}.ts`, |
| 183 | + `.${platform}.js`, |
| 184 | + ".aot.ts", |
| 185 | + ".ts", |
| 186 | + ".js", |
| 187 | + ".css", |
| 188 | + `.${platform}.css`, |
| 189 | + ]); |
| 190 | +} |
0 commit comments