|
| 1 | +const webpack = require('webpack'); |
| 2 | +const path = require('path'); |
| 3 | +const paths = require('./paths'); |
| 4 | +const HtmlWebpackPlugin = require('html-webpack-plugin'); |
| 5 | +const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin'); |
| 6 | +const CopyWebpackPlugin = require('copy-webpack-plugin'); |
| 7 | +const HappyPack = require('happypack'); |
| 8 | +const childProcess = require('child_process'); |
| 9 | +const WatchMissingNodeModulesPlugin = require('../scripts/utils/WatchMissingNodeModulesPlugin'); |
| 10 | +const env = require('./env'); |
| 11 | + |
| 12 | +const babelDev = require('./babel.dev'); |
| 13 | +const babelProd = require('./babel.prod'); |
| 14 | + |
| 15 | +const NODE_ENV = JSON.parse(env['process.env.NODE_ENV']); |
| 16 | +const __DEV__ = NODE_ENV === 'development'; // eslint-disable-line no-underscore-dangle |
| 17 | +const __PROD__ = NODE_ENV === 'production'; // eslint-disable-line no-underscore-dangle |
| 18 | +const babelConfig = __DEV__ ? babelDev : babelProd; |
| 19 | + |
| 20 | +const COMMIT_COUNT = childProcess |
| 21 | + .execSync('git rev-list --count HEAD') |
| 22 | + .toString(); |
| 23 | + |
| 24 | +const COMMIT_HASH = childProcess |
| 25 | + .execSync('git rev-parse --short HEAD') |
| 26 | + .toString(); |
| 27 | +const VERSION = `${COMMIT_COUNT}-${COMMIT_HASH}`; |
| 28 | + |
| 29 | +module.exports = { |
| 30 | + entry: { |
| 31 | + app: [require.resolve('./polyfills'), path.join(paths.appSrc, 'index.js')], |
| 32 | + sandbox: [ |
| 33 | + require.resolve('babel-polyfill'), |
| 34 | + require.resolve('./polyfills'), |
| 35 | + path.join(paths.sandboxSrc, 'index.js') |
| 36 | + ], |
| 37 | + embed: [ |
| 38 | + require.resolve('./polyfills'), |
| 39 | + path.join(paths.embedSrc, 'index.js') |
| 40 | + ], |
| 41 | + vendor: ['react', 'react-dom', 'styled-components'] |
| 42 | + }, |
| 43 | + target: 'web', |
| 44 | + node: { |
| 45 | + fs: 'empty', |
| 46 | + module: 'empty', |
| 47 | + child_process: 'empty' |
| 48 | + }, |
| 49 | + output: { |
| 50 | + path: paths.appBuild, |
| 51 | + pathinfo: true, |
| 52 | + publicPath: '/' |
| 53 | + }, |
| 54 | + |
| 55 | + module: { |
| 56 | + rules: [ |
| 57 | + { |
| 58 | + test: /create-zip\/.*\/files\/.*\.ico$/, |
| 59 | + loader: 'base64-loader' |
| 60 | + }, |
| 61 | + { |
| 62 | + test: /create-zip\/.*\/files\/.*$/, |
| 63 | + exclude: [/create-zip\/.*\/files\/.*\.ico$/], |
| 64 | + loader: 'raw-loader' |
| 65 | + }, |
| 66 | + { |
| 67 | + test: /\.js$/, |
| 68 | + include: paths.src, |
| 69 | + exclude: [ |
| 70 | + /eslint\.4\.1\.0\.min\.js$/, |
| 71 | + /typescriptServices\.js$/, |
| 72 | + // Don't do the node modules of the codesandbox module itself |
| 73 | + /codesandbox\/node_modules/, |
| 74 | + /create-zip\/.*\/files\/.*$/ |
| 75 | + ], |
| 76 | + loader: 'happypack/loader' |
| 77 | + }, |
| 78 | + // JSON is not enabled by default in Webpack but both Node and Browserify |
| 79 | + // allow it implicitly so we also enable it. |
| 80 | + { |
| 81 | + test: /\.json$/, |
| 82 | + loader: 'json-loader', |
| 83 | + exclude: [/create-zip\/.*\/files\/.*$/] |
| 84 | + }, |
| 85 | + // "postcss" loader applies autoprefixer to our CSS. |
| 86 | + // "css" loader resolves paths in CSS and adds assets as dependencies. |
| 87 | + // "style" loader turns CSS into JS modules that inject <style> tags. |
| 88 | + // In production, we use a plugin to extract that CSS to a file, but |
| 89 | + // in development "style" loader enables hot editing of CSS. |
| 90 | + { |
| 91 | + test: /\.css$/, |
| 92 | + loaders: ['style-loader', 'css-loader'], |
| 93 | + exclude: [/create-zip\/.*\/files\/.*$/] |
| 94 | + }, |
| 95 | + // For importing README.md |
| 96 | + { |
| 97 | + test: /\.md$/, |
| 98 | + loader: 'raw-loader', |
| 99 | + exclude: [/create-zip\/.*\/files\/.*$/] |
| 100 | + }, |
| 101 | + // "file" loader makes sure those assets get served by WebpackDevServer. |
| 102 | + // When you `import` an asset, you get its (virtual) filename. |
| 103 | + // In production, they would get copied to the `build` folder. |
| 104 | + { |
| 105 | + test: /\.(ico|jpg|png|gif|eot|otf|webp|svg|ttf|woff|woff2)(\?.*)?$/, |
| 106 | + exclude: [/\/favicon.ico$/, /create-zip\/.*\/files\/.*$/], |
| 107 | + loader: 'file-loader', |
| 108 | + options: { |
| 109 | + name: 'static/media/[name].[hash:8].[ext]' |
| 110 | + } |
| 111 | + }, |
| 112 | + // A special case for favicon.ico to place it into build root directory. |
| 113 | + { |
| 114 | + test: /\/favicon.ico$/, |
| 115 | + include: [paths.src], |
| 116 | + exclude: [/create-zip\/.*\/files\/.*$/], |
| 117 | + loader: 'file-loader', |
| 118 | + options: { |
| 119 | + name: 'favicon.ico?[hash:8]' |
| 120 | + } |
| 121 | + }, |
| 122 | + // "url" loader works just like "file" loader but it also embeds |
| 123 | + // assets smaller than specified size as data URLs to avoid requests. |
| 124 | + { |
| 125 | + test: /\.(mp4|webm)(\?.*)?$/, |
| 126 | + loader: 'url-loader', |
| 127 | + exclude: [/create-zip\/.*\/files\/.*$/], |
| 128 | + options: { |
| 129 | + limit: 10000, |
| 130 | + name: 'static/media/[name].[hash:8].[ext]' |
| 131 | + } |
| 132 | + }, |
| 133 | + // "html" loader is used to process template page (index.html) to resolve |
| 134 | + // resources linked with <link href="./relative/path"> HTML tags. |
| 135 | + { |
| 136 | + test: /\.html$/, |
| 137 | + loader: 'html-loader', |
| 138 | + exclude: [/create-zip\/.*\/files\/.*$/], |
| 139 | + options: { |
| 140 | + attrs: ['link:href'] |
| 141 | + } |
| 142 | + } |
| 143 | + ], |
| 144 | + |
| 145 | + noParse: [/eslint\.4\.1\.0\.min\.js$/, /typescriptServices\.js$/] |
| 146 | + }, |
| 147 | + |
| 148 | + resolve: { |
| 149 | + mainFields: ['browser', 'module', 'jsnext:main', 'main'], |
| 150 | + modules: ['src', 'node_modules'], |
| 151 | + |
| 152 | + extensions: ['.js', '.json'], |
| 153 | + |
| 154 | + alias: { |
| 155 | + moment: 'moment/moment.js' |
| 156 | + } |
| 157 | + }, |
| 158 | + |
| 159 | + plugins: [ |
| 160 | + new HappyPack({ |
| 161 | + loaders: [ |
| 162 | + { |
| 163 | + path: 'babel-loader', |
| 164 | + query: babelConfig |
| 165 | + } |
| 166 | + ] |
| 167 | + }), |
| 168 | + // Generates an `index.html` file with the <script> injected. |
| 169 | + new HtmlWebpackPlugin({ |
| 170 | + inject: true, |
| 171 | + chunks: ['vendor', 'common', 'app'], |
| 172 | + filename: 'app.html', |
| 173 | + template: paths.appHtml, |
| 174 | + minify: __PROD__ && { |
| 175 | + removeComments: true, |
| 176 | + collapseWhitespace: true, |
| 177 | + removeRedundantAttributes: true, |
| 178 | + useShortDoctype: true, |
| 179 | + removeEmptyAttributes: true, |
| 180 | + removeStyleLinkTypeAttributes: true, |
| 181 | + keepClosingSlash: true, |
| 182 | + minifyJS: true, |
| 183 | + minifyCSS: true, |
| 184 | + minifyURLs: true |
| 185 | + } |
| 186 | + }), |
| 187 | + new HtmlWebpackPlugin({ |
| 188 | + inject: true, |
| 189 | + chunks: ['vendor', 'common', 'sandbox'], |
| 190 | + filename: 'frame.html', |
| 191 | + template: paths.sandboxHtml, |
| 192 | + minify: __PROD__ && { |
| 193 | + removeComments: true, |
| 194 | + collapseWhitespace: true, |
| 195 | + removeRedundantAttributes: true, |
| 196 | + useShortDoctype: true, |
| 197 | + removeEmptyAttributes: true, |
| 198 | + removeStyleLinkTypeAttributes: true, |
| 199 | + keepClosingSlash: true, |
| 200 | + minifyJS: true, |
| 201 | + minifyCSS: true, |
| 202 | + minifyURLs: true |
| 203 | + } |
| 204 | + }), |
| 205 | + new HtmlWebpackPlugin({ |
| 206 | + inject: true, |
| 207 | + chunks: ['vendor', 'embed'], |
| 208 | + filename: 'embed.html', |
| 209 | + template: path.join(paths.embedSrc, 'index.html'), |
| 210 | + minify: __PROD__ && { |
| 211 | + removeComments: true, |
| 212 | + collapseWhitespace: true, |
| 213 | + removeRedundantAttributes: true, |
| 214 | + useShortDoctype: true, |
| 215 | + removeEmptyAttributes: true, |
| 216 | + removeStyleLinkTypeAttributes: true, |
| 217 | + keepClosingSlash: true, |
| 218 | + minifyJS: true, |
| 219 | + minifyCSS: true, |
| 220 | + minifyURLs: true |
| 221 | + } |
| 222 | + }), |
| 223 | + // Makes some environment variables available to the JS code, for example: |
| 224 | + // if (process.env.NODE_ENV === 'development') { ... }. See `env.js`. |
| 225 | + new webpack.DefinePlugin(env), |
| 226 | + new webpack.DefinePlugin({ VERSION: JSON.stringify(VERSION) }), |
| 227 | + // Watcher doesn't work well if you mistype casing in a path so we use |
| 228 | + // a plugin that prints an error when you attempt to do this. |
| 229 | + // See https://github.com/facebookincubator/create-react-app/issues/240 |
| 230 | + new CaseSensitivePathsPlugin(), |
| 231 | + // If you require a missing module and then `npm install` it, you still have |
| 232 | + // to restart the development server for Webpack to discover it. This plugin |
| 233 | + // makes the discovery automatic so you don't have to restart. |
| 234 | + // See https://github.com/facebookincubator/create-react-app/issues/186 |
| 235 | + new WatchMissingNodeModulesPlugin(paths.appNodeModules), |
| 236 | + // Make the monaco editor work |
| 237 | + new CopyWebpackPlugin([ |
| 238 | + { |
| 239 | + from: __DEV__ |
| 240 | + ? 'node_modules/monaco-editor/dev/vs' |
| 241 | + : 'node_modules/monaco-editor/min/vs', |
| 242 | + to: 'public/vs' |
| 243 | + }, |
| 244 | + { |
| 245 | + from: 'static', |
| 246 | + to: 'static' |
| 247 | + }, |
| 248 | + { |
| 249 | + from: 'src/homepage/static', |
| 250 | + to: 'static' |
| 251 | + } |
| 252 | + ]), |
| 253 | + // Try to dedupe duplicated modules, if any: |
| 254 | + new webpack.optimize.CommonsChunkPlugin({ |
| 255 | + name: 'common', |
| 256 | + chunks: ['app', 'sandbox'] |
| 257 | + }), |
| 258 | + new webpack.optimize.CommonsChunkPlugin({ |
| 259 | + name: 'vendor', |
| 260 | + minChunks: Infinity |
| 261 | + }), |
| 262 | + new webpack.optimize.CommonsChunkPlugin({ |
| 263 | + async: true, |
| 264 | + children: true, |
| 265 | + minChunks: 2 |
| 266 | + }), |
| 267 | + new webpack.NamedModulesPlugin() |
| 268 | + ] |
| 269 | +}; |
0 commit comments