博客
关于我
Webpack教程:如何从头开始设置 webpack 5
阅读量:557 次
发布时间:2019-03-09

本文共 3520 字,大约阅读时间需要 11 分钟。

Webpack 实用指南

Webpack 是现代前端开发中不可或缺的工具,它能够将复杂的项目打包成高效的 bundles。以下是使用 Webpack 的实用指南,涵盖从安装到高级配置的全过程。

1. 安装

首先,创建一个项目目录并初始化包.json:

mkdir webpack-tutorialcd webpack-tutorialnpm init -y

安装必要的依赖项:

npm install -D webpack webpack-cli html-webpack-plugin clean-webpack-plugin sass-loader postcss-loader style-loader babel-loader

创建 src 目录并添加 index.js 文件:

// src/index.jsconsole.log('Interesting!');

创建 webpack 配置文件:

// webpack.config.jsconst path = require('path');const HtmlWebpackPlugin = require('html-webpack-plugin');const CleanWebpackPlugin = require('clean-webpack-plugin');module.exports = {  entry: {    main: path.resolve(__dirname, './src/index.js'),  },  output: {    path: path.resolve(__dirname, './dist'),    filename: '[name].bundle.js',  },  module: {    rules: [      {        test: /\.js$/,        exclude: /node_modules/,        use: 'babel-loader',      },    ],  },  plugins: [    new HtmlWebpackPlugin({      title: 'Webpack Boilerplate',      template: path.resolve(__dirname, './src/template.html'),      filename: 'index.html',    }),    new CleanWebpackPlugin(),  ],};

2. 基本配置

Entry

entry 配置指定了 Webpack 从哪里开始打包。例如:

entry: {  main: path.resolve(__dirname, './src/index.js'),},

Output

output 配置定义了打包后的文件路径和文件名。例如:

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

在 package.json 中添加构建脚本:

{  "scripts": {    "build": "webpack"  }}

运行构建命令:

npm run build

3. 插件

Webpack 提供了强大的插件系统,可以扩展其功能。常用的插件包括:

  • html-webpack-plugin:自动生成 HTML 模板
  • clean-webpack-plugin:清理构建目录
  • sass-loader:处理 SCSS 文件
  • babel-loader:将现代 JavaScript 转换为浏览器支持的格式

4. HTML 模板

创建或修改 src/template.html:

  
<%= htmlWebpackPlugin.options.title %>

在 webpack.config.js 中添加 HTML 插件配置:

plugins: [  new HtmlWebpackPlugin({    title: 'Webpack Boilerplate',    template: path.resolve(__dirname, './src/template.html'),    filename: 'index.html',  }),  new CleanWebpackPlugin(),],

5. 模块加载

Webpack 可以处理多种文件类型。例如:

  • JavaScript:使用 babel-loader
  • CSS 和 SCSS:使用 sass-loadercss-loader
  • 图像:使用 asset/resource
  • 字体和 SVG:使用 asset/inline

在 webpack.config.js 中添加样式处理规则:

module: {  rules: [    {      test: /\.js$/,      exclude: /node_modules/,      use: 'babel-loader',    },    {      test: /\.(scss|css)$/,      use: ['style-loader', 'css-loader', 'postcss-loader', 'sass-loader'],    },    {      test: /\.(ico|gif|png|jpg|jpeg)$/,      type: 'asset/resource',    },    {      test: /\.(woff(2)?|eot|ttf|otf|svg)/,      type: 'asset/inline',    },  ],},

6. 开发服务器

为了提升开发效率,使用 webpack-dev-server

安装依赖项:

npm install -D webpack-dev-server

修改 package.json:

{  "scripts": {    "start": "webpack serve"  }}

在 webpack.config.js 中启用开发模式:

module.exports = {  mode: 'development',  devServer: {    historyApiFallback: true,    contentBase: path.resolve(__dirname, './dist'),    open: true,    compress: true,    hot: true,    port: 8080,  },  plugins: [    new webpack.HotModuleReplacementPlugin(),  ],};

运行开发服务器:

npm start

7. 高级配置

模块分隔

使用 webpack-merge 合并不同环境的配置:

安装依赖项:

npm install -D webpack-merge

创建不同的配置文件:

// webpack/prod.config.jsconst baseConfig = require('./webpack.config.js');module.exports = merge(baseConfig, {  mode: 'production',  output: {    filename: '[name].min.js',  },});
// webpack/dev.config.jsconst baseConfig = require('./webpack.config.js');module.exports = merge(baseConfig, {  mode: 'development',  devServer: {    // 开发服务器配置  },});

8. 注意事项

  • Webpack 5:与之前版本有显著差异,特别是对模块类型的处理。
  • 依赖项管理:确保所有依赖项都通过 npm安装或 yarn add。
  • 构建缓存:可以使用 cache 插件提升构建速度。

通过以上配置,您可以轻松使用 Webpack 进行项目打包和开发。随着对 Webpack 的深入了解,您可以根据项目需求灵活配置,充分发挥其强大功能。

转载地址:http://ocxsz.baihongyu.com/

你可能感兴趣的文章
Nginx反向代理与正向代理配置
查看>>
Nginx多域名,多证书,多服务配置,实用版
查看>>
nginx异常:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf
查看>>
nginx总结及使用Docker创建nginx教程
查看>>
nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:128
查看>>
nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in usrlocalnginxconfnginx.conf128
查看>>
nginx最最最详细教程来了
查看>>
Nginx服务器上安装SSL证书
查看>>
Nginx服务器的安装
查看>>
Nginx模块 ngx_http_limit_conn_module 限制连接数
查看>>
nginx添加模块与https支持
查看>>
Nginx用户认证
查看>>
Nginx的location匹配规则的关键问题详解
查看>>
Nginx的Rewrite正则表达式,匹配非某单词
查看>>
Nginx的使用总结(一)
查看>>
Nginx的使用总结(三)
查看>>
Nginx的使用总结(二)
查看>>
Nginx的可视化神器nginx-gui的下载配置和使用
查看>>
Nginx的是什么?干什么用的?
查看>>
Nginx访问控制_登陆权限的控制(http_auth_basic_module)
查看>>