webpack.config.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * React Static Boilerplate
  3. * Copyright (c) Konstantin Tarkus | MIT License
  4. */
  5. import path from 'path';
  6. import minimist from 'minimist';
  7. import webpack from 'webpack';
  8. import gutil from 'gulp-util';
  9. import autoprefixer from 'autoprefixer-core';
  10. import merge from 'lodash/object/merge';
  11. const argv = minimist(process.argv.slice(2));
  12. const DEBUG = !argv.release;
  13. const VERBOSE = !!argv.verbose;
  14. const WATCH = global.watch;
  15. const JSX_LOADER = WATCH ? 'react-hot!babel' : 'babel';
  16. const AUTOPREFIXER_BROWSERS = [
  17. 'Android 2.3',
  18. 'Android >= 4',
  19. 'Chrome >= 20',
  20. 'Firefox >= 24',
  21. 'Explorer >= 8',
  22. 'iOS >= 6',
  23. 'Opera >= 12',
  24. 'Safari >= 6'
  25. ];
  26. // Base configuration
  27. const config = {
  28. output: {
  29. path: path.join(__dirname, 'build'),
  30. publicPath: './',
  31. sourcePrefix: ' '
  32. },
  33. cache: false,
  34. debug: DEBUG,
  35. stats: {
  36. colors: gutil.colors.supportsColor,
  37. reasons: DEBUG,
  38. hash: VERBOSE,
  39. version: VERBOSE,
  40. timings: VERBOSE,
  41. chunks: VERBOSE,
  42. chunkModules: VERBOSE,
  43. cached: VERBOSE,
  44. cachedAssets: VERBOSE
  45. },
  46. plugins: [
  47. new webpack.optimize.OccurenceOrderPlugin(),
  48. new webpack.DefinePlugin({
  49. 'process.env.NODE_ENV': DEBUG ? '"development"' : '"production"',
  50. '__DEV__': DEBUG
  51. })
  52. ],
  53. module: {
  54. loaders: [{
  55. test: /\.css$/,
  56. loader: 'style-loader/useable!' +
  57. 'css-loader' + (DEBUG ? '/minimize' : '') + '!postcss-loader'
  58. }, {
  59. test: /\.jsx?$/,
  60. exclude: /node_modules/,
  61. loader: JSX_LOADER
  62. }, {
  63. test: /routes\.jsx?$/,
  64. loader: './routes-loader.js'
  65. }, {
  66. test: /\.gif/,
  67. loader: 'url-loader?limit=10000&mimetype=image/gif'
  68. }, {
  69. test: /\.jpg/,
  70. loader: 'url-loader?limit=10000&mimetype=image/jpg'
  71. }, {
  72. test: /\.png/,
  73. loader: 'url-loader?limit=10000&mimetype=image/png'
  74. }, {
  75. test: /\.svg/,
  76. loader: 'url-loader?limit=10000&mimetype=image/svg+xml'
  77. }]
  78. },
  79. postcss: [autoprefixer(AUTOPREFIXER_BROWSERS)]
  80. };
  81. // Configuration for the client-side bundle
  82. const appConfig = merge({}, config, {
  83. entry: (WATCH ? [
  84. 'webpack-dev-server/client?http://localhost:3000',
  85. 'webpack/hot/only-dev-server'
  86. ] : []).concat([
  87. './scripts/app.js'
  88. ]),
  89. output: {
  90. filename: 'app.js'
  91. },
  92. plugins: config.plugins.concat(WATCH ? [
  93. new webpack.HotModuleReplacementPlugin(),
  94. new webpack.NoErrorsPlugin()
  95. ] : [])
  96. });
  97. // Configuration for server-side pre-rendering bundle
  98. const pagesConfig = merge({}, config, {
  99. entry: './scripts/pages.js',
  100. output: {
  101. filename: 'pages.js',
  102. libraryTarget: 'commonjs2'
  103. },
  104. target: 'node',
  105. externals: /^[a-z][a-z\.\-0-9]*$/,
  106. node: {
  107. console: false,
  108. global: false,
  109. process: false,
  110. Buffer: false,
  111. __filename: false,
  112. __dirname: false
  113. }
  114. });
  115. export default [appConfig, pagesConfig];