Home Web Front-end JS Tutorial What should newbies get started with Webpack?

What should newbies get started with Webpack?

Jun 26, 2017 pm 03:24 PM
web webpack Getting Started Tutorial study newbie notes

WebpackGetting Started Tutorial(Study Notes)

1 Introduction This tutorial is not in-depth and removes a lot of complicated

things. The record also insists on the simplest, so that beginners can have a general understanding of

webpackHave a simple understanding of the system, and it is better to further learn in depthwebpack.

##Webpack is a

Javscript

's packaging program, webpack will automatically analyze the dependencies between each module, and then package these dependencies into one or more files.

##webpackThe most powerful thing is that it can pass Official and third-party plug-ins and loaders(loader)

are used to parse and compile various files.

WebpackThe four most important core concepts: Entry( entry)

, output

(output), loader(loader), plug-in(plugin), below I will try to explain a few by taking notes. The meaning of this concept. If you want to go deeper or if you don’t understand something in your notes, you can check it in the documentation on the official website.

#2

、entry

(entry)webpack The entry of is equivalent to the index file of a web page. With the entry file,

webpack

will know where to start. webpack will use this entry The file analyzes all the files that the entry file depends on, and then packages all the dependent files into one or more files. ##provided by webpack

Single entry syntax and object syntax are included. The single entry syntax is also the simplest one, with only one entry file, one in and one out. For example, the one below.

1 {2   entry: './index.js'3 }
Copy after login

Object syntax is mainly for applications with multiple pages. It tells webpack that there are three entry files. When the packaging is completed There are also three files. These three files are independent of each other. Each file only contains the files it depends on. For example:

1  {2   entry: {3    hello1: './hello1.js',4    hello2: './hello2.js'5   }6  }
Copy after login

 

 

3、输出(output)

webpack提供了output属性,来控制webpack如何把编译好的文件写入到硬盘中,输入和输出是对应的,有输入就有输出。但是必须注意一点,可以存在多个输入,但是只能存在一个输出,那怎么来输出多个独立的编译好的文件呢?webpack中当然有应对的机制。

 

webpack要求output属性为对象,并且必须包含两个属性:filenamepath。顾名思义filename即输出文件的文件名,而path则为输出文件的绝对路径(注意,path必须为决定路径)

 

单个入口output属性写法:

