webpack.config.js 6.9 KB

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