Skip to content

Commit 1bb4e27

Browse files
committed
use async in onEmit function
1 parent 1dd9593 commit 1bb4e27

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

index.ts

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -147,38 +147,39 @@ export default class I18nextPlugin {
147147
compiler.plugin("after-emit", this.onAfterEmit.bind(this));
148148
}
149149

150-
protected onEmit(compilation: wp.Compilation, callback: (err?: Error) => void) {
150+
protected async onEmit(compilation: wp.Compilation, callback: (err?: Error) => void) {
151151
// emit translation files
152-
const promises: Promise<any>[] = [];
153-
154-
for (const lng of this.option.languages) {
155-
const resourceTemplate = path.join(this.context, getPath(this.option.resourcePath, lng));
156-
try {
152+
try {
153+
await Promise.all(_.map(this.option.languages, lng => {
154+
const resourceTemplate = path.join(this.context, getPath(this.option.resourcePath, lng));
157155
const resourceDir = path.dirname(resourceTemplate);
158-
fs.statSync(resourceDir);
159-
// compilation.contextDependencies.push(resourceDir);
160-
} catch (e) {
156+
if (!exists(resourceDir)) {
157+
compilation.missingDependencies.push(resourceDir);
158+
}
161159

162-
}
160+
return _.map(this.option.namespaces, async ns => {
161+
const resourcePath = getPath(resourceTemplate, undefined, ns);
162+
const outPath = getPath(this.option.outPath, lng, ns);
163163

164-
for (const ns of this.option.namespaces) {
165-
const resourcePath = getPath(resourceTemplate, undefined, ns);
166-
const outPath = getPath(this.option.outPath, lng, ns);
164+
try {
165+
const v = await readFile(resourcePath);
166+
compilation.assets[outPath] = {
167+
size() { return v.length; },
168+
source() { return v; }
169+
};
167170

168-
promises.push(readFile(resourcePath).then(v => {
169-
compilation.assets[outPath] = {
170-
size() { return v.length; },
171-
source() { return v; }
172-
};
171+
compilation.fileDependencies.push(path.resolve(resourcePath));
172+
} catch (e) {
173+
compilation.missingDependencies.push(resourcePath);
174+
compilation.warnings.push(`Can't emit ${outPath}. It looks like ${resourcePath} is not exists.`);
175+
}
176+
});
177+
}));
173178

174-
compilation.fileDependencies.push(path.resolve(resourcePath));
175-
}).catch(() => {
176-
compilation.warnings.push(`Can't emit ${outPath}. It looks like ${resourcePath} is not exists.`);
177-
}));
178-
}
179+
callback();
180+
} catch (e) {
181+
callback(e);
179182
}
180-
181-
Promise.all(promises).then(() => callback()).catch(callback);
182183
}
183184

184185
protected async onAfterEmit(compilation: wp.Compilation, callback: (err?: Error) => void) {

0 commit comments

Comments
 (0)