search
HomeWeb Front-endJS TutorialLearn more about the module packaging tool webpack

What is webpack? This article will introduce you to the module packaging tool webpack. I hope it will be helpful to you!

1. What is webpack

1. Official explanation

Essentially speaking, Webpack is a static modular packaging tool for current JavaScript applications. (This sentence is summarized from two points, namely module and packaging)

Learn more about the module packaging tool webpack

##Let’s talk about

module## The concept of #andpackaging!

2. Front-end modularization

Some solutions for front-end modularization: AMD, CMD, CommonJS, ES6 (browser
    cannot recognize
  • them, but webpack can be their underlying support before modular development can be carried out)
  • ES6
  • Previously, if we wanted to carry out modular development, we had to use other tools so that we could carry out modular development and after completing the project through modular development, we still needed to process
  • Various dependencies between modules
  • , and integrate and package them
  • webpack
  • appears at this time, one of the cores is to make it possible Carry out modular development, and will help us deal with dependencies between modules. Instead of just
  • JavaScript
  • files, our CSS, images, json files, etc. are in webpack All can be used as modules.

3. How to understand packaging?

webpack can help us
    modularize
  • and handle various complex relationships between modules Finally, the concept of packaging is easy to understand. is to package and merge various resource modules in webpack into multiple packages (Bundle)
  • and during the packaging process, you can also Resource processing
  • , such as compressing images, converting scss to css, converting ES6 syntax to ES5 syntax, converting TypeScript to JavaScript, etc.
  • Packaging tools and grunt/gulp
  • Comparison between webpack and grunt/gulp

The core of grunt/gulp is Task
    1. You can
  • configure

    a series of tasks, and define the
    transactions to be processed by the task (such as ES6, ts conversion, image compression , convert scss to css) 2. Then let grunt/gulp execute these tasks in sequence, and automate the entire
    process

    Let’s look at a gulp task
  • 1. The following task is to convert all js files under src into ES5 syntax.
  • 2. And finally output to the dist folder.




    Learn more about the module packaging tool webpack

    #When to use grunt/gulp?
  • 1. The project module dependencies are very simple, and the concept of modularization is not even used
  • 2. Only simple merging and compression are needed, just use grunt/gulp

    3. If the entire project uses Modular management and very strong interdependence, we can use webpack.


    What is the difference between grunt/gulp and webpack?
  • 1.
  • grunt/gulp puts more emphasis on the automation of the front-end process, and modularization is not its core


    2.webpack puts more emphasis on modularizationdevelopment management, and file Compression, merging, preprocessing and other functions are its included functions

  • 2. Installation of webpack

wepack

In order to operate normally, it must rely on the

node environment, and in order for the node environment to execute normally, it must use the npm tool to manage various dependent packages in node)So to install webpack, you must first install Node.js. Node.js comes with the package management tool npm

    Install webpack globally (npm install webpack@3.6.0 -g)
  • Partial installation webpck (npm install webpack@3.6.0 --save-dev) – save-dev is a development dependency and does not need to be used after the project is packaged.
  • 3. Webpack configuration

##1. File and folder parsing

  • dist folder: is used to store the files that will be packaged later
  • src folder: is used to store the source files we wrote
    main.js: The entry file of the project.
    mathUtils.js: Defines some mathematical tool functions that can be referenced and used in other places.
    index.html: The browser opens the displayed homepage html (the content of the final packaged file in src, that is, the dist folder is quoted here).
    package.json: File generated through npm init and managed by npm package.

