webpack.config.js 3.1 KB

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