webpack.config.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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() {
  81. return [
  82. require('postcss-import')({
  83. onImport: files => files.forEach(this.addDependency),
  84. }),
  85. require('precss')(),
  86. require('autoprefixer')({
  87. browsers: AUTOPREFIXER_BROWSERS,
  88. }),
  89. ];
  90. },
  91. };
  92. // Configuration for the client-side bundle
  93. const appConfig = merge({}, config, {
  94. entry: [
  95. ...(WATCH ? ['webpack-hot-middleware/client'] : []),
  96. './app.js',
  97. ],
  98. output: {
  99. filename: 'app.js',
  100. },
  101. // http://webpack.github.io/docs/configuration.html#devtool
  102. devtool: DEBUG ? 'cheap-module-eval-source-map' : false,
  103. plugins: [
  104. ...config.plugins,
  105. ...(DEBUG ? [] : [
  106. new webpack.optimize.DedupePlugin(),
  107. new webpack.optimize.UglifyJsPlugin({
  108. compress: {
  109. warnings: VERBOSE,
  110. },
  111. }),
  112. new webpack.optimize.AggressiveMergingPlugin(),
  113. ]),
  114. ...(WATCH ? [
  115. new webpack.HotModuleReplacementPlugin(),
  116. new webpack.NoErrorsPlugin(),
  117. ] : []),
  118. ],
  119. module: {
  120. loaders: [
  121. WATCH ? Object.assign({}, JS_LOADER, {
  122. query: {
  123. // Wraps all React components into arbitrary transforms
  124. // https://github.com/gaearon/babel-plugin-react-transform
  125. plugins: ['react-transform'],
  126. extra: {
  127. 'react-transform': {
  128. transforms: [
  129. {
  130. transform: 'react-transform-hmr',
  131. imports: ['react'],
  132. locals: ['module'],
  133. }, {
  134. transform: 'react-transform-catch-errors',
  135. imports: ['react', 'redbox-react'],
  136. },
  137. ],
  138. },
  139. },
  140. },
  141. }) : JS_LOADER,
  142. ...config.module.loaders,
  143. {
  144. test: /\.scss$/,
  145. loaders: ['style-loader', 'css-loader', 'postcss-loader'],
  146. },
  147. ],
  148. },
  149. });
  150. // Configuration for server-side pre-rendering bundle
  151. const pagesConfig = merge({}, config, {
  152. entry: './app.js',
  153. output: {
  154. filename: 'app.node.js',
  155. libraryTarget: 'commonjs2',
  156. },
  157. target: 'node',
  158. node: {
  159. console: false,
  160. global: false,
  161. process: false,
  162. Buffer: false,
  163. __filename: false,
  164. __dirname: false,
  165. },
  166. externals: /^[a-z][a-z\.\-\/0-9]*$/i,
  167. plugins: config.plugins.concat([
  168. new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }),
  169. ]),
  170. module: {
  171. loaders: [
  172. JS_LOADER,
  173. ...config.module.loaders,
  174. {
  175. test: /\.scss$/,
  176. loaders: ['css-loader', 'postcss-loader'],
  177. },
  178. ],
  179. },
  180. });
  181. export default [appConfig, pagesConfig];