webpack.config.js 6.4 KB

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