The following is the code in the mathUtils.js file and the main.js file: (CommonJS modular specification, CommonJS is a modular standard, nodejs It is the implementation of CommodJS (modularization)

Learn more about the module packaging tool webpack

2, command

webpack. /src/main.js ./dist/bundle.js(Package the main.js file into a bundle.js file)

Note: In the same way, you can also use the ES6 modular specification

Learn more about the module packaging tool webpack

3. Create the webpack.config.js file to simplify the packaging command

(map the packaging command to packaging Entry and exit)

Code in this file:

Learn more about the module packaging tool webpack

##entry: is the packaged entry

output: For the packaged export

    we need to obtain the path in the output dynamically,
  • so we can use Node.js syntax to import A module. This module is path (const path = require('path'). The file within the quotation marks needs to be found in the node package)
  • Used through the
  • npm init command (initialization)node.js
  • Then generate the
  • package.json file (this file describes the information of the current project) license: "ISC" (open source for the project), the following is package.json Code

Explanation: If you want to use node, you must rely on the package.json file

Learn more about the module packaging tool webpack

Run

After npm install webpack@3.6.0 --save-dev, add the following dependencies

Learn more about the module packaging tool webpack

4. Map the webpack command to npm run

In addition to mapping

webpack to entry and exit, you can also map webpack The command is mapped to npm run Some operations (need to be modified in the **"script"** script tag in package.json). .

4. How to use loader

1. What is loader

  • loader is a very core concept in webpack
Now let’s think about what webpack is used for?

    We mainly use webpack to process js code, and webpack will automatically handle dependencies related to js before.
  • However, in development, it is not only processed by the basic js code, but also needs to be loaded. css, pictures, including some advanced ones such as converting ES6 to ES5 code, converting TypeScript to ES5 code, converting scss and less to css, converting .jsx and .vue files into js files, etc.
  • Regarding the capabilities of webpack itself, these transformations are not supported.
  • At this time, just extend the
  • webpack extension corresponding to the loader.

2. Loader usage process

1), css file processing

Preparation work:

1. Create a css file in the src directory, and create a normal.css file in it

2. Reorganize the directory structure of the file and place scattered js files in The code in

3 and normal.css in a js folder is very simple, that is, setting the body to red


Learn more about the module packaging tool webpack

4, but at this time the code in normal.css The style will not take effect yet, because it is not referenced, and webpack cannot find it, because we only have one entry, and webpack will look for other dependent files starting from the entry.

5. At this time we must reference

main.js that is entry file and then we need to import Corresponding loader

is used!

  • Step 1:Install the loader you need to use through npm
    (npm install --save-dev css-loader) (npm install - -save-dev style-loader)
    In the official website of webpack, find the following usage method of style loader:
    Learn more about the module packaging tool webpack
    Learn more about the module packaging tool webpack

  • Step 2: Configure under the modules keyword in webpack.config.js

Instructions: Among them css-loader is only responsible for loading css files, and is not responsible for embedding specific css styles into documents

At this time, we also need a style- loaderHelp us handle

Learn more about the module packaging tool webpack

Note: style-loader needs to be placed in front of css-loader.

2), less file processing

  • Step 1: Install the corresponding loader (note: here Less is also installed because webpack will use less to compile less files). Command: npm install --save-dev less-loader less

  • Step 2: Modify the corresponding configuration file (in webpack.config .js) for processing .less files. As follows:

Learn more about the module packaging tool webpack

3), image file processing

  • Step 1: Add two pictures to the project (one is less than 8kb, the other is larger than 8kb)

  • Step 2: First consider adding css Reference images in the style, is as follows

    Learn more about the module packaging tool webpack

  • ##Step 3: Modify the corresponding configuration file (in webpack .config.js) for processing image files. As follows:

    Learn more about the module packaging tool webpack

  • Step 4: An error was found after packaging, because images larger than 8kb will pass file-loader Processing, but there is no file-loader in our project. (You need to install file-loader, command npm install --save-dev file-loader). After installation and packaging, you will find that there is an additional image file in the dist folder.

    Learn more about the module packaging tool webpack

Description:

  • found that webpack automatically generated a very Long name

    1. This is a 32-bit hash value, the purpose is to prevent name duplication

    2. However, in actual development, there may be certain requirements for packaged image names

  • So, we can add the following options in options:

    1. img: the folder where the file is to be packaged

    2. name: get the original name of the picture and put it in This position
    3. hash8: In order to prevent image name conflicts, hash is still used, but only 8 bits are retained
    4. ext: Use the original extension of the image
    as follows:

    Learn more about the module packaging tool webpack

  • You also need to configure and modify the path used by the image

    1. By default, webpack will return the generated path to the user

    2. However, The entire program is packaged in the dist folder, so here you need to add another dist/
    to the path as follows:

    Learn more about the module packaging tool webpack

