webpack.config.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /**
  2. * React Static Boilerplate
  3. * https://github.com/koistya/react-static-boilerplate
  4. *
  5. * Copyright © 2015-2016 Konstantin Tarkus (@koistya)
  6. *
  7. * This source code is licensed under the MIT license found in the
  8. * LICENSE.txt file in the root directory of this source tree.
  9. */
  10. const path = require('path');
  11. const webpack = require('webpack');
  12. const extend = require('extend');
  13. const DEBUG = !(process.argv.slice(2) == '--release');
  14. const VERBOSE = process.argv.slice(2) == '--verbose';
  15. const AUTOPREFIXER_BROWSERS = [
  16. 'Android 2.3',
  17. 'Android >= 4',
  18. 'Chrome >= 35',
  19. 'Firefox >= 31',
  20. 'Explorer >= 9',
  21. 'iOS >= 7',
  22. 'Opera >= 12',
  23. 'Safari >= 7.1',
  24. ];
  25. /**
  26. * Webpack configuration (core/main.js => build/bundle.js)
  27. * http://webpack.github.io/docs/configuration.html
  28. */
  29. const config = {
  30. // The base directory
  31. context: path.resolve(__dirname, '../src'),
  32. // The entry point for the bundle
  33. entry: [
  34. 'es5-shim',
  35. 'es5-shim/es5-sham',
  36. 'babel-polyfill',
  37. './main.js'
  38. ],
  39. // Options affecting the output of the compilation
  40. output: {
  41. path: path.resolve(__dirname, '../build/assets'),
  42. publicPath: '/assets/',
  43. file: 'build/[name].js',
  44. sourcePrefix: ' ',
  45. },
  46. // Switch loaders to debug or release mode
  47. debug: DEBUG,
  48. cache: DEBUG,
  49. // Developer tool to enhance debugging, source maps
  50. // http://webpack.github.io/docs/configuration.html#devtool
  51. devtool: DEBUG ? 'source-map' : false,
  52. // What information should be printed to the console
  53. stats: {
  54. colors: true,
  55. reasons: DEBUG,
  56. hash: VERBOSE,
  57. version: VERBOSE,
  58. timings: true,
  59. chunks: VERBOSE,
  60. chunkModules: VERBOSE,
  61. cached: VERBOSE,
  62. cachedAssets: VERBOSE,
  63. },
  64. // The list of plugins for Webpack compiler
  65. plugins: [
  66. new webpack.optimize.OccurenceOrderPlugin(),
  67. new webpack.DefinePlugin({
  68. 'process.env.NODE_ENV': DEBUG ? '"development"' : '"production"',
  69. __DEV__: DEBUG,
  70. }),
  71. ],
  72. // Options affecting the normal modules
  73. module: {
  74. loaders: [
  75. {
  76. test: /\.jsx?$/,
  77. include: [
  78. path.resolve(__dirname, '../src'),
  79. ],
  80. loader: 'babel-loader',
  81. query: {
  82. // https://github.com/babel/babel-loader#options
  83. cacheDirectory: DEBUG,
  84. // https://babeljs.io/docs/usage/options/
  85. babelrc: false,
  86. presets: [
  87. 'react',
  88. 'es2015-loose',
  89. 'stage-1',
  90. ],
  91. plugins: [
  92. 'transform-runtime',
  93. ...DEBUG ? [] : [
  94. 'transform-react-remove-prop-types',
  95. 'transform-react-constant-elements',
  96. 'transform-react-inline-elements',
  97. ],
  98. ],
  99. },
  100. },
  101. {
  102. test: /\.css/,
  103. loaders: [
  104. 'style-loader',
  105. `css-loader?${JSON.stringify({
  106. sourceMap: DEBUG,
  107. // CSS Modules https://github.com/css-modules/css-modules
  108. modules: true,
  109. localIdentName: DEBUG ? '[name]_[local]_[hash:base64:3]' : '[hash:base64:4]',
  110. // CSS Nano http://cssnano.co/options/
  111. minimize: !DEBUG,
  112. })}`,
  113. 'postcss-loader?pack=default',
  114. ],
  115. },
  116. {
  117. test: /\.scss$/,
  118. loaders: [
  119. 'style-loader',
  120. `css-loader?${JSON.stringify({ sourceMap: DEBUG, minimize: !DEBUG })}`,
  121. 'postcss-loader?pack=sass',
  122. 'sass-loader',
  123. ],
  124. },
  125. {
  126. test: /\.json$/,
  127. loader: 'json-loader',
  128. },
  129. {
  130. test: /\.(png|jpg|jpeg|gif|svg|woff|woff2)$/,
  131. loader: 'url-loader',
  132. query: {
  133. name: DEBUG ? '[path][name].[ext]?[hash]' : '[hash].[ext]',
  134. limit: 10000,
  135. },
  136. },
  137. {
  138. test: /\.(eot|ttf|wav|mp3)$/,
  139. loader: 'file-loader',
  140. query: {
  141. name: DEBUG ? '[path][name].[ext]?[hash]' : '[hash].[ext]',
  142. },
  143. },
  144. ]
  145. },
  146. // The list of plugins for PostCSS
  147. // https://github.com/postcss/postcss
  148. postcss(bundler) {
  149. return {
  150. default: [
  151. // Transfer @import rule by inlining content, e.g. @import 'normalize.css'
  152. // https://github.com/postcss/postcss-import
  153. require('postcss-import')({ addDependencyTo: bundler }),
  154. // W3C variables, e.g. :root { --color: red; } div { background: var(--color); }
  155. // https://github.com/postcss/postcss-custom-properties
  156. require('postcss-custom-properties')(),
  157. // W3C CSS Custom Media Queries, e.g. @custom-media --small-viewport (max-width: 30em);
  158. // https://github.com/postcss/postcss-custom-media
  159. require('postcss-custom-media')(),
  160. // CSS4 Media Queries, e.g. @media screen and (width >= 500px) and (width <= 1200px) { }
  161. // https://github.com/postcss/postcss-media-minmax
  162. require('postcss-media-minmax')(),
  163. // W3C CSS Custom Selectors, e.g. @custom-selector :--heading h1, h2, h3, h4, h5, h6;
  164. // https://github.com/postcss/postcss-custom-selectors
  165. require('postcss-custom-selectors')(),
  166. // W3C calc() function, e.g. div { height: calc(100px - 2em); }
  167. // https://github.com/postcss/postcss-calc
  168. require('postcss-calc')(),
  169. // Allows you to nest one style rule inside another
  170. // https://github.com/jonathantneal/postcss-nesting
  171. require('postcss-nesting')(),
  172. // W3C color() function, e.g. div { background: color(red alpha(90%)); }
  173. // https://github.com/postcss/postcss-color-function
  174. require('postcss-color-function')(),
  175. // Convert CSS shorthand filters to SVG equivalent, e.g. .blur { filter: blur(4px); }
  176. // https://github.com/iamvdo/pleeease-filters
  177. require('pleeease-filters')(),
  178. // Generate pixel fallback for "rem" units, e.g. div { margin: 2.5rem 2px 3em 100%; }
  179. // https://github.com/robwierzbowski/node-pixrem
  180. require('pixrem')(),
  181. // W3C CSS Level4 :matches() pseudo class, e.g. p:matches(:first-child, .special) { }
  182. // https://github.com/postcss/postcss-selector-matches
  183. require('postcss-selector-matches')(),
  184. // Transforms :not() W3C CSS Level 4 pseudo class to :not() CSS Level 3 selectors
  185. // https://github.com/postcss/postcss-selector-not
  186. require('postcss-selector-not')(),
  187. // Add vendor prefixes to CSS rules using values from caniuse.com
  188. // https://github.com/postcss/autoprefixer
  189. require('autoprefixer')({ browsers: AUTOPREFIXER_BROWSERS }),
  190. ],
  191. sass: [
  192. require('autoprefixer')({ browsers: AUTOPREFIXER_BROWSERS }),
  193. ],
  194. };
  195. },
  196. };
  197. // Optimize the bundle in release (production) mode
  198. if (!DEBUG) {
  199. config.plugins.push(new webpack.optimize.DedupePlugin());
  200. config.plugins.push(new webpack.optimize.UglifyJsPlugin({ compress: { warnings: VERBOSE } }));
  201. config.plugins.push(new webpack.optimize.AggressiveMergingPlugin());
  202. }
  203. module.exports = config;