@taro-hmr-compile/plugin-webpack 0.0.1-alpha.3

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 (46) hide show
  1. package/dist/compile/compile-state.d.ts +3 -0
  2. package/dist/compile/compile-state.js +21 -0
  3. package/dist/compile/compile-state.js.map +1 -0
  4. package/dist/compile/hmr-compile-loader.d.ts +11 -0
  5. package/dist/compile/hmr-compile-loader.js +52 -0
  6. package/dist/compile/hmr-compile-loader.js.map +1 -0
  7. package/dist/compile/hmr-compiler-plugin.d.ts +38 -0
  8. package/dist/compile/hmr-compiler-plugin.js +158 -0
  9. package/dist/compile/hmr-compiler-plugin.js.map +1 -0
  10. package/dist/compile/inject-app-hmr-runtime.d.ts +1 -0
  11. package/dist/compile/inject-app-hmr-runtime.js +46 -0
  12. package/dist/compile/inject-app-hmr-runtime.js.map +1 -0
  13. package/dist/constants.d.ts +3 -0
  14. package/dist/constants.js +7 -0
  15. package/dist/constants.js.map +1 -0
  16. package/dist/index.d.ts +8 -0
  17. package/dist/index.js +49 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/patch/hmr-patch-manager.d.ts +30 -0
  20. package/dist/patch/hmr-patch-manager.js +75 -0
  21. package/dist/patch/hmr-patch-manager.js.map +1 -0
  22. package/dist/patch/hmr-patch-plugin.d.ts +16 -0
  23. package/dist/patch/hmr-patch-plugin.js +239 -0
  24. package/dist/patch/hmr-patch-plugin.js.map +1 -0
  25. package/dist/patch/hmr-patch-runtime-plugin.d.ts +4 -0
  26. package/dist/patch/hmr-patch-runtime-plugin.js +142 -0
  27. package/dist/patch/hmr-patch-runtime-plugin.js.map +1 -0
  28. package/dist/server/hmr-server-plugin.d.ts +13 -0
  29. package/dist/server/hmr-server-plugin.js +29 -0
  30. package/dist/server/hmr-server-plugin.js.map +1 -0
  31. package/dist/utils.d.ts +4 -0
  32. package/dist/utils.js +25 -0
  33. package/dist/utils.js.map +1 -0
  34. package/package.json +51 -0
  35. package/src/compile/compile-state.ts +15 -0
  36. package/src/compile/hmr-compile-loader.ts +72 -0
  37. package/src/compile/hmr-compiler-plugin.ts +191 -0
  38. package/src/compile/inject-app-hmr-runtime.ts +44 -0
  39. package/src/constants.ts +5 -0
  40. package/src/index.ts +67 -0
  41. package/src/patch/hmr-patch-manager.ts +102 -0
  42. package/src/patch/hmr-patch-plugin.ts +237 -0
  43. package/src/patch/hmr-patch-runtime-plugin.ts +150 -0
  44. package/src/server/hmr-server-plugin.ts +34 -0
  45. package/src/utils.ts +25 -0
  46. package/tsconfig.json +21 -0
