• 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. }

  • 相关阅读:
    Keras训练一个基本体系化的分类模型流程案例
    今天是1024,获取一下纪念牌
    MyBatis环境配置及查询操作
    一同走进Linux的“基操”世界
    【解读】基于PREEvision的诊断设计
    数字峰会人气火爆,城链科技引发新一轮商业变革
    使用Docker辅助图像识别程序开发:在Docker中显示GUI、访问GPU、USB相机以及网络
    pandas时间序列之 pd.to_datetime()
    Postman使用总结2
    「软考」复习方法+备考资料赠送
  • 原文地址:https://blog.csdn.net/weixin_44827643/article/details/126309661