Skip to content

Commit 07ffe89

Browse files
authored
Move locale processing to a separate method in ProcessLocalesPlugin (#8310)
1 parent f60c229 commit 07ffe89

File tree

1 file changed

+57
-56
lines changed

1 file changed

+57
-56
lines changed

_scripts/ProcessLocalesPlugin.js

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -45,81 +45,30 @@ class ProcessLocalesPlugin {
4545

4646
/** @param {import('webpack').Compiler} compiler */
4747
apply(compiler) {
48-
const { CachedSource, RawSource } = compiler.webpack.sources
4948
const { Compilation, DefinePlugin } = compiler.webpack
5049

5150
new DefinePlugin({
5251
'process.env.HOT_RELOAD_LOCALES': this.hotReload
5352
}).apply(compiler)
5453

5554
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
56-
const IS_DEV_SERVER = !!compiler.watching
57-
5855
compilation.hooks.processAssets.tapPromise({
5956
name: PLUGIN_NAME,
6057
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
6158
}, async (_assets) => {
6259
// While running in the webpack dev server, this hook gets called for every incremental build.
6360
// For incremental builds we can return the already processed versions, which saves time
6461
// and makes webpack treat them as cached
65-
const promises = []
6662

6763
/** @type {[string, string][]} */
6864
const updatedLocales = []
6965
if (this.hotReload && !this.notifyLocaleChange) {
7066
console.warn('ProcessLocalesPlugin: Unable to live reload locales as `notifyLocaleChange` is not set.')
7167
}
7268

73-
for (let [locale, data] of this.locales) {
74-
// eslint-disable-next-line no-async-promise-executor
75-
promises.push(new Promise(async (resolve) => {
76-
if (IS_DEV_SERVER && compiler.fileTimestamps) {
77-
const filePath = join(this.inputDir, `${locale}.yaml`)
78-
79-
const timestamp = compiler.fileTimestamps.get(filePath)?.safeTime
80-
81-
if (timestamp && timestamp > (this.previousTimestamps.get(locale) ?? this.startTime)) {
82-
this.previousTimestamps.set(locale, timestamp)
83-
84-
const contents = await readFile(filePath, 'utf-8')
85-
data = loadYaml(contents)
86-
} else {
87-
const { filename, source } = this.cache.get(locale)
88-
compilation.emitAsset(filename, source, { minimized: true })
89-
resolve()
90-
return
91-
}
92-
}
93-
94-
this.removeEmptyValues(data)
95-
96-
let filename = `${this.outputDir}/${locale}.json`
97-
let output = JSON.stringify(data)
98-
99-
if (this.hotReload && compiler.fileTimestamps) {
100-
updatedLocales.push([locale, output])
101-
}
102-
103-
if (this.compress) {
104-
filename += '.br'
105-
output = await this.compressLocale(output)
106-
}
107-
108-
let source = new RawSource(output)
109-
110-
if (IS_DEV_SERVER) {
111-
source = new CachedSource(source)
112-
this.cache.set(locale, { filename, source })
113-
114-
// we don't need the unmodified sources anymore, as we use the cache `this.cache`
115-
// so we can clear this to free some memory
116-
this.locales.set(locale, null)
117-
}
118-
119-
compilation.emitAsset(filename, source, { minimized: true })
120-
121-
resolve()
122-
}))
69+
const promises = []
70+
for (const [locale, data] of this.locales) {
71+
promises.push(this.processLocale(locale, data, updatedLocales, compiler, compilation))
12372
}
12473

12574
await Promise.all(promises)
@@ -131,14 +80,66 @@ class ProcessLocalesPlugin {
13180
})
13281

13382
compiler.hooks.afterCompile.tap(PLUGIN_NAME, (compilation) => {
134-
// eslint-disable-next-line no-extra-boolean-cast
135-
if (!!compiler.watching) {
83+
if (compiler.watching) {
13684
// watch locale files for changes
13785
compilation.fileDependencies.addAll(this.filePaths)
13886
}
13987
})
14088
}
14189

90+
/**
91+
* @param {string} locale
92+
* @param {any} data
93+
* @param {[string, string][]} updatedLocales
94+
* @param {import('webpack').Compiler} compiler
95+
* @param {import('webpack').Compilation} compilation
96+
*/
97+
async processLocale(locale, data, updatedLocales, compiler, compilation) {
98+
if (compiler.watching && compiler.fileTimestamps) {
99+
const filePath = join(this.inputDir, `${locale}.yaml`)
100+
101+
const timestamp = compiler.fileTimestamps.get(filePath)?.safeTime
102+
103+
if (timestamp && timestamp > (this.previousTimestamps.get(locale) ?? this.startTime)) {
104+
this.previousTimestamps.set(locale, timestamp)
105+
106+
const contents = await readFile(filePath, 'utf-8')
107+
data = loadYaml(contents)
108+
} else {
109+
const { filename, source } = this.cache.get(locale)
110+
compilation.emitAsset(filename, source, { minimized: true })
111+
return
112+
}
113+
}
114+
115+
this.removeEmptyValues(data)
116+
117+
let filename = `${this.outputDir}/${locale}.json`
118+
let output = JSON.stringify(data)
119+
120+
if (this.hotReload && compiler.fileTimestamps) {
121+
updatedLocales.push([locale, output])
122+
}
123+
124+
if (this.compress) {
125+
filename += '.br'
126+
output = await this.compressLocale(output)
127+
}
128+
129+
let source = new compiler.webpack.sources.RawSource(output)
130+
131+
if (compiler.watching) {
132+
source = new compiler.webpack.sources.CachedSource(source)
133+
this.cache.set(locale, { filename, source })
134+
135+
// we don't need the unmodified sources anymore, as we use the cache `this.cache`
136+
// so we can clear this to free some memory
137+
this.locales.set(locale, null)
138+
}
139+
140+
compilation.emitAsset(filename, source, { minimized: true })
141+
}
142+
142143
loadLocales() {
143144
const activeLocales = JSON.parse(readFileSync(`${this.inputDir}/activeLocales.json`))
144145

0 commit comments

Comments
 (0)