• webpack的基本使用


    本文主要介绍webpack的基本使用,通过简单的打包流程进行具体说明。

    1.新建pack文件夹,运行npm init -y初始化package.json文件,不用-y则需要手动进行配置。

    package.json文件内容如下:

    1. {
    2. "name": "pack",
    3. "version": "1.0.0",
    4. "description": "",
    5. "main": "index.js",
    6. "scripts": {
    7. "test": "echo \"Error: no test specified\" && exit 1",
    8. "dev": "webpack-dev-server"
    9. },
    10. "keywords": [],
    11. "author": "",
    12. "license": "ISC",
    13. "dependencies": {
    14. "jquery": "^3.6.0"
    15. },
    16. "devDependencies": {
    17. "webpack": "^5.74.0",
    18. "webpack-cli": "^4.10.0",
    19. "webpack-dev-server": "^4.10.0"
    20. }
    21. }

    2.新建src文件夹,新建index.html和index.js文件

    3.安装jquery,书写index.js文件如下

    1. import $ from 'jquery'
    2. $(function() {
    3. $('.btn').click(function() {
    4. $('#showContent').slideDown();
    5. $('#showContent').css('backgroundColor', 'yellow')
    6. })
    7. })

    4.安装webpack和webpack-cli包,创建webpack.config.js文件,配置内容如下:

    1. module.exports = {
    2. mode: 'development',
    3. devServer: {
    4. open: true,
    5. host: '127.0.0.1',
    6. port: 8800,
    7. static: './'
    8. }
    9. }

     5.安装webpack-dev-server包,修改命令如下:

    1. "scripts": {
    2. "test": "echo \"Error: no test specified\" && exit 1",
    3. "dev": "webpack-dev-server"
    4. },

     6.index.html文件引入"/bundle.js"文件,运行npm run dev命令即可

    7.上述文件运行后并不会直接打开首页,故需要进行配置。首先运行npm i html-webpack-plugin -D

     命令,安装html-webpack-plugin包,配置预览页面。在webpack.config.js中的具体配置如下:

    1. const HtmlWebpack = require('html-webpack-plugin')
    2. const h = new HtmlWebpack({
    3. template: './src/index.html',
    4. filename: 'index.html'
    5. })
    6. module.exports = {
    7. mode: 'development',
    8. devServer: {
    9. open: true,
    10. host: '127.0.0.1',
    11. port: 8800,
    12. static: './'
    13. },
    14. plugins: [h]
    15. }

    8.在package.json文件中定义如下内容

    1. "scripts": {
    2. "test": "echo \"Error: no test specified\" && exit 1",
    3. "dev": "webpack-dev-server",
    4. "build": "webpack"
    5. }

    运行npm run build命令即可对文件进行打包,会生成dist这一最终的文件夹。

    9.可进行项目入口与出口文件的配置。默认入口文件是src目录下的index.js文件,出口文件是main.js,现配置为bundle.js.需要使用npm内置的path包,完整代码如下:

    1. const HtmlWebpack = require('html-webpack-plugin')
    2. const path = require('path')
    3. const h = new HtmlWebpack({
    4. template: './src/index.html',
    5. filename: 'index.html'
    6. })
    7. module.exports = {
    8. mode: 'development',
    9. devServer: {
    10. open: true,
    11. host: '127.0.0.1',
    12. port: 8800,
    13. static: './'
    14. },
    15. plugins: [h],
    16. entry: path.join(__dirname, './src/index.js'),
    17. output: {
    18. path: path.join(__dirname, './dist'),
    19. filename: 'bundle.js'
    20. }
    21. }

  • 相关阅读:
    2022中国五金制品行业发展前景分析
    BI技巧丨矩阵高亮
    day03-2-拓展
    C# IO Stream 流(二)扩展类_封装器
    图解LeetCode——1413. 逐步求和得到正数的最小值(难度:简单)
    2011--利用数学知识解题
    Gateway服务网关
    HTML是指什么 HTML的基本工作原理
    基于Django框架的Python学生管理系统
    从Hugging Face下载数据测试whisper、fast_whisper耗时
  • 原文地址:https://blog.csdn.net/weixin_44827643/article/details/126309661