@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.
Files changed (38) hide show
  1. package/README.md +101 -0
  2. package/dist/compile/hmr-compiler-plugin.d.ts +7 -7
  3. package/dist/compile/hmr-compiler-plugin.js +2 -29
  4. package/dist/compile/hmr-compiler-plugin.js.map +1 -1
  5. package/dist/compile/hmr-output-plugin.d.ts +28 -0
  6. package/dist/compile/hmr-output-plugin.js +145 -0
  7. package/dist/compile/hmr-output-plugin.js.map +1 -0
  8. package/dist/compile/hmr-style-plugin.d.ts +25 -0
  9. package/dist/compile/hmr-style-plugin.js +53 -0
  10. package/dist/compile/hmr-style-plugin.js.map +1 -0
  11. package/dist/compile/lazy-compiler-loader.d.ts +2 -1
  12. package/dist/compile/lazy-compiler-loader.js +112 -10
  13. package/dist/compile/lazy-compiler-loader.js.map +1 -1
  14. package/dist/constants.d.ts +1 -0
  15. package/dist/constants.js +2 -1
  16. package/dist/constants.js.map +1 -1
  17. package/dist/index.d.ts +2 -0
  18. package/dist/index.js +17 -6
  19. package/dist/index.js.map +1 -1
  20. package/dist/patch/hmr-patch-plugin.d.ts +6 -0
  21. package/dist/patch/hmr-patch-plugin.js +53 -4
  22. package/dist/patch/hmr-patch-plugin.js.map +1 -1
  23. package/dist/patch/hmr-patch-processor.d.ts +2 -1
  24. package/dist/patch/hmr-patch-processor.js +37 -21
  25. package/dist/patch/hmr-patch-processor.js.map +1 -1
  26. package/dist/patch/utils.js +3 -3
  27. package/dist/patch/utils.js.map +1 -1
  28. package/package.json +4 -2
  29. package/src/compile/hmr-compiler-plugin.ts +9 -34
  30. package/src/compile/hmr-output-plugin.ts +158 -0
  31. package/src/compile/hmr-style-plugin.ts +70 -0
  32. package/src/compile/lazy-compiler-loader.ts +85 -10
  33. package/src/constants.ts +2 -0
  34. package/src/index.ts +21 -9
  35. package/src/patch/hmr-patch-plugin.ts +53 -5
  36. package/src/patch/hmr-patch-processor.ts +45 -27
  37. package/src/patch/utils.ts +3 -3
  38. 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 {createHash} from "crypto";
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 ModuleHashesByFullHash = Map<string, Map<string, ModuleHash>>
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 fromFactoryHash = fromModuleHashes.get(moduleId)?.factoryHash
66
- const toFactoryHash = toModuleHashes.get(moduleId)?.factoryHash
67
- if (fromFactoryHash !== toFactoryHash) changedModuleIds.add(moduleId)
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 {hash: fullHash} = opt.compilation
109
- const modules = parseChunkAsset(opt.source)
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 (opt.isBootstrapChunk) {
114
- const compilationFactories = this.bootstrapFactoriesByCompilation.get(opt.compilation) ?? new Map()
116
+ if (isBootstrapChunk) {
117
+ const compilationFactories = this.bootstrapFactoriesByCompilation.get(compilation) ?? new Map()
115
118
  compilationFactories.set(moduleId, {moduleId, factory})
116
- this.bootstrapFactoriesByCompilation.set(opt.compilation, compilationFactories)
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
- if (!fullHash) return
138
+ const normalizedModuleId = String(moduleId)
120
139
 
121
- const moduleHashes = (() => {
122
- const hasCurFullHashModuleHashMap = this.moduleHashesByFullHash.has(fullHash)
123
- if (!hasCurFullHashModuleHashMap) this.moduleHashesByFullHash.set(fullHash, new Map())
124
- return this.moduleHashesByFullHash.get(fullHash)!
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 {code: source} = generator(factory, {compact: true, comments: false, minified: true,})
128
- const factoryHash = createHash('sha256').update(source).digest('hex')
129
- moduleHashes.set(moduleId, {moduleId, factoryHash})
145
+ const hashes = moduleHashes.get(normalizedModuleId) ?? new Set<string>()
146
+ hashes.add(moduleHash)
147
+ moduleHashes.set(normalizedModuleId, hashes)
130
148
  })
131
149
  }
132
150
 
@@ -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: [...moduleFactories.values()],
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 {}