webpack.common.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. alias: {
  33. '@src': path.resolve(__dirname, 'src/'),
  34. },
  35. },
  36. module: {
  37. rules: [
  38. {
  39. test: /\.tsx?$/,
  40. exclude: /node_modules/,
  41. loader: require.resolve('babel-loader'),
  42. },
  43. {
  44. test: /\.(png|jpe?g|svg|woff2?|ttf|eot)$/,
  45. loader: 'url-loader',
  46. options: {
  47. limit: 8192,
  48. name: './assets/[hash].[ext]',
  49. },
  50. },
  51. ],
  52. },
  53. optimization: {
  54. splitChunks: {
  55. cacheGroups: {
  56. vendor: {
  57. test: /node_modules/,
  58. chunks: 'initial',
  59. name: 'vendor',
  60. enforce: true,
  61. },
  62. },
  63. },
  64. },
  65. }