webpack.common.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. const HtmlWebpackPlugin = require('html-webpack-plugin')
  2. const webpack = require('webpack')
  3. const path = require('path')
  4. const { CleanWebpackPlugin } = require('clean-webpack-plugin')
  5. const CopyPlugin = require('copy-webpack-plugin')
  6. const dotenv = require('dotenv')
  7. const dotenvConfig = dotenv && dotenv.config && dotenv.config()
  8. const env = (dotenvConfig && dotenvConfig.parsed) || process.env
  9. const envKeys = Object.keys(env).reduce((prev, next) => {
  10. // eslint-disable-next-line no-param-reassign
  11. prev[`process.env.${next}`] = JSON.stringify(env[next])
  12. return prev
  13. }, {})
  14. module.exports = {
  15. entry: './src/index.tsx',
  16. output: {
  17. filename: '[name].[hash].bundle.js',
  18. chunkFilename: '[name].[hash].bundle.js',
  19. path: path.resolve(__dirname, 'dist'),
  20. publicPath: '/',
  21. },
  22. performance: { hints: false },
  23. plugins: [
  24. new webpack.DefinePlugin(envKeys),
  25. new CleanWebpackPlugin(),
  26. new CopyPlugin({ patterns: ['./public'] }),
  27. new HtmlWebpackPlugin({ template: './public/index.html' }),
  28. ],
  29. resolve: {
  30. modules: [__dirname, 'src', 'node_modules'],
  31. extensions: ['*', '.js', '.jsx', '.tsx', '.ts'],
  32. },
  33. module: {
  34. rules: [
  35. {
  36. test: /\.tsx?$/,
  37. exclude: /node_modules/,
  38. loader: require.resolve('babel-loader'),
  39. },
  40. {
  41. test: /\.(png|jpe?g|svg|woff2?|ttf|eot)$/,
  42. loader: 'url-loader',
  43. options: {
  44. limit: 8192,
  45. name: './assets/[hash].[ext]',
  46. },
  47. },
  48. ],
  49. },
  50. optimization: {
  51. splitChunks: {
  52. cacheGroups: {
  53. vendor: {
  54. test: /node_modules/,
  55. chunks: 'initial',
  56. name: 'vendor',
  57. enforce: true,
  58. },
  59. },
  60. },
  61. },
  62. }