Webpack Getting Started tutorials ( Learning notes )
1 , Introduction
This tutorial involves not in-depth, and remove a lot of complex miscellaneous things, record also adhere to the simplest mainly, let beginners probably to Webpack have a simple system understanding, better to further in-depth study Webpack.
Webpack is a javscript wrapper ,Webpack automatically analyzes the dependencies between each module, and then packages those dependencies together into one or more files.
Webpack the most powerful place is the official, third-party plug-ins and loaders (loader) to achieve a variety of file parsing, Compilation.
Webpack the most important four core concepts: ingress (entry),output , loader (loader),plug-in ( Plugin), below, I try to explain the meaning of a few concepts in the way of taking notes, if you want to go into the notes or do not quite understand the place, you can visit the official website of the document to Check.
2 , entrance (entry)
Webpack 's entrance is equivalent to a Web page index file, with the entry file, so that Webpack know where to start,Webpack will be based on this portal file to analyze all the files that the portal file depends on , and then package all of these dependent files into one or more files.
Webpack provides a single entry syntax, object syntax. The single entry syntax is also the simplest one, with only one entry file, i.e. one in one Out. For example, the following ONE.
1 {2 entry: './index.js '3 }
Object syntax is mainly for multiple pages of the application, tell Webpack There are three portal files, when the package is completed is also three files, the three files are independent of each other, each file contains only the files that they depend On. such as the following:
1 {2 entry: {3 hello1: './hello1.js ',4 hello2: '. /hello2.js '5 }6 }
3 , outputs (output)
Webpack provides the output property to control how the Webpack writes the compiled file to the hard disk, the input and output are corresponding, and there is an Input. But it's important to note that there can be multiple inputs, but only one output, how do you output multiple independent compiled files? webpack, of course, has a coping mechanism.
Webpack requires an output property to be an object, and must contain two properties:filename,path. As the name implies , filename is the file name of the output file, and path is the absolute path to the output file ( note that path must be the decision path ).
Single entry output attribute notation:
1 {2 entry: './index.js ',3 output: {4 // Path is a library of Nodejs itself. __dirname is an environment variable in the run process for nodejs, which is the full directory name of the current Folder. The Resolve method is to parse the relative path of the app directory into a decision PATH. 56 filename: ' bundle.js '7 }8 }
Webpack contains multiple variables to handle multiple entry files, such as [name],[hash],[id],[chunkhash], By using variables to guarantee the uniqueness of each file to generate multiple files, webpack will replace these variables with corresponding strings to guarantee the uniqueness of the file during the build Process.
Multiple entry output attribute notation:
1 {2 entry: './index.js ',3 output: {4 path: Path.resolve (__dirname, ' app '),5 filename: [name]-[hash]-bundle.js6 }7 }
4 , loader (loader)
Loader can be compiled for different types of files, such as jsx,typescript directly in the browser run is not able to run, then we need to write the program with the help of jsx and Typescript and other efficient libraries to improve the efficiency of our programming, but we need to be able to use normal, if each file type we have a conversion tool, then it is very cumbersome, so Laoder is to deal with such Work.
First in the use of loader when we need to install the appropriate plug-ins, such as es2015, then we install babel-loader, if it is css, then we install Css-loader, use the rules array inside the module property below to set loader for the file you want to convert .
1 {2Entry: './index.js ',3 Output: {4Path:path.resolve (__dirname, ' app '),5Filename: [name]-[hash]-Bundle.js6 },7 module: {8 Rules: [9{test:/\.js$/, use: ' Babel-loader '}Ten ] one } a}
The above rules are an array, each element is an object, the object contains two properties test, use, the value of test is a regular expression, it is the role of the current Loader for what file, here the regular expression is used to match the file type you need to convert, use is the current match to the file with what loader to convert, compile.
There are three ways to use the loader loader
1.webpack configuration file
2. use in require statements
3.using the command line
The first one above we have said, the following is a brief introduction of the second and third, the second way we use the require or import files can be used directly, such as the following code:
1 require ('babel-loader!. /hello.js')
Or
1 Import ('babel-loader!. /hello.js')
The third Way is to use the command-line tools provided by Webpack directly -module-bind , such as the following code:
1 ' Js=babel-loader '
5 , plugins (plugin)
Plug-ins are used to solve loader can not solve things, such as to each JS file to add work tags, compressed files and other functions, each plug-in may have parameter options, each plug-in use must also use the new operator to create an instance of a Plug-in. The plug-in is set by the plugins property,plugins is an array, and each element represents an instance of a Plug-in. Because Plug-ins have official and third-party, so will not say how to use, just to give you a simple demonstration, we need to use which plug-in to check the plugin API.
1Const Htmlwebpackplugin = require (' html-webpack-plugin ');2 //to use the plugin first, you must first introduce the plugin3 4 {5Entry: './index.js ',6 Output: {7Path:path.resolve (__dirname, ' app '),8Filename: [name]-[hash]-Bundle.js9 },Tenmodule: { one Rules: [ a{test:/\.js$/, use: ' Babel-loader '} - ] - }, the Plugins: [ - NewHtmlwebpackplugin ({telmplate: './index.html '})//using plugins you need to use Plugins. - ] -}
6 , Summary
Through the above study, you can understand the Webpack of the four core, the entrance, output, loader, plug-in, The portal is the file you want to compile, specified after the webpack will look for the dependent file package Compilation. The output is where the file is written to the hard disk after the compilation is Converted. The loader is the conversion of different types of files, allowing the browser to run Directly. Plugins do things that loader can't Solve.
In fact, webpack configuration is not as complex as the imagination ,webpack configuration file is a js file, as long as the Webpack have a system of understanding, You know where I should start and where to Begin.
Webpack Beginner's Introductory Tutorial (study notes)