From 95f8e53edc0979cdc17f2901c0f161ab525dd96a Mon Sep 17 00:00:00 2001 From: Chen-jj <798095202@qq.com> Date: Mon, 20 Nov 2023 17:35:21 +0800 Subject: [PATCH] =?UTF-8?q?feat(service):=20=E5=85=BC=E5=AE=B9=E5=B0=8F?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=BC=80=E5=8F=91=E8=80=85=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=9A=84=20hmr=20=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/platform-plugin-base/mini.ts | 16 ++++++++ .../webpack/hmr-plugin.ts | 37 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 packages/taro-service/src/platform-plugin-base/webpack/hmr-plugin.ts diff --git a/packages/taro-service/src/platform-plugin-base/mini.ts b/packages/taro-service/src/platform-plugin-base/mini.ts index ef48e03e0852..4f9f02e4f0d6 100644 --- a/packages/taro-service/src/platform-plugin-base/mini.ts +++ b/packages/taro-service/src/platform-plugin-base/mini.ts @@ -1,5 +1,6 @@ import { recursiveMerge } from '@tarojs/helper' import { isObject, PLATFORM_TYPE } from '@tarojs/shared' +import * as path from 'path' import { getPkgVersion } from '../utils/package' import TaroPlatform from './platform' @@ -24,6 +25,8 @@ export abstract class TaroPlatformBase extends Taro projectConfigJson?: string taroComponentsPath?: string + private projectConfigJsonOutputPath: string + /** * 1. 清空 dist 文件夹 * 2. 输出编译提示 @@ -53,6 +56,18 @@ export abstract class TaroPlatformBase extends Taro const { printLog, processTypeEnum } = this.ctx.helper printLog(processTypeEnum.START, '开发者工具-项目目录', `${this.ctx.paths.outputPath}`) } + // Webpack5 代码自动热重载 + if (this.compiler === 'webpack5' && this.config.isWatch && this.projectConfigJsonOutputPath) { + try { + const projectConfig = require(this.projectConfigJsonOutputPath) + if (projectConfig.setting?.compileHotReLoad === true) { + this.ctx.modifyWebpackChain(({ chain }) => { + chain.plugin('TaroMiniHMRPlugin') + .use(require(path.join(__dirname, './webpack/hmr-plugin.js')).default) + }) + } + } catch (_) {} + } } protected printDevelopmentTip (platform: string) { @@ -165,6 +180,7 @@ ${exampleCommand}`)) srcConfigName: src, distConfigName: dist }) + this.projectConfigJsonOutputPath = `${this.ctx.paths.outputPath}/${dist}` } /** diff --git a/packages/taro-service/src/platform-plugin-base/webpack/hmr-plugin.ts b/packages/taro-service/src/platform-plugin-base/webpack/hmr-plugin.ts new file mode 100644 index 000000000000..fefac2d5eb91 --- /dev/null +++ b/packages/taro-service/src/platform-plugin-base/webpack/hmr-plugin.ts @@ -0,0 +1,37 @@ +export default class TaroMiniHMRPlugin { + apply (compiler) { + compiler.hooks.thisCompilation.tap('TaroMiniHMRPlugin', compilation => { + compilation.hooks.beforeChunkAssets.tap('TaroMiniHMRPlugin', () => { + compilation.chunks.forEach(chunk => { + if (chunk.hasRuntime() && chunk.name === 'runtime') { + const runtimeModules = compilation.chunkGraph.getChunkRuntimeModulesInOrder(chunk) + for (const module of runtimeModules) { + if (module.name === 'jsonp chunk loading') { + const runtimeSource = compilation.codeGenerationResults.getSource( + module, + chunk.runtime, + 'runtime' + ) + runtimeSource._value += ` +var miniHMRCallback = function(parentChunkLoadingFunction, data) { + var chunkIds = data[0]; + var moreModules = data[1]; + if(chunkIds.some(function(id) { return installedChunks[id] === 0 })) { + chunkIds.forEach(id => { + delete installedChunks[id] + }) + Object.keys(moreModules).forEach(id => { + delete __webpack_module_cache__[id] + }) + } + if(parentChunkLoadingFunction) parentChunkLoadingFunction(data); +} +chunkLoadingGlobal.push = miniHMRCallback.bind(null, chunkLoadingGlobal.push.bind(chunkLoadingGlobal));` + } + } + } + }) + }) + }) + } +}