In summary, after packaging , the image file is like this


Learn more about the module packaging tool webpack

4), ES6 to ES5 babel

Learn more about the module packaging tool webpack

5 , Configure Vue in webpack

    Step 1: Terminal execution command (npm install --save vue)
  • Step 2: Import vue in main.js (import Vue from 'vue' ) as follows

Learn more about the module packaging tool webpack

  • Step 3: Hang p to vue in index.html For example, as follows

Learn more about the module packaging tool webpack

  • Step 4: An error is found after packaging. We need to specify that the vue we are using is the runtime-compiler version of.

Learn more about the module packaging tool webpack
Specific operations: You need to add resolve to webpack and take an alias (alias), as follows:

Learn more about the module packaging tool webpack

6. How to use Vue in webpack

Step 1: Hang p on the vue instance in index.html

Learn more about the module packaging tool webpack

Step 2: Import the APP component into the main.js file, and register the APP, in the Vue instance Use this component APP (Componentization) in the Vue template

Learn more about the module packaging tool webpack

Step 3: Create the APP.vue file and change the # of the vue page ##Template and js code, css code are separated, as follows

Learn more about the module packaging tool webpack

Step 4: Configure the corresponding loader of vue ,

Learn more about the module packaging tool webpack

Modify the configuration file of webpack.config.js:

Learn more about the module packaging tool webpack

7. Use of plugin

1. Get to know the plugin

    What is the plugin?
  • 1. Plugin means plug-in, which is usually used to extend an existing architecture.
    2. Plug-ins in webpack are various extensions to the existing functions of webpack, such as packaging optimization, file Compression, etc.
  • The difference between loader and plugin
  • 1. Loader is mainly used to convert certain types of modules. It is a
    converter 2. Plugin is a plug-in. The extension of webpack itself is an
    extender
  • plugin usage process
  • 1. Step 1: Installing through npm requires the use of plugins (some webpack already built-in plugins do not need to Installation)
    2. Step 2: Configure the plug-in in plugins in webpack.config.js

2. Webpack-Add copyright information Plugin usage

Learn more about the module packaging tool webpack

3. Plugin for packaging html

Learn more about the module packaging tool webpack

4. js compression Plugin

Learn more about the module packaging tool webpack

##8. Build a local server

    Webpack provides an optional
  • local development server

    . This local server is built based on node.js and uses the express framework internally to achieve what we want for the browser Automatically refresh to display the modified results .

  • But it is a separate module, you need to install it before using it in webpck
  • Command: (npm install --save-dev webpack-dev-server@2.9.1 )


  • devserver is also an
  • option

    in webpack. The option itself can be set with the following properties: 1. contentBase: which folder provides local services, the default is the root folder, we need to fill in ./dist 2. port: port number
    3. inline: page refresh in real time
    4. historyApiFallback: In the SPA page, rely on the history mode of HTML5

  • The webpack.config.js file configuration is modified as follows:

  • Learn more about the module packaging tool webpack

  • –open parameter means opening the browser directly

In addition, Learn more about the module packaging tool webpack
below we want to separate the webpack configuration file: that is,

The things you need to use during development

are separated from the things you need to publish(compile). as follows:

Learn more about the module packaging tool webpack

9. Vue CLI

#1. What does CLI mean?

  • is Command-Line Interface, translated as command line interface, but commonly known as scaffolding
  • Vue CLI is an officially released vue.js project scaffolding
  • Using vue-cli can quickly build the Vue development environment and the corresponding webpack configuration

2. Prerequisites for using Vue CLI - Node (node ​​needs to be installed)

However, using Node must involve npm,

What is NPM?

  • The full name of NPM is Node Package Manager
  • is a NodeJS package management and distribution tool that has become an unofficial standard for publishing Node modules (packages)
  • NPM is often used to install some dependency packages during the development process.

3. Use of Vue CLI

  • Install Vue scaffolding

npm install -g @vue/cli

Note: The version installed above is the Vue CLI3 version. If you need to initialize the project according to the Vue CLI2 method, it is not possible

  • Vue CLI2 initialization project

