|
| 1 | +var webpack = require("webpack"); |
| 2 | +var nsWebpack = require("nativescript-dev-webpack"); |
| 3 | +var nativescriptTarget = require("nativescript-dev-webpack/nativescript-target"); |
| 4 | +var path = require("path"); |
| 5 | +var CopyWebpackPlugin = require("copy-webpack-plugin"); |
| 6 | +var ExtractTextPlugin = require("extract-text-webpack-plugin"); |
| 7 | +var AotPlugin = require("@ngtools/webpack").AotPlugin; |
| 8 | + |
| 9 | +module.exports = function (platform, destinationApp) { |
| 10 | + if (!destinationApp) { |
| 11 | + //Default destination inside platforms/<platform>/... |
| 12 | + destinationApp = nsWebpack.getAppPath(platform); |
| 13 | + } |
| 14 | + var entry = {}; |
| 15 | + //Discover entry module from package.json |
| 16 | + entry.bundle = "./" + nsWebpack.getEntryModule(); |
| 17 | + //Vendor entry with third party libraries. |
| 18 | + entry.vendor = "./vendor"; |
| 19 | + //app.css bundle |
| 20 | + entry["app.css"] = "./app.css"; |
| 21 | + |
| 22 | + var plugins = [ |
| 23 | + new ExtractTextPlugin("app.css"), |
| 24 | + //Vendor libs go to the vendor.js chunk |
| 25 | + new webpack.optimize.CommonsChunkPlugin({ |
| 26 | + name: ["vendor"] |
| 27 | + }), |
| 28 | + //Define useful constants like TNS_WEBPACK |
| 29 | + new webpack.DefinePlugin({ |
| 30 | + "global.TNS_WEBPACK": "true", |
| 31 | + }), |
| 32 | + //Copy assets to out dir. Add your own globs as needed. |
| 33 | + new CopyWebpackPlugin([ |
| 34 | + { from: "app.css" }, |
| 35 | + { from: "css/**" }, |
| 36 | + { from: "fonts/**" }, |
| 37 | + { from: "**/*.jpg" }, |
| 38 | + { from: "**/*.png" }, |
| 39 | + { from: "**/*.xml" }, |
| 40 | + ], { ignore: ["App_Resources/**"] }), |
| 41 | + //Generate a bundle starter script and activate it in package.json |
| 42 | + new nsWebpack.GenerateBundleStarterPlugin([ |
| 43 | + "./vendor", |
| 44 | + "./bundle", |
| 45 | + ]), |
| 46 | + |
| 47 | + //Angular AOT compiler |
| 48 | + new AotPlugin({ |
| 49 | + tsConfigPath: "tsconfig.aot.json", |
| 50 | + entryModule: path.resolve(__dirname, "app/app.module#AppModule"), |
| 51 | + typeChecking: false |
| 52 | + }), |
| 53 | + new nsWebpack.StyleUrlResolvePlugin({platform}), |
| 54 | + ]; |
| 55 | + |
| 56 | + if (process.env.npm_config_uglify) { |
| 57 | + plugins.push(new webpack.LoaderOptionsPlugin({ |
| 58 | + minimize: true |
| 59 | + })); |
| 60 | + |
| 61 | + //Work around an Android issue by setting compress = false |
| 62 | + var compress = platform !== "android"; |
| 63 | + plugins.push(new webpack.optimize.UglifyJsPlugin({ |
| 64 | + mangle: { |
| 65 | + except: nsWebpack.uglifyMangleExcludes, |
| 66 | + }, |
| 67 | + compress: compress, |
| 68 | + })); |
| 69 | + } |
| 70 | + |
| 71 | + return { |
| 72 | + context: path.resolve("./app"), |
| 73 | + target: nativescriptTarget, |
| 74 | + entry: entry, |
| 75 | + output: { |
| 76 | + pathinfo: true, |
| 77 | + path: path.resolve(destinationApp), |
| 78 | + libraryTarget: "commonjs2", |
| 79 | + filename: "[name].js", |
| 80 | + }, |
| 81 | + resolve: { |
| 82 | + //Resolve platform-specific modules like module.android.js |
| 83 | + extensions: [ |
| 84 | + ".aot.ts", |
| 85 | + ".ts", |
| 86 | + ".js", |
| 87 | + ".css", |
| 88 | + "." + platform + ".ts", |
| 89 | + "." + platform + ".js", |
| 90 | + "." + platform + ".css", |
| 91 | + ], |
| 92 | + //Resolve {N} system modules from tns-core-modules |
| 93 | + modules: [ |
| 94 | + "node_modules/tns-core-modules", |
| 95 | + "node_modules" |
| 96 | + ] |
| 97 | + }, |
| 98 | + node: { |
| 99 | + //Disable node shims that conflict with NativeScript |
| 100 | + "http": false, |
| 101 | + "timers": false, |
| 102 | + "setImmediate": false, |
| 103 | + "fs": "empty", |
| 104 | + }, |
| 105 | + module: { |
| 106 | + loaders: [ |
| 107 | + { |
| 108 | + test: /\.html$|\.xml$/, |
| 109 | + loaders: [ |
| 110 | + "raw-loader", |
| 111 | + ] |
| 112 | + }, |
| 113 | + // Root app.css file gets extracted with bundled dependencies |
| 114 | + { |
| 115 | + test: /app\.css$/, |
| 116 | + loader: ExtractTextPlugin.extract([ |
| 117 | + "resolve-url-loader", |
| 118 | + "nativescript-css-loader", |
| 119 | + "nativescript-dev-webpack/platform-css-loader", |
| 120 | + ]), |
| 121 | + }, |
| 122 | + // Other CSS files get bundled using the raw loader |
| 123 | + { |
| 124 | + test: /\.css$/, |
| 125 | + exclude: /app\.css$/, |
| 126 | + loaders: [ |
| 127 | + "raw-loader", |
| 128 | + ] |
| 129 | + }, |
| 130 | + // Compile TypeScript files with ahead-of-time compiler. |
| 131 | + { |
| 132 | + test: /\.ts$/, |
| 133 | + loaders: [ |
| 134 | + "nativescript-dev-webpack/tns-aot-loader", |
| 135 | + "@ngtools/webpack", |
| 136 | + ] |
| 137 | + }, |
| 138 | + // SASS support |
| 139 | + { |
| 140 | + test: /\.scss$/, |
| 141 | + loaders: [ |
| 142 | + "raw-loader", |
| 143 | + "resolve-url-loader", |
| 144 | + "sass-loader" |
| 145 | + ] |
| 146 | + }, |
| 147 | + ] |
| 148 | + }, |
| 149 | + plugins: plugins, |
| 150 | + }; |
| 151 | +}; |
0 commit comments