1. 程式人生 > >webpack 4: migration guide for plugins/loaders

webpack 4: migration guide for plugins/loaders

We made a quick plugin migration video courtesy of https://webpack.academy

For plugins:

New Plugin system

webpack 4 uses a new plugin system, which is only partially compatible with the old one.

The plugin method on tapable objects is theoretically backward-compatible, but it causes a deprecation warning, so plugins should consider switching to tap

instead.

Each function attached to a hook must now have a name. This name is used by i. e. the ProgressPlugin to display the plugin in the progress or the ProfilingPlugin to name the profiled timings for function.

There are 3 variants of tap : tap, tapAsync and tapPromise. Choose what fits best for you. Note that some hooks only allow the sync tap

variant.

There are additional steps needed when defining custom hooks: You need to declare the hook now by adding a property to the hooks object.

enhanced-resolve major upgrade

enhanced-resolve was also upgraded to the new plugin system. This required a few interfaces changes too. The callback function no longer carries additional properties. Instead a resolverContext object is passed as additional argument.

Resolver plugins are backward-compatible, but should be migrated to the new plugin system.

Templates render manifest

Templates can now generate multiple assets. To do so a new method getRenderManifest returns an array of asset configurations with render functions and filename template.

This can be modified via plugin hook. Plugins can use this to generate assets from chunks.

Chunk graph and ChunkGroups

A major change happened with the chunk graph. In webpack 3 and lower chunks were connected with parent-child-relationships and contain modules. A chunk with multiple parents could only rely on the assumption that at least one parent is already loaded at runtime.

These schema is insufficient for advanced optimization, especially when aggressively splitting chunks into smaller pieces.

The new chunk graph in webpack 4 has chunk groups connected via parent-child-relationships which contain chunks which contain modules. All chunks of a chunk group are loaded in parallel and can rely on the fact that at least one parent chunk group is already loaded at runtime.

Chunk groups do not affect the output. Only chunks and modules are known at runtime. They are only used for optimization.

An AsyncDependenciesBlock (import()) now points to a single chunk group, instead of to a list of chunks as in webpack ≤ 3.

In addition to that change some mapXXX and forEachXXX helper methods in Module and Chunk were removed/deprecated in favor of using Array.from(xxxIterable).

Parser renames and definitions

The Parser used to track definitions and variable renames in scope via an Array and an Object used as map. To check existence in an Array indexOf was used. When entering a child scope these info has to be carried over but when leaving a child scope it should not leak out of the scope.

webpack 3 uses clever tricks to avoid copying the data: For Arrays length is reseted back to original value when leaving the scope. For Objects a prototype chain is used via Object.create.

Migrating this to ES15 and Map and Set was not trivial, so a new datastructure was introduced. It uses a multiple levels for Maps to avoid copying data while keeping fast access. It has a Map/Set-like interface.

Compiler

While applying plugins Compiler.options is no longer accessible. Plugins should not access options. They should only depend on options passed to the plugin via constructor.

The Compilation.notCacheable flag was removed

EcmaScript modules dependencies

The internal Dependencies for ESM support has changed fundamentally.

Module.buildMeta.harmony was removed. Module.buildMeta.exportsType was added. exportsType defines how the exports of a module are handled by an import statement (and import()).

exportsType = undefined handles them like CommonJS. The exports are exposed via default import. In non-strict esm mode named imports are mapped to properties on the exports at runtime. A truthy __esModule property maps the default import to the default property instead (non-strict esm mode).

exportsType = "namespace" handle them like ESM. Named imports are mapped to properties of the exports object. The default import is mapped to the default property.

exportsType = "named": Named imports are mapped to properties of the exports object. The default import is mapped directly to the exports object. (This is used for JSON)

When usedExports is an Array, properties are mangled.

Dependency

Dependency.getReference() may now return a weak property. Dependency.weak is now used by the Dependency base class and returned in the base impl of getReference().

