Skip to content

Commit

Permalink
fix: only merge and emit when i18n files changed (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
janvennemann committed May 28, 2020
1 parent 6fcdd9c commit 39397b0
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 25 deletions.
93 changes: 74 additions & 19 deletions lib/plugins/I18nPlugin.js
@@ -1,6 +1,9 @@
const { utils: { XML } } = require('alloy-utils');
const globby = require('globby');
const path = require('path');
const { RawSource } = require('webpack-sources');

const i18nFilePattern = /app[/\\]i18n[/\\].*\.xml$/;

/**
* @typedef I18nSource
Expand Down Expand Up @@ -29,31 +32,79 @@ class I18nPlugin {
}

apply(compiler) {
let i18nSources = this.getI18nSourceLocations();
const i18nSources = this.getI18nSourceLocations();
const currentAssets = new Map();
let fileDependencies = [];
compiler.hooks.emit.tap('AlloyLoaderPlugin', compilation => {
const { files, content, errors, warnings } = this.mergeI18n(i18nSources);
fileDependencies = files;
if (errors.length > 0) {
compilation.errors.push(...errors);
return;
}
if (warnings.length > 0) {
compilation.warnings.push(...warnings);
let firstCompilation = true;
let i18nFileChanged = false;

compiler.hooks.watchRun.tap('AlloyI18nPlugin', () => {
compiler.removedFiles.forEach(filePathAndName => {
if (i18nFilePattern.test(filePathAndName)) {
currentAssets.delete(filePathAndName);
}
});
});
compiler.hooks.invalid.tap('AlloyI18nPlugin', filePathAndName => {
firstCompilation = false;
if (i18nFilePattern.test(filePathAndName)) {
i18nFileChanged = true;
}
content.forEach((xmlContent, identifier) => {
const targetPath = path.join('..', 'i18n', identifier);
compilation.assets[targetPath] = {
size() {
return xmlContent.length;
},
source() {
return xmlContent;
});
compiler.hooks.compilation.tap('AlloyI18nPlugin', compilation => {
const outputPath = compilation.getPath(compiler.outputPath);
compilation.hooks.additionalAssets.tap('AlloyI18nPlugin', () => {
const emitAsset = (targetFile, content) => {
const targetPath = compiler.outputFileSystem.join(
outputPath,
targetFile
);
const source = new RawSource(content);
if (currentAssets.get(targetPath) === content) {
// set `existsAt` so Webpack 4 will skip emitting this asset
source.existsAt = targetPath;
}
if (typeof compilation.emitAsset === 'function') {
compilation.emitAsset(targetFile, source);
} else {
compilation.assets[targetFile] = source;
}
};

if (!firstCompilation && !i18nFileChanged) {
// if no i18n files where changed in a subsequent watch compilation,
// just add back the current assets
currentAssets.forEach(
(content, filePathAndName) => emitAsset(
path.relative(outputPath, filePathAndName), content
)
);
return;
}

currentAssets.clear();
const { files, content, errors, warnings } = this.mergeI18n(i18nSources);
fileDependencies = files;
if (errors.length > 0) {
compilation.errors.push(...errors);
return;
}
if (warnings.length > 0) {
compilation.warnings.push(...warnings);
}
content.forEach((xmlContent, identifier) => {
const targetFile = path.join('..', 'i18n', identifier);
const targetPath = compiler.outputFileSystem.join(
outputPath,
targetFile
);
emitAsset(targetFile, xmlContent);
currentAssets.set(targetPath, content);
});
});
});
compiler.hooks.afterEmit.tap('AlloyLoaderPlugin', compilation => {

compiler.hooks.afterEmit.tap('AlloyI18nPlugin', compilation => {
const addDependencies = (target, deps) => {
if ('addAll' in target) {
target.addAll(deps);
Expand All @@ -68,6 +119,10 @@ class I18nPlugin {
// watch existing files for changes
addDependencies(compilation.fileDependencies, fileDependencies);
});

compiler.hooks.done.tap('AlloyI18nPlugin', () => {
i18nFileChanged = false;
});
}

getI18nSourceLocations() {
Expand Down
7 changes: 2 additions & 5 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -27,7 +27,8 @@
"globby": "^11.0.0",
"loader-utils": "^2.0.0",
"neo-async": "^2.6.1",
"source-map": "^0.7.3"
"source-map": "^0.7.3",
"webpack-sources": "^1.4.3"
},
"devDependencies": {
"eslint": "^6.6.0",
Expand Down

0 comments on commit 39397b0

Please sign in to comment.