webpack.config.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. const path = require('path')
  15. const HtmlWebpackPlugin = require('html-webpack-plugin')
  16. const splitVendor = require('webpack-blocks-split-vendor')
  17. const happypack = require('webpack-blocks-happypack')
  18. const {
  19. addPlugins, createConfig, entryPoint, env, setOutput,
  20. sourceMaps, defineConstants, webpack,
  21. } = require('@webpack-blocks/webpack2')
  22. const sourceDir = 'src'
  23. const publicPath = `/${process.env.PUBLIC_PATH || ''}/`.replace('//', '/')
  24. const sourcePath = path.join(process.cwd(), sourceDir)
  25. const outputPath = path.join(process.cwd(), 'dist')
  26. const babel = () => () => ({
  27. module: {
  28. rules: [
  29. { test: /\.jsx?$/, exclude: /node_modules/, loader: 'babel-loader' },
  30. ],
  31. },
  32. })
  33. const assets = () => () => ({
  34. module: {
  35. rules: [
  36. {
  37. test: /\.(png|jpe?g|svg|woff2?|ttf|eot)$/,
  38. loader: 'url-loader',
  39. options: {
  40. limit: 8192,
  41. name: './assets/[hash].[ext]',
  42. },
  43. },
  44. ],
  45. },
  46. })
  47. const resolveModules = modules => () => ({
  48. resolve: {
  49. modules: [].concat(modules, ['node_modules']),
  50. },
  51. })
  52. const config = createConfig([
  53. setOutput({
  54. filename: '[name].js',
  55. path: outputPath,
  56. publicPath,
  57. }),
  58. defineConstants({
  59. 'process.env.NODE_ENV': process.env.NODE_ENV,
  60. 'process.env.PUBLIC_PATH': publicPath.replace(/\/$/, ''),
  61. }),
  62. addPlugins([
  63. new webpack.ProgressPlugin(),
  64. new HtmlWebpackPlugin({
  65. filename: 'index.html',
  66. template: path.join(process.cwd(), 'public/index.html'),
  67. }),
  68. ]),
  69. happypack([
  70. babel(),
  71. ]),
  72. assets(),
  73. resolveModules(sourceDir),
  74. env('development', [
  75. entryPoint({
  76. app: [sourcePath, 'webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000'],
  77. }),
  78. sourceMaps(),
  79. addPlugins([
  80. new webpack.NamedModulesPlugin(),
  81. new webpack.HotModuleReplacementPlugin(),
  82. new webpack.NoEmitOnErrorsPlugin(),
  83. ]),
  84. ]),
  85. env('production', [
  86. entryPoint({
  87. app: sourcePath,
  88. }),
  89. splitVendor(),
  90. addPlugins([
  91. new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false } }),
  92. ]),
  93. ]),
  94. ])
  95. module.exports = config