webpack核心配置

entry

单入口 string
多入口 object

  1. config.entry = {
  2. a:'src/a.js',
  3. b:'src/b.js'
  4. }
  5. config.entry = './a/index.js'

output

与entry对应用于输出配置

  1. config.output = {
  2. filename:'[name].[chunkHash].js',
  3. path:path.resolve(__dirname,'dist),
  4. chunkName:'[name].[chunkhash].js',
  5. publicPath:'/',
  6. }

plugins

  1. config.plugins = [
  2. new HtmlWepbackPlugin({
  3. filename:'index.html',
  4. temlate:'./public/index.html',
  5. inject:true,
  6. chunk:['app']
  7. }),
  8. new CopyWebpackPlugin([
  9. {
  10. from:path.resolve(__dirname,'static'),
  11. to:path.resolve(__dirname,'dist/static')
  12. }
  13. ]),
  14. new CleanWebpackPlugin(),
  15. new webpack.DefinePlugin({
  16. ENV:JSON.stringify('production')
  17. }),
  18. <!-- const VueLoaderPlugin = require('vue-loader/lib/plugin'); -->
  19. new VueLoaderPlugin(),
  20. new webpack.HashedModuleIdsPlugin({
  21. }),
  22. new CompressionWebpackPlugin({
  23. filename: '[path].br[query]',
  24. algorithm: 'gzip', // 使用gzip压缩
  25. test: /\.(js|css)$/,
  26. threshold: 10240,
  27. minRatio: 0.8,
  28. // deleteOriginalAssets: false,
  29. })
  30. ]

module

1.file-loader

2.base-loader

  1. config.module = {
  2. rules:[{
  3. test:/\.css$/,
  4. use:[{
  5. loader:'css-loader'
  6. },{
  7. loader:'style-loader'
  8. }]
  9. },
  10. {
  11. test:/\.js|jsx$/,
  12. use:[{
  13. loader:'babel-loader'
  14. }]
  15. },
  16. {
  17. test:/\.jpg|png|gif$/,
  18. use:['file-loader']
  19. },
  20. {
  21. test:/\.vue$/,
  22. use:[{
  23. loader:'vue-loader',
  24. option:{
  25. name:'[name].[contenthash].[ext]',
  26. outputPath:'static/assets/',
  27. publicPath:'static/assets/',
  28. }
  29. }]
  30. }
  31. ]
  32. }


devServer

  1. config.devServer = {
  2. contentBase:'./public/index.html',
  3. hot:true,
  4. compress:true,
  5. port:'9000'
  6. }

.babelrc

  1. {
  2. "preset":["@babel/preset-env","@babel/preset-react"],
  3. "plugins":[
  4. ["babel-plugin-transform-remove-console",{
  5. "exclude":["error","warn"]
  6. }]
  7. ]
  8. }

watch & watchOptions

  1. watch: true,
  2. // 自定义监视模式
  3. watchOptions: {
  4. // 排除监听
  5. ignored: /node_modules/,
  6. // 监听到变化发生后,延迟 300ms(默认) 再去执行动作,
  7. // 防止文件更新太快导致重新编译频率太高
  8. aggregateTimeout: 300,
  9. // 判断文件是否发生变化是通过不停的去询问系统指定文件有没有变化实现的
  10. // 默认 1000ms 询问一次
  11. poll: 1000
  12. }

webpack-target

  1. const publicHtml = './public/index.html';
  2. const path = require('path');
  3. const webpack = require('webpack');
  4. const HtmlWepbackPlguin = require('html-webpack-plugin');
  5. const {CleanWebpackPlugin} = require('clean-webpack-plugin');
  6. const configA = {
  7. target:'web',
  8. entry:{
  9. a:'./src/test/a.js',
  10. b:'./src/test/b.js'
  11. },
  12. output:{
  13. filename:'[name].js',
  14. path:path.resolve(__dirname,'testDist')
  15. },
  16. devServer:{
  17. host:'127.0.0.1',
  18. port:'5555',
  19. contentBase:publicHtml
  20. },
  21. plugins:[
  22. // new CleanWebpackPlugin(),
  23. new HtmlWepbackPlguin({
  24. filename:'index.html',
  25. temlate:publicHtml,
  26. inject:true
  27. }),
  28. new webpack.DefinePlugin({
  29. env:JSON.stringify(process.env)
  30. })
  31. ],
  32. // optimization: {
  33. // runtimeChunk: 'single',
  34. // splitChunks: {
  35. // chunks: 'all',
  36. // maxInitialRequests: Infinity,
  37. // minSize: 0,
  38. // cacheGroups: {
  39. // vendor: {
  40. // test: /[\\/]node_modules[\\/]/,
  41. // name(module) {
  42. // // 获取第三方包名
  43. // const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];
  44. // // npm 软件包名称是 URL 安全的,但是某些服务器不喜欢@符号
  45. // return `npm.${packageName.replace('@', '')}`;
  46. // },
  47. // },
  48. // },
  49. // },
  50. // },
  51. watch: true,
  52. // 自定义监视模式
  53. watchOptions: {
  54. // 排除监听
  55. ignored: /node_modules/,
  56. // 监听到变化发生后,延迟 300ms(默认) 再去执行动作,
  57. // 防止文件更新太快导致重新编译频率太高
  58. aggregateTimeout: 300,
  59. // 判断文件是否发生变化是通过不停的去询问系统指定文件有没有变化实现的
  60. // 默认 1000ms 询问一次
  61. poll: 1000
  62. }
  63. }
  64. const configB = {
  65. target:'node',
  66. entry:{
  67. a:'./src/test/a.js',
  68. b:'./src/test/b.js'
  69. },
  70. output:{
  71. filename:'[name].node.js',
  72. path:path.resolve(__dirname,'testDist')
  73. }
  74. }
  75. module.exports = [configA,configB]