@@ -0,0 +1,3 @@
1
+ export declare const markPageCompiled: (appPath: string, page: string) => void;
2
+ export declare const isPageCompiled: (appPath: string, page: string) => boolean;
3
+ export declare const clearCompiledPages: (appPath: string) => void;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.clearCompiledPages = exports.isPageCompiled = exports.markPageCompiled = void 0;
4
+ const compiledPagesByApp = new Map();
5
+ const markPageCompiled = (appPath, page) => {
6
+ var _a;
7
+ const compiledPages = (_a = compiledPagesByApp.get(appPath)) !== null && _a !== void 0 ? _a : new Set();
8
+ compiledPages.add(page);
9
+ compiledPagesByApp.set(appPath, compiledPages);
10
+ };
11
+ exports.markPageCompiled = markPageCompiled;
12
+ const isPageCompiled = (appPath, page) => {
13
+ var _a, _b;
14
+ return (_b = (_a = compiledPagesByApp.get(appPath)) === null || _a === void 0 ? void 0 : _a.has(page)) !== null && _b !== void 0 ? _b : false;
15
+ };
16
+ exports.isPageCompiled = isPageCompiled;
17
+ const clearCompiledPages = (appPath) => {
18
+ compiledPagesByApp.delete(appPath);
19
+ };
20
+ exports.clearCompiledPages = clearCompiledPages;
21
+ //# sourceMappingURL=compile-state.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compile-state.js","sourceRoot":"","sources":["../../src/compile/compile-state.ts"],"names":[],"mappings":";;;AAAA,MAAM,kBAAkB,GAAG,IAAI,GAAG,EAAuB,CAAA;AAElD,MAAM,gBAAgB,GAAG,CAAC,OAAe,EAAE,IAAY,EAAE,EAAE;;IAC9D,MAAM,aAAa,GAAG,MAAA,kBAAkB,CAAC,GAAG,CAAC,OAAO,CAAC,mCAAI,IAAI,GAAG,EAAU,CAAA;IAC1E,aAAa,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;IACvB,kBAAkB,CAAC,GAAG,CAAC,OAAO,EAAE,aAAa,CAAC,CAAA;AAClD,CAAC,CAAA;AAJY,QAAA,gBAAgB,oBAI5B;AAEM,MAAM,cAAc,GAAG,CAAC,OAAe,EAAE,IAAY,EAAE,EAAE;;IAC5D,OAAO,MAAA,MAAA,kBAAkB,CAAC,GAAG,CAAC,OAAO,CAAC,0CAAE,GAAG,CAAC,IAAI,CAAC,mCAAI,KAAK,CAAA;AAC9D,CAAC,CAAA;AAFY,QAAA,cAAc,kBAE1B;AAEM,MAAM,kBAAkB,GAAG,CAAC,OAAe,EAAE,EAAE;IAClD,kBAAkB,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;AACtC,CAAC,CAAA;AAFY,QAAA,kBAAkB,sBAE9B"}
@@ -0,0 +1,11 @@
1
+ import type { LoaderDefinitionFunction } from 'webpack';
2
+ import type { AppConfig } from "@tarojs/taro";
3
+ interface LoaderOptions {
4
+ appPath: string;
5
+ sourcePath: string;
6
+ appConfig: AppConfig;
7
+ framework: 'react';
8
+ compileUrl: string;
9
+ }
10
+ declare const hmrCompileLoader: LoaderDefinitionFunction<LoaderOptions>;
11
+ export default hmrCompileLoader;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const path_1 = __importDefault(require("path"));
7
+ const utils_1 = require("../utils");
8
+ const compile_state_1 = require("./compile-state");
9
+ const inject_app_hmr_runtime_1 = require("./inject-app-hmr-runtime");
10
+ const createReactShell = (opt) => {
11
+ const { page, compileUrl } = opt;
12
+ return `
13
+ import Taro from '@tarojs/taro'
14
+ import { Text, View } from '@tarojs/components'
15
+ import { useEffect } from 'react'
16
+ const PageShell = () => {
17
+ useEffect(() => {
18
+ const data = { page: '${page}' }
19
+ const url = '${compileUrl}'
20
+ Taro.request({ url, method: 'POST', data }).catch(error => console.error('[taro-hmr-compile] 页面编译请求失败', error))
21
+ }, [])
22
+
23
+ return (
24
+ <View style={{ position: 'fixed', top: 0, right: 0, bottom: 0, left: 0, zIndex: 9999, display: 'flex', flexDirection: 'column', alignItems: 'flex-start', justifyContent: 'center', boxSizing: 'border-box', padding: '32px', color: '#fff', background: 'rgba(0, 0, 0, 0.88)' }}>
25
+ <Text style={{ display: 'block', color: '#ff5555', fontFamily: 'monospace', fontSize: '20px', fontWeight: 'bold' }}>HMR PageShell</Text>
26
+ <Text style={{ display: 'block', marginTop: '12px', color: '#fff', fontFamily: 'monospace', lineHeight: 1.6 }}>Waiting for page patch...</Text>
27
+ <Text style={{ display: 'block', marginTop: '6px', color: '#b3b3b3', fontFamily: 'monospace', lineHeight: 1.6, wordBreak: 'break-all' }}>${page}</Text>
28
+ </View>
29
+ )
30
+ }
31
+ export default PageShell
32
+ `;
33
+ };
34
+ const createShell = (opt) => {
35
+ if (opt.framework === 'react')
36
+ return createReactShell(opt);
37
+ throw new Error(`unsupported framework: ${opt.framework}`);
38
+ };
39
+ const hmrCompileLoader = function (source) {
40
+ const { appPath, framework, appConfig, sourcePath, compileUrl } = this.getOptions();
41
+ const { dir: resourceDir, name: resourceName } = path_1.default.parse(path_1.default.relative(sourcePath, this.resourcePath));
42
+ const resourcePath = path_1.default.join(resourceDir, resourceName);
43
+ const isPageEntry = (0, utils_1.getAllPages)(appConfig).includes(resourcePath);
44
+ const isAppEntry = resourcePath === 'app';
45
+ if (isAppEntry)
46
+ return (0, inject_app_hmr_runtime_1.injectAppHmrRuntime)(source);
47
+ if (!isPageEntry || (0, compile_state_1.isPageCompiled)(appPath, resourcePath))
48
+ return source;
49
+ return createShell({ page: resourcePath, framework, compileUrl });
50
+ };
51
+ exports.default = hmrCompileLoader;
52
+ //# sourceMappingURL=hmr-compile-loader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hmr-compile-loader.js","sourceRoot":"","sources":["../../src/compile/hmr-compile-loader.ts"],"names":[],"mappings":";;;;;AACA,gDAAwB;AACxB,oCAAoC;AACpC,mDAA8C;AAE9C,qEAA4D;AAS5D,MAAM,gBAAgB,GAAG,CAAC,GAAmB,EAAE,EAAE;IAC7C,MAAM,EAAC,IAAI,EAAE,UAAU,EAAC,GAAG,GAAG,CAAA;IAC9B,OAAO;;;;;;gCAMqB,IAAI;uBACb,UAAU;;;;;;;;qJAQoH,IAAI;;;;;KAKpJ,CAAA;AACL,CAAC,CAAA;AAED,MAAM,WAAW,GAAG,CAAC,GAAmB,EAAE,EAAE;IACxC,IAAI,GAAG,CAAC,SAAS,KAAK,OAAO;QAAE,OAAO,gBAAgB,CAAC,GAAG,CAAC,CAAA;IAC3D,MAAM,IAAI,KAAK,CAAC,0BAA0B,GAAG,CAAC,SAAS,EAAE,CAAC,CAAA;AAC9D,CAAC,CAAA;AAWD,MAAM,gBAAgB,GAA4C,UAAU,MAAM;IAC9E,MAAM,EAAC,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,UAAU,EAAE,UAAU,EAAC,GAAG,IAAI,CAAC,UAAU,EAAE,CAAA;IAEjF,MAAM,EAAC,GAAG,EAAE,WAAW,EAAE,IAAI,EAAE,YAAY,EAAC,GAAG,cAAI,CAAC,KAAK,CAAC,cAAI,CAAC,QAAQ,CAAC,UAAU,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC,CAAA;IAEvG,MAAM,YAAY,GAAG,cAAI,CAAC,IAAI,CAAC,WAAW,EAAE,YAAY,CAAC,CAAA;IAEzD,MAAM,WAAW,GAAG,IAAA,mBAAW,EAAC,SAAS,CAAC,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAA;IAEjE,MAAM,UAAU,GAAG,YAAY,KAAK,KAAK,CAAA;IAEzC,IAAI,UAAU;QAAE,OAAO,IAAA,4CAAmB,EAAC,MAAM,CAAC,CAAA;IAElD,IAAI,CAAC,WAAW,IAAI,IAAA,8BAAc,EAAC,OAAO,EAAE,YAAY,CAAC;QAAE,OAAO,MAAM,CAAA;IAExE,OAAO,WAAW,CAAC,EAAC,IAAI,EAAE,YAAY,EAAE,SAAS,EAAE,UAAU,EAAC,CAAC,CAAA;AACnE,CAAC,CAAA;AAED,kBAAe,gBAAgB,CAAA"}
@@ -0,0 +1,38 @@
1
+ import type { IPaths } from '@tarojs/service';
2
+ import type { AppConfig } from '@tarojs/taro';
3
+ import { SyncHook } from 'tapable';
4
+ import type { Compilation, Compiler } from 'webpack';
5
+ import { ConcatSource } from 'webpack-sources';
6
+ import type { Source } from 'webpack-sources';
7
+ type StyleFilename = NonNullable<Compiler['options']['output']['cssFilename']>;
8
+ export interface HmrCompilerPluginOptions {
9
+ paths: IPaths;
10
+ appConfig: AppConfig;
11
+ styleFilename?: StyleFilename;
12
+ }
13
+ export interface HmrCompilerHooks {
14
+ compileRequested: SyncHook<[page: string]>;
15
+ }
16
+ export interface HmrCompilationHooks {
17
+ pageChunkModules: SyncHook<[source: string]>;
18
+ }
19
+ export declare const applyPatchBeforeStartup: (source: Source, request: string) => ConcatSource;
20
+ export declare class HmrCompilerPlugin {
21
+ private readonly options;
22
+ private static readonly compilerHooksMap;
23
+ private static readonly compilationHooksMap;
24
+ private compiler?;
25
+ private hasCompletedInitialBuild;
26
+ private readonly pendingFiles;
27
+ private readonly rebuildingFiles;
28
+ constructor(options: HmrCompilerPluginOptions);
29
+ static getCompilerHooks(compiler: Compiler): HmrCompilerHooks;
30
+ static getCompilationHooks(compilation: Compilation): HmrCompilationHooks;
31
+ apply(compiler: Compiler): void;
32
+ private requestCompile;
33
+ private isPageChunk;
34
+ /** PageShell 不包含真实页面的样式依赖,为缺失的页面样式补充空资产。 */
35
+ private emitMissingPageStyleAssets;
36
+ private reset;
37
+ }
38
+ export {};
@@ -0,0 +1,158 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.HmrCompilerPlugin = exports.applyPatchBeforeStartup = void 0;
7
+ const path_1 = __importDefault(require("path"));
8
+ const helper_1 = require("@tarojs/helper");
9
+ const tapable_1 = require("tapable");
10
+ const webpack_sources_1 = require("webpack-sources");
11
+ const constants_1 = require("../constants");
12
+ const compile_state_1 = require("./compile-state");
13
+ const utils_1 = require("../utils");
14
+ const PLUGIN_NAME = 'HmrCompilerPlugin';
15
+ const applyPatchBeforeStartup = (source, request) => {
16
+ const patchRequest = JSON.stringify(`${request}.js`);
17
+ return new webpack_sources_1.ConcatSource(`globalThis.miniHmrApplyPatch(require(${patchRequest}));\n`, source);
18
+ };
19
+ exports.applyPatchBeforeStartup = applyPatchBeforeStartup;
20
+ class HmrCompilerPlugin {
21
+ constructor(options) {
22
+ this.options = options;
23
+ this.hasCompletedInitialBuild = false;
24
+ // 页面编译请求可能在任意时刻到达。先排队到下一轮 watchRun,避免与正在执行的编译相互覆盖。
25
+ this.pendingFiles = new Set();
26
+ // 当前编译批次的文件快照。与 pendingFiles 分离,确保编译期间新到达的请求留给下一批处理。
27
+ this.rebuildingFiles = new Set();
28
+ }
29
+ static getCompilerHooks(compiler) {
30
+ let hooks = HmrCompilerPlugin.compilerHooksMap.get(compiler);
31
+ if (!hooks) {
32
+ hooks = { compileRequested: new tapable_1.SyncHook(['page']) };
33
+ HmrCompilerPlugin.compilerHooksMap.set(compiler, hooks);
34
+ }
35
+ return hooks;
36
+ }
37
+ static getCompilationHooks(compilation) {
38
+ let hooks = HmrCompilerPlugin.compilationHooksMap.get(compilation);
39
+ if (!hooks) {
40
+ hooks = { pageChunkModules: new tapable_1.SyncHook(['source']) };
41
+ HmrCompilerPlugin.compilationHooksMap.set(compilation, hooks);
42
+ }
43
+ return hooks;
44
+ }
45
+ apply(compiler) {
46
+ this.compiler = compiler;
47
+ HmrCompilerPlugin.getCompilerHooks(compiler).compileRequested.tap(PLUGIN_NAME, page => {
48
+ this.requestCompile(page);
49
+ });
50
+ compiler.hooks.watchRun.tap(PLUGIN_NAME, currentCompiler => {
51
+ var _a;
52
+ if (this.pendingFiles.size === 0)
53
+ return;
54
+ // 页面是否使用 Shell 由内存中的 compile-state 决定,源文件本身没有发生变化。
55
+ // 因此需要把请求显式加入 modifiedFiles,并保存当前批次供 needBuild 强制重建。
56
+ this.rebuildingFiles.clear();
57
+ this.pendingFiles.forEach(file => this.rebuildingFiles.add(file));
58
+ currentCompiler.modifiedFiles = new Set([...((_a = currentCompiler.modifiedFiles) !== null && _a !== void 0 ? _a : []), ...this.pendingFiles]);
59
+ this.pendingFiles.clear();
60
+ });
61
+ compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => {
62
+ const pages = new Set((0, utils_1.getAllPages)(this.options.appConfig));
63
+ const isPageChunk = (chunk) => this.isPageChunk(compilation, pages, chunk);
64
+ const hmrHooks = HmrCompilerPlugin.getCompilationHooks(compilation);
65
+ const javascriptHooks = compiler.webpack.javascript.JavascriptModulesPlugin.getCompilationHooks(compilation);
66
+ // 只修改 Page Chunk 的 JS transport。Webpack 的 ModuleGraph / ChunkGraph 保持不变,
67
+ // 因而 hash、HMR diff 和 hot-update 仍以完整模块图为准。
68
+ javascriptHooks.renderChunk.tap({ name: PLUGIN_NAME, stage: -10000 }, (source, { chunk }) => {
69
+ if (!isPageChunk(chunk))
70
+ return source;
71
+ hmrHooks.pageChunkModules.call(source.source().toString());
72
+ return new webpack_sources_1.RawSource('{}');
73
+ });
74
+ javascriptHooks.renderStartup.tap(PLUGIN_NAME, (source, _module, { chunk }) => {
75
+ if (!isPageChunk(chunk))
76
+ return source;
77
+ const filenameTemplate = compiler.webpack.javascript.JavascriptModulesPlugin
78
+ .getChunkFilenameTemplate(chunk, compilation.outputOptions);
79
+ const assetName = compilation.getPath(filenameTemplate, {
80
+ chunk,
81
+ contentHashType: 'javascript',
82
+ hash: compilation.hash,
83
+ });
84
+ const patchModule = constants_1.HMR_PATCH_ASSET.replace(/\.js$/, '');
85
+ const relativePath = path_1.default.posix.relative(path_1.default.posix.dirname(assetName), patchModule);
86
+ const request = relativePath.startsWith('.') ? relativePath : `./${relativePath}`;
87
+ // 微信要求 Page() 在当前页面脚本的同步执行阶段完成。bootstrap 会同步补齐工厂,
88
+ // update 继续异步排队,因此这里不能用 Promise.then 包裹 Webpack startup。
89
+ return (0, exports.applyPatchBeforeStartup)(source, request);
90
+ });
91
+ compiler.webpack.NormalModule.getCompilationHooks(compilation).needBuild.tapAsync(PLUGIN_NAME, (module, _context, callback) => {
92
+ var _a, _b;
93
+ const [resourcePath] = (_b = (_a = module.resource) === null || _a === void 0 ? void 0 : _a.split('?')) !== null && _b !== void 0 ? _b : [];
94
+ // 绕过 Webpack 的模块缓存,让 loader 根据最新 compile-state 将页面 Shell 替换为真实页面。
95
+ if (resourcePath && this.rebuildingFiles.has(resourcePath))
96
+ return callback(null, true);
97
+ callback();
98
+ });
99
+ const stage = compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_REPORT;
100
+ compilation.hooks.processAssets.tap({ name: PLUGIN_NAME, stage }, () => {
101
+ if (!this.hasCompletedInitialBuild && compilation.hash && compilation.errors.length === 0) {
102
+ this.emitMissingPageStyleAssets(compilation);
103
+ }
104
+ });
105
+ });
106
+ compiler.hooks.afterEmit.tap(PLUGIN_NAME, compilation => {
107
+ const isSuccessful = compilation.errors.length === 0 && Boolean(compilation.hash);
108
+ this.hasCompletedInitialBuild = this.hasCompletedInitialBuild || isSuccessful;
109
+ });
110
+ // 当前批次完成后释放快照;编译期间新进入 pendingFiles 的请求不会被清理。
111
+ compiler.hooks.done.tap(PLUGIN_NAME, () => this.rebuildingFiles.clear());
112
+ compiler.hooks.watchClose.tap(PLUGIN_NAME, () => this.reset());
113
+ }
114
+ requestCompile(page) {
115
+ var _a, _b;
116
+ try {
117
+ if ((0, compile_state_1.isPageCompiled)(this.options.paths.appPath, page))
118
+ return;
119
+ // loader 在下一轮构建中读取该状态;必须先标记,再触发 watch invalidation。
120
+ (0, compile_state_1.markPageCompiled)(this.options.paths.appPath, page);
121
+ this.pendingFiles.add((0, helper_1.resolveMainFilePath)(path_1.default.resolve(this.options.paths.sourcePath, page)));
122
+ (_b = (_a = this.compiler) === null || _a === void 0 ? void 0 : _a.watching) === null || _b === void 0 ? void 0 : _b.invalidate();
123
+ }
124
+ catch (error) {
125
+ console.error('[taro-hmr-compile] 页面编译触发失败', error);
126
+ }
127
+ }
128
+ isPageChunk(compilation, pages, chunk) {
129
+ if (chunk.name && pages.has(chunk.name))
130
+ return true;
131
+ return Array.from(pages).some(page => { var _a; return ((_a = compilation.entrypoints.get(page)) === null || _a === void 0 ? void 0 : _a.getEntrypointChunk()) === chunk; });
132
+ }
133
+ /** PageShell 不包含真实页面的样式依赖,为缺失的页面样式补充空资产。 */
134
+ emitMissingPageStyleAssets(compilation) {
135
+ const { appConfig, styleFilename } = this.options;
136
+ if (!styleFilename)
137
+ return;
138
+ (0, utils_1.getAllPages)(appConfig).forEach(page => {
139
+ var _a;
140
+ const chunk = (_a = compilation.entrypoints.get(page)) === null || _a === void 0 ? void 0 : _a.getEntrypointChunk();
141
+ if (!chunk)
142
+ return;
143
+ const assetName = compilation.getPath(styleFilename, { chunk, contentHashType: 'css/mini-extract' });
144
+ if (!compilation.getAsset(assetName))
145
+ compilation.emitAsset(assetName, new webpack_sources_1.RawSource(''));
146
+ });
147
+ }
148
+ reset() {
149
+ (0, compile_state_1.clearCompiledPages)(this.options.paths.appPath);
150
+ this.hasCompletedInitialBuild = false;
151
+ this.rebuildingFiles.clear();
152
+ this.pendingFiles.clear();
153
+ }
154
+ }
155
+ exports.HmrCompilerPlugin = HmrCompilerPlugin;
156
+ HmrCompilerPlugin.compilerHooksMap = new WeakMap();
157
+ HmrCompilerPlugin.compilationHooksMap = new WeakMap();
158
+ //# sourceMappingURL=hmr-compiler-plugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hmr-compiler-plugin.js","sourceRoot":"","sources":["../../src/compile/hmr-compiler-plugin.ts"],"names":[],"mappings":";;;;;;AAAA,gDAAuB;AACvB,2CAAkD;AAGlD,qCAAgC;AAEhC,qDAAuD;AAEvD,4CAA4C;AAC5C,mDAAoF;AACpF,oCAAoC;AAEpC,MAAM,WAAW,GAAG,mBAAmB,CAAA;AAkBhC,MAAM,uBAAuB,GAAG,CAAC,MAAc,EAAE,OAAe,EAAE,EAAE;IACvE,MAAM,YAAY,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,OAAO,KAAK,CAAC,CAAA;IACpD,OAAO,IAAI,8BAAY,CACnB,wCAAwC,YAAY,OAAO,EAC3D,MAAM,CACT,CAAA;AACL,CAAC,CAAA;AANY,QAAA,uBAAuB,2BAMnC;AAED,MAAa,iBAAiB;IAe1B,YAA6B,OAAiC;QAAjC,YAAO,GAAP,OAAO,CAA0B;QARtD,6BAAwB,GAAG,KAAK,CAAA;QAExC,mDAAmD;QAClC,iBAAY,GAAG,IAAI,GAAG,EAAU,CAAA;QAEjD,qDAAqD;QACpC,oBAAe,GAAG,IAAI,GAAG,EAAU,CAAA;IAEa,CAAC;IAElE,MAAM,CAAC,gBAAgB,CAAC,QAAkB;QACtC,IAAI,KAAK,GAAG,iBAAiB,CAAC,gBAAgB,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;QAC5D,IAAI,CAAC,KAAK,EAAE,CAAC;YACT,KAAK,GAAG,EAAC,gBAAgB,EAAE,IAAI,kBAAQ,CAAC,CAAC,MAAM,CAAC,CAAC,EAAC,CAAA;YAClD,iBAAiB,CAAC,gBAAgB,CAAC,GAAG,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAA;QAC3D,CAAC;QACD,OAAO,KAAK,CAAA;IAChB,CAAC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AAC,SAAS,EAAE,IAAI,2BAAS,CAAC,EAAE,CAAC,CAAC,CAAA;QAC7F,CAAC,CAAC,CAAA;IACN,CAAC;IAEO,KAAK;QACT,IAAA,kCAAkB,EAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;QAC9C,IAAI,CAAC,wBAAwB,GAAG,KAAK,CAAA;QACrC,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAA;QAC5B,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,CAAA;IAC7B,CAAC;;AAvJL,8CAwJC;AAvJ2B,kCAAgB,GAAG,IAAI,OAAO,EAA8B,AAA5C,CAA4C;AAE5D,qCAAmB,GAAG,IAAI,OAAO,EAAoC,AAAlD,CAAkD"}
@@ -0,0 +1 @@
1
+ export declare const injectAppHmrRuntime: (source: string) => string;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.injectAppHmrRuntime = void 0;
7
+ const template_1 = __importDefault(require("@babel/template"));
8
+ const traverse_1 = __importDefault(require("@babel/traverse"));
9
+ const generator_1 = __importDefault(require("@babel/generator"));
10
+ const parser_1 = __importDefault(require("@babel/parser"));
11
+ const webpack_1 = __importDefault(require("webpack"));
12
+ const injectAppHmrRuntime = (source) => {
13
+ const ast = parser_1.default.parse(source, { sourceType: 'module', plugins: ['typescript', 'jsx'] });
14
+ const buildHmrCode = template_1.default.statements(`
15
+ import { mergePatches, injectHmrLifecycle } from '@taro-hmr-compile/core/runtime'
16
+ injectHmrLifecycle()
17
+ globalThis.miniHmrApplyPatch = (patches)=>{
18
+ const bootstrapPatch = patches.find(patch => patch.type === 'bootstrap')
19
+ return __webpack_require__.miniHmr.applyPatch(bootstrapPatch).then(() => {
20
+ const updatePatch = mergePatches(${webpack_1.default.RuntimeGlobals.getFullHash}(), patches)
21
+ return __webpack_require__.miniHmr.applyPatch(updatePatch)
22
+ }).catch(error => {
23
+ console.error('[taro-hmr-compile] patch apply failed', error)
24
+ })
25
+ };
26
+ `, { placeholderPattern: false });
27
+ (0, traverse_1.default)(ast, {
28
+ Program(path) {
29
+ const body = path.get('body');
30
+ const hasInjected = body.some(item => {
31
+ return item.isImportDeclaration() && item.node.source.value === '@taro-hmr-compile/core/runtime';
32
+ });
33
+ if (hasInjected)
34
+ return;
35
+ const nodes = buildHmrCode();
36
+ const lastImport = [...body].reverse().find(item => item.isImportDeclaration());
37
+ if (lastImport)
38
+ lastImport.insertAfter(nodes);
39
+ if (!lastImport)
40
+ path.unshiftContainer('body', nodes);
41
+ }
42
+ });
43
+ return (0, generator_1.default)(ast).code;
44
+ };
45
+ exports.injectAppHmrRuntime = injectAppHmrRuntime;
46
+ //# sourceMappingURL=inject-app-hmr-runtime.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inject-app-hmr-runtime.js","sourceRoot":"","sources":["../../src/compile/inject-app-hmr-runtime.ts"],"names":[],"mappings":";;;;;;AAAA,+DAAsC;AACtC,+DAAsC;AACtC,iEAAwC;AACxC,2DAAkC;AAClC,sDAA8B;AAEvB,MAAM,mBAAmB,GAAG,CAAC,MAAc,EAAE,EAAE;IAClD,MAAM,GAAG,GAAG,gBAAM,CAAC,KAAK,CAAC,MAAM,EAAE,EAAC,UAAU,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,YAAY,EAAE,KAAK,CAAC,EAAC,CAAC,CAAA;IAExF,MAAM,YAAY,GAAG,kBAAQ,CAAC,UAAU,CAAC;;;;;;6CAMA,iBAAO,CAAC,cAAc,CAAC,WAAW;;;;;;KAM1E,EAAE,EAAC,kBAAkB,EAAE,KAAK,EAAC,CAAC,CAAA;IAE/B,IAAA,kBAAQ,EAAC,GAAG,EAAE;QACV,OAAO,CAAC,IAAI;YACR,MAAM,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;YAE7B,MAAM,WAAW,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;gBACjC,OAAO,IAAI,CAAC,mBAAmB,EAAE,IAAI,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,KAAK,gCAAgC,CAAA;YACpG,CAAC,CAAC,CAAA;YAEF,IAAI,WAAW;gBAAE,OAAM;YAEvB,MAAM,KAAK,GAAG,YAAY,EAAE,CAAA;YAE5B,MAAM,UAAU,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC,OAAO,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,mBAAmB,EAAE,CAAC,CAAA;YAE/E,IAAI,UAAU;gBAAE,UAAU,CAAC,WAAW,CAAC,KAAK,CAAC,CAAA;YAE7C,IAAI,CAAC,UAAU;gBAAE,IAAI,CAAC,gBAAgB,CAAC,MAAM,EAAE,KAAK,CAAC,CAAA;QACzD,CAAC;KACJ,CAAC,CAAA;IACF,OAAO,IAAA,mBAAS,EAAC,GAAG,CAAC,CAAC,IAAI,CAAA;AAC9B,CAAC,CAAA;AArCY,QAAA,mBAAmB,uBAqC/B"}
@@ -0,0 +1,3 @@
1
+ export declare const HMR_OUTPUT_PATH = "hmr";
2
+ export declare const HMR_PATCH_ASSET = "hmr/patch.js";
3
+ export declare const DEFAULT_HMR_PATCH_HISTORY_LIMIT = 20;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_HMR_PATCH_HISTORY_LIMIT = exports.HMR_PATCH_ASSET = exports.HMR_OUTPUT_PATH = void 0;
4
+ exports.HMR_OUTPUT_PATH = 'hmr';
5
+ exports.HMR_PATCH_ASSET = `${exports.HMR_OUTPUT_PATH}/patch.js`;
6
+ exports.DEFAULT_HMR_PATCH_HISTORY_LIMIT = 20;
7
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,eAAe,GAAG,KAAK,CAAA;AAEvB,QAAA,eAAe,GAAG,GAAG,uBAAe,WAAW,CAAA;AAE/C,QAAA,+BAA+B,GAAG,EAAE,CAAA"}
@@ -0,0 +1,8 @@
1
+ import type { IPluginContext } from '@tarojs/service';
2
+ export interface WebpackHmrCompileOpt {
3
+ host: string;
4
+ port: number;
5
+ patchHistoryLimit: number;
6
+ }
7
+ declare const _default: (ctx: IPluginContext, pluginOpts: Partial<WebpackHmrCompileOpt>) => void;
8
+ export default _default;
package/dist/index.js ADDED
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const constants_1 = require("./constants");
4
+ const utils_1 = require("./utils");
5
+ const hmr_compiler_plugin_1 = require("./compile/hmr-compiler-plugin");
6
+ const hmr_patch_plugin_1 = require("./patch/hmr-patch-plugin");
7
+ const hmr_server_plugin_1 = require("./server/hmr-server-plugin");
8
+ const compiler_1 = require("@taro-hmr-compile/react-refresh/compiler");
9
+ const compiler_2 = require("@taro-hmr-compile/core/compiler");
10
+ exports.default = (ctx, pluginOpts) => {
11
+ var _a, _b, _c;
12
+ if (!((_a = ctx.runOpts.options) === null || _a === void 0 ? void 0 : _a.isWatch))
13
+ return;
14
+ const { appPath, sourcePath, outputPath } = ctx.paths;
15
+ const { framework } = ctx.initialConfig;
16
+ const appConfig = (0, utils_1.getAppConfig)(ctx);
17
+ const host = (_b = pluginOpts.host) !== null && _b !== void 0 ? _b : '127.0.0.1';
18
+ const port = (_c = pluginOpts.port) !== null && _c !== void 0 ? _c : 8080;
19
+ const compileUrl = `http://${host}:${port}/compile`;
20
+ (0, compiler_1.setupReactRefresh)(ctx);
21
+ ctx.onBuildStart(() => (0, compiler_2.emptyDirFile)(outputPath));
22
+ ctx.modifyRunnerOpts(({ opts }) => {
23
+ var _a;
24
+ (_a = opts.output) !== null && _a !== void 0 ? _a : (opts.output = {});
25
+ opts.output.clean = false;
26
+ });
27
+ ctx.modifyWebpackChain(({ chain, webpack }) => {
28
+ var _a;
29
+ chain.module
30
+ .rule('hmr-compile')
31
+ .test(/\.[jt]sx?$/)
32
+ .include.add(ctx.paths.sourcePath)
33
+ .end()
34
+ .use('hmr-compile-loader')
35
+ .loader(require.resolve('./compile/hmr-compile-loader'))
36
+ .options({ appPath, sourcePath, appConfig, framework, compileUrl })
37
+ .end();
38
+ chain.output
39
+ .set('hotUpdateChunkFilename', `${constants_1.HMR_OUTPUT_PATH}/[id].[fullhash].hot-update.js`)
40
+ .set('hotUpdateMainFilename', `${constants_1.HMR_OUTPUT_PATH}/[runtime].[fullhash].hot-update.json`);
41
+ const { patchHistoryLimit } = pluginOpts;
42
+ const [{ filename: styleFilename }] = ((_a = chain.plugin('miniCssExtractPlugin')) === null || _a === void 0 ? void 0 : _a.get('args')) || [];
43
+ chain.plugin('hmr-compiler-plugin').use(hmr_compiler_plugin_1.HmrCompilerPlugin, [{ paths: ctx.paths, appConfig, styleFilename }]);
44
+ chain.plugin('hmr-plugin').use(webpack.HotModuleReplacementPlugin);
45
+ chain.plugin('hmr-patch-plugin').use(hmr_patch_plugin_1.HmrPatchPlugin, [{ patchHistoryLimit }]);
46
+ chain.plugin('hmr-server-plugin').use(hmr_server_plugin_1.HmrServerPlugin, [{ host, port }]);
47
+ });
48
+ };
49
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAEA,2CAA2C;AAC3C,mCAAoC;AACpC,uEAA+D;AAC/D,+DAAuD;AACvD,kEAA0D;AAC1D,uEAA0E;AAC1E,8DAA4D;AAQ5D,kBAAe,CAAC,GAAmB,EAAE,UAAyC,EAAE,EAAE;;IAC9E,IAAI,CAAC,CAAA,MAAA,GAAG,CAAC,OAAO,CAAC,OAAO,0CAAE,OAAO,CAAA;QAAE,OAAM;IAEzC,MAAM,EAAC,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ACP,GAAG,CAAC,wBAAwB,EAAE,GAAG,2BAAe,gCAAgC,CAAC;aACjF,GAAG,CAAC,uBAAuB,EAAE,GAAG,2BAAe,uCAAuC,CAAC,CAAA;QAE5F,MAAM,EAAC,iBAAiB,EAAC,GAAG,UAAU,CAAA;QAEtC,MAAM,CAAC,EAAC,QAAQ,EAAE,aAAa,EAAC,CAAC,GAAG,CAAA,MAAA,KAAK,CAAC,MAAM,CAAC,sBAAsB,CAAC,0CAAE,GAAG,CAAC,MAAM,CAAC,KAAI,EAAE,CAAA;QAE3F,KAAK,CAAC,MAAM,CAAC,qBAAqB,CAAC,CAAC,GAAG,CAAC,uCAAiB,EAAE,CAAC,EAAC,KAAK,EAAE,GAAG,CAAC,KAAK,EAAE,SAAS,EAAE,aAAa,EAAC,CAAC,CAAC,CAAA;QAE1G,KAAK,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,0BAA0B,CAAC,CAAA;QAElE,KAAK,CAAC,MAAM,CAAC,kBAAkB,CAAC,CAAC,GAAG,CAAC,iCAAc,EAAE,CAAC,EAAC,iBAAiB,EAAC,CAAC,CAAC,CAAA;QAE3E,KAAK,CAAC,MAAM,CAAC,mBAAmB,CAAC,CAAC,GAAG,CAAC,mCAAe,EAAE,CAAC,EAAC,IAAI,EAAE,IAAI,EAAC,CAAC,CAAC,CAAA;IAC1E,CAAC,CAAC,CAAA;AACN,CAAC,CAAA"}
@@ -0,0 +1,30 @@
1
+ import type { Expression } from '@babel/types';
2
+ export interface BootstrapPatchSource {
3
+ type: 'bootstrap';
4
+ moduleFactories: Map<string, Expression>;
5
+ }
6
+ export interface UpdatePatchSource {
7
+ type: 'update';
8
+ fromHash: string;
9
+ toHash: string;
10
+ moduleFactories: Map<string, Expression>;
11
+ removedModules: string[];
12
+ }
13
+ export type PatchSource = BootstrapPatchSource | UpdatePatchSource;
14
+ /** 仅维护 bootstrap/update Patch 状态、连续性和压缩,不依赖 Webpack 编译对象。 */
15
+ export declare class HmrPatchManager {
16
+ private bootstrapPatch?;
17
+ private compactedUpdatePatch?;
18
+ private readonly recentUpdatePatches;
19
+ private readonly limit;
20
+ constructor(limit: number);
21
+ clear(): void;
22
+ setBootstrap(moduleFactories: Map<string, Expression>): void;
23
+ /** 追加 update;发现 hash 不连续时丢弃旧 update 链,并在追加后执行容量压缩。 */
24
+ appendUpdate(patch: UpdatePatchSource): void;
25
+ /** 按客户端消费顺序返回 bootstrap、压缩 update 与近期 update。 */
26
+ getPatches(): PatchSource[];
27
+ private clearUpdateHistory;
28
+ /** 将超出上限的旧 update 逐步合并到首个 update;bootstrap 永不参与 merge。 */
29
+ private compactUpdateHistory;
30
+ }
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HmrPatchManager = void 0;
4
+ /** 合并两个连续 update,并按后一份 update 的最终模块状态解决修改和删除冲突。 */
5
+ const mergeUpdatePatches = (basePatch, nextPatch) => {
6
+ if (basePatch.toHash !== nextPatch.fromHash) {
7
+ throw new Error(`[taro-hmr-compile] 无法合并不连续的 HMR patch:${basePatch.toHash} !== ${nextPatch.fromHash}`);
8
+ }
9
+ const moduleFactories = new Map(basePatch.moduleFactories);
10
+ const removedModules = new Set(basePatch.removedModules);
11
+ nextPatch.moduleFactories.forEach((factory, moduleId) => {
12
+ moduleFactories.set(moduleId, factory);
13
+ removedModules.delete(moduleId);
14
+ });
15
+ nextPatch.removedModules.forEach(moduleId => {
16
+ moduleFactories.delete(moduleId);
17
+ removedModules.add(moduleId);
18
+ });
19
+ return {
20
+ type: 'update',
21
+ fromHash: basePatch.fromHash,
22
+ toHash: nextPatch.toHash,
23
+ moduleFactories,
24
+ removedModules: Array.from(removedModules),
25
+ };
26
+ };
27
+ /** 仅维护 bootstrap/update Patch 状态、连续性和压缩,不依赖 Webpack 编译对象。 */
28
+ class HmrPatchManager {
29
+ constructor(limit) {
30
+ this.recentUpdatePatches = [];
31
+ this.limit = Math.max(1, limit);
32
+ }
33
+ clear() {
34
+ this.bootstrapPatch = undefined;
35
+ this.clearUpdateHistory();
36
+ }
37
+ setBootstrap(moduleFactories) {
38
+ this.bootstrapPatch = moduleFactories.size > 0
39
+ ? { type: 'bootstrap', moduleFactories: new Map(moduleFactories) }
40
+ : undefined;
41
+ }
42
+ /** 追加 update;发现 hash 不连续时丢弃旧 update 链,并在追加后执行容量压缩。 */
43
+ appendUpdate(patch) {
44
+ var _a;
45
+ const latestPatch = (_a = this.recentUpdatePatches[this.recentUpdatePatches.length - 1]) !== null && _a !== void 0 ? _a : this.compactedUpdatePatch;
46
+ if (latestPatch && latestPatch.toHash !== patch.fromHash)
47
+ this.clearUpdateHistory();
48
+ this.recentUpdatePatches.push(patch);
49
+ this.compactUpdateHistory();
50
+ }
51
+ /** 按客户端消费顺序返回 bootstrap、压缩 update 与近期 update。 */
52
+ getPatches() {
53
+ const updatePatches = this.compactedUpdatePatch
54
+ ? [this.compactedUpdatePatch, ...this.recentUpdatePatches]
55
+ : this.recentUpdatePatches;
56
+ return this.bootstrapPatch
57
+ ? [this.bootstrapPatch, ...updatePatches]
58
+ : updatePatches;
59
+ }
60
+ clearUpdateHistory() {
61
+ this.compactedUpdatePatch = undefined;
62
+ this.recentUpdatePatches.length = 0;
63
+ }
64
+ /** 将超出上限的旧 update 逐步合并到首个 update;bootstrap 永不参与 merge。 */
65
+ compactUpdateHistory() {
66
+ while (this.recentUpdatePatches.length > this.limit) {
67
+ const oldestUpdatePatch = this.recentUpdatePatches.shift();
68
+ this.compactedUpdatePatch = this.compactedUpdatePatch
69
+ ? mergeUpdatePatches(this.compactedUpdatePatch, oldestUpdatePatch)
70
+ : oldestUpdatePatch;
71
+ }
72
+ }
73
+ }
74
+ exports.HmrPatchManager = HmrPatchManager;
75
+ //# sourceMappingURL=hmr-patch-manager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hmr-patch-manager.js","sourceRoot":"","sources":["../../src/patch/hmr-patch-manager.ts"],"names":[],"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MAAM,GAAG,IAAI,CAAC,KAAK,EAAE,CAAC;YAClD,MAAM,iBAAiB,GAAG,IAAI,CAAC,mBAAmB,CAAC,KAAK,EAAG,CAAA;YAC3D,IAAI,CAAC,oBAAoB,GAAG,IAAI,CAAC,oBAAoB;gBACjD,CAAC,CAAC,kBAAkB,CAAC,IAAI,CAAC,oBAAoB,EAAE,iBAAiB,CAAC;gBAClE,CAAC,CAAC,iBAAiB,CAAA;QAC3B,CAAC;IACL,CAAC;CACJ;AAxDD,0CAwDC"}
@@ -0,0 +1,16 @@
1
+ import type { Compiler } from 'webpack';
2
+ export interface HmrPatchPluginOptions {
3
+ patchHistoryLimit?: number;
4
+ }
5
+ export declare class HmrPatchPlugin {
6
+ private hasCompletedInitialBuild;
7
+ private readonly patchManager;
8
+ private readonly emittedAssetSources;
9
+ constructor(options: HmrPatchPluginOptions);
10
+ apply(compiler: Compiler): void;
11
+ private processAssets;
12
+ /** 热更新转换完成后,保留固定 patch 与发生变化的模板、平台样式资产。 */
13
+ private removeSupersededAssets;
14
+ private emitPatchAsset;
15
+ private reset;
16
+ }