Blog
  • README
  • Daily-life
    • 关于简历
    • 如何做好劳逸结合
    • 探索历程
    • 如何走出个人舒适圈
    • 怎样提升领导能力
    • 个人装换机之 Windows 系列
    • 工作之余 - 技能修炼
    • Search: 业余的专业搜索技能
    • 自我迭代
    • 优秀的自我介绍很重要
    • 工作心得
  • Checklist
    • 开发自检清单
    • 代码教鞭:不值得犯第二次的错
  • Docs
    • Bash: 入门重点
    • CSS 特殊用法
    • ChatGPT 使用总结
    • Console
    • Corn 表达式
    • DOS command
    • Emoji: 供日常学习、查询及使用
    • Git 命令集
    • GitBook
    • Github Action
    • Github
    • HTTP: 小知识巧总结
    • JS 精选
    • Jest learning manual
    • License: 解说
    • Mac
    • Markdown
    • 小程序
    • NPM and Plugin Guide
    • 渐进式 Web 应用(PWA)
    • Regexp
    • Rollup
    • SEO
    • SQL
    • 专业名词
    • Testing
    • TypeScript
    • Vue - fragment knowledge
    • Web API
    • 站点:那些可以经常逛逛的开发工具网站
    • Webpack
    • Windows 之路遇精彩
  • Project
    • README 模板:Project Name
    • 借鉴-项目规范
  • React
    • Antd typescript
    • React: 总结整理
    • React Hook - 速览
    • React-生命周期方法转 Hooks-1
    • React: 生命周期方法转 Hooks-2
    • Redux
    • React router
    • useEffect
  • Share
    • StartToGit
    • 3步自动同步你的 Github 仓库到 Gitee 仓库
    • 软考高级“信息系统项目管理师”考试忠告
    • 轻松使用WebWorker,解放耗时较大的算法代码
  • Tool
    • 最好的工具: 理解并使用那些耳目一新的巧工具
    • IntelliJ IDEA
    • VS Code:发挥应有的效率
  • Office
    • Excel 常用操作
    • Word: 文档操作精选技巧
由 GitBook 提供支持
在本页
  • 版本
  • Skills tree
  • 自动清理构建目录产物:
  • PostCSS 插件 autoprefixer 自动补齐 CSS3 前缀
  • 移动端 px 自动转 rem
  • 静态资源内联
  • 多页面应用打包通用方案
  • scripts

这有帮助吗?

  1. Docs

Webpack

上一页站点:那些可以经常逛逛的开发工具网站下一页Windows 之路遇精彩

最后更新于3年前

这有帮助吗?

版本

    • 重大变更:功能清除

    • 重大变更:长期缓存

    • 重大变更:支持崭新的 Web 平台特性

    • 重大变更:支持全新的 Node.js 生态特性

    • 重大变更:开发体验

    • 重大变更: 构建优化

    • 重大变更:性能优化

    • 重大变更:长期未解决的问题

    • 重大变更:未来计划

    • 重大内部变更

Skills tree

自动清理构建目录产物:

PostCSS 插件 autoprefixer 自动补齐 CSS3 前缀

移动端 px 自动转 rem

静态资源内联

多页面应用打包通用方案

动态获取 entry 和设置 html-webpack-plugin 数量

  • 利用 glob.sync

entry: blog.sync(path.join(__dirname, './src/*/index.js'));
  • webpack config entry

const {entry, htmlWebpackPlugins} = setMPA('./src/*/index.js', /src\/(.*)\/index\.js/);

// 1. set entry with above entry
// 2. concat htmlWebpackPlugins with above htmlWebpackPlugins
// 3. note: related .html

scripts

{
  "scripts": {
    "build": "webpack --mode production"
  }
}

Entry

module.exports = {
  entry: './src/index.js' 
}
// ===
module.exports = {
  entry: {
    main: './src/index.js'
  }
}

// 或者配置多个入口
module.exports = {
  entry: {
    foo: './src/page-foo.js',
    bar: './src/page-bar.js', 
    // ...
  }
}
// 使用数组来对多个文件进行打包
module.exports = {
  entry: {
    main: [
      './src/foo.js',
      './src/bar.js'
    ]
  }
}

Loader

module.exports = {
	// ...
	module: {
		// ...
		rules: [
			{
				test: /\.jsx?/, // 匹配文件路径的正则表达式,通常我们都是匹配文件类型后缀
				include: [
					path.resolve(__dirname, 'src') // 指定哪些路径下的文件需要经过 loader 处理
				],
				use: 'babel-loader', // 指定使用的 loader
			},
		]
	}
}

Plugin

const UglifyPlugin = require('uglifyjs-webpack-plugin')

module.exports = {
  plugins: [
    new UglifyPlugin()
  ],
}

Output

module.exports = {
  // ...
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js',
  },
}

// 或者多个入口生成不同文件
module.exports = {
  entry: {
    foo: './src/foo.js',
    bar: './src/bar.js',
  },
  output: {
    filename: '[name].js',
    path: __dirname + '/dist',
  },
}
// 路径中使用 hash,每次构建时会有一个不同 hash 值,避免发布新版本时线上使用浏览器缓存
module.exports = {
  // ...
  output: {
    filename: '[name].js',
    path: __dirname + '/dist/[hash]',
  },
}

Simple Webpack.config.js

const path = require('path')
const UglifyPlugin = require('uglifyjs-webpack-plugin')

module.exports = {
  entry: './src/index.js',

  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js',
  },

  module: {
    rules: [
      {
        test: /\.jsx?/,
        include: [
          path.resolve(__dirname, 'src')
        ],
        use: 'babel-loader',
      },
    ],
  },

  // 代码模块路径解析的配置
  resolve: {
    modules: [
      "node_modules",
      path.resolve(__dirname, 'src')
    ],

    extensions: [".wasm", ".mjs", ".js", ".json", ".jsx"],
  },

  plugins: [
    new UglifyPlugin(), 
    // 使用 uglifyjs-webpack-plugin 来压缩 JS 代码
    // 如果你留意了我们一开始直接使用 webpack 构建的结果,你会发现默认已经使用了 JS 代码压缩的插件
    // 这其实也是我们命令中的 --mode production 的效果,后续的小节会介绍 webpack 的 mode 参数
  ],
}
Webpack 5