目录
2、在命令行中修改打包的默认入口文件名、默认出口文件名和默认出口文件
3、在webpack.config.js 配置文件中修改webpack 的配置
4、修改了webpack.config.js 的文件名称后,如何运行配置文件?
npx webpack --entry ./src/main.js --output-filename bundle.js --output-path ./build
- const path = require("path")
-
- // 因为webpack 是运行在node中,所以要使用node 的commonJS 模块化语法
- module.exports = {
- entry: "./src/main.js",
- output: {
- filename: "bundle.js",
- // __dirname 获取当前文件所在路径
- // path 的值需要使用绝对路径
- path: path.resolve(__dirname, "./build")
- }
- }
- // 默认的打包方式 webpack.config.js
- "scripts": {
- "build": "webpack"
- }
- // 然后在命令行中运行脚本
- npm run build
- // 修改配置文件名后的打包方式
- "scripts": {
- "build": "webpack --config wb.config.js"
- }
-
- // 然后在命令行中运行脚本
- npm run build
- npm install css-loader -D
- // css-loader 只负责解析css 文件,不会将文件插入到页面中
- // 所以这时需要用到style-loader
- npm install style-loader -D
-
- const path = require("path")
- module.exports = {
- entry: "./src/main.js",
- output: {
- filename: "bundle.js",
- path: path.resolve(__dirname, "./build")
- }
- module: {
- rules: [
- {
- // 告诉webpack 匹配什么类型的文件, 通常会设置成正则表达式
- test: /\.css$/,
- // 告诉webpack 使用什么样的loader 来处理匹配到的文件
- // use 中多个loader 的使用顺序是从后往前的
- use: [
- { loader: "style-loader", "css-loader" }
- // { loader: "style-loader" },
- // { loader: "css-loader" }
- ]
- }
- ]
- }
- }
- npm install less-loader -D
-
- const path = require("path")
- module.exports = {
- entry: "./src/main.js",
- output: {
- filename: "bundle.js",
- path: path.resolve(__dirname, "./build")
- }
- module: {
- rules: [
- {
- test: /\.css$/,
- use: [
- { loader: "style-loader", "css-loader" }
- ]
- },
- {
- test: /\.less$/,
- use: [
- { loader: "style-loader", "less-loader", "less-loader"}
- ]
- }
- ]
- }
- }
当使用user-selece: none; (用户是否能选中)这样的具有兼容性问题的css 样式时,需要在该样式前加浏览器前缀,PostCSS 可以自动检测该css 样式是否具有兼容性问题,并自动添加浏览器前缀
- npm install postcss-loader -D
- // 自动添加浏览器前缀的插件
- npm install autoprefixer -D
-
- const path = require("path")
- module.exports = {
- entry: "./src/main.js",
- output: {
- filename: "bundle.js",
- path: path.resolve(__dirname, "./build")
- }
- module: {
- rules: [
- {
- test: /\.css$/,
- use: [
- "style-loader",
- "css-loader",
- {
- loader: "postcss-loader",
- options: {
- postcssOptions: {
- plugins: [ "autoprefixer" ]
- }
- }
- }
- ]
- },
- {
- test: /\.less$/,
- use: [
- "style-loader",
- "css-loader",
- "less-loader",
- "postcss-loader"
- ]
- }
- ]
- }
- }
或通过postcss.config.js 文件来配置postcss-loader
- npm install postcss-loader -D
- // 自动添加浏览器前缀的插件
- npm install autoprefixer -D
-
- // webpack.config.js
- const path = require("path")
- module.exports = {
- entry: "./src/main.js",
- output: {
- filename: "bundle.js",
- path: path.resolve(__dirname, "./build")
- }
- module: {
- rules: [
- {
- test: /\.css$/,
- use: [
- "style-loader",
- "css-loader",
- "postcss-loader"
- ]
- },
- {
- test: /\.less$/,
- use: [
- "style-loader",
- "css-loader",
- "less-loader",
- "postcss-loader"
- ]
- }
- ]
- }
- }
-
- // postcss.config.js
- module.exports = {
- plugins: [
- "autoprefixer"
- ]
- }
- npm install postcss-loader -D
- npm install postcss-preset-env -D
-
- // webpack.config.js
- const path = require("path")
- module.exports = {
- entry: "./src/main.js",
- output: {
- filename: "bundle.js",
- path: path.resolve(__dirname, "./build")
- }
- module: {
- rules: [
- {
- test: /\.css$/,
- use: [
- "style-loader",
- "css-loader",
- "postcss-loader"
- ]
- },
- {
- test: /\.less$/,
- use: [
- "style-loader",
- "css-loader",
- "less-loader",
- "postcss-loader"
- ]
- }
- ]
- }
- }
-
- // postcss.config.js
- module.exports = {
- plugins: [
- "postcss-preset-env"
- ]
- }
资源模块类型,通过添加4种新的模块类型,来替换loader
- const path = require("path")
- module.exports = {
- entry: "./src/main.js",
- output: {
- filename: "bundle.js",
- path: path.resolve(__dirname, "./build")
- }
- module: {
- rules: [
- {
- test: /\.css$/,
- use: [ "style-loader", "css-loader", "postcss-loader" ]
- },
- {
- test: /\.less$/,
- use: [ "style-loader", "css-loader", "less-loader", "postcss-loader" ]
- },
- {
- test: /\.(png|jpe?g|svg|gif)$/,
- // 资源模块类型
- type: "asset"
- }
- ]
- }
- }
- const path = require("path")
- module.exports = {
- entry: "./src/main.js",
- output: {
- filename: "bundle.js",
- path: path.resolve(__dirname, "./build")
- }
- module: {
- rules: [
- {
- test: /\.(png|jpe?g|svg|gif)$/,
- // 资源模块类型
- type: "asset",
- // 为不用大小的图片区分打包的方式
- parser: {
- dataUrlCondition: {
- // 小于60kb 就使用base64, 大于64kb 就单独进行打包
- maxSize: 60 * 1024
- }
- }
- // 对打包后的图片重新命名
- generator: {
- // img/: 表示将图片资源单独放到img 文件夹
- // name: 用于指向原来的图片名称
- // hash: webpack 生成的唯一的hash 值(:8 截取前8位)
- // ext: 用于指向原来的图片后缀名
- filename: "img/[name]_[hash:8][ext]"
- }
- }
- ]
- }
- }
- npm install vue-loader -D
-
- const path = require("path")
- const { VueLoaderPlugin } from = require('vue-loader/dist/index')
-
- module.exports = {
- entry: "./src/main.js",
- output: {
- filename: "bundle.js",
- path: path.resolve(__dirname, "./build")
- }
- module: {
- rules: [
- {
- test: /\.css$/,
- use: [ "style-loader", "css-loader", "postcss-loader" ]
- },
- {
- test: /\.less$/,
- use: [ "style-loader", "css-loader", "less-loader", "postcss-loader" ]
- },
- {
- test: /\.(png|jpe?g|svg|gif)$/,
- // 资源模块类型
- type: "asset"
- },
- {
- test: /\.vue$/,
- use: [ "vue-loader" ]
- }
- ]
- },
- plugins: [
- new VueLoaderPlugin()
- ]
- }
- const path = require("path")
- module.exports = {
- entry: "./src/main.js",
- output: {
- filename: "bundle.js",
- path: path.resolve(__dirname, "./build")
- }
- module: {
- rules: [
- {
- test: /\.css$/,
- use: [ "style-loader", "css-loader", "postcss-loader" ]
- },
- {
- test: /\.less$/,
- use: [ "style-loader", "css-loader", "less-loader", "postcss-loader" ]
- },
- {
- test: /\.(png|jpe?g|svg|gif)$/,
- // 资源模块类型
- type: "asset"
- },
- {
- test: /\.vue$/,
- use: [ "vue-loader" ]
- }
- ]
- },
- resolve: {
- extensions: [".js", ".json", ".vue", ".jsx", ".ts", ".tsx"],
- // 为固定的目录配置重命名
- alias: {
- src: path.resolve(__dirname, "./src")
- }
- }
- plugins: [
- new VueLoaderPlugin()
- ]
- }
当遇到ES6+ 、ts、jsx 的代码时通过babel 进行处理转成es5 的代码
babel 和postcss 一样可以单独拿出来配置,放到babel.config.js 配置文件中
babel 的预设:@babel/preset-env
- npm install babel-loader -D
- // babel 的预设
- npm install @babel/preset-env -D
-
- // webpack.config.js
- const path = require("path")
- module.exports = {
- entry: "./src/main.js",
- output: {
- filename: "bundle.js",
- path: path.resolve(__dirname, "./build")
- }
- module: {
- rules: [
- {
- test: /\.js$/,
- use: {
- loader: "babel-loader",
- // options: {
- // presets: [
- // "@babel/preset-env"
- // ]
- }
- }
- }
- ]
- }
- }
-
- // babel.config.js
- module.exports = {
- presets: [
- "@babel/preset-env"
- ]
- }
当重新打包时,都需要手动删除dist 文件夹,可以通过clean-webpack-plugin 在打包时自动删除dist 文件夹
- npm install clean-webpack-plugin -D
-
- const path = require("path");
- const { CleanWebpackPlugin } = require("clean-webpack=plugin");
- module.exports = {
- entry: "./src/main.js",
- output: {
- filename: "bundle.js",
- path: path.resolve(__dirname, "./build")
- }
- plugins: [
- new CleanWebpackPlugin()
- ]
- }
-
当进行打包时,不会打包index.html 文件,可以通过html-webpack-plugin 在打包时自动生成index.html 文件,并进行打包
- npm install clean-webpack-plugin -D
- npm install html-webpack-plugin -D
-
- const path = require("path");
- const { CleanWebpackPlugin } = require("clean-webpack=plugin");
- const HtmlWebpackPlugin = require("html-webpack-plugin");
-
- module.exports = {
- entry: "./src/main.js",
- output: {
- filename: "bundle.js",
- path: path.resolve(__dirname, "./build")
- }
- plugins: [
- new CleanWebpackPlugin(),
- new HtmlWebpackPlugin({
- title: '网页标题',
- // 当不想使用默认生成的index.html 模板时,可以自己设置要使用的index.html 文件
- template: './index.html'
- })
- ]
- }
-
- npm install clean-webpack-plugin -D
- npm install html-webpack-plugin -D
-
- const path = require("path");
- const { CleanWebpackPlugin } = require("clean-webpack=plugin");
- const HtmlWebpackPlugin = require("html-webpack-plugin");
- const { DefinePlugin } = require("webpack");
-
- module.exports = {
- mode: "development",
- entry: "./src/main.js",
- output: {
- filename: "bundle.js",
- path: path.resolve(__dirname, "./build")
- }
- plugins: [
- new CleanWebpackPlugin(),
- new HtmlWebpackPlugin({
- title: '网页标题',
- // 当不想使用默认生成的index.html 模板时,可以自己设置要使用的index.html 文件
- template: './index.html'
- }),
- new DefinePlugin({
- BASE_URL: "'./'",
- coderwhy: "'why'",
- counter: "123"
- })
- ]
- }
-
- // 定义的是全局变量,可以在任何地方直接使用
- // main.js
- console.log(BASE_URL);
- console.log(process.env.NODE_ENV);
-
-
开启一个本地服务器,供开发时使用
- npm install webpack-dev-server -D
-
- // package.json
- "scripts": {
- "serve": "webpack serve --config webpack.config.js"
- }
-
- npm run serve
- const path = require("path");
-
- module.exports = {
- mode: "development",
- entry: "./src/main.js",
- output: {
- filename: "bundle.js",
- path: path.resolve(__dirname, "./build");
- }
- devServer: {
- // 默认为true
- hot: true,
- // 默认为localhost(127.0.0.1),也可以修改为0.0.0
- host: "0.0.0",
- // 默认是8080
- port: 8888,
- // 运行代码后自动打开浏览器
- open: true
- // 是否对文件进行压缩
- compress: true
- }
- }
- // main.js
- if(module.hot) {
- module.hot.accept("./utils.js", () => {
- console.log("utils更新了")
- })
- }
proxy 是用来设置代理来解决跨域访问的问题:
我们可以进行如下的设置:
changeOrigin 是要修改代理请求中的headers 中的host 属性
- npm install webpack-merge -D
-
- "scripts": {
- "serve": "webpack serve --config ./webpack.dev.config.js",
- "build": "webpack --config ./webpack.prod.config.js"
- }
-
- npm run serve
- npm run build
-
- // webpack.dev.config.js
- const { merge } = require("webpack-merge");
- const commonConfig = require("./webpack.comn.config.js");
- module.exports = merge(commonConfig, {
- mode: "development",
- entry: "",
- output: ""
- })
-
- // webpack.prod.config.js
- const { merge } = require("webpack-merge");
- const commonConfig = require("./webpack.comn.config.js");
- module.exports = merge(commonConfig, {
- mode: "production",
- entry: "",
- output: ""
- })
-
- // webpack.comn.config.js
- // 用于书写公共配置