webpack.config.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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' || process.argv.slice(2) == '-r');
  14. const VERBOSE = process.argv.slice(2) == '--verbose' || process.argv.slice(2) == '-v';
  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. // Developer tool to enhance debugging, source maps
  49. // http://webpack.github.io/docs/configuration.html#devtool
  50. devtool: DEBUG ? 'source-map' : false,
  51. // What information should be printed to the console
  52. stats: {
  53. colors: true,
  54. reasons: DEBUG,
  55. hash: VERBOSE,
  56. version: VERBOSE,
  57. timings: true,
  58. chunks: VERBOSE,
  59. chunkModules: VERBOSE,
  60. cached: VERBOSE,
  61. cachedAssets: VERBOSE,
  62. },
  63. // The list of plugins for Webpack compiler
  64. plugins: [
  65. new webpack.optimize.OccurenceOrderPlugin(),
  66. new webpack.DefinePlugin({
  67. 'process.env.NODE_ENV': DEBUG ? '"development"' : '"production"',
  68. __DEV__: DEBUG,
  69. }),
  70. ],
  71. // Options affecting the normal modules
  72. module: {
  73. loaders: [
  74. {
  75. test: /\.jsx?$/,
  76. include: [
  77. path.resolve(__dirname, '../src/components'),
  78. path.resolve(__dirname, '../src/containers'),
  79. path.resolve(__dirname, '../src/layouts'),
  80. path.resolve(__dirname, '../src/routes'),
  81. path.resolve(__dirname, '../src/store'),
  82. path.resolve(__dirname, '../src/main'),
  83. ],
  84. loader: 'babel-loader',
  85. query: {
  86. // https://github.com/babel/babel-loader#options
  87. cacheDirectory: DEBUG,
  88. // https://babeljs.io/docs/usage/options/
  89. babelrc: false,
  90. presets: [
  91. 'react',
  92. 'es2015',
  93. 'stage-0',
  94. ],
  95. plugins: [
  96. 'transform-runtime',
  97. ...DEBUG ? [] : [
  98. 'transform-react-remove-prop-types',
  99. 'transform-react-constant-elements',
  100. 'transform-react-inline-elements',
  101. ],
  102. ],
  103. },
  104. },
  105. {
  106. test: /\.css/,
  107. loaders: [
  108. 'style-loader',
  109. `css-loader?${JSON.stringify({
  110. sourceMap: DEBUG,
  111. // CSS Modules https://github.com/css-modules/css-modules
  112. modules: true,
  113. localIdentName: DEBUG ? '[name]_[local]_[hash:base64:3]' : '[hash:base64:4]',
  114. // CSS Nano http://cssnano.co/options/
  115. minimize: !DEBUG,
  116. })}`,
  117. 'postcss-loader?pack=default',
  118. ],
  119. },
  120. {
  121. test: /\.scss$/,
  122. loaders: [
  123. 'style-loader',
  124. `css-loader?${JSON.stringify({ sourceMap: DEBUG, minimize: !DEBUG })}`,
  125. 'postcss-loader?pack=sass',
  126. 'sass-loader',
  127. ],
  128. },
  129. {
  130. test: /\.json$/,
  131. loader: 'json-loader',
  132. },
  133. {
  134. test: /\.md$/,
  135. loader: path.resolve(__dirname, './webpack.markdown-loader.js'),
  136. },
  137. {
  138. test: /\.(png|jpg|jpeg|gif|svg|woff|woff2)$/,
  139. loader: 'url-loader?limit=10000',
  140. },
  141. {
  142. test: /\.(eot|ttf|wav|mp3)$/,
  143. loader: 'file-loader',
  144. },
  145. ],
  146. postLoaders: [
  147. {
  148. test: /\.js$/,
  149. loaders: ['es3ify-loader']
  150. }
  151. ]
  152. },
  153. // The list of plugins for PostCSS
  154. // https://github.com/postcss/postcss
  155. postcss(bundler) {
  156. return {
  157. default: [
  158. // Transfer @import rule by inlining content, e.g. @import 'normalize.css'
  159. // https://github.com/postcss/postcss-import
  160. require('postcss-import')({ addDependencyTo: bundler }),
  161. // W3C variables, e.g. :root { --color: red; } div { background: var(--color); }
  162. // https://github.com/postcss/postcss-custom-properties
  163. require('postcss-custom-properties')(),
  164. // W3C CSS Custom Media Queries, e.g. @custom-media --small-viewport (max-width: 30em);
  165. // https://github.com/postcss/postcss-custom-media
  166. require('postcss-custom-media')(),
  167. // CSS4 Media Queries, e.g. @media screen and (width >= 500px) and (width <= 1200px) { }
  168. // https://github.com/postcss/postcss-media-minmax
  169. require('postcss-media-minmax')(),
  170. // W3C CSS Custom Selectors, e.g. @custom-selector :--heading h1, h2, h3, h4, h5, h6;
  171. // https://github.com/postcss/postcss-custom-selectors
  172. require('postcss-custom-selectors')(),
  173. // W3C calc() function, e.g. div { height: calc(100px - 2em); }
  174. // https://github.com/postcss/postcss-calc
  175. require('postcss-calc')(),
  176. // Allows you to nest one style rule inside another
  177. // https://github.com/jonathantneal/postcss-nesting
  178. require('postcss-nesting')(),
  179. // W3C color() function, e.g. div { background: color(red alpha(90%)); }
  180. // https://github.com/postcss/postcss-color-function
  181. require('postcss-color-function')(),
  182. // Convert CSS shorthand filters to SVG equivalent, e.g. .blur { filter: blur(4px); }
  183. // https://github.com/iamvdo/pleeease-filters
  184. require('pleeease-filters')(),
  185. // Generate pixel fallback for "rem" units, e.g. div { margin: 2.5rem 2px 3em 100%; }
  186. // https://github.com/robwierzbowski/node-pixrem
  187. require('pixrem')(),
  188. // W3C CSS Level4 :matches() pseudo class, e.g. p:matches(:first-child, .special) { }
  189. // https://github.com/postcss/postcss-selector-matches
  190. require('postcss-selector-matches')(),
  191. // Transforms :not() W3C CSS Level 4 pseudo class to :not() CSS Level 3 selectors
  192. // https://github.com/postcss/postcss-selector-not
  193. require('postcss-selector-not')(),
  194. // Add vendor prefixes to CSS rules using values from caniuse.com
  195. // https://github.com/postcss/autoprefixer
  196. require('autoprefixer')({ browsers: AUTOPREFIXER_BROWSERS }),
  197. ],
  198. sass: [
  199. require('autoprefixer')({ browsers: AUTOPREFIXER_BROWSERS }),
  200. ],
  201. };
  202. },
  203. };
  204. // Optimize the bundle in release (production) mode
  205. if (!DEBUG) {
  206. config.plugins.push(new webpack.optimize.DedupePlugin());
  207. config.plugins.push(new webpack.optimize.UglifyJsPlugin({ compress: { warnings: VERBOSE } }));
  208. config.plugins.push(new webpack.optimize.AggressiveMergingPlugin());
  209. }
  210. module.exports = config;