webpack.config.js 6.9 KB

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