Skip to content

Commit

Permalink
feat(service): 兼容小程序开发者工具的 hmr 功能
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen-jj committed Nov 21, 2023
1 parent f80dd91 commit 95f8e53
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/taro-service/src/platform-plugin-base/mini.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -24,6 +25,8 @@ export abstract class TaroPlatformBase<T extends TConfig = TConfig> extends Taro
projectConfigJson?: string
taroComponentsPath?: string

private projectConfigJsonOutputPath: string

/**
* 1. 清空 dist 文件夹
* 2. 输出编译提示
Expand Down Expand Up @@ -53,6 +56,18 @@ export abstract class TaroPlatformBase<T extends TConfig = TConfig> 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 (_) {}

Check warning on line 69 in packages/taro-service/src/platform-plugin-base/mini.ts

View workflow job for this annotation

GitHub Actions / build (14.x)

Empty block statement

Check warning on line 69 in packages/taro-service/src/platform-plugin-base/mini.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Empty block statement
}
}

protected printDevelopmentTip (platform: string) {
Expand Down Expand Up @@ -165,6 +180,7 @@ ${exampleCommand}`))
srcConfigName: src,
distConfigName: dist
})
this.projectConfigJsonOutputPath = `${this.ctx.paths.outputPath}/${dist}`
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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));`
}
}
}
})
})
})
}
}

0 comments on commit 95f8e53

Please sign in to comment.