diff --git a/_scripts/ProcessLocalesPlugin.js b/_scripts/ProcessLocalesPlugin.js index e3b2dd7a8..51ff870c6 100644 --- a/_scripts/ProcessLocalesPlugin.js +++ b/_scripts/ProcessLocalesPlugin.js @@ -40,17 +40,17 @@ class ProcessLocalesPlugin { /** @type {(updatedLocales: [string, string][]) => void|null} */ this.notifyLocaleChange = null - if (this.hotReload) { - this.hotReloadScript = readFileSync(`${__dirname}/_hotReloadLocalesScript.js`, 'utf-8') - } - this.loadLocales() } /** @param {import('webpack').Compiler} compiler */ apply(compiler) { const { CachedSource, RawSource } = compiler.webpack.sources; - const { Compilation } = compiler.webpack + const { Compilation, DefinePlugin } = compiler.webpack + + new DefinePlugin({ + 'process.env.HOT_RELOAD_LOCALES': this.hotReload + }).apply(compiler) compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => { const IS_DEV_SERVER = !!compiler.watching @@ -136,19 +136,6 @@ class ProcessLocalesPlugin { compilation.fileDependencies.addAll(this.filePaths) } }) - - compiler.hooks.emit.tap(PLUGIN_NAME, (compilation) => { - if (this.hotReload) { - // Find generated JavaScript output file (e.g. renderer.js or web.js) - // and inject the code snippet that listens for locale updates and replaces vue-i18n's locales - - /** @type {string} */ - const filename = [...[...compilation.chunks][0].files] - .find(file => file.endsWith('.js')) - - compilation.assets[filename]._source._children.push(`\n${this.hotReloadScript}`) - } - }) } loadLocales() { diff --git a/_scripts/_hotReloadLocalesScript.js b/_scripts/_hotReloadLocalesScript.js deleted file mode 100644 index 8cbfb6ef8..000000000 --- a/_scripts/_hotReloadLocalesScript.js +++ /dev/null @@ -1,18 +0,0 @@ -const websocket = new WebSocket('ws://localhost:9080/ws') - -websocket.onmessage = (event) => { - const message = JSON.parse(event.data) - - if (message.type === 'freetube-locale-update') { - const i18n = document.getElementById('app').__vue__.$i18n - - for (const [locale, data] of message.data) { - // Only update locale data if it was already loaded - if (i18n.availableLocales.includes(locale)) { - const localeData = JSON.parse(data) - - i18n.setLocaleMessage(locale, localeData) - } - } - } -} diff --git a/src/renderer/i18n/index.js b/src/renderer/i18n/index.js index 79cb20778..3feed824a 100644 --- a/src/renderer/i18n/index.js +++ b/src/renderer/i18n/index.js @@ -50,4 +50,24 @@ export async function loadLocale(locale) { i18n.setLocaleMessage(locale, data) } +// Set by _scripts/ProcessLocalesPlugin.js +if (process.env.HOT_RELOAD_LOCALES) { + const websocket = new WebSocket('ws://localhost:9080/ws') + + websocket.onmessage = (event) => { + const message = JSON.parse(event.data) + + if (message.type === 'freetube-locale-update') { + for (const [locale, data] of message.data) { + // Only update locale data if it was already loaded + if (i18n.availableLocales.includes(locale)) { + const localeData = JSON.parse(data) + + i18n.setLocaleMessage(locale, localeData) + } + } + } + } +} + export default i18n