Skip to content

Commit

Permalink
fix: kill file watcher when vite server closed to avoid duplicate wat…
Browse files Browse the repository at this point in the history
…cher
  • Loading branch information
hemengke1997 committed Jun 1, 2024
1 parent 42d7d22 commit c23601d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
11 changes: 7 additions & 4 deletions src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function i18nAlly(opts?: I18nAllyOptions): Promise<any> {
const { virtualModules, resolvedIds } = localeDetector.localeModules

if (id in virtualModules) {
return RESOLVED_VIRTUAL_PREFIX + id // e.g. \0/@i18n-ally/virtual:i18n-ally-en
return RESOLVED_VIRTUAL_PREFIX + id // E.g. \0/@i18n-ally/virtual:i18n-ally-en
}

if (importer) {
Expand Down Expand Up @@ -87,10 +87,10 @@ export async function i18nAlly(opts?: I18nAllyOptions): Promise<any> {
},
configureServer(server) {
debug('Watch target:', localeDetector.localeDirs)
initWatcher(localeDetector.localeDirs, async (_type, p, pnext) => {
const watcher = initWatcher(localeDetector.localeDirs, async (_type, p, pnext) => {
if (!p) return

debug('watcher', p, '=========>', pnext)
debug('Watcher', p, '=========>', pnext)
const _hmr = async () => {
await localeDetector.init()
hmr(server, localeDetector)
Expand All @@ -106,12 +106,15 @@ export async function i18nAlly(opts?: I18nAllyOptions): Promise<any> {
return
}
})
server.httpServer?.addListener('close', () => {
watcher?.close()
})
},
async handleHotUpdate({ file, server }) {
const updated = await localeDetector.onFileChanged({ fsPath: file })

if (updated) {
debug('hmr', file)
debug('Hmr', file)
hmr(server, localeDetector)
}
},
Expand Down
26 changes: 13 additions & 13 deletions src/plugin/locale-detector/LocaleDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,21 @@ export class LocaleDetector {

async init() {
if (await this.findLocaleDirs()) {
debug(`🚀 Initializing loader "${this._rootPath}"`)
debug(`Initializing loader "${this._rootPath}"`)

if (this._pathMatcher) {
debug(`🗃 Custom Path Matcher: ${this._pathMatcher.matcher}`)
debug(`🗃 Path Matcher Regex: ${this._pathMatcher.regex}`)
debug(`Custom Path Matcher: ${this._pathMatcher.matcher}`)
debug(`Path Matcher Regex: ${this._pathMatcher.regex}`)
} else {
const pathMatcherGussed = await this.resolvePathMatcherByDirStructure()
this._pathMatcher = {
regex: ParsePathMatcher(pathMatcherGussed, this.enabledParserExts()),
matcher: pathMatcherGussed,
}
debug(`🗃 Path Matcher: ${this._pathMatcher.matcher}`)
debug(`Path Matcher: ${this._pathMatcher.matcher}`)
}

debug(`\n💯 The real I18nAlly options: `, {
debug(`\nThe real I18nAlly options: `, {
root: this._rootPath,
localesPaths: this._localesPaths,
pathMatcher: this._pathMatcher.matcher,
Expand All @@ -99,7 +99,7 @@ export class LocaleDetector {
}

private update() {
debug('Loading finished')
debug('Loading finished')
this.updateLocaleModule()
}

Expand Down Expand Up @@ -138,7 +138,7 @@ export class LocaleDetector {
resolvedIds,
}

debug('📦 Module locale updated', this._localeModules)
debug('Module locale updated', this._localeModules)
}

get localeModules() {
Expand All @@ -162,7 +162,7 @@ export class LocaleDetector {
return
}

debug(`🔄 File changed ${relative} ${dirpath}`)
debug(`File changed ${relative} ${dirpath}`)

return this.lazyLoadFile(dirpath, relative)
}
Expand Down Expand Up @@ -220,7 +220,7 @@ export class LocaleDetector {
private async loadAll() {
for (const pathname of this._localeDirs) {
try {
debug(`📂 Loading locales under ${pathname}`)
debug(`Loading locales under ${pathname}`)
await this.loadDirectory(pathname)
} catch (e) {
console.error(e)
Expand Down Expand Up @@ -258,7 +258,7 @@ export class LocaleDetector {
return
}

debug(`📑 Loading (${locale}) ${relativePath}`)
debug(`Loading (${locale}) ${relativePath}`)

const data = await parser.load(filepath)

Expand All @@ -277,7 +277,7 @@ export class LocaleDetector {
return true
} catch (e) {
this.unsetFile(relativePath)
debug(`🐛 Failed to load ${e}`)
debug(`Failed to load ${e}`)
console.error(e)
}
}
Expand Down Expand Up @@ -381,7 +381,7 @@ export class LocaleDetector {
}
}
if (this._localeDirs.length === 0) {
logger.error('\n❌ No locales paths.')
logger.error('\nNo locales paths.')
return false
}

Expand Down Expand Up @@ -412,7 +412,7 @@ export class LocaleDetector {

async resolvePathMatcherByDirStructure() {
this._dirStructure = await this.guessDirStructure()
debug(`📂 Directory structure: ${this._dirStructure}`)
debug(`Directory structure: ${this._dirStructure}`)
return this.resolvePathMatcher(this._dirStructure)
}

Expand Down
2 changes: 2 additions & 0 deletions src/plugin/utils/file-watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export function initWatcher(target: string[], cb: Callback) {
watcher.on('renameDir', (...args) => onChange('renameDir', ...args))
watcher.on('unlinkDir', (...args) => onChange('unlinkDir', ...args))
watcher.on('unlink', (...args) => onChange('unlink', ...args))

return watcher
} catch (error) {
console.error(error)
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/utils/init-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function getDefaultOptions(options?: I18nAllyOptions): I18nAllyOptions {
typeof options.useVscodeI18nAllyConfig === 'object' ? options.useVscodeI18nAllyConfig.stopAt : undefined
const i18nAlly = new I18nAllyVscodeSetting(options?.root || (DEFAULT_OPTIONS.root as string), stopAt).init()

debug('i18n-ally config:', i18nAlly)
debug('I18n-ally config:', i18nAlly)

return {
...DEFAULT_OPTIONS,
Expand Down

0 comments on commit c23601d

Please sign in to comment.