What's new in webpack4? What needs attention?
This article brings you what is new in webpack4? What needs attention? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
While developers are still experiencing the afterglow of webpack3.x, webpack4.x has quietly arrived.
For users, the most anticipated questions are as follows:
What are the changes in the new version compared to the old version?
Migration from webpack3.x to webapck4.x?
What should we pay attention to when using webpack4.x?
New features of webpack
The power of webpack as a build tool lies in:
Many unique features can be configured in webpack.config.js;
Its configuration is flexible and changeable;
But because of this, That's also the bad thing about it. Because it is too casual, it is difficult to control, causing the following problems:
The cost of learning, using, and researching webpack is too high (the progression curve is too steep);
Building a small application also requires configuring webpack.config.js like building a large application (the sparrow is small but has all the internal organs);
And webpack4.x is a new The first generation version of webpack has greatly solved the existing problems.
webpackk4.x does not need to use the webpack.config.js configuration file
You can use the following 6 steps to complete the construction of the project:
-
Create one Project directory (webpack-demo), and then enter the changed directory
mkdir webpack-demo && cd webpack-demo
-
Initialize the package.json file
npm init -y
-
Load webpack and webpack-cli dependencies
npm install webpack webpack-cli --save-dev
Add the ~/src/index.js file to the project (index.js is the default entry file, and the default entry directory is ~/src. Of course, you can also customize the entry file. You need to modify the main configuration in package.json The item is the specified file)
index.js file code is as follows:
console.log('hello webpack.')
Open package.json and add the following code in the scripts configuration item:
"scripts": { "build": "webpack" }
Note: This is the NPM scripts command
Run the npm run build command, and then you will see a ~/dist/main.js file in the project. In the command window, you should notice the following warning prompt:
WARNING in configuration The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment. You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/concepts/mode/
Ignore this prompt message. We found that the project initialization configuration of webpack4.x is not much different from webpack3.x, but webpack4.x is missing. The required webpack.config.js configuration file.
Changes in packaging modeLet’s look back at the above prompt message. It means: 'If the configuration item of packaging mode is not set, then the default packaging mode is production mode (production), and for development Mode (development), you need to configure the mode configuration item. At this point, I think you readers should understand that webpack4. Save time and effort.
But in actual applications, we often distinguish between development mode and production mode, but this is not difficult in webpack4.x. Just modify the scripts in package.json as follows:
"scripts": { "dev": "webpack --mode development", // 用于开发模式 "build": "webpack --mode production" // 用于生产模式 }
'right! webpack4.x is that simple’. We don't need to define two configuration files respectively for development mode and production mode like webpack3.x.
Overload the default configuration entry/exitThere is no configuration file webpack.config.js, which not only reduces our configuration workload, but also brings some questions to us who are just getting a glimpse of the method. For example: How to customize entrance/exit?
In the absence of webpack.config.js, we can add entry/exit configuration items in the command line, the code is as follows:
"scripts": { "dev": "webpack --mode development ./src/entry.js --output ./dist/bundle.js", // 用于开发模式 "build": "webpack --mode production ./src/entry.js --output ./dist/bundle.min.js" // 用于生产模式 }
This is just without using webpack.config.js A plan.
The above are the overall changes that webpack4.x has brought to us.
But the function implementation in the module and plugins configuration items in the original webpack.config.js configuration file still requires the use of webpack.config.js. Although the webpack team's plan is to configure some commonly used loaders and plugins, only the UglifyJSPlugin built-in plug-in is implemented, which can compress *.js code without introducing it in production mode. Other loaders and plugins can only be introduced through webpack.config.js.
Webpack migration and precautions
Seeing these changes in webpack4.x, many people will not only ask whether the migration from webpack3.x to webpack4.x is easy Trouble, actually not troublesome, webpack4.x is backward compatible with webpack.3x.
In order not to introduce webpack.config.js, we used npm scripts. At that time, like the reloading of entrance/exit, we can also complete it in the webpack.config.js configuration file. The configuration is the same as the original are similar, but webpack4.x has the following issues that need attention:
升级到webpack4.x,你会发现在使用 extract-text-webpack-plugin 分离 *.css 出文件时经常出错,这是 extract-text-webpack-plugin 本身的问题,官方推荐使用 mini-css-extract-plugin 来避免问题的出现,但使用 mini-css-extract-plugin 有一个限制就是webapck须是4.2.0版本以上(较低的版本不支持)。
使用 使用babel-loader 转化ES6->ES5时将不需要 .babelrc 配置文件,你只需要在 package.json 的 scripts 中添加 --module-bind js=babel-loader 即可完成对 babel-loader 的配置。
其他的loader和plugin没有什么大的变化。其实讲到这里基本完了,下面是用webpack4.x构建的一个demo。
webpack4.x的demo
紧接上面的配置:
首先,添加 html-wepback-plugin 和 html-loader 依赖:
npm install html-webpack-plugin html-loader --save-dev
html-webpack-plugin生成html文件(html文件用来加载打包生成 bundle.js 文件),当然你也可以使用webpack支持的各种模板loader,这里使用 html-loader 支持的 *.html 类型模板来生成。
其次,添加 mini-css-extract-plugin
和 css-loader
依赖:
npm install mini-css-extract-plugin css-loader --save-dev
loader和plugin配置与webpack3.x类同,也可参考下面提供代码中的 webpack.config.js 文件。
然后,添加 babel-loader 、@babel/babel-core 和 @babel/babel-preset 依赖:
npm install @babel/core babel-loader @babel/preset-env --save-dev
loader和plugin配置与webpack3.x类同,也可参考下面提供源码中的 webpack.config.js 文件。
修改 package.json 中 scripts 如下:
"scripts": { "dev": "webpack --mode development --module-bind js=babel-loader ./src/entry.js --output ./dist/bundle.js", "build": "webpack --mode production ./src/entry.js --module-bind js=babel-loader --output ./dist/bundle.min.js" },
最后,添加 webpack-dev-server 依赖,实现项目文件修改,浏览器及时刷新
npm install webpack-dev-server
在 package.json 中 scripts 的 dev 替换 webpack 为 webpack-dev-server 即可,代码如下:
"scripts": { "dev": "webpack-dev-server --mode development --module-bind js=babel-loader ./src/entry.js --output ./dist/bundle.js", "build": "webpack --mode production ./src/entry.js --module-bind js=babel-loader --output ./dist/bundle.min.js" },
这样一个简单的demo就完成了。
其他的loader和plugin配置和webpack3.x类同。
The above is the detailed content of What's new in webpack4? What needs attention?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Vue is an excellent JavaScript framework that can help us quickly build interactive and efficient web applications. Vue3 is the latest version of Vue, which introduces many new features and functionality. Webpack is currently one of the most popular JavaScript module packagers and build tools, which can help us manage various resources in our projects. This article will introduce how to use Webpack to package and build Vue3 applications. 1. Install Webpack

Differences: 1. The startup speed of the webpack server is slower than that of Vite; because Vite does not require packaging when starting, there is no need to analyze module dependencies and compile, so the startup speed is very fast. 2. Vite hot update is faster than webpack; in terms of HRM of Vite, when the content of a certain module changes, just let the browser re-request the module. 3. Vite uses esbuild to pre-build dependencies, while webpack is based on node. 4. The ecology of Vite is not as good as webpack, and the loaders and plug-ins are not rich enough.

With the continuous development of web development technology, front-end and back-end separation and modular development have become a widespread trend. PHP is a commonly used back-end language. When doing modular development, we need to use some tools to manage and package modules. Webpack is a very easy-to-use modular packaging tool. This article will introduce how to use PHP and webpack for modular development. 1. What is modular development? Modular development refers to decomposing a program into different independent modules. Each module has its own function.

Configuration method: 1. Use the import method to put the ES6 code into the packaged js code file; 2. Use the npm tool to install the babel-loader tool, the syntax is "npm install -D babel-loader @babel/core @babel/preset- env"; 3. Create the configuration file ".babelrc" of the babel tool and set the transcoding rules; 4. Configure the packaging rules in the webpack.config.js file.

As the complexity of modern web applications continues to increase, building excellent front-end engineering and plug-in systems has become increasingly important. With the popularity of Spring Boot and Webpack, they have become a perfect combination for building front-end projects and plug-in systems. SpringBoot is a Java framework that creates Java applications with minimal configuration requirements. It provides many useful features, such as automatic configuration, so that developers can build and deploy web applications faster and easier. W

Webpack is a module packaging tool. It creates modules for different dependencies and packages them all into manageable output files. This is especially useful for single-page applications (the de facto standard for web applications today).

In vue, webpack can package js, css, pictures, json and other files into appropriate formats for browser use; in webpack, js, css, pictures, json and other file types can be used as modules. Various module resources in webpack can be packaged and merged into one or more packages, and during the packaging process, the resources can be processed, such as compressing images, converting scss to css, converting ES6 syntax to ES5, etc., which can be recognized by HTML. file type.

Webpack in vue is installed using the node package manager "npm" or the npm image "cnpm". Webpack is a static module packaging tool for modern JavaScript applications. It is developed based on node.js. It requires node.js component support when using it. It needs to be installed using npm or cnpm. The syntax is "npm install webpack -g" or "cnpm install webpack -g".