vue init webpack my-project

For more node-related knowledge, please visit: nodejs tutorial!

The above is the detailed content of Learn more about the module packaging tool webpack. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete
VUE3入门教程:使用Webpack进行打包和构建VUE3入门教程:使用Webpack进行打包和构建Jun 15, 2023 pm 06:17 PM

Vue是一款优秀的JavaScript框架,它可以帮助我们快速构建交互性强、高效性好的Web应用程序。Vue3是Vue的最新版本,它引入了很多新的特性和功能。Webpack是目前最流行的JavaScript模块打包器和构建工具之一,它可以帮助我们管理项目中的各种资源。本文就为大家介绍如何使用Webpack打包和构建Vue3应用程序。1.安装Webpack

vite和webpack的区别是什么vite和webpack的区别是什么Jan 11, 2023 pm 02:55 PM

区别:1、webpack服务器启动速度比vite慢;由于vite启动的时候不需要打包,也就无需分析模块依赖、编译,所以启动速度非常快。2、vite热更新比webpack快;vite在HRM方面,当某个模块内容改变时,让浏览器去重新请求该模块即可。3、vite用esbuild预构建依赖,而webpack基于node。4、vite的生态不及webpack,加载器、插件不够丰富。

如何使用PHP和webpack进行模块化开发如何使用PHP和webpack进行模块化开发May 11, 2023 pm 03:52 PM

随着Web开发技术的不断发展,前后端分离、模块化开发已经成为了一个广泛的趋势。PHP作为一种常用的后端语言,在进行模块化开发时,我们需要借助一些工具来实现模块的管理和打包,其中webpack是一个非常好用的模块化打包工具。本文将介绍如何使用PHP和webpack进行模块化开发。一、什么是模块化开发模块化开发是指将程序分解成不同的独立模块,每个模块都有自己的作

vue webpack可打包哪些文件vue webpack可打包哪些文件Dec 20, 2022 pm 07:44 PM

在vue中,webpack可以将js、css、图片、json等文件打包为合适的格式,以供浏览器使用;在webpack中js、css、图片、json等文件类型都可以被当做模块来使用。webpack中各种模块资源可打包合并成一个或多个包,并且在打包的过程中,可以对资源进行处理,如压缩图片、将scss转成css、将ES6语法转成ES5等可以被html识别的文件类型。

Webpack是什么?详解它是如何工作的?Webpack是什么?详解它是如何工作的?Oct 13, 2022 pm 07:36 PM

Webpack是一款模块打包工具。它为不同的依赖创建模块,将其整体打包成可管理的输出文件。这一点对于单页面应用(如今Web应用的事实标准)来说特别有用。

webpack怎么将es6转成es5的模块webpack怎么将es6转成es5的模块Oct 18, 2022 pm 03:48 PM

配置方法:1、用导入的方法把ES6代码放到打包的js代码文件中;2、利用npm工具安装babel-loader工具,语法“npm install -D babel-loader @babel/core @babel/preset-env”;3、创建babel工具的配置文件“.babelrc”并设定转码规则;4、在webpack.config.js文件中配置打包规则即可。

使用Spring Boot和Webpack构建前端工程和插件系统使用Spring Boot和Webpack构建前端工程和插件系统Jun 22, 2023 am 09:13 AM

随着现代Web应用程序的复杂性不断增加,构建优秀的前端工程和插件系统变得越来越重要。随着SpringBoot和Webpack的流行,它们成为了一个构建前端工程和插件系统的完美组合。SpringBoot是一个Java框架,它以最小的配置要求来创建Java应用程序。它提供了很多有用的功能,比如自动配置,使开发人员可以更快、更容易地搭建和部署Web应用程序。W

webpack处理css浏览器兼容性问题webpack处理css浏览器兼容性问题Aug 09, 2022 pm 02:50 PM

本篇文章给大家带来了关于javascript的相关知识,其中主要介绍了关于webpack处理css浏览器兼容性的相关问题,包括了postcss-loader和postcss-preset-env插件使用的相关内容,下面一起来看一下,希望对大家有帮助。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft