Home > Web Front-end > JS Tutorial > body text

Entry, output, module analysis of webpack3.x

php中世界最好的语言
Release: 2018-03-10 13:56:07
Original
1976 people have browsed it

This time I will bring you the entry, output, and module analysis of webpack3. one time.
As a popular tool today, webpack is inseparable from the three major front-end frameworks, so it is necessary to learn it;

Prerequisites: There is a node environment and npm tools; (new version The node comes with the npm tool);

Now let’s do it step by step:

1. First select a directory as the location where your project is stored;

cmd The tool enters the project directory (assuming mine is D:\webpack-demo4); and then uses nmp

to install

webpack: npm install webpack --save-dev("Global installation is not recommended"); Use npm init after finishing your project directory, followed by some descriptive content. How to omit it and directly npm init -y;

Entry, output, module analysis of webpack3.x dist and src directories I created it myself, dist is used to store compiled files, and src is used to store source files; node_modules is a newly initialized folder with various modules in it. In the future, all modules related to project construction will be (it should also be ) placed in this directory; webpack.config.js is the webpack configuration item; package.json is some configuration for node operations (in fact, it is for webpack);

Webpack.config.js configuration (so many for now) );

Entry, output, module analysis of webpack3.xFirst of all, let’s talk about what this configuration is used for. In fact, it is to tell webpack how to package; explain them one by one; ##Entry file

; It means which js file it starts from; talk about this relative path, this is calculated from the directory you enter with the cmd command line; for example, mine is:

[ webpack- demo4] -->There is dist directory and src directory below; then when I run the configuration, I should go to webpack-demo4 and then webpack --config webpack-config.js (or webpack directly);

output The output of the setting: The result of the above setting is that after webpack is packaged, app.bundle.js and print.bundle.js will be generated in the js folder in the dist directory. Name is actually the key in the entry;

module There are two loaders for loading files and css stored in it;

Of course, before running webpack, you need to install npm install style-loader css loader --save -dev and npm install file-loader --save -dev; When you need to cd to the webpak-demo4 directory;

Now I have an index002.html written by myself in the dist directory: as follows;

<html>
  <head>
   <title>Output Management(输出)</title>
    <meta charset=utf-8>
  </head>
  <body> 
  </body>
</html>
<script src="./js/app.bundle.js"></script>
Copy after login

There are two js files in the src directory;

 index.js
import _ from &#39;lodash&#39;;     
import &#39;../css/style1.css&#39;;
import icon1 from &#39;../img/aa00.jpg&#39;;
import printMe from &#39;./print.js&#39;;
function component() {
    var div = document.createElement(&#39;div&#39;);
    var btn = document.createElement(&#39;button&#39;);
    // Lodash(目前通过一个 script 脚本引入)对于执行这一行是必需的
    div.innerHTML = _.join([&#39;Hello&#39;, &#39;webpack&#39;], &#39; &#39;);
    var img = new Image();
    img.src = icon1;
    div.appendChild(img);
    btn.innerHTML = &#39;Click me and check the console!&#39;;
    btn.onclick = printMe;
    div.appendChild(btn);
    return div;
}
document.body.appendChild(component()); //放回元素给到页面;
Copy after login

Let’s talk about something important; import '../css/style1.css'; is that there is style1.css under css in the superior directory (src) relative to index.js; for example, I wrote The background of the body is blue; a picture is also stored under img;

Then the picture and print.js are introduced respectively;

print.js:

export default function printMe() {    console.log(&#39;print.js...&#39;);}
    完成之后,直接
Copy after login

Command webpack in cmd; pictures and styles appear; check that the packaged js and pictures will appear in the dist directory; the above example shows how webpack packages css, pictures and other simple operations;

believe it After reading the case in this article, you have mastered the method. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Related reading:

How VUE uses anmate.css

How to solve the css Hack of IE11

The above is the detailed content of Entry, output, module analysis of webpack3.x. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!