config.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 SCRIPT_LOADERS = WATCH ? ['react-hot', 'babel'] : ['babel'];
  13. const AUTOPREFIXER_BROWSERS = [
  14. 'Android 2.3',
  15. 'Android >= 4',
  16. 'Chrome >= 35',
  17. 'Firefox >= 31',
  18. 'Explorer >= 9',
  19. 'iOS >= 7',
  20. 'Opera >= 12',
  21. 'Safari >= 7.1',
  22. ];
  23. // Base configuration
  24. const config = {
  25. output: {
  26. path: path.join(__dirname, '../build'),
  27. publicPath: '/',
  28. sourcePrefix: ' ',
  29. },
  30. cache: false,
  31. debug: DEBUG,
  32. stats: {
  33. colors: true,
  34. reasons: DEBUG,
  35. hash: VERBOSE,
  36. version: VERBOSE,
  37. timings: true,
  38. chunks: VERBOSE,
  39. chunkModules: VERBOSE,
  40. cached: VERBOSE,
  41. cachedAssets: VERBOSE,
  42. },
  43. plugins: [
  44. new webpack.optimize.OccurenceOrderPlugin(),
  45. new webpack.DefinePlugin({
  46. 'process.env.NODE_ENV': DEBUG ? '"development"' : '"production"',
  47. '__DEV__': DEBUG,
  48. }),
  49. ],
  50. module: {
  51. loaders: [
  52. {
  53. test: /\.jsx?$/,
  54. include: [
  55. path.resolve(__dirname, '../components'),
  56. path.resolve(__dirname, '../lib'),
  57. path.resolve(__dirname, '../pages'),
  58. path.resolve(__dirname, '../app.js'),
  59. path.resolve(__dirname, '../config.js'),
  60. ],
  61. loaders: SCRIPT_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() {
  81. return [
  82. require('postcss-import')({
  83. onImport: files => files.forEach(this.addDependency),
  84. }),
  85. require('precss')(),
  86. require('autoprefixer')({browsers: AUTOPREFIXER_BROWSERS}),
  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({compress: {warnings: VERBOSE}}),
  106. new webpack.optimize.AggressiveMergingPlugin(),
  107. ]),
  108. ...(WATCH ? [
  109. new webpack.HotModuleReplacementPlugin(),
  110. ] : []),
  111. ],
  112. module: {
  113. loaders: [
  114. ...config.module.loaders,
  115. {
  116. test: /\.scss$/,
  117. loaders: ['style-loader', 'css-loader', 'postcss-loader'],
  118. },
  119. ],
  120. },
  121. });
  122. // Configuration for server-side pre-rendering bundle
  123. const pagesConfig = merge({}, config, {
  124. entry: './app.js',
  125. output: {
  126. filename: 'app.node.js',
  127. libraryTarget: 'commonjs2',
  128. },
  129. target: 'node',
  130. node: {
  131. console: false,
  132. global: false,
  133. process: false,
  134. Buffer: false,
  135. __filename: false,
  136. __dirname: false,
  137. },
  138. externals: /^[a-z][a-z\.\-\/0-9]*$/i,
  139. plugins: config.plugins.concat([
  140. new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }),
  141. ]),
  142. module: {
  143. loaders: [
  144. ...config.module.loaders,
  145. {
  146. test: /\.scss$/,
  147. loaders: ['css-loader', 'postcss-loader'],
  148. },
  149. ],
  150. },
  151. });
  152. export default [appConfig, pagesConfig];