Home>Article>Web Front-end> Entry, output, module analysis of webpack3.x
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 installwebpack: 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;
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) );
First 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); 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;There are two js files in the src directory;Output Management(输出)
index.js import _ from 'lodash'; import '../css/style1.css'; import icon1 from '../img/aa00.jpg'; import printMe from './print.js'; function component() { var div = document.createElement('div'); var btn = document.createElement('button'); // Lodash(目前通过一个 script 脚本引入)对于执行这一行是必需的 div.innerHTML = _.join(['Hello', 'webpack'], ' '); var img = new Image(); img.src = icon1; div.appendChild(img); btn.innerHTML = 'Click me and check the console!'; btn.onclick = printMe; div.appendChild(btn); return div; } document.body.appendChild(component()); //放回元素给到页面;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('print.js...');} 完成之后,直接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!