webpack.config.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. const path = require('path');
  2. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  3. const HtmlWebpackPlugin = require('html-webpack-plugin');
  4. const {
  5. CleanWebpackPlugin
  6. } = require('clean-webpack-plugin');
  7. const CopyWebpackPlugin = require('copy-webpack-plugin');
  8. let entry = { index: './src/index.ts' },
  9. plugins = [
  10. new CleanWebpackPlugin(),
  11. new CopyWebpackPlugin([{
  12. from: path.join(__dirname, './src/assets'),
  13. to: path.join(__dirname, './dist/assets')
  14. }]),
  15. new MiniCssExtractPlugin({
  16. filename: './assets/styles/index.[hash:8].css'
  17. }),
  18. new HtmlWebpackPlugin({
  19. // favicon: './src/img/favicon.ico',
  20. title: '磁铁布局',
  21. template: './src/index.html',
  22. filename: 'index.html',
  23. minify: {
  24. // 压缩
  25. removeComments: true, // 移除HTML中的注释
  26. collapseWhitespace: true, // 删除空白符与换行符
  27. removeAttributeQuotes: true // 去除属性引用
  28. },
  29. chunks: ['index']
  30. })
  31. ];
  32. if (process.env.NODE_ENV !== 'production') {
  33. entry.login = './src/login.ts';
  34. plugins.push(
  35. new HtmlWebpackPlugin({
  36. title: '登陆',
  37. template: './src/login.html',
  38. filename: 'login.html',
  39. minify: {
  40. removeComments: true,
  41. collapseWhitespace: true,
  42. removeAttributeQuotes: true
  43. },
  44. chunks: ['login']
  45. })
  46. );
  47. }
  48. module.exports = {
  49. mode: process.env.NODE_ENV,
  50. entry,
  51. devtool: process.env.NODE_ENV === 'production' ? 'cheap-module-source-map' : 'cheap-module-eval-source-map',
  52. output: {
  53. publicPath: '',
  54. path: path.resolve(__dirname, 'dist'),
  55. filename: './assets/js/[name].[hash:8].js'
  56. },
  57. resolve: {
  58. extensions: ['.js', '.ts', '.scss'],
  59. alias: {
  60. // css: path.resolve(__dirname, './src/styles'),
  61. imgs: path.resolve(__dirname, './src/assets/imgs')
  62. }
  63. },
  64. module: {
  65. rules: [{
  66. test: /\.ts$/,
  67. exclude: path.resolve(__dirname, 'node_modules'),
  68. include: path.resolve(__dirname, 'src'),
  69. loader: ['babel-loader', 'ts-loader']
  70. },
  71. {
  72. test: /\.scss$/,
  73. use: [
  74. // {loader: "style-loader"},
  75. {
  76. loader: MiniCssExtractPlugin.loader
  77. },
  78. {
  79. loader: 'css-loader',
  80. options: {
  81. url: false
  82. // importLoaders: 1
  83. }
  84. },
  85. {
  86. // 自动添加前缀
  87. loader: 'postcss-loader',
  88. options: {
  89. plugins: [require('autoprefixer')]
  90. }
  91. },
  92. {
  93. loader: 'sass-loader'
  94. }
  95. ]
  96. },
  97. {
  98. test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
  99. loader: 'url-loader',
  100. options: {
  101. limit: 100,
  102. name: './imgs/[name].[hash:7].[ext]'
  103. }
  104. }
  105. ]
  106. },
  107. plugins,
  108. devServer: {
  109. host: '0.0.0.0',
  110. port: 8083,
  111. open: true,
  112. proxy: {
  113. '/api': {
  114. target: 'http://192.168.100.254:10000/',
  115. pathRewrite: {
  116. '^/api': ''
  117. },
  118. changeOrigin: true
  119. }
  120. }
  121. }
  122. };