为什么突然想认真学一下webpack?
目前主流框架 Vue、React等都基于此,因此学一下基本使用时必要的
最近打算开发一个基于 html5、canvas 的小框架,涉及到页面频繁调试、浏览器兼容等;另外,开发过程中经常使用 import、ES6。如果直接基于 html + js + css来开发,那么效率很低还要处理很多兼容问题。
我们可以看到 webpack 的优势
基于以上几点,webpack 的使用时必需的,它会极大的加快开发效率,减少一些不必要的开发成本,从而将开发重心集中于框架本身的开发测试上。
npm install webpack webpack-dev-server -S
module.exports = {
//....
}
entry 指定入口文件,也就是告诉 webpack 应该从哪个文件开始读取
const path = require("path");
module.exports = {
entry: "index.js"
}
output 指定输出文件的位置,名称
const path = require("path");
module.exports = {
entry: "index.js",
output: {
filename: "[name].js",
path: path.resolve(__dirname, "dist"),
},
}
webpack 只能处理 javascript 和 json,所以需要依靠外部模块来处理其他文件。通常是放在 module 的 rules 属性中,以数组形式排列。
每一组 loader 有以下常见属性
const HtmlWepackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
const path = require("path");
module.exports = {
//...出口, 入口配置
// loader
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: "babel-loader",
},
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
],
},
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
"sass-loader" ,
],
},
],
},
}
插件,可以对文件作进一步处理,整合、注入等等
const HtmlWepackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
const path = require("path");
module.exports = {
//...出口、入口、loader配置
plugins: [
new MiniCssExtractPlugin({
filename: "css/[name].css"
}),
new HtmlWepackPlugin({
filename: "index.html",
template: "./src/index.html",
}),
],
}
来源于 webpack-dev-server,是 webpack 提供的服务器模块,可以将打包生成后的文件放置与临时创建的 http 服务上,采可直接通过 ip:port 访问
const HtmlWepackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
const path = require("path");
module.exports = {
//...出口、入口、loader、plugins配置
devServer: {
static: {
directory: path.join(__dirname, 'public'),
},
compress: true,
port: 9000,
hot: true,
},
}
const HtmlWepackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
const path = require("path");
module.exports = {
mode: "development",
entry: "./src/index.js",
output: {
filename: "[name].js",
path: path.resolve(__dirname, "dist"),
},
devServer: {
static: {
directory: path.join(__dirname, 'public'),
},
compress: true,
port: 9000,
hot: true,
},
plugins: [
new MiniCssExtractPlugin({
filename: "css/[name].css"
}),
new HtmlWepackPlugin({
filename: "index.html",
template: "./src/index.html",
}),
],
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: "babel-loader",
},
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
],
},
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
"sass-loader" ,
],
},
],
},
};
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有