webpack.config.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * React Static Boilerplate
  3. * https://github.com/koistya/react-static-boilerplate
  4. * Copyright (c) Konstantin Tarkus (@koistya) | MIT license
  5. */
  6. import path from 'path';
  7. import webpack from 'webpack';
  8. import merge from 'lodash.merge';
  9. const DEBUG = !process.argv.includes('release');
  10. const VERBOSE = process.argv.includes('verbose');
  11. const WATCH = global.watch;
  12. const AUTOPREFIXER_BROWSERS = [
  13. 'Android 2.3',
  14. 'Android >= 4',
  15. 'Chrome >= 35',
  16. 'Firefox >= 31',
  17. 'Explorer >= 9',
  18. 'iOS >= 7',
  19. 'Opera >= 12',
  20. 'Safari >= 7.1',
  21. ];
  22. const JS_LOADER = {
  23. test: /\.jsx?$/,
  24. include: [
  25. path.resolve(__dirname, '../components'),
  26. path.resolve(__dirname, '../core'),
  27. path.resolve(__dirname, '../pages'),
  28. path.resolve(__dirname, '../app.js'),
  29. path.resolve(__dirname, '../config.js'),
  30. ],
  31. loader: 'babel-loader',
  32. };
  33. // Base configuration
  34. const config = {
  35. output: {
  36. path: path.join(__dirname, '../build'),
  37. publicPath: '/',
  38. sourcePrefix: ' ',
  39. },
  40. cache: false,
  41. debug: DEBUG,
  42. stats: {
  43. colors: true,
  44. reasons: DEBUG,
  45. hash: VERBOSE,
  46. version: VERBOSE,
  47. timings: true,
  48. chunks: VERBOSE,
  49. chunkModules: VERBOSE,
  50. cached: VERBOSE,
  51. cachedAssets: VERBOSE,
  52. },
  53. plugins: [
  54. new webpack.optimize.OccurenceOrderPlugin(),
  55. new webpack.DefinePlugin({
  56. 'process.env.NODE_ENV': DEBUG ? '"development"' : '"production"',
  57. '__DEV__': DEBUG,
  58. }),
  59. ],
  60. module: {
  61. loaders: [
  62. {
  63. test: /[\\\/]app\.js$/,
  64. loader: path.join(__dirname, './lib/routes-loader.js'),
  65. }, {
  66. test: /\.json$/,
  67. loader: 'json-loader',
  68. }, {
  69. test: /\.txt$/,
  70. loader: 'raw-loader',
  71. }, {
  72. test: /\.(png|jpg|jpeg|gif|svg|woff|woff2)$/,
  73. loader: 'url-loader?limit=10000',
  74. }, {
  75. test: /\.(eot|ttf|wav|mp3)$/,
  76. loader: 'file-loader',
  77. },
  78. ],
  79. },
  80. postcss: function plugins(bundler) {
  81. return [
  82. require('postcss-import')({ addDependencyTo: bundler }),
  83. require('precss')(),
  84. require('autoprefixer')({
  85. browsers: AUTOPREFIXER_BROWSERS,
  86. }),
  87. ];
  88. },
  89. };
  90. // Configuration for the client-side bundle
  91. const appConfig = merge({}, config, {
  92. entry: [
  93. ...(WATCH ? ['webpack-hot-middleware/client'] : []),
  94. './app.js',
  95. ],
  96. output: {
  97. filename: 'app.js',
  98. },
  99. // http://webpack.github.io/docs/configuration.html#devtool
  100. devtool: DEBUG ? 'cheap-module-eval-source-map' : false,
  101. plugins: [
  102. ...config.plugins,
  103. ...(DEBUG ? [] : [
  104. new webpack.optimize.DedupePlugin(),
  105. new webpack.optimize.UglifyJsPlugin({
  106. compress: {
  107. warnings: VERBOSE,
  108. },
  109. }),
  110. new webpack.optimize.AggressiveMergingPlugin(),
  111. ]),
  112. ...(WATCH ? [
  113. new webpack.HotModuleReplacementPlugin(),
  114. new webpack.NoErrorsPlugin(),
  115. ] : []),
  116. ],
  117. module: {
  118. loaders: [
  119. WATCH ? Object.assign({}, JS_LOADER, {
  120. query: {
  121. // Wraps all React components into arbitrary transforms
  122. // https://github.com/gaearon/babel-plugin-react-transform
  123. plugins: ['react-transform'],
  124. extra: {
  125. 'react-transform': {
  126. transforms: [
  127. {
  128. transform: 'react-transform-hmr',
  129. imports: ['react'],
  130. locals: ['module'],
  131. }, {
  132. transform: 'react-transform-catch-errors',
  133. imports: ['react', 'redbox-react'],
  134. },
  135. ],
  136. },
  137. },
  138. },
  139. }) : JS_LOADER,
  140. ...config.module.loaders,
  141. {
  142. test: /\.scss$/,
  143. loaders: ['style-loader', 'css-loader', 'postcss-loader'],
  144. },
  145. ],
  146. },
  147. });
  148. // Configuration for server-side pre-rendering bundle
  149. const pagesConfig = merge({}, config, {
  150. entry: './app.js',
  151. output: {
  152. filename: 'app.node.js',
  153. libraryTarget: 'commonjs2',
  154. },
  155. target: 'node',
  156. node: {
  157. console: false,
  158. global: false,
  159. process: false,
  160. Buffer: false,
  161. __filename: false,
  162. __dirname: false,
  163. },
  164. externals: /^[a-z][a-z\.\-\/0-9]*$/i,
  165. plugins: config.plugins.concat([
  166. new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }),
  167. ]),
  168. module: {
  169. loaders: [
  170. JS_LOADER,
  171. ...config.module.loaders,
  172. {
  173. test: /\.scss$/,
  174. loaders: ['css-loader', 'postcss-loader'],
  175. },
  176. ],
  177. },
  178. });
  179. export default [appConfig, pagesConfig];