webpack.config.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * React Starter Kit (https://www.reactstarterkit.com/)
  3. *
  4. * Copyright © 2014-2016 Kriasoft, LLC. All rights reserved.
  5. *
  6. * This source code is licensed under the MIT license found in the
  7. * LICENSE.txt file in the root directory of this source tree.
  8. */
  9. import path from 'path';
  10. import webpack from 'webpack';
  11. import extend from 'extend';
  12. import AssetsPlugin from 'assets-webpack-plugin';
  13. const DEBUG = !process.argv.includes('--release');
  14. const VERBOSE = process.argv.includes('--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. const GLOBALS = {
  26. 'process.env.NODE_ENV': DEBUG ? '"development"' : '"production"',
  27. __DEV__: DEBUG,
  28. };
  29. //
  30. // Common configuration chunk to be used for both
  31. // client-side (client.js) and server-side (server.js) bundles
  32. // -----------------------------------------------------------------------------
  33. const config = {
  34. output: {
  35. publicPath: '/',
  36. sourcePrefix: ' ',
  37. },
  38. cache: DEBUG,
  39. debug: DEBUG,
  40. stats: {
  41. colors: true,
  42. reasons: DEBUG,
  43. hash: VERBOSE,
  44. version: VERBOSE,
  45. timings: true,
  46. chunks: VERBOSE,
  47. chunkModules: VERBOSE,
  48. cached: VERBOSE,
  49. cachedAssets: VERBOSE,
  50. },
  51. plugins: [
  52. new webpack.optimize.OccurenceOrderPlugin(),
  53. ],
  54. resolve: {
  55. extensions: ['', '.webpack.js', '.web.js', '.js', '.jsx', '.json'],
  56. },
  57. module: {
  58. loaders: [
  59. {
  60. test: /\.jsx?$/,
  61. include: [
  62. path.resolve(__dirname, '../node_modules/react-routing/src'),
  63. path.resolve(__dirname, '../src'),
  64. ],
  65. loader: 'babel-loader',
  66. }, {
  67. test: /\.scss$/,
  68. loaders: [
  69. 'isomorphic-style-loader',
  70. `css-loader?${DEBUG ? 'sourceMap&' : 'minimize&'}modules&localIdentName=` +
  71. `${DEBUG ? '[name]_[local]_[hash:base64:3]' : '[hash:base64:4]'}`,
  72. 'postcss-loader?parser=postcss-scss',
  73. ],
  74. }, {
  75. test: /\.json$/,
  76. loader: 'json-loader',
  77. }, {
  78. test: /\.txt$/,
  79. loader: 'raw-loader',
  80. }, {
  81. test: /\.(png|jpg|jpeg|gif|woff|woff2)$/,
  82. loader: 'url-loader?limit=10000',
  83. }, {
  84. test: /\.(eot|ttf|wav|mp3)$/,
  85. loader: 'file-loader',
  86. }, {
  87. test: /\.jade$/,
  88. loader: 'jade-loader',
  89. },
  90. { test: /\.svg$/, loader: 'babel?presets[]=es2015,presets[]=react!svg-react' }
  91. ],
  92. },
  93. postcss: function plugins(bundler) {
  94. return [
  95. require('postcss-import')({ addDependencyTo: bundler }),
  96. require('precss')(),
  97. require('autoprefixer')({ browsers: AUTOPREFIXER_BROWSERS }),
  98. ];
  99. },
  100. };
  101. //
  102. // Configuration for the client-side bundle (client.js)
  103. // -----------------------------------------------------------------------------
  104. const clientConfig = extend(true, {}, config, {
  105. entry: './src/client.js',
  106. output: {
  107. path: path.join(__dirname, '../build/public'),
  108. filename: DEBUG ? '[name].js?[hash]' : '[name].[hash].js',
  109. },
  110. // Choose a developer tool to enhance debugging
  111. // http://webpack.github.io/docs/configuration.html#devtool
  112. devtool: DEBUG ? 'cheap-module-eval-source-map' : false,
  113. plugins: [
  114. ...config.plugins,
  115. new webpack.DefinePlugin({ ...GLOBALS, 'process.env.BROWSER': true }),
  116. new AssetsPlugin({
  117. path: path.join(__dirname, '../build'),
  118. filename: 'assets.js',
  119. processOutput: x => `module.exports = ${JSON.stringify(x)};`,
  120. }),
  121. ...(!DEBUG ? [
  122. new webpack.optimize.DedupePlugin(),
  123. new webpack.optimize.UglifyJsPlugin({
  124. compress: {
  125. // jscs:disable requireCamelCaseOrUpperCaseIdentifiers
  126. screw_ie8: true,
  127. // jscs:enable requireCamelCaseOrUpperCaseIdentifiers
  128. warnings: VERBOSE,
  129. },
  130. }),
  131. new webpack.optimize.AggressiveMergingPlugin(),
  132. ] : []),
  133. ],
  134. });
  135. //
  136. // Configuration for the server-side bundle (server.js)
  137. // -----------------------------------------------------------------------------
  138. const serverConfig = extend(true, {}, config, {
  139. entry: './src/server.js',
  140. output: {
  141. path: './build',
  142. filename: 'server.js',
  143. libraryTarget: 'commonjs2',
  144. },
  145. target: 'node',
  146. externals: [
  147. /^\.\/assets$/,
  148. function filter(context, request, cb) {
  149. const isExternal =
  150. request.match(/^[@a-z][a-z\/\.\-0-9]*$/i) &&
  151. !request.match(/^react-routing/) &&
  152. !context.match(/[\\/]react-routing/);
  153. cb(null, Boolean(isExternal));
  154. },
  155. ],
  156. node: {
  157. console: false,
  158. global: false,
  159. process: false,
  160. Buffer: false,
  161. __filename: false,
  162. __dirname: false,
  163. },
  164. devtool: 'source-map',
  165. plugins: [
  166. ...config.plugins,
  167. new webpack.DefinePlugin({ ...GLOBALS, 'process.env.BROWSER': false }),
  168. new webpack.BannerPlugin('require("source-map-support").install();',
  169. { raw: true, entryOnly: false }),
  170. ],
  171. });
  172. export default [clientConfig, serverConfig];