Skip to content

支持 extractCommon & extractVendor #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 23, 2021
Merged

Conversation

liaoyu
Copy link

@liaoyu liaoyu commented Apr 20, 2021

参考:
https://webpack.js.org/plugins/split-chunks-plugin/
https://webpack.js.org/concepts/entry-points/#separate-app-and-vendor-entries

  • extractVendor
    • BREAKING CHANGE: extractVendor 不再支持指定 entry name,请指定固定依赖的包名数组,例如 ["react", "react-dom"] (改动原因是 webpack 5 不再支持通过指定 entry 来抽取公共依赖)
    • 行为是将所指定的依赖在 node_modules 的包都打包进 vendor,指定 true 时则抽取所有 node_modules 下的依赖包
  • extractCommon
    • 2 个 entry 以上时抽取公共依赖

以 portal-platform 为例,在不改动配置时输出如下:
image

手动增加一个 entry 共同开启:
image

config: Configuration, cacheGroups: SplitChunksCacheGroups
): Configuration {
return produce(config, newConfig => {
newConfig.optimization = newConfig.optimization || {}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIP: 最近发现可以写成

newConfig.optimization ||= {}

或者

newConfig.optimization ??= {}

这俩写法都已经 stage 4 了好像,我们用的 TS 版本应该也支持的

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done 🐮 🍺

@@ -458,3 +458,49 @@ function makeBabelLoaderOptions(
nextOptions.sourceType = nextOptions.sourceType || 'unambiguous'
})
}

export interface SplitChunksCacheGroup {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是从哪里拷的吗?webpack 本身没有定义或者暴露这个类型?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是从 webpack 里拷贝出来,它没有将这些类型导出

config = addTransforms(config, buildConfig)

const htmlPlugins = Object.entries(buildConfig.pages).map(([ name, { template, entries } ]) => {
return new HtmlPlugin({
template: abs(template),
filename: getPageFilename(name),
chunks: entries
chunks: [...baseChunks, ...entries],
chunksSortMode: 'manual'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

指定 manual 有什么说法吗?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

测试发现不指定顺序有可能 vendor.js 会在 entry.js 后面


if (extractVendor) {
if (typeof extractVendor === 'string') {
logger.warn('BREAKING CHANGE: The type of extractVendor no longer a string, please use an array, like ["react", "react-dom"]')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIP: 这个像是在暗示,原本的 'vendor' 应该改成 ['vendor']..

这边并不是一个简单的 string -> string[] 的变化,而是调整了需要指定的信息,建议这边直接让他看最新的 extractVendor 的文档就好

logger.warn('BREAKING CHANGE: The type of extractVendor no longer a string, please use an array, like ["react", "react-dom"]')
} else {
baseChunks.push(chunks.vendor)
// extractVendor 传空数组时,默认将依赖的 node_modules 都打包进 vendor
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

['react'] -> 抽取 react
['react', 'react-dom'] -> 抽取 react & react-dom
[] -> 抽取所有第三方依赖

感觉不太符合直觉?

如果确实有抽取所有第三方依赖的需求,我觉得 extractVendor: trueextractVendor: [] 更符合直觉一点?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done,增加了 boolean 的类型,改成 extractVendor 传空数组不进行 vendor 抽取

} else {
baseChunks.push(chunks.vendor)
// extractVendor 传空数组时,默认将依赖的 node_modules 都打包进 vendor
const vendorModules = extractVendor.length > 0 ? `(${extractVendor.join('|')})[\\\\/]` : ''
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这边的内容会被拿去构造正则,值需不需要做正则的 escape?即,第三方依赖的包名里有没有可能有正则的特殊字符?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIP: 正则如果容易出错的话,也可以考虑用 test function

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改成 test function 来处理了

@@ -63,13 +64,50 @@ export async function getConfig(): Promise<Configuration> {
}
}

const baseChunks: string[] = []

if (getEnv() === Env.Prod) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getConfig 函数有点长了,这一块是相对独立的一块内容,建议拆到旁边单独的函数里,这边调用之?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


if (extractVendor) {
if (typeof extractVendor === 'string') {
logger.warn('BREAKING CHANGE: extractVendor 不再支持指定 entry name,请指定固定依赖的包名数组,例如 ["react", "react-dom"]')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

发 2.0 还要温和吗?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

温和?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不是直接不处理或者报错吗。。
然后有个 immigration 的东西告诉我们怎么从 1 迁移到 2

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

哦原来你说的真的是”温和“..

我觉得报错也是可以的

然后有个 immigration 的东西告诉我们怎么从 1 迁移到 2

这个就是我提到的,让使用方来看最新文档就好

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里改成抛 Error 并提示查看帮助文档了

@nighca
Copy link
Owner

nighca commented Apr 21, 2021

对了,有几个点测试的时候需要稍微注意下:

  1. vendor & common 对应的 css,是不是像默认 css 一样,是在比 Javascript 更早的时候加载的(插在 <head> 里)
  2. 依赖内容不变,修改 src 内容,构建后生成的 vendor chunk,其内容及文件名(hash)应该不变
  3. 新增或修改别的依赖内容,但不改变 extractVendor 对应的依赖,构建后生成的 vendor chunk,其内容及文件名(hash)应该不变

const resource = module.resource
if (!resource) return false

if (typeof extractVendor === 'boolean') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extractVendor === false 的话也走这个分支吗?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不会的,前面还有一个 if (extractVendor) {} 做前置判断才会走到这里边

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

Comment on lines 522 to 524
return extractVendor.findIndex(packageName => {
return resource.includes(path.join('node_modules', packageName))
}) !== -1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIP:

Suggested change
return extractVendor.findIndex(packageName => {
return resource.includes(path.join('node_modules', packageName))
}) !== -1
return extractVendor.some(packageName => {
return resource.includes(path.join('node_modules', packageName))
})

}

return extractVendor.findIndex(packageName => {
return resource.includes(path.join('node_modules', packageName))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

考虑取名 xxx_node_modules 的自定义目录,node_modules 前面也带个杠的话严谨一点?上边的

return resource.includes('node_modules')

同理

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

大佬严谨,我前后都加一下

@liaoyu
Copy link
Author

liaoyu commented Apr 23, 2021

对了,有几个点测试的时候需要稍微注意下:

  1. vendor & common 对应的 css,是不是像默认 css 一样,是在比 Javascript 更早的时候加载的(插在 <head> 里)
  2. 依赖内容不变,修改 src 内容,构建后生成的 vendor chunk,其内容及文件名(hash)应该不变
  3. 新增或修改别的依赖内容,但不改变 extractVendor 对应的依赖,构建后生成的 vendor chunk,其内容及文件名(hash)应该不变

这 3 个场景已经验证符合预期

Copy link
Owner

@nighca nighca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🌋

@nighca nighca merged commit 8108530 into nighca:v2 Apr 23, 2021
@nighca nighca mentioned this pull request Aug 27, 2021
nighca added a commit that referenced this pull request Aug 30, 2021
* sv

* without svg

* use file & svgr for svg

* typo

* samples change

* update config

* dev deps & script

* optimize svg support

* drop support for ENV_VARIABLES_FILE

* sv

* update samples

* debug devServer route fallback & module resolution

* enable ts-loader allowTsInNodeModules

* update details

* update README

* extract css

* optimization.addPolyfill

* use asset module instead of loaders

* optimize deps

* update TODO

* beta.1

* throw error in logLifecycle

* beta.2

* upload

* beta.3

* beta.4

* disable svgo for svg transform

* beta.5

* 支持 extractCommon & extractVendor (#2)

* extractCommon & extractVendor

* default vendors

* fix extractVendor

* remove build-config.md

* fix review

* fix extractVendor

* tsconfig 配置调整 (#4)

* open esModuleInterop

* target to es6

* 调整默认的目标浏览器 (#3)

* adjust default target browsers

* use svgr for qiniu portal

* npm audit

* filter ts-loader transpileOnly warning

* beta.7

* 新增 analyze 命令以分析 bundle 依赖 (qiniu#132)

* add analyz

* add analyze command

* fix self review

* fix review

* 修复 CI (qiniu#134)

* debug build-sample.sh

* debug samples for CI

* update browserslist db

* update circle ci node versions

* serve 时监测配置文件变更 (qiniu#133)

* watch build config file change

* typo

* watch for tsconfig.json

* remove useless code

* comment

* typo

* 修复样式修改 hot reload 不生效的问题 (qiniu#135)

* fix css hot reload

* optimize env logic

* beta.8

* Upgrade typescript 4.1.x (qiniu#137)

* use typescript 4.1.x

* beta.9

* log pretty (qiniu#138)

* beta.10

* source map 行为优化 (qiniu#139)

* process source map

* beta.11

* upgrade ts-loader

* typo

* use cheap-module-source-map instead of eval-source-map

* move webpack-related fns to utils/webpack

* build-config 支持 npm package 作为 `extends` 目标 (qiniu#140)

* support npm package as extends target

* update build-config doc

* support file in npm package

* beta.12

* 支持 `optimization.compressImage` (qiniu#142)

* remove sourcemap from TODO

* update build script

* specify webpack config target

* update comment for getServeConfig

* add repository info in package.json

* update command prepare & test

* optimization.compressImage

* update samples

* beta.13

* fix dynamic-import (qiniu#143)

* beta.14

* fix Typescript moduleResolution (qiniu#144)

* beta.15

Co-authored-by: liaoyu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants