Home > Web Front-end > JS Tutorial > Loader analysis of webpack3

Loader analysis of webpack3

小云云
Release: 2018-01-02 14:08:43
Original
1913 people have browsed it

What can webpack do? The answer given by the official website is, in one sentence, it makes everything simple! Various loaders emerge in endlessly, leaving us at a loss when building. Here, we summarize the full analysis of loaders. This article mainly introduces the full analysis of the loader of webpack3. The editor thinks it is quite good. Now I will share it with you and give you a reference. Let’s follow the editor to take a look, I hope it can help everyone.

Concept

loader, as the name suggests, loader, the English explanation is as follows:

Loaders are transformations that are applied on the source code of a module . They allow you to pre-process files as you import or “load” them. Thus, loaders are kind of like “tasks” in other build tools, and provide a powerful way to handle front-end build steps. Loaders can transform files from a different language (like TypeScript) to JavaScript, or inline images as data URLs. Loaders even allow you to do things like import CSS files directly from your JavaScript modules!

The Chinese translation is:

loader is used to convert the source code of the module. Loaders allow you to preprocess files when importing or "loading" modules. Therefore, loaders are similar to "tasks" in other build tools and provide a powerful way to handle front-end build steps. Loaders can convert files from different languages ​​(such as TypeScript) to JavaScript, or inline images to data URLs. The loader even allows you to import CSS files directly in JavaScript modules!

From this, we can see the powerful role of loader. Let’s analyze:

  1. The role of conversion. Everything used in development is converted into files in required formats such as html+css+js+img, which are necessary for web page loading.

  2. The conversion object is the source code. The loader only converts the source code. As for other functions, the plugins take over what it cannot do.

To summarize: loader, a loading machine, is like a soymilk machine. Put your ingredients and it will start working seriously!

Commonly used loaders

1、babel-loader

This package allows transpiling JavaScript files using Babel and webpack.

Load ES2015+ code, and then use Babel to translate to ES5

Installation:


npm install --save-dev babel-loader babel-core babel-preset-env webpack
Copy after login

Use:


{
 test: /\.js$/,
 exclude: /node_modules/,
 loader: 'babel-loader'
}
Copy after login

2、style-loader

Adds CSS to the DOM by injecting a