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

Loader usage analysis of webpack3

php中世界最好的语言
Release: 2018-04-16 15:47:47
Original
1367 people have browsed it

This time I will bring you the loader usage analysis of webpack3. What are the precautions for loader usage analysis in webpack3? The following is a practical case, let's take a look.

First show the official website of webpack. 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.

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:

The loader is used to convert the source code of the module. A loader allows you to pre-process files

when importing or "loading" a module. Therefore, loader Similar to "tasks" in other build tools, and provides a powerful way to handle front-end build steps. The loader can load files from different languages ​​such as TypeScript) to JavaScript, or inline images to data URLs. loader even allows you to directly use JavaScript Import CSS files in the module! From this, we can see the powerful role of loader. Let’s analyze it:

    The role of conversion. Everything used in development is converted into files in required formats such as html css js img necessary for web page loading.
  1. 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.
  2. To sum up in one sentence: 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 use Babel to transpile it 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