Skip to content

Commit

Permalink
fix: hmr on locale dir/file rename or deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Oct 17, 2023
1 parent 245e60a commit 8e826a6
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 27 deletions.
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,19 @@
"vite": ">=4.0.0"
},
"dependencies": {
"bcp-47": "^2.1.0",
"clone-deep": "^4.0.1",
"debug": "^4.3.4",
"fast-glob": "^3.3.1",
"fs-extra": "^11.1.1",
"iso-639-1": "^3.1.0",
"json5": "^2.2.3",
"parse-glob": "^3.0.4",
"picocolors": "^1.0.0",
"string.prototype.trimend": "^1.0.7",
"uniq": "^1.0.1"
"throttle-debounce": "^5.0.0",
"uniq": "^1.0.1",
"watcher": "^2.3.0"
},
"devDependencies": {
"@minko-fe/eslint-config": "^2.0.0",
Expand All @@ -79,6 +83,7 @@
"@types/fs-extra": "^11.0.2",
"@types/parse-glob": "^3.0.30",
"@types/react": "^18.2.28",
"@types/throttle-debounce": "^5.0.0",
"bumpp": "^9.2.0",
"conventional-changelog-cli": "^4.1.0",
"eslint": "8.51.0",
Expand Down
1 change: 1 addition & 0 deletions playground/spa/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function App() {
<div id='language' style={{ marginBottom: '16px' }}>
{t('test.key')}
</div>
<div>extra: {t('more.a.b.c')}</div>
<button
onClick={async () => {
await i18n.changeLanguage('zh')
Expand Down
6 changes: 3 additions & 3 deletions playground/spa/src/locale/de/more.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"mo": {
"fdsa": {
"ff": "fdasf"
"a": {
"b": {
"c": "dxx"
}
}
}
7 changes: 7 additions & 0 deletions playground/spa/src/locale/en/more.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"a": {
"b": {
"c": "aaaa"
}
}
}
72 changes: 72 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 23 additions & 14 deletions src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { RESOLVED_VIRTUAL_PREFIX, RESOURCE_VIRTURL_HELPER, VIRTUAL } from './uti
import { LocaleDetector } from './locale-detector/LocaleDetector'
import { debug } from './utils/debugger'
import { type EnableParsersType } from './parsers'
import { initWatcher } from './utils/file-watcher'
import { hmr } from './utils/hmr'

export interface I18nDetectorOptions {
/**
Expand Down Expand Up @@ -92,7 +94,7 @@ export async function i18nDetector(options: I18nDetectorOptions) {
let code = `export default { `
for (const k of Object.keys(modules)) {
// Currently rollup doesn't support inline chunkName
// TODO: chunk name
// TODO: inline chunk name
code += `'${k}': () => import('${VIRTUAL}-${k}'),`
}
code += ' };'
Expand All @@ -108,25 +110,32 @@ export async function i18nDetector(options: I18nDetectorOptions) {

return null
},
configureServer(server) {
initWatcher(options.localesPaths, async (_type, p, pnext) => {
if (!p) return

debug('watcher', p, '=========>', pnext)
const _hmr = async () => {
await localeDetector.init()
hmr(server, localeDetector)
}

if (path.extname(p) && localeDetector.allLocaleFiles.has(p)) {
// file
_hmr()
}
if (localeDetector.allLocaleDirs.has(p)) {
_hmr()
}
})
},
async handleHotUpdate({ file, server }) {
const updated = await localeDetector.onFileChanged({ fsPath: file })

if (updated) {
const { resolvedIds } = localeDetector.localeModules
debug('hmr', resolvedIds)
for (const [, value] of resolvedIds) {
const { moduleGraph, ws } = server
const module = moduleGraph.getModuleById(RESOLVED_VIRTUAL_PREFIX + value)
if (module) {
moduleGraph.invalidateModule(module)
if (ws) {
ws.send({
type: 'full-reload',
path: '*',
})
}
}
}
hmr(server, localeDetector)
}
},
} as PluginOption
Expand Down
Loading

0 comments on commit 8e826a6

Please sign in to comment.