config.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 minimist from 'minimist';
  8. import webpack from 'webpack';
  9. import merge from 'lodash/object/merge';
  10. const argv = minimist(process.argv.slice(2));
  11. const DEBUG = !argv.release;
  12. const VERBOSE = !!argv.verbose;
  13. const WATCH = global.watch;
  14. const SCRIPT_LOADERS = WATCH ? ['react-hot', 'babel'] : ['babel'];
  15. const AUTOPREFIXER_BROWSERS = [
  16. 'Android 2.3',
  17. 'Android >= 4',
  18. 'Chrome >= 20',
  19. 'Firefox >= 24',
  20. 'Explorer >= 8',
  21. 'iOS >= 6',
  22. 'Opera >= 12',
  23. 'Safari >= 6'
  24. ];
  25. // Base configuration
  26. const config = {
  27. output: {
  28. path: path.join(__dirname, '../build'),
  29. publicPath: '/',
  30. sourcePrefix: ' '
  31. },
  32. cache: false,
  33. debug: DEBUG,
  34. stats: {
  35. colors: true,
  36. reasons: DEBUG,
  37. hash: VERBOSE,
  38. version: VERBOSE,
  39. timings: VERBOSE,
  40. chunks: VERBOSE,
  41. chunkModules: VERBOSE,
  42. cached: VERBOSE,
  43. cachedAssets: VERBOSE
  44. },
  45. plugins: [
  46. new webpack.optimize.OccurenceOrderPlugin(),
  47. new webpack.DefinePlugin({
  48. 'process.env.NODE_ENV': DEBUG ? '"development"' : '"production"',
  49. '__DEV__': DEBUG
  50. })
  51. ],
  52. module: {
  53. loaders: [{
  54. test: /\.css$/,
  55. loader: 'style-loader/useable!' +
  56. 'css-loader' + (DEBUG ? '' : '/minimize') + '!postcss-loader'
  57. }, {
  58. test: /\.jsx?$/,
  59. include: [
  60. path.resolve(__dirname, '../src')
  61. ],
  62. loaders: SCRIPT_LOADERS
  63. }, {
  64. test: /[\\\/]app\.js$/,
  65. loader: path.join(__dirname, './lib/routes-loader.js')
  66. }, {
  67. test: /\.gif/,
  68. loader: 'url-loader?limit=10000&mimetype=image/gif'
  69. }, {
  70. test: /\.jpg/,
  71. loader: 'url-loader?limit=10000&mimetype=image/jpg'
  72. }, {
  73. test: /\.png/,
  74. loader: 'url-loader?limit=10000&mimetype=image/png'
  75. }, {
  76. test: /\.svg/,
  77. loader: 'url-loader?limit=10000&mimetype=image/svg+xml'
  78. }]
  79. },
  80. postcss: [
  81. require('cssnext')({ browsers: AUTOPREFIXER_BROWSERS }),
  82. require('postcss-nested')()
  83. ]
  84. };
  85. // Configuration for the client-side bundle
  86. const appConfig = merge({}, config, {
  87. entry: (WATCH ? [
  88. 'webpack/hot/dev-server',
  89. 'webpack-hot-middleware/client'
  90. ] : []).concat([
  91. './src/js/app.js'
  92. ]),
  93. output: {
  94. filename: 'app.js'
  95. },
  96. plugins: [
  97. ...config.plugins,
  98. ...(DEBUG ? [] : [
  99. new webpack.optimize.DedupePlugin(),
  100. new webpack.optimize.UglifyJsPlugin({compress: {warnings: VERBOSE}}),
  101. new webpack.optimize.AggressiveMergingPlugin()
  102. ]),
  103. ...(WATCH ? [
  104. new webpack.HotModuleReplacementPlugin(),
  105. new webpack.NoErrorsPlugin()
  106. ] : [])
  107. ]
  108. });
  109. // Configuration for server-side pre-rendering bundle
  110. const pagesConfig = merge({}, config, {
  111. entry: './src/js/app.js',
  112. output: {
  113. filename: 'app.node.js',
  114. libraryTarget: 'commonjs2'
  115. },
  116. target: 'node',
  117. externals: /^[a-z][a-z\.\-\/0-9]*$/i,
  118. plugins: config.plugins.concat([
  119. new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 })
  120. ]),
  121. node: {
  122. console: false,
  123. global: false,
  124. process: false,
  125. Buffer: false,
  126. __filename: false,
  127. __dirname: false
  128. }
  129. });
  130. export default [appConfig, pagesConfig];