search
HomeWeb Front-endJS Tutorialwebpack3.x migration and upgrade

This time I will bring you webpack3.x migration and upgrade. What are the precautions for webpack3.x migration and upgrade? The following is a practical case, let’s take a look.

Digression: Don’t look at the 0 configuration as it is very gimmick. It basically cannot meet the needs of most users. However, adding more default configurations does make it more convenient for users. The configuration is relatively simple and is a kind of Out of the box way. After all, the 0 configuration of parcel did steal a lot of the limelight of webpack before, and then I also worked on a simple template project of parcel using parcel vue. If you are interested, you can take a look.

1. New things in webpack4

0. Installation

is no longer just installationwebpack is enough, you also need to install a webpack-cli:

Global installation

sudo npm install -g webpack webpack-cli

Partial installation (current folder)

npm install --save-dev webpack webpack-cli

1. 0 configuration

The default entry is './src/' and the default exit './dist';

The setting of compression (optimization.minimize) is turned on by default during production and turned off during development.

The default configuration is not limited to the above two items.

2. mode/–mode parameter

The mode/--mode parameter is added to indicate whether it is development or production. mode has two optional values: development And production, production does not support monitoring, production focuses on the packaged file size, and development focuses on the speed of the build.

webpack --mode development

can also be configured in the configuration file:

// webpack.config.js
module.exports = {
 mode: "production",
 // ...
}

3. Upgrade uglifyjs

The webpack-based project created by the previous vue scaffolding , in webpack.prod.conf.js you can clearly see this line in the configuration:

// UglifyJs do not support ES6, you can also use babel- minify for better treeshaking: https://github.com/babel/minify

In webpack4.0, es6 code can already be compressed, such as:

class user {
 getUsername() {
  // to do
 }
}

After compression:

4. To remove loaders, you must use rules

In webpack3.x, the loaders of the previous version are still retained and coexist with the rules. All can be used. Loaders have been completely removed in the new version, and rules must be used.

5. sideEffects

Add sideEffects:false in the module's package.js. When using export to export separately, other files other than export will not be packaged. , making the packaged file smaller.

Related links: https://github.com/webpack/webpack/tree/master/examples/side-effects

6. webpack4 supports multiple forms of module types, But sometimes you may need to add type for coordination

If it is CommonJS, AMD, ESM three types of modules, use javascript/auto;

If it is EcmaScript modules(.mjs ), use javascript/esm, other module types will not take effect;

If only CommonJS and EcmaScript modules are not available, use javascript/dynamic;

If it is a json type file, it is allowed to be used To import json with require and import, use json;

If it is Webassembly, use webassembly/experimental - it is officially an experimental feature.

For example:

rules: [
 {
  test: /\.json$/,
  type: "javascript/auto"
 }
]

7. Supports ES6 import of JSON files, and can filter useless code

The following are three imports Method of json file:

let jsonData = require('./data.json');
import jsonData from './data.json'
import { first } from './data.json'

The json file introduced using import { first } from './data.json' will ignore the unimported code, and only the first one will be included when packaging.

8. Remove CommonsChunkPlugin and replace it with optimization.splitChunks and optimization.runtimeChunk

There is a lot of content here, so I won’t give a detailed introduction. There may be a follow-up article Articles about optimization.splitChunks will be briefly introduced below when migrating from 3.x. In addition to the new version of relesase link above, here are some good links recommended:

RIP CommonsChunkPlugin
Article about removing CommonsChunkPlugin on medium
Article about webpack4 update on medium

二、webpack 3.X 迁移到 webpack4.X

1. 更新webpack依赖

npm install -g webpack webpack-cli
npm install --save-dev webpack webpack-cli

2. 更新对应模块

在webpack升级的同时,对应的许多依赖也需要相应的进行升级,下面是在vuec-cli的脚手架中需要的更新;对于其他工程,注意看控制台的报错,是哪个插件报的错就去升级那个插件,如果存在找不到模块之类的错误就去升级对应的loader。

html-webpack-plugin => npm i -D html-webpacl-plugin

webpack-dev-server或者改为webpack-serve => npm i -D webpack-dev-server/npm i -D webpack-serve

vue-loader => npm i -D vue-loader

extract-text-webpack-plugin@next => npm i -D extract-text-webpack-plugin@next <br>…

3. 使用mode/–mode

在命令中加入--mode development/ --mode production或者在配置文件中加入mode: 'development'/mode: 'production'。

4. 如果使用CommonsChunkPlugin替换为optimization.splitChunks

使用栗子:

const webpack = require('webpack');
new webpack.optimize.SplitChunksPlugin({
 chunks: "all",
 minSize: 30000,
 minChunks: 1,
 maxAsyncRequests: 5,
 maxInitialRequests: 3,
 name: true,
 cacheGroups: {
  default: {
   minChunks: 2,
   priority: -20,
   reuseExistingChunk: true,
  },
  vendors: {
   test: /[\\/]node_modules[\\/]/,
   priority: -10
  }
 }
})

写在后面

对于webpack的这次升级这里没有一一列举,选了几个相对关键的点,具体可以去看这里;有关从webpack3.x迁移到新版的时候可能还有其他的坑需要慢慢去趟,目前遇到的基本都解决了。

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

vue+update()使用详解

具体分析webpack样式加载

The above is the detailed content of webpack3.x migration and upgrade. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C   and JavaScript: The Connection ExplainedC and JavaScript: The Connection ExplainedApr 23, 2025 am 12:07 AM

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.