webpack.config.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 WebpackLoggingPlugin = require('webpack-logging-plugin')
  19. const {
  20. addPlugins, createConfig, entryPoint, env, setOutput,
  21. sourceMaps, defineConstants, webpack,
  22. } = require('@webpack-blocks/webpack2')
  23. const sourceDir = 'src'
  24. const publicPath = `/${process.env.PUBLIC_PATH || ''}/`.replace('//', '/')
  25. const sourcePath = path.join(process.cwd(), sourceDir)
  26. const outputPath = path.join(process.cwd(), 'dist')
  27. const babel = () => () => ({
  28. module: {
  29. rules: [
  30. { test: /\.jsx?$/, exclude: /node_modules/, loader: 'babel-loader' },
  31. ],
  32. },
  33. })
  34. const assets = () => () => ({
  35. module: {
  36. rules: [
  37. {
  38. test: /\.(png|jpe?g|svg|woff2?|ttf|eot)$/,
  39. loader: 'url-loader',
  40. options: {
  41. limit: 8192,
  42. name: './assets/[hash].[ext]',
  43. },
  44. },
  45. ],
  46. },
  47. })
  48. const resolveModules = modules => () => ({
  49. resolve: {
  50. modules: [].concat(modules, ['node_modules']),
  51. },
  52. })
  53. const node = () => () => ({
  54. node: {
  55. console: true,
  56. fs: 'empty',
  57. net: 'empty',
  58. tls: 'empty',
  59. },
  60. })
  61. const config = createConfig([
  62. setOutput({
  63. filename: '[name].js',
  64. path: outputPath,
  65. publicPath,
  66. }),
  67. defineConstants({
  68. 'process.env.NODE_ENV': process.env.NODE_ENV,
  69. 'process.env.PUBLIC_PATH': publicPath.replace(/\/$/, ''),
  70. }),
  71. () => ({ stats: 'errors-only' }),
  72. addPlugins([
  73. new HtmlWebpackPlugin({
  74. filename: 'index.html',
  75. template: path.join(process.cwd(), 'public/index.html'),
  76. }),
  77. new WebpackLoggingPlugin(),
  78. ]),
  79. happypack([
  80. babel(),
  81. ]),
  82. assets(),
  83. resolveModules(sourceDir),
  84. node(),
  85. env('development', [
  86. entryPoint({
  87. app: [sourcePath, 'webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000'],
  88. }),
  89. sourceMaps(),
  90. addPlugins([
  91. new webpack.NamedModulesPlugin(),
  92. new webpack.HotModuleReplacementPlugin(),
  93. new webpack.NoEmitOnErrorsPlugin(),
  94. ]),
  95. ]),
  96. env('production', [
  97. entryPoint({
  98. app: sourcePath,
  99. }),
  100. splitVendor(),
  101. addPlugins([
  102. new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false } }),
  103. ]),
  104. ]),
  105. ])
  106. module.exports = config