webpack.config.js 7.3 KB

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