@taro-hmr-compile/plugin-webpack 0.0.1-alpha.9 → 0.0.2
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 +101 -0
- package/dist/compile/hmr-compiler-plugin.d.ts +7 -7
- package/dist/compile/hmr-compiler-plugin.js +2 -29
- package/dist/compile/hmr-compiler-plugin.js.map +1 -1
- package/dist/compile/hmr-output-plugin.d.ts +28 -0
- package/dist/compile/hmr-output-plugin.js +145 -0
- package/dist/compile/hmr-output-plugin.js.map +1 -0
- package/dist/compile/hmr-style-plugin.d.ts +25 -0
- package/dist/compile/hmr-style-plugin.js +53 -0
- package/dist/compile/hmr-style-plugin.js.map +1 -0
- package/dist/compile/lazy-compiler-loader.d.ts +2 -1
- package/dist/compile/lazy-compiler-loader.js +112 -10
- package/dist/compile/lazy-compiler-loader.js.map +1 -1
- package/dist/constants.d.ts +1 -0
- package/dist/constants.js +2 -1
- package/dist/constants.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +17 -6
- package/dist/index.js.map +1 -1
- package/dist/patch/hmr-patch-plugin.d.ts +6 -0
- package/dist/patch/hmr-patch-plugin.js +53 -4
- package/dist/patch/hmr-patch-plugin.js.map +1 -1
- package/dist/patch/hmr-patch-processor.d.ts +2 -1
- package/dist/patch/hmr-patch-processor.js +37 -21
- package/dist/patch/hmr-patch-processor.js.map +1 -1
- package/dist/patch/utils.js +3 -3
- package/dist/patch/utils.js.map +1 -1
- package/package.json +4 -2
- package/src/compile/hmr-compiler-plugin.ts +9 -34
- package/src/compile/hmr-output-plugin.ts +158 -0
- package/src/compile/hmr-style-plugin.ts +70 -0
- package/src/compile/lazy-compiler-loader.ts +85 -10
- package/src/constants.ts +2 -0
- package/src/index.ts +21 -9
- package/src/patch/hmr-patch-plugin.ts +53 -5
- package/src/patch/hmr-patch-processor.ts +45 -27
- package/src/patch/utils.ts +3 -3
- package/src/types/taro-service.d.ts +15 -0
|
@@ -1,23 +1,18 @@
|
|
|
1
|
-
import {type Compilation,} from "webpack";
|
|
2
|
-
import generator from '@babel/generator'
|
|
1
|
+
import {type Chunk, type Compilation,} from "webpack";
|
|
3
2
|
import {
|
|
4
3
|
parseHotUpdateAssets,
|
|
5
4
|
ParseHotUpdateAssetsOptions,
|
|
6
5
|
parseChunkAsset, mergeUpdatePatches, generatePatchAssetSource,
|
|
7
6
|
} from "./utils";
|
|
8
|
-
import
|
|
7
|
+
import isNil from 'lodash/isNil'
|
|
9
8
|
import {ModuleFactory, UpdatePatchSource} from "./types";
|
|
10
9
|
import {DEFAULT_HMR_PATCH_HISTORY_LIMIT} from "../constants";
|
|
11
10
|
|
|
12
|
-
interface ModuleHash {
|
|
13
|
-
moduleId: string;
|
|
14
|
-
factoryHash: string;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
11
|
interface ProcessChunkModulesOptions {
|
|
18
12
|
source: string
|
|
19
13
|
compilation: Compilation,
|
|
20
|
-
isBootstrapChunk: boolean
|
|
14
|
+
isBootstrapChunk: boolean,
|
|
15
|
+
chunk: Chunk
|
|
21
16
|
}
|
|
22
17
|
|
|
23
18
|
export interface HmrPatchProcessorOptions {
|
|
@@ -31,7 +26,14 @@ interface ProcessUpdateOptions extends ParseHotUpdateAssetsOptions {
|
|
|
31
26
|
|
|
32
27
|
type BootstrapFactories = Map<string, ModuleFactory>
|
|
33
28
|
|
|
34
|
-
type
|
|
29
|
+
type ModuleHashes = Map<string, Set<string>>
|
|
30
|
+
|
|
31
|
+
type ModuleHashesByFullHash = Map<string, ModuleHashes>
|
|
32
|
+
|
|
33
|
+
const areModuleHashesEqual = (left: Set<string> | undefined, right: Set<string> | undefined): boolean => {
|
|
34
|
+
if (!left || !right || left.size !== right.size) return false
|
|
35
|
+
return Array.from(left).every(hash => right.has(hash))
|
|
36
|
+
}
|
|
35
37
|
|
|
36
38
|
export class HmrPatchProcessor {
|
|
37
39
|
private readonly bootstrapFactories: BootstrapFactories = new Map()
|
|
@@ -62,9 +64,9 @@ export class HmrPatchProcessor {
|
|
|
62
64
|
|
|
63
65
|
const changedModuleIds = new Set<string>()
|
|
64
66
|
moduleIds.forEach(moduleId => {
|
|
65
|
-
const
|
|
66
|
-
const
|
|
67
|
-
if (
|
|
67
|
+
const fromModuleHash = fromModuleHashes.get(moduleId)
|
|
68
|
+
const toModuleHash = toModuleHashes.get(moduleId)
|
|
69
|
+
if (!areModuleHashesEqual(fromModuleHash, toModuleHash)) changedModuleIds.add(moduleId)
|
|
68
70
|
})
|
|
69
71
|
|
|
70
72
|
return changedModuleIds
|
|
@@ -105,28 +107,44 @@ export class HmrPatchProcessor {
|
|
|
105
107
|
}
|
|
106
108
|
|
|
107
109
|
processChunkModules(opt: ProcessChunkModulesOptions) {
|
|
108
|
-
const {
|
|
109
|
-
const
|
|
110
|
+
const {chunk, compilation, isBootstrapChunk, source} = opt
|
|
111
|
+
const {hash: fullHash} = compilation
|
|
112
|
+
const modules = parseChunkAsset(source)
|
|
110
113
|
modules.forEach(moduleFactory => {
|
|
111
114
|
const {moduleId, factory} = moduleFactory
|
|
112
115
|
|
|
113
|
-
if (
|
|
114
|
-
const compilationFactories = this.bootstrapFactoriesByCompilation.get(
|
|
116
|
+
if (isBootstrapChunk) {
|
|
117
|
+
const compilationFactories = this.bootstrapFactoriesByCompilation.get(compilation) ?? new Map()
|
|
115
118
|
compilationFactories.set(moduleId, {moduleId, factory})
|
|
116
|
-
this.bootstrapFactoriesByCompilation.set(
|
|
119
|
+
this.bootstrapFactoriesByCompilation.set(compilation, compilationFactories)
|
|
117
120
|
}
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
if (!fullHash) return
|
|
124
|
+
|
|
125
|
+
const moduleHashes = (() => {
|
|
126
|
+
const hasCurFullHashModuleHashMap = this.moduleHashesByFullHash.has(fullHash)
|
|
127
|
+
if (!hasCurFullHashModuleHashMap) this.moduleHashesByFullHash.set(fullHash, new Map())
|
|
128
|
+
return this.moduleHashesByFullHash.get(fullHash)!
|
|
129
|
+
})()
|
|
130
|
+
|
|
131
|
+
const { chunkGraph, codeGenerationResults } = compilation
|
|
132
|
+
|
|
133
|
+
Array.from(chunkGraph.getChunkModulesIterable(chunk)).forEach(module => {
|
|
134
|
+
const moduleId = chunkGraph.getModuleId(module)
|
|
135
|
+
|
|
136
|
+
if (isNil(moduleId)) return
|
|
118
137
|
|
|
119
|
-
|
|
138
|
+
const normalizedModuleId = String(moduleId)
|
|
120
139
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
})()
|
|
140
|
+
// 获取模块在当前 chunk.runtime 下的 Hash,优先从 codeGenerationResults 获取,如果没有对应的代码生成结果,则回退到 chunkGraph 获取
|
|
141
|
+
const moduleHash = codeGenerationResults?.has(module, chunk.runtime)
|
|
142
|
+
? codeGenerationResults.getHash(module, chunk.runtime)
|
|
143
|
+
: chunkGraph.getModuleHash(module, chunk.runtime)
|
|
126
144
|
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
moduleHashes.set(
|
|
145
|
+
const hashes = moduleHashes.get(normalizedModuleId) ?? new Set<string>()
|
|
146
|
+
hashes.add(moduleHash)
|
|
147
|
+
moduleHashes.set(normalizedModuleId, hashes)
|
|
130
148
|
})
|
|
131
149
|
}
|
|
132
150
|
|
package/src/patch/utils.ts
CHANGED
|
@@ -17,7 +17,7 @@ const parseModuleFactories = (expression: ObjectExpression) => {
|
|
|
17
17
|
|
|
18
18
|
const moduleFactories = new Map<string, ModuleFactory>()
|
|
19
19
|
|
|
20
|
-
if (!t.isObjectExpression(expression)) return moduleFactories.values()
|
|
20
|
+
if (!t.isObjectExpression(expression)) return Array.from(moduleFactories.values())
|
|
21
21
|
|
|
22
22
|
expression.properties.forEach(property => {
|
|
23
23
|
if (!t.isObjectProperty(property) || !t.isExpression(property.value)) throw new Error('[taro-hmr-compile] HMR 模块对象包含无法识别的属性')
|
|
@@ -33,7 +33,7 @@ const parseModuleFactories = (expression: ObjectExpression) => {
|
|
|
33
33
|
moduleFactories.set(moduleId, {factory, moduleId})
|
|
34
34
|
})
|
|
35
35
|
|
|
36
|
-
return moduleFactories.values()
|
|
36
|
+
return Array.from(moduleFactories.values())
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
|
|
@@ -128,7 +128,7 @@ export const mergeUpdatePatches = (basePatch: UpdatePatchSource, nextPatch: Upda
|
|
|
128
128
|
type: 'update',
|
|
129
129
|
fromHash: basePatch.fromHash,
|
|
130
130
|
toHash: nextPatch.toHash,
|
|
131
|
-
moduleFactories:
|
|
131
|
+
moduleFactories: Array.from(moduleFactories.values()),
|
|
132
132
|
runtimeModules: [...basePatch.runtimeModules, ...nextPatch.runtimeModules],
|
|
133
133
|
removedModules: [...removedModules],
|
|
134
134
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { IProjectConfig } from '@tarojs/taro/types/compile'
|
|
2
|
+
import type { IComponentConfig } from '@tarojs/taro/types/compile/hooks'
|
|
3
|
+
|
|
4
|
+
declare module '@tarojs/service' {
|
|
5
|
+
interface IPluginContext {
|
|
6
|
+
modifyComponentConfig: (
|
|
7
|
+
callback: (args: {
|
|
8
|
+
componentConfig: IComponentConfig
|
|
9
|
+
config: IProjectConfig
|
|
10
|
+
}) => void,
|
|
11
|
+
) => void
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export {}
|