模块化打包工具的特点
- 新特性代码编译为环境支持的代码
- 模块化Javascript打包
- 支持不同类型的资源模块
webpack打包工具
配置文件:webpack.config.js
{ // 这是一个运行在node环境中的文件 const path = require('path') module.exports = { mode: 'development', // 打包模式:production|development|none entry: './src/main.js', // 打包的入口文件,相对路径的时候,前面的./不能省略 output: { filename: 'bundle.js', // 打包后的文件名 path: path.join(__dirname, 'dest'), // 打包后文件的输出目录,必须为绝对路径 publicPath: 'dest/' // 打包后的资源所在的目录,即服务器所伺服的静态资源目录。 ‘/’不能省略 }, module: { rules: [ { test: /\.js$/, use: { loader: 'babel-loader', // 需要同时安装 babel-loader、@babel/core、@babel/preset-env options: { presets: ['@babel/preset-env'] } } }, { test: /\.css$/, use: [ 'style-loader', 'css-loader' ] }, { test: /\.png$/, use: { loader: 'url-loader', options: { limit: 10 * 1024 // 10kb 小于10kb会使用url-loader转换为data-urls,嵌入代码中; // 大于10kb的文件会自动调用file-loader加载器,单独提取存放,所以 // file-loader还是需要安装的,虽然在配置文件中不用配置。 } } }, { test: /\.html$/, use: { loader: 'html-loader', options: ['img:src', 'a:href'] // html中的资源加载方式的支持属性 } } ] } }}
常用加载器分类