@taro-minify-pack/plugin-async-pack 0.1.5-alpha.0 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @taro-minify-pack/plugin-async-pack
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
将 `import()` 动态导入的 JS 模块拆到异步分包,优化主包体积;样式默认同步加载,可为自定义分包启用异步样式。
|
|
4
4
|
|
|
5
5
|
## ✨ 功能特性
|
|
6
6
|
|
|
@@ -86,12 +86,13 @@ const module = await import('./dynamic-module')
|
|
|
86
86
|
### React 组件懒加载
|
|
87
87
|
```tsx
|
|
88
88
|
import { lazy, Suspense } from 'react'
|
|
89
|
+
import { View } from '@tarojs/components'
|
|
89
90
|
|
|
90
91
|
const DynamicComponent = lazy(() => import('./DynamicComponent'))
|
|
91
92
|
|
|
92
93
|
function App() {
|
|
93
94
|
return (
|
|
94
|
-
<Suspense fallback={<
|
|
95
|
+
<Suspense fallback={<View>Loading...</View>}>
|
|
95
96
|
<DynamicComponent />
|
|
96
97
|
</Suspense>
|
|
97
98
|
)
|
|
@@ -114,7 +115,7 @@ function App() {
|
|
|
114
115
|
</template>
|
|
115
116
|
|
|
116
117
|
<script setup>
|
|
117
|
-
import {defineAsyncComponent
|
|
118
|
+
import { defineAsyncComponent } from 'vue'
|
|
118
119
|
const AsyncComponent = defineAsyncComponent(() => import('./async-component')
|
|
119
120
|
)
|
|
120
121
|
</script>
|
|
@@ -131,9 +132,9 @@ const AsyncComponent = defineAsyncComponent(() => import('./async-component')
|
|
|
131
132
|
2. **样式处理**:
|
|
132
133
|
- 默认会先把未开启异步样式的分包样式合并成 `${dynamicPackageNamePrefix}.wxss`
|
|
133
134
|
- 编译 `app.wxss` 时自动补上一条 `@import './${dynamicPackageNamePrefix}.wxss'`,让这部分样式继续跟随主包同步加载
|
|
134
|
-
- 当 `customDynamicPackages[].asyncStyle = true` 时,会在对应自定义分包下额外生成一个 `inject-style` 组件
|
|
135
|
+
- 当 `customDynamicPackages[].asyncStyle = true` 时,会在对应自定义分包下额外生成一个 `inject-dynamic-style` 组件
|
|
135
136
|
- 这个组件会收集该分包目录下的所有 `.wxss` 文件,并生成多个 `@import './xxx.wxss'` 引用,让样式跟随分包一起下发
|
|
136
|
-
- 构建结束后会继续改写 `app.json` 和页面 `wxml`:把 `inject-style` 注册到 `usingComponents` / `componentPlaceholder`,再把组件节点追加到页面模板中,借此触发该分包样式的异步加载
|
|
137
|
+
- 构建结束后会继续改写 `app.json` 和页面 `wxml`:把 `inject-dynamic-style` 注册到 `usingComponents` / `componentPlaceholder`,再把组件节点追加到页面模板中,借此触发该分包样式的异步加载
|
|
137
138
|
|
|
138
139
|
3. **小程序配置修改**:
|
|
139
140
|
- 自动修改 `app.json`,添加异步分包配置
|
|
@@ -166,7 +167,7 @@ interface CustomDynamicPackage {
|
|
|
166
167
|
|
|
167
168
|
- `name`:自定义异步分包标识,最终会拼成 `${dynamicPackageNamePrefix}-${name}`,例如 `dynamic-package-report`
|
|
168
169
|
- `test`:直接透传给 webpack `splitChunks.cacheGroups[packageName].test`,命中的异步模块会被拆到该分包
|
|
169
|
-
- `asyncStyle`:默认 `false`。开启后会为该分包额外生成 `inject-style` 组件,并自动注入到页面模板中,让该分包内样式跟随组件异步加载
|
|
170
|
+
- `asyncStyle`:默认 `false`。开启后会为该分包额外生成 `inject-dynamic-style` 组件,并自动注入到页面模板中,让该分包内样式跟随组件异步加载
|
|
170
171
|
|
|
171
172
|
### `customDynamicPackages` 配置示例
|
|
172
173
|
|
|
@@ -190,7 +191,7 @@ plugins: [
|
|
|
190
191
|
]
|
|
191
192
|
```
|
|
192
193
|
|
|
193
|
-
上述配置会额外生成 `dynamic-package-sync-style` 和 `dynamic-package-async-style` 两个异步分包;其中 `async-style` 分包会同时产出 `inject-style` 组件,并在编译时自动追加到 `app.json` 与页面模板中。
|
|
194
|
+
上述配置会额外生成 `dynamic-package-sync-style` 和 `dynamic-package-async-style` 两个异步分包;其中 `async-style` 分包会同时产出 `inject-dynamic-style` 组件,并在编译时自动追加到 `app.json` 与页面模板中。
|
|
194
195
|
|
|
195
196
|
## 🤝 常见问题
|
|
196
197
|
|
|
@@ -202,7 +203,7 @@ plugins: [
|
|
|
202
203
|
|
|
203
204
|
### 2. 为什么样式文件没有异步?
|
|
204
205
|
- 先检查 `customDynamicPackages[].test` 是否真的命中了目标模块;如果没有命中,样式会走默认同步合并逻辑,而不会进入对应自定义异步分包
|
|
205
|
-
- 再检查该自定义分包的 `asyncStyle` 是否为 `true`;只有开启后才会额外生成 `inject-style` 组件,并触发异步样式注入
|
|
206
|
+
- 再检查该自定义分包的 `asyncStyle` 是否为 `true`;只有开启后才会额外生成 `inject-dynamic-style` 组件,并触发异步样式注入
|
|
206
207
|
- `module.identifier()` 代表 webpack 给当前模块生成的唯一标识,通常会包含 loader 链、查询参数和源文件绝对路径,比单纯看文件名更适合排查 `test` 是否命中
|
|
207
208
|
- 排查时可以先在 `test` 回调里临时打印 `module.identifier()`,确认真实值后再收窄正则规则,例如只匹配其中稳定的源码路径片段:
|
|
208
209
|
|