Dependency.isEqualResource has been replaced with Dependency.getResourceIdentifier for performance reasons.

Module types

Constructor arguments changed for all Modules. Most of them use an options object now. A type argument was added to the Module constructor.

Per-module Resolvers

webpack now supports different Resolvers per Module. This means it’s no longer possible to change the “global” resolvers. Instead hooks can be used to modify resolvers when created. Compiler.resolvers is no longer available. Instead Compiler.resolverFactory has hooks to hook into resolver creation.

Template

The Template class used to be a base class of all Templates. It’s now a static class with helper methods.

RuntimeTemplate

A new RuntimeTemplate class has been added and outputOptions and requestShortener has been moved to this class.

Many methods has been updated to use the RuntimeTemplate instead. Some generated code has been move into the RuntimeTemplate. We plan to add hooks to the RuntimeTemplate to modify the generated code, but this has not happened yet (Won’t be a breaking change).

Module

Module.meta has been replaced with Module.buildMeta and Module.buildInfo. buildMeta carry all information which is required to import a module correctly. buildInfo carries internal information which is not required for importing, but may be required for generating the module’s source. The plan is to keep less properties directly on the Module class. Most properties has been moved into buildInfo or factoryMeta, which has also been added.

Generator

NormalModules now take a Generator instance as constructor argument. This instance handles the Code Generation of the Module. It was separate to allow customizing Code Generation for custom module types.

generator and createGenerator hooks has been added to the NormalModuleFactory to customize it.

Maps and Sets

Compiler.fileTimestamps and Compiler.contextTimestamps are now Maps instead of Objects used as map.

相關推薦

webpack 4: migration guide for plugins/loaders

We made a quick plugin migration video courtesy of https://webpack.academyFor plugins:New Plugin systemwebpack 4 uses a new plugin system, which is only pa

zabbix3.0.4使用percona-monitoring-plugins插件來監控mysql5.6的詳細實現過程

sta moni nod .rpm exp oss percona 密碼 slave zabbix3.0.4使用percona-monitoring-plugins插件來監控mysql5.6的詳細實現過程 因為Zabbix自帶的MySQL監控沒有提供可以直接使用的Key,所

Mysql加鎖過程詳解(4)-select for update/lock in share mode 對事務並發性影響

per inno targe 允許 evel transacti 修改 not null warn select for update/lock in share mode 對事務並發性影響 事務並發性理解 事務並發性,粗略的理解就是單位時間內能夠執行的事務數量,常見的單

guide for install intel opencl

environ usr evel complete clas diag ocl override component 1.install centos7.32.install quartus_prime_complete : quartus, ocl sdk for fpg

webpack 4.0 配置方法以及錯誤解決

文件目錄 pts 版本 創建目錄 efault ebp 大堆 添加 安裝 選取一個空目錄來試驗 全局安裝webpack4.1之後 創建目錄 mkdir webpacktest && cd webpacktes 初始化package.json npm init

4-1 For 實現批量主機ping探測

linux shell for 循環 ping ctrl +c只能關閉一次循環,所以感覺好像沒有效果如果不加wait,只要代碼一執行,就繼續執行下一條指令,加入了wait指令後,只有後臺指令結束後,才會繼續執行任何程序執行的前面都可以加一個time來統計運行時間4-1 For 實現批量主機

4-5 For 實現批量主機密碼修改

linux shell for 循環 密碼修改 什麽都不要交互,因為交互需要耗費一個人力註意循環的並發問題如何不登錄遠程主機改密碼vim modify_password.sh#!/usr/bin/bash4-5 For 實現批量主機密碼修改

webpack 4.0嘗鮮

.com size cti webp 添加 出口 執行 解釋 速度 發布不久得webpack 4.0據說速度快了68% - 98%,然後還支持沒配置文件,所以看起來很牛逼得樣子 所以嘗試一發 webpack和webpack-cli分離 現在執行webpack命令 必須要we

webpack 4+ vue-loader 配置

gui spa plugin 函數 package load kde uid eve webpack 4+ vue-loader 配置 寫的demo,clone下來後,npm dev即可, 可能會由於版本問題,配置會有些許改動,暫時都是可用的 具體配置文件在webpack.

Webpack 4.X 從入門到精通 - entry與output(一)

需要 不可 程序猿 導致 import 應用 驗證 解決 doctype 回顧歷史 Web應用日益復雜,前端開發也發生了翻天覆地的變化變得盤根錯節,到今天已經非常復雜和龐大了!用html、css、javascript老老實實的寫個頁面的時代早已過去。而現在要完成工作需要借助

Webpack 4.X 從入門到精通 - plugin(二)

-h 打包 不同 .com aca spa 編譯 option tput 通過上一篇文章,我們明白了webpack的兩個配置參數入口與出口,webpack會找到入口文件的地址,進去後一頓蹂躪,再通過你給的輸出地址就把編譯後的文件給你了。這篇文章接著去豐富webpack.co

webpack 4.X 與 Vue 2.X結合

itcast tin tps resource depend hot develop desc rom # Vue.js ## 註意: 有時候使用`npm i node-sass -D`裝不上,這時候,就必須使用 `cnpm i node-sass -D` ## 在普通頁面

Webpack 4.X 從入門到精通 - devServer與mode(三)

安裝使用 clas 情況下 屬性 端口 屬性。 npm demo title 上一篇文章裏詳細介紹了一下插件的用法,這一篇文章接著豐富module.exports裏的屬性。如今的前端發展已經非常迅速了,伴隨而來的是開發模式的轉變。現在已經不再是寫個靜態頁面並放在瀏覽器裏打開

手把手使用 Webpack 4 建立 VUE 項目

node cnp test 初始化 初始 ini pack ack init 手把手使用 Webpack 4 建立 VUE 項目 安裝 node.js 略 安裝 cnpm 略 安裝 webpack cnpm install webpack -g 安裝 vue-cli cnp

Webpack 4.X 從入門到精通 - 第三方庫(六)

ofo 分享 ctype mod 找到 ebp 問題 title 效率 在開發的時候會時常用到第三方的庫或者框架,比如耳熟能詳的jquery。借助它們能提高開發效率,但是如何在webpack中使用呢。這篇文章介紹兩個東西,如何使用第三方庫以及如何提取第三方庫。 使用第三方庫

vue - webpack.dev.conf.js for merge

.html ubd history name kcon pac tool 有用 sin webpack-merge提供了一個merge連接數組並合並創建新對象的對象的函數。如果遇到函數,它將執行它們,通過算法運行結果,然後再次將返回的值包裝在函數中。 這種行為在配置web

webpack 4.x 初級學習記錄

但是 模式 基本概念 寫入 dex www 命令 output 參數 目錄 webpack 4.x 安裝 webpack 4.x 基本打包編譯 webpack 配置 概念 webpack-dev-server 安裝 基本概念 配置webpack.config.js 配

vue - webpack.dev.conf.js for CopyWebpackPlugin

pan 忽略 rect pyw npm sset plugin ring path 描述:將單個文件或整個目錄復制到構建目錄 官網地址:https://www.npmjs.com/package/copy-webpack-plugin 1 //

vue - webpack.dev.conf.js for HtmlWebpackPlugin

www. 一個 載器 .org http oda 哈希 dash 技術分享 描述:   這是一個webpack插件,可以簡化HTML文件的創建,為您的webpack捆綁服務提供服務。   這對於webpack包含文件名中包含哈希值的bundle 來說尤其有用,它會更改每

vue - webpack.dev.conf.js for FriendlyErrorsPlugin

ssi 格式 控制 描述 mil func nal 每次 class 描述:webpack網頁端友好的報錯信息就來自它 官網:https://www.npmjs.com/package/friendly-errors-webpack-plugin