1 {2     entry: './index.js',3     output: {4       path: path.resolve(__dirname, 'app'), //path为nodejs自身的库。__dirname为nodejs在运行过程中的一个环境变量,里面是当前文件夹的完整目录名。resolve方法是把相对路径的app目录解析为一个决定路径。5 6      filename: 'bundle.js'7  }8 }
Copy after login

 

webpack内置了多个变量来应对多个入口文件,如[name][hash][id][chunkhash],通过变量来保证每个文件的唯一性来达到生成多个文件,在生成过程中webpack会把这几个变量替换为相应的字符串用于保证文件的唯一性。

 

多个入口output属性写法:

1 {2     entry: './index.js',3     output: {4         path: path.resolve(__dirname, 'app'),5         filename: [name]-[hash]-bundle.js6      }7 }
Copy after login

 

 

4、加载器(loader)

loader可以对不同类型的文件进行编译转换,比如jsxtypescript直接拿在浏览器上运行是不能运行的,那么我们在编写程序的时候需要借助jsx以及typescript等高效的库来提高我们编写程序的效率,但是我们又需要能正常使用,如果每种文件类型我们都通过一种转换工具,那么就显的很麻烦,所以laoder就是来处理这样的工作。

 

首先在使用loader的时候我们需要安装相应的插件,比如es2015,那我们安装babel-loader,如果是css,那我们安装css-loader,通过下面的module属性里面的rules数组来对需要转换的文件设置loader

 

 1 { 2     entry: './index.js', 3     output: { 4         path: path.resolve(__dirname, 'app'), 5         filename: [name]-[hash]-bundle.js 6     }, 7     module: { 8         rules : [ 9             {test: /\.js$/, use: 'babel-loader'}10          ]11      }12 }
Copy after login

 

上面的rules是一个数组,每个元素是一个对象,对象里面包含了两个属性testusetest的值是一个正则表达式,它的作用是将当前loader用于什么文件,这里正则表达式就是用来匹配你需要转换的文件类型,use是当前匹配到的文件用什么加载器来转换、编译。

 

有三种方式来使用loader加载器

1webpack配置文件

2require语句中使用

3、通过命令行使用

 

第一种上面我们已经说了,下面简单的介绍一下第二种和第三种,第二种使用方法是我们在require或者import文件的时候可以直接使用,比如下面的代码:

 

1 require('babel-loader!./hello.js')
Copy after login

或者

1 Import('babel-loader!./hello.js')
Copy after login

 

第三种方式是直接通过webpack提供的命令行工具—module-bind使用,比如下面的代码:

1 webpack —module-bind 'js=babel-loader'
Copy after login

 

5、插件(plugin)

插件用于解决loader无法解决的事情,比如给每个js文件进行添加著作标记、压缩文件等功能,每个插件都可能有参数选项,每个插件在使用的时候也必须使用new操作符来建立一个插件的实例。插件通过plugins属性来设置,plugins是一个数组,每个元素代表一个插件的实例。因为插件有官方的还有第三方的,所以不会一一去说怎么使用,只是给大家简单演示一下,大家需要用到哪个插件再去查这个插件的api

 

 1 const HtmlWebpackPlugin = require('html-webpack-plugin'); 2 //首先要使用插件,必须先引入插件 3   4 { 5   entry: './index.js', 6   output: { 7    path: path.resolve(__dirname, 'app'), 8    filename: [name]-[hash]-bundle.js 9    },10   module: {11     rules : [12        {test: /\.js$/, use: 'babel-loader'}13     ]14    },15    plugins: [16     new HtmlWebpackPlugin({telmplate : './index.html'})  //通过plugins来使用你需要使用插件。17    ]18 }
Copy after login

6、总结

通过上面的学习,你可以了解到webpack的四个核心,入口、输出、加载器、插件,入口就是你要编译的是哪个文件,指定了过后webpack会自行寻找依赖的文件打包编译。输出就是编译转换好了过后把文件写入到硬盘的哪里。加载器就是对不同类型的文件转换,从而让浏览器能直接运行。插件做的是loader无法解决的事情。

 

 

其实webpack的配置并没有想象中的那么复杂,webpack的配置文件就是一个js文件,只要对webpack有一个系统的认识后,你就知道我该从哪里下手,该从哪里入手了。

 

 

 

 

The above is the detailed content of What should newbies get started with Webpack?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to delete Xiaohongshu notes How to delete Xiaohongshu notes Mar 21, 2024 pm 08:12 PM

How to delete Xiaohongshu notes? Notes can be edited in the Xiaohongshu APP. Most users don’t know how to delete Xiaohongshu notes. Next, the editor brings users pictures and texts on how to delete Xiaohongshu notes. Tutorial, interested users come and take a look! Xiaohongshu usage tutorial How to delete Xiaohongshu notes 1. First open the Xiaohongshu APP and enter the main page, select [Me] in the lower right corner to enter the special area; 2. Then in the My area, click on the note page shown in the picture below , select the note you want to delete; 3. Enter the note page, click [three dots] in the upper right corner; 4. Finally, the function bar will expand at the bottom, click [Delete] to complete.

What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? Mar 21, 2024 pm 09:30 PM

As a Xiaohongshu user, we have all encountered the situation where published notes suddenly disappeared, which is undoubtedly confusing and worrying. In this case, what should we do? This article will focus on the topic of "What to do if the notes published by Xiaohongshu are missing" and give you a detailed answer. 1. What should I do if the notes published by Xiaohongshu are missing? First, don't panic. If you find that your notes are missing, staying calm is key and don't panic. This may be caused by platform system failure or operational errors. Checking release records is easy. Just open the Xiaohongshu App and click "Me" → "Publish" → "All Publications" to view your own publishing records. Here you can easily find previously published notes. 3.Repost. If found

How to add product links in notes in Xiaohongshu Tutorial on adding product links in notes in Xiaohongshu How to add product links in notes in Xiaohongshu Tutorial on adding product links in notes in Xiaohongshu Mar 12, 2024 am 10:40 AM

How to add product links in notes in Xiaohongshu? In the Xiaohongshu app, users can not only browse various contents but also shop, so there is a lot of content about shopping recommendations and good product sharing in this app. If If you are an expert on this app, you can also share some shopping experiences, find merchants for cooperation, add links in notes, etc. Many people are willing to use this app for shopping, because it is not only convenient, but also has many Experts will make some recommendations. You can browse interesting content and see if there are any clothing products that suit you. Let’s take a look at how to add product links to notes! How to add product links to Xiaohongshu Notes Open the app on the desktop of your mobile phone. Click on the app homepage

Revealing the appeal of C language: Uncovering the potential of programmers Revealing the appeal of C language: Uncovering the potential of programmers Feb 24, 2024 pm 11:21 PM

The Charm of Learning C Language: Unlocking the Potential of Programmers With the continuous development of technology, computer programming has become a field that has attracted much attention. Among many programming languages, C language has always been loved by programmers. Its simplicity, efficiency and wide application make learning C language the first step for many people to enter the field of programming. This article will discuss the charm of learning C language and how to unlock the potential of programmers by learning C language. First of all, the charm of learning C language lies in its simplicity. Compared with other programming languages, C language

Let's learn how to input the root number in Word together Let's learn how to input the root number in Word together Mar 19, 2024 pm 08:52 PM

When editing text content in Word, you sometimes need to enter formula symbols. Some guys don’t know how to input the root number in Word, so Xiaomian asked me to share with my friends a tutorial on how to input the root number in Word. Hope it helps my friends. First, open the Word software on your computer, then open the file you want to edit, and move the cursor to the location where you need to insert the root sign, refer to the picture example below. 2. Select [Insert], and then select [Formula] in the symbol. As shown in the red circle in the picture below: 3. Then select [Insert New Formula] below. As shown in the red circle in the picture below: 4. Select [Radical Formula], and then select the appropriate root sign. As shown in the red circle in the picture below:

How to enable administrative access from the cockpit web UI How to enable administrative access from the cockpit web UI Mar 20, 2024 pm 06:56 PM

Cockpit is a web-based graphical interface for Linux servers. It is mainly intended to make managing Linux servers easier for new/expert users. In this article, we will discuss Cockpit access modes and how to switch administrative access to Cockpit from CockpitWebUI. Content Topics: Cockpit Entry Modes Finding the Current Cockpit Access Mode Enable Administrative Access for Cockpit from CockpitWebUI Disabling Administrative Access for Cockpit from CockpitWebUI Conclusion Cockpit Entry Modes The cockpit has two access modes: Restricted Access: This is the default for the cockpit access mode. In this access mode you cannot access the web user from the cockpit

C Language vs. C: Which Is Better for New Programmers C Language vs. C: Which Is Better for New Programmers Mar 19, 2024 am 08:30 AM

C Language or C: Which Is More Suitable for New Programmers In the era of rapid development of modern technology, learning programming has become an increasingly popular choice, whether as part of career development or as a way to improve logical thinking skills. Among many programming languages, C language and C are both very classic and representative languages. Many people are confused about how to choose C language or C as an entry-level programming language. So, is C language more suitable for programming novices, or is C more suitable? Need specific code examples to

How to publish notes tutorial on Xiaohongshu? Can it block people by posting notes? How to publish notes tutorial on Xiaohongshu? Can it block people by posting notes? Mar 25, 2024 pm 03:20 PM

As a lifestyle sharing platform, Xiaohongshu covers notes in various fields such as food, travel, and beauty. Many users want to share their notes on Xiaohongshu but don’t know how to do it. In this article, we will detail the process of posting notes on Xiaohongshu and explore how to block specific users on the platform. 1. How to publish notes tutorial on Xiaohongshu? 1. Register and log in: First, you need to download the Xiaohongshu APP on your mobile phone and complete the registration and login. It is very important to complete your personal information in the personal center. By uploading your avatar, filling in your nickname and personal introduction, you can make it easier for other users to understand your information, and also help them pay better attention to your notes. 3. Select the publishing channel: At the bottom of the homepage, click the "Send Notes" button and select the channel you want to publish.

See all articles