|
| 1 | +const webpack = require("webpack"), |
| 2 | + HtmlWebpackPlugin = require("html-webpack-plugin"), |
| 3 | + MiniCssExtractPlugin = require("mini-css-extract-plugin"), |
| 4 | + { merge } = require("webpack-merge"), |
| 5 | + path = require("path"); |
| 6 | + |
| 7 | +let production = process.env.npm_lifecycle_event && process.env.npm_lifecycle_event.indexOf("build") == 0; |
| 8 | + |
| 9 | +let common = { |
| 10 | + resolve: { |
| 11 | + // alias: { |
| 12 | + // cx: path.resolve(path.join(__dirname, "../packages/cx")), |
| 13 | + // }, |
| 14 | + extensions: [".js", ".ts", ".tsx", ".json"], |
| 15 | + }, |
| 16 | + |
| 17 | + module: { |
| 18 | + rules: [ |
| 19 | + { |
| 20 | + test: /\.json$/, |
| 21 | + loader: "json-loader", |
| 22 | + }, |
| 23 | + { |
| 24 | + test: /\.(ts|tsx)$/, |
| 25 | + include: /(litmus-ts)/, |
| 26 | + exclude: /node_modules/, |
| 27 | + use: [ |
| 28 | + { |
| 29 | + loader: "babel-loader", |
| 30 | + options: { |
| 31 | + plugins: [["transform-cx-imports", { useSrc: true }]], |
| 32 | + }, |
| 33 | + }, |
| 34 | + { |
| 35 | + loader: "ts-loader", |
| 36 | + options: { |
| 37 | + colors: false, |
| 38 | + logLevel: "info", |
| 39 | + }, |
| 40 | + }, |
| 41 | + ], |
| 42 | + }, |
| 43 | + ], |
| 44 | + }, |
| 45 | + entry: { |
| 46 | + app: [__dirname + "/index.tsx", __dirname + "/index.scss"], |
| 47 | + }, |
| 48 | + output: { |
| 49 | + path: __dirname, |
| 50 | + filename: "[name].js", |
| 51 | + }, |
| 52 | + plugins: [ |
| 53 | + //new webpack.optimize.CommonsChunkPlugin("vendor"), |
| 54 | + new HtmlWebpackPlugin({ |
| 55 | + template: path.join(__dirname, "index.html"), |
| 56 | + }), |
| 57 | + // new CxScssManifestPlugin({ |
| 58 | + // outputPath: path.join(__dirname, "manifest.scss") |
| 59 | + // }) |
| 60 | + ], |
| 61 | + stats: { |
| 62 | + usedExports: true, |
| 63 | + }, |
| 64 | + cache: { |
| 65 | + // 1. Set cache type to filesystem |
| 66 | + type: "filesystem", |
| 67 | + |
| 68 | + buildDependencies: { |
| 69 | + // 2. Add your config as buildDependency to get cache invalidation on config change |
| 70 | + config: [__filename], |
| 71 | + |
| 72 | + // 3. If you have other things the build depends on you can add them here |
| 73 | + // Note that webpack, loaders and all modules referenced from your config are automatically added |
| 74 | + }, |
| 75 | + }, |
| 76 | +}; |
| 77 | + |
| 78 | +let specific; |
| 79 | + |
| 80 | +if (production) { |
| 81 | + specific = { |
| 82 | + mode: "production", |
| 83 | + target: ["web", "es2022"], // Modern browsers for better tree-shaking |
| 84 | + module: { |
| 85 | + rules: [ |
| 86 | + { |
| 87 | + test: /\.scss$/, |
| 88 | + use: [ |
| 89 | + MiniCssExtractPlugin.loader, |
| 90 | + "css-loader", |
| 91 | + { |
| 92 | + loader: "sass-loader", |
| 93 | + options: { |
| 94 | + sassOptions: { |
| 95 | + quietDeps: true, |
| 96 | + silenceDeprecations: ["legacy-js-api", "import", "global-builtin"], |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + ], |
| 101 | + }, |
| 102 | + { |
| 103 | + test: /\.css$/, |
| 104 | + use: [MiniCssExtractPlugin.loader, "css-loader"], |
| 105 | + }, |
| 106 | + ], |
| 107 | + }, |
| 108 | + |
| 109 | + plugins: [ |
| 110 | + new webpack.DefinePlugin({ |
| 111 | + "process.env.NODE_ENV": JSON.stringify("production"), |
| 112 | + }), |
| 113 | + |
| 114 | + new MiniCssExtractPlugin({ |
| 115 | + filename: "app.css", |
| 116 | + }), |
| 117 | + ], |
| 118 | + |
| 119 | + output: { |
| 120 | + path: path.join(__dirname, "dist"), |
| 121 | + publicPath: ".", |
| 122 | + }, |
| 123 | + |
| 124 | + optimization: { |
| 125 | + usedExports: true, |
| 126 | + sideEffects: false, |
| 127 | + }, |
| 128 | + }; |
| 129 | +} else { |
| 130 | + specific = { |
| 131 | + module: { |
| 132 | + rules: [ |
| 133 | + { |
| 134 | + test: /\.scss$/, |
| 135 | + use: [ |
| 136 | + "style-loader", |
| 137 | + "css-loader", |
| 138 | + { |
| 139 | + loader: "sass-loader", |
| 140 | + options: { |
| 141 | + sassOptions: { |
| 142 | + quietDeps: true, |
| 143 | + silenceDeprecations: ["legacy-js-api", "import", "global-builtin"], |
| 144 | + }, |
| 145 | + }, |
| 146 | + }, |
| 147 | + ], |
| 148 | + }, |
| 149 | + { |
| 150 | + test: /\.css$/, |
| 151 | + use: ["style-loader", "css-loader"], |
| 152 | + }, |
| 153 | + ], |
| 154 | + }, |
| 155 | + mode: "development", |
| 156 | + //target: ["web", "es5"], //Uncomment for IE testing |
| 157 | + plugins: [ |
| 158 | + new webpack.DefinePlugin({ |
| 159 | + "process.env.NODE_ENV": JSON.stringify("development"), |
| 160 | + "process.env.NODE_DEBUG": JSON.stringify(false), |
| 161 | + }), |
| 162 | + ], |
| 163 | + output: { |
| 164 | + publicPath: "/", |
| 165 | + }, |
| 166 | + performance: { |
| 167 | + hints: false, |
| 168 | + }, |
| 169 | + //devtool: "eval", |
| 170 | + devServer: { |
| 171 | + //contentBase: "/", |
| 172 | + hot: true, |
| 173 | + port: 8091, |
| 174 | + //noInfo: false, |
| 175 | + //inline: true, |
| 176 | + historyApiFallback: true, |
| 177 | + //quiet: true |
| 178 | + }, |
| 179 | + }; |
| 180 | +} |
| 181 | + |
| 182 | +module.exports = merge(common, specific); |
0 commit comments