@zohodesk/client_build_tool 0.0.10 → 0.0.11-exp.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +0 -10
- package/README.md +146 -8
- package/README_backup.md +148 -0
- package/lib/schemas/defaultConfigValues.js +17 -5
- package/lib/schemas/defaultConfigValuesOnly.js +16 -1
- package/lib/shared/babel/getBabelPlugin.js +4 -9
- package/lib/shared/babel/runBabelForTsFile.js +1 -1
- package/lib/shared/bundler/webpack/custom_plugins/I18nNumericIndexPlugin/I18nNumericIndexHtmlInjectorPlugin.js +49 -0
- package/lib/shared/bundler/webpack/custom_plugins/I18nNumericIndexPlugin/I18nNumericIndexPlugin.js +161 -0
- package/lib/shared/bundler/webpack/custom_plugins/I18nNumericIndexPlugin/I18nNumericIndexPlugin_simplified.js +129 -0
- package/lib/shared/bundler/webpack/custom_plugins/I18nNumericIndexPlugin/i18nDataLoader.js +134 -0
- package/lib/shared/bundler/webpack/custom_plugins/I18nNumericIndexPlugin/utils/i18nDataLoader.js +113 -0
- package/lib/shared/bundler/webpack/custom_plugins/I18nSplitPlugin/utils/collectAstKeys.js +96 -0
- package/lib/shared/bundler/webpack/jsLoaders.js +20 -2
- package/lib/shared/bundler/webpack/loaderConfigs/i18nIdReplaceLoaderConfig.js +74 -0
- package/lib/shared/bundler/webpack/loaders/i18nIdReplaceLoader.js +105 -0
- package/lib/shared/bundler/webpack/pluginConfigs/configI18nNumericIndexPlugin.js +70 -0
- package/lib/shared/bundler/webpack/plugins.js +3 -1
- package/lib/shared/bundler/webpack/utils/i18n/collectAstKeys.js +96 -0
- package/lib/shared/bundler/webpack/utils/propertiesParser.js +103 -0
- package/lib/shared/server/mockApiHandler.js +0 -7
- package/lib/shared/server/urlConcat.js +1 -1
- package/npm-shrinkwrap.json +32 -8240
- package/package.json +5 -6
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const path = require('path');
|
|
4
|
+
|
|
5
|
+
const {
|
|
6
|
+
sources,
|
|
7
|
+
Compilation
|
|
8
|
+
} = require('webpack');
|
|
9
|
+
|
|
10
|
+
const {
|
|
11
|
+
decodeUnicodeEscapes
|
|
12
|
+
} = require('../../utils/propertiesParser');
|
|
13
|
+
|
|
14
|
+
const {
|
|
15
|
+
loadNumericMap,
|
|
16
|
+
loadI18nData
|
|
17
|
+
} = require('./utils/i18nDataLoader');
|
|
18
|
+
|
|
19
|
+
const {
|
|
20
|
+
RawSource
|
|
21
|
+
} = sources;
|
|
22
|
+
const pluginName = 'I18nNumericIndexPlugin';
|
|
23
|
+
|
|
24
|
+
class I18nNumericIndexPlugin {
|
|
25
|
+
constructor(options = {}) {
|
|
26
|
+
this.options = options;
|
|
27
|
+
this.numericMap = null;
|
|
28
|
+
this.i18nData = null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
getNumericMap(compilation) {
|
|
32
|
+
if (!this.numericMap) {
|
|
33
|
+
const mapPath = path.resolve(compilation.compiler.context, this.options.numericMapPath);
|
|
34
|
+
this.numericMap = loadNumericMap(mapPath, compilation);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return this.numericMap;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
getI18nData(compilation) {
|
|
41
|
+
if (!this.i18nData) {
|
|
42
|
+
this.i18nData = loadI18nData(this.options, compilation);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return this.i18nData;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
emitChunk(compilation, filename, locale, data) {
|
|
49
|
+
const content = decodeUnicodeEscapes(JSON.stringify(data));
|
|
50
|
+
const fileContent = `${this.options.jsonpFunc}(${content});`;
|
|
51
|
+
const outputPath = filename.replace(/\[locale\]/g, locale);
|
|
52
|
+
compilation.emitAsset(outputPath, new RawSource(fileContent));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
apply(compiler) {
|
|
56
|
+
compiler.hooks.thisCompilation.tap(pluginName, compilation => {
|
|
57
|
+
compilation.hooks.processAssets.tapAsync({
|
|
58
|
+
name: pluginName,
|
|
59
|
+
stage: Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE
|
|
60
|
+
}, (assets, callback) => {
|
|
61
|
+
if (!this.options.enable) return callback();
|
|
62
|
+
const {
|
|
63
|
+
sortedKeys,
|
|
64
|
+
totalKeys
|
|
65
|
+
} = this.getNumericMap(compilation);
|
|
66
|
+
const {
|
|
67
|
+
jsResourceBase,
|
|
68
|
+
allI18n,
|
|
69
|
+
locales
|
|
70
|
+
} = this.getI18nData(compilation);
|
|
71
|
+
if (!locales.length) return callback(); // Collect dynamic keys used in comments
|
|
72
|
+
|
|
73
|
+
const dynamicKeys = new Set();
|
|
74
|
+
|
|
75
|
+
for (const module of compilation.modules) {
|
|
76
|
+
if (module.buildInfo?.loaderIdentifiedCommentI18nKeys) {
|
|
77
|
+
module.buildInfo.loaderIdentifiedCommentI18nKeys.forEach(key => dynamicKeys.add(key));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const numericKeysSet = new Set(sortedKeys);
|
|
82
|
+
const englishData = allI18n.en || jsResourceBase; // Process each locale
|
|
83
|
+
|
|
84
|
+
locales.forEach(locale => {
|
|
85
|
+
const localeData = allI18n[locale] || {}; // Build numeric data
|
|
86
|
+
|
|
87
|
+
const numericData = {};
|
|
88
|
+
|
|
89
|
+
for (let i = 0; i < totalKeys; i++) {
|
|
90
|
+
const key = sortedKeys[i];
|
|
91
|
+
|
|
92
|
+
if (key && jsResourceBase[key] !== undefined) {
|
|
93
|
+
numericData[i] = localeData[key] ?? englishData[key];
|
|
94
|
+
}
|
|
95
|
+
} // Build dynamic data
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
const dynamicData = {}; // Add comment keys
|
|
99
|
+
|
|
100
|
+
dynamicKeys.forEach(key => {
|
|
101
|
+
if (!numericKeysSet.has(key) && jsResourceBase[key] !== undefined) {
|
|
102
|
+
dynamicData[key] = localeData[key] ?? englishData[key];
|
|
103
|
+
}
|
|
104
|
+
}); // Add remaining keys
|
|
105
|
+
|
|
106
|
+
Object.keys(jsResourceBase).forEach(key => {
|
|
107
|
+
if (!numericKeysSet.has(key) && !dynamicData[key]) {
|
|
108
|
+
dynamicData[key] = localeData[key] ?? englishData[key];
|
|
109
|
+
}
|
|
110
|
+
}); // Emit chunks
|
|
111
|
+
|
|
112
|
+
if (Object.keys(numericData).length > 0) {
|
|
113
|
+
this.emitChunk(compilation, this.options.numericFilenameTemplate, locale, numericData);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (Object.keys(dynamicData).length > 0) {
|
|
117
|
+
this.emitChunk(compilation, this.options.dynamicFilenameTemplate, locale, dynamicData);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
callback();
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
module.exports = {
|
|
128
|
+
I18nNumericIndexPlugin
|
|
129
|
+
};
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
|
|
5
|
+
const path = require('path');
|
|
6
|
+
|
|
7
|
+
const {
|
|
8
|
+
parseProperties
|
|
9
|
+
} = require('../../utils/propertiesParser');
|
|
10
|
+
/**
|
|
11
|
+
* Loads and parses a properties file
|
|
12
|
+
* @param {string} filePath - Path to the properties file
|
|
13
|
+
* @param {Object} compilation - Webpack compilation object for error reporting
|
|
14
|
+
* @param {string} description - Description for error messages
|
|
15
|
+
* @returns {Object} Parsed properties object
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
function loadPropertiesFile(filePath, compilation, description) {
|
|
20
|
+
try {
|
|
21
|
+
const content = fs.readFileSync(filePath, 'utf-8');
|
|
22
|
+
return parseProperties(content);
|
|
23
|
+
} catch (err) {
|
|
24
|
+
if (compilation) {
|
|
25
|
+
compilation.errors.push(new Error(`I18nNumericIndexPlugin: Error loading ${description}: ${err.message}`));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return {};
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Loads numeric mapping from JSON file
|
|
33
|
+
* @param {string} numericMapPath - Path to numeric map JSON file
|
|
34
|
+
* @param {Object} compilation - Webpack compilation object
|
|
35
|
+
* @returns {Object} Object with sortedKeys array and totalKeys count
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
function loadNumericMap(numericMapPath, compilation) {
|
|
40
|
+
try {
|
|
41
|
+
const fileContent = fs.readFileSync(numericMapPath, 'utf-8');
|
|
42
|
+
const parsedData = JSON.parse(fileContent);
|
|
43
|
+
const sortedKeys = new Array(parsedData.totalKeysInMap);
|
|
44
|
+
Object.entries(parsedData.originalKeyToNumericId).forEach(([key, id]) => {
|
|
45
|
+
sortedKeys[id] = key;
|
|
46
|
+
});
|
|
47
|
+
return {
|
|
48
|
+
sortedKeys,
|
|
49
|
+
totalKeys: parsedData.totalKeysInMap
|
|
50
|
+
};
|
|
51
|
+
} catch (err) {
|
|
52
|
+
if (compilation) {
|
|
53
|
+
compilation.errors.push(new Error(`I18nNumericIndexPlugin: Error loading numeric map: ${err.message}`));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
sortedKeys: [],
|
|
58
|
+
totalKeys: 0
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Loads all locale-specific properties files
|
|
64
|
+
* @param {string} propertiesPath - Path to properties folder
|
|
65
|
+
* @param {string} baseFileName - Base filename (e.g., 'JSResources')
|
|
66
|
+
* @param {Object} compilation - Webpack compilation object
|
|
67
|
+
* @returns {Object} Object with allI18n translations and locales array
|
|
68
|
+
*/
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
function loadAllLocaleFiles(propertiesPath, baseFileName, compilation) {
|
|
72
|
+
const allI18n = {};
|
|
73
|
+
const locales = [];
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
const files = fs.readdirSync(propertiesPath);
|
|
77
|
+
files.forEach(file => {
|
|
78
|
+
if (!file.endsWith('.properties')) return;
|
|
79
|
+
const name = path.basename(file, '.properties');
|
|
80
|
+
let locale = null;
|
|
81
|
+
|
|
82
|
+
if (name === baseFileName) {
|
|
83
|
+
locale = 'en';
|
|
84
|
+
} else if (name.startsWith(baseFileName + '_')) {
|
|
85
|
+
locale = name.substring(baseFileName.length + 1);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (locale) {
|
|
89
|
+
const filePath = path.join(propertiesPath, file);
|
|
90
|
+
allI18n[locale] = loadPropertiesFile(filePath, compilation, `locale ${locale}`);
|
|
91
|
+
locales.push(locale);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
} catch (err) {
|
|
95
|
+
if (compilation) {
|
|
96
|
+
compilation.errors.push(new Error(`I18nNumericIndexPlugin: Error reading properties folder: ${err.message}`));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
allI18n,
|
|
102
|
+
locales
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Loads all i18n data including base resources and locale-specific translations
|
|
107
|
+
* @param {Object} options - Plugin options
|
|
108
|
+
* @param {Object} compilation - Webpack compilation object
|
|
109
|
+
* @returns {Object} Object with jsResourceBase, allI18n, and locales
|
|
110
|
+
*/
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
function loadI18nData(options, compilation) {
|
|
114
|
+
const jsResourcePath = path.resolve(compilation.compiler.context, options.jsResourcePath);
|
|
115
|
+
const propertiesPath = path.resolve(compilation.compiler.context, options.propertiesFolderPath);
|
|
116
|
+
const baseFileName = path.basename(options.jsResourcePath, '.properties');
|
|
117
|
+
const jsResourceBase = loadPropertiesFile(jsResourcePath, compilation, 'JS resources');
|
|
118
|
+
const {
|
|
119
|
+
allI18n,
|
|
120
|
+
locales
|
|
121
|
+
} = loadAllLocaleFiles(propertiesPath, baseFileName, compilation);
|
|
122
|
+
return {
|
|
123
|
+
jsResourceBase,
|
|
124
|
+
allI18n,
|
|
125
|
+
locales
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
module.exports = {
|
|
130
|
+
loadPropertiesFile,
|
|
131
|
+
loadNumericMap,
|
|
132
|
+
loadAllLocaleFiles,
|
|
133
|
+
loadI18nData
|
|
134
|
+
};
|
package/lib/shared/bundler/webpack/custom_plugins/I18nNumericIndexPlugin/utils/i18nDataLoader.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
|
|
5
|
+
const path = require('path');
|
|
6
|
+
|
|
7
|
+
const {
|
|
8
|
+
parseProperties
|
|
9
|
+
} = require('../../../utils/propertiesParser');
|
|
10
|
+
|
|
11
|
+
function loadPropertiesFile(filePath, compilation, description) {
|
|
12
|
+
try {
|
|
13
|
+
const content = fs.readFileSync(filePath, 'utf-8');
|
|
14
|
+
const parsed = parseProperties(content);
|
|
15
|
+
return parsed;
|
|
16
|
+
} catch (err) {
|
|
17
|
+
if (compilation) {
|
|
18
|
+
compilation.errors.push(new Error(`I18nNumericIndexPlugin: Error loading ${description}: ${err.message}`));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return {};
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function loadNumericMap(numericMapPath, compilation) {
|
|
26
|
+
try {
|
|
27
|
+
const fileContent = fs.readFileSync(numericMapPath, 'utf-8');
|
|
28
|
+
const parsedData = JSON.parse(fileContent);
|
|
29
|
+
const sortedKeys = new Array(parsedData.totalKeysInMap);
|
|
30
|
+
Object.entries(parsedData.originalKeyToNumericId).forEach(([key, id]) => {
|
|
31
|
+
sortedKeys[id] = key;
|
|
32
|
+
});
|
|
33
|
+
return {
|
|
34
|
+
sortedKeys,
|
|
35
|
+
totalKeys: parsedData.totalKeysInMap
|
|
36
|
+
};
|
|
37
|
+
} catch (err) {
|
|
38
|
+
if (compilation) {
|
|
39
|
+
compilation.errors.push(new Error(`I18nNumericIndexPlugin: Error loading numeric map: ${err.message}`));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
sortedKeys: [],
|
|
44
|
+
totalKeys: 0
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function loadAllLocaleFiles(propertiesPath, baseFileName, compilation) {
|
|
50
|
+
const allI18n = {};
|
|
51
|
+
const locales = [];
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
const files = fs.readdirSync(propertiesPath);
|
|
55
|
+
files.forEach(file => {
|
|
56
|
+
if (file === baseFileName + '.properties') {
|
|
57
|
+
const filePath = path.join(propertiesPath, file);
|
|
58
|
+
const baseData = loadPropertiesFile(filePath, compilation, 'JSResources base');
|
|
59
|
+
allI18n['en_US'] = baseData;
|
|
60
|
+
locales.push('en_US');
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
files.forEach(file => {
|
|
64
|
+
if (!file.endsWith('.properties')) return;
|
|
65
|
+
const match = file.match(/^ApplicationResources_([a-z]{2}_[A-Z]{2})\.properties$/);
|
|
66
|
+
|
|
67
|
+
if (match) {
|
|
68
|
+
const locale = match[1];
|
|
69
|
+
const filePath = path.join(propertiesPath, file);
|
|
70
|
+
const localeData = loadPropertiesFile(filePath, compilation, `locale ${locale}`);
|
|
71
|
+
allI18n[locale] = { ...allI18n['en_US'],
|
|
72
|
+
...localeData
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
if (!locales.includes(locale)) {
|
|
76
|
+
locales.push(locale);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
} catch (err) {
|
|
81
|
+
if (compilation) {
|
|
82
|
+
compilation.errors.push(new Error(`I18nNumericIndexPlugin: Error reading properties folder: ${err.message}`));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
allI18n,
|
|
88
|
+
locales
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function loadI18nData(options, compilation) {
|
|
93
|
+
const jsResourcePath = path.resolve(compilation.compiler.context, options.jsResourcePath);
|
|
94
|
+
const propertiesPath = path.resolve(compilation.compiler.context, options.propertiesFolderPath);
|
|
95
|
+
const baseFileName = path.basename(options.jsResourcePath, '.properties');
|
|
96
|
+
const jsResourceBase = loadPropertiesFile(jsResourcePath, compilation, 'JS resources');
|
|
97
|
+
const {
|
|
98
|
+
allI18n,
|
|
99
|
+
locales
|
|
100
|
+
} = loadAllLocaleFiles(propertiesPath, baseFileName, compilation);
|
|
101
|
+
return {
|
|
102
|
+
jsResourceBase,
|
|
103
|
+
allI18n,
|
|
104
|
+
locales
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
module.exports = {
|
|
109
|
+
loadPropertiesFile,
|
|
110
|
+
loadNumericMap,
|
|
111
|
+
loadAllLocaleFiles,
|
|
112
|
+
loadI18nData
|
|
113
|
+
};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
walk
|
|
5
|
+
} = require('estree-walker');
|
|
6
|
+
|
|
7
|
+
const PREFIX_I18N_COMMENT = 'I18N';
|
|
8
|
+
const PREFIX_I18N_COMMENT_DYNAMIC = 'dynamic-i18n-key';
|
|
9
|
+
|
|
10
|
+
function getI18nKeysFromSingleComment(commentNode, validKeysSet) {
|
|
11
|
+
const foundKeysInComment = [];
|
|
12
|
+
|
|
13
|
+
if (!commentNode || typeof commentNode.value !== 'string') {
|
|
14
|
+
return foundKeysInComment;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const commentString = commentNode.value.trim();
|
|
18
|
+
let i18nKeyStr;
|
|
19
|
+
|
|
20
|
+
if (commentString.startsWith(PREFIX_I18N_COMMENT)) {
|
|
21
|
+
i18nKeyStr = commentString.slice(PREFIX_I18N_COMMENT.length).trim();
|
|
22
|
+
} else if (commentString.startsWith(PREFIX_I18N_COMMENT_DYNAMIC)) {
|
|
23
|
+
i18nKeyStr = commentString.slice(PREFIX_I18N_COMMENT_DYNAMIC.length).trim();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (!i18nKeyStr) {
|
|
27
|
+
return foundKeysInComment;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const potentialKeys = i18nKeyStr.split(',').map(key => key.trim()).filter(key => key);
|
|
31
|
+
potentialKeys.forEach(key => {
|
|
32
|
+
if (validKeysSet.has(key)) {
|
|
33
|
+
foundKeysInComment.push(key);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
return foundKeysInComment;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Traverses an AST and its comments to collect and categorize i18n keys.
|
|
40
|
+
*
|
|
41
|
+
* @param {object} astProgramNode - The Program node of the AST.
|
|
42
|
+
* @param {object[]} commentsArray - An array of comment nodes from the AST.
|
|
43
|
+
* @param {object} allI18nKeysMasterMap - Object map of all valid i18n keys (from JSResources).
|
|
44
|
+
* @param {boolean} [isDebug=false] - Flag for verbose logging.
|
|
45
|
+
* @returns {{literalKeys: Set<string>, commentKeys: Set<string>}}
|
|
46
|
+
* literalKeys: Set of valid i18n keys found as string literals.
|
|
47
|
+
* commentKeys: Set of valid i18n keys found in comments.
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
function collectAndCategorizeUsedI18nKeys(astProgramNode, commentsArray, allI18nKeysMasterMap, isDebug = false) {
|
|
52
|
+
const foundLiteralKeys = new Set();
|
|
53
|
+
const foundCommentKeys = new Set();
|
|
54
|
+
const validKeysSet = new Set(Object.keys(allI18nKeysMasterMap || {}));
|
|
55
|
+
|
|
56
|
+
if (validKeysSet.size === 0 && isDebug) {
|
|
57
|
+
console.warn('[collectAndCategorizeUsedI18nKeys] allI18nKeysMasterMap is empty. No keys can be collected.');
|
|
58
|
+
} // 1. Collect keys from AST string literals
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
if (astProgramNode) {
|
|
62
|
+
try {
|
|
63
|
+
walk(astProgramNode, {
|
|
64
|
+
enter(node) {
|
|
65
|
+
if ((node.type === 'Literal' || node.type === 'StringLiteral') && typeof node.value === 'string') {
|
|
66
|
+
if (validKeysSet.has(node.value)) {
|
|
67
|
+
foundLiteralKeys.add(node.value);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
});
|
|
73
|
+
} catch (error) {
|
|
74
|
+
console.error('[collectAndCategorizeUsedI18nKeys] Error during AST walk:', error);
|
|
75
|
+
}
|
|
76
|
+
} // 2. Collect keys from comments
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
if (commentsArray && Array.isArray(commentsArray)) {
|
|
80
|
+
commentsArray.forEach(commentNode => {
|
|
81
|
+
const keysFromComment = getI18nKeysFromSingleComment(commentNode, validKeysSet);
|
|
82
|
+
keysFromComment.forEach(key => {
|
|
83
|
+
foundCommentKeys.add(key);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
literalKeys: foundLiteralKeys,
|
|
90
|
+
commentKeys: foundCommentKeys
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
module.exports = {
|
|
95
|
+
collectAndCategorizeUsedI18nKeys: collectAndCategorizeUsedI18nKeys
|
|
96
|
+
};
|
|
@@ -7,11 +7,29 @@ exports.jsLoaders = jsLoaders;
|
|
|
7
7
|
|
|
8
8
|
var _babelLoaderConfig = require("./loaderConfigs/babelLoaderConfig");
|
|
9
9
|
|
|
10
|
+
const {
|
|
11
|
+
i18nIdReplaceLoaderConfig
|
|
12
|
+
} = require('./loaderConfigs/i18nIdReplaceLoaderConfig');
|
|
13
|
+
|
|
10
14
|
function jsLoaders(options) {
|
|
15
|
+
const useLoaders = [];
|
|
16
|
+
useLoaders.push((0, _babelLoaderConfig.babelLoaderConfig)(options));
|
|
17
|
+
const shouldUseNumericIndexing = options.i18nIndexing && options.i18nIndexing.enable || options.i18nChunkSplit && options.i18nChunkSplit.chunkSplitEnable && options.i18nChunkSplit.useNumericIndexing;
|
|
18
|
+
|
|
19
|
+
if (shouldUseNumericIndexing) {
|
|
20
|
+
try {
|
|
21
|
+
const loaderConfig = i18nIdReplaceLoaderConfig(options, options.context);
|
|
22
|
+
|
|
23
|
+
if (loaderConfig) {
|
|
24
|
+
useLoaders.push(loaderConfig);
|
|
25
|
+
}
|
|
26
|
+
} catch (err) {// Silently skip if configuration fails
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
11
30
|
return [{
|
|
12
31
|
test: /\.js$/,
|
|
13
32
|
exclude: /node_modules/,
|
|
14
|
-
use:
|
|
15
|
-
|
|
33
|
+
use: useLoaders
|
|
16
34
|
}];
|
|
17
35
|
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const path = require('path');
|
|
4
|
+
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
|
|
7
|
+
const {
|
|
8
|
+
getPropertiesAsJSON
|
|
9
|
+
} = require('../custom_plugins/I18nSplitPlugin/utils/propertiesUtils');
|
|
10
|
+
|
|
11
|
+
function loadJSResourcesOnce(options) {
|
|
12
|
+
let jsResourcePath;
|
|
13
|
+
|
|
14
|
+
if (options.i18nIndexing && options.i18nIndexing.enable) {
|
|
15
|
+
jsResourcePath = options.i18nIndexing.jsResourcePath;
|
|
16
|
+
} else if (options.i18nChunkSplit && options.i18nChunkSplit.chunkSplitEnable && options.i18nChunkSplit.useNumericIndexing) {
|
|
17
|
+
jsResourcePath = options.i18nChunkSplit.jsResource;
|
|
18
|
+
} else {
|
|
19
|
+
throw new Error('i18nIdReplaceLoader requires either i18nIndexing to be enabled or i18nChunkSplit with useNumericIndexing');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (!jsResourcePath) {
|
|
23
|
+
throw new Error('Missing required jsResourcePath in i18n options');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
const i18nData = getPropertiesAsJSON(jsResourcePath);
|
|
28
|
+
|
|
29
|
+
if (Object.keys(i18nData).length === 0) {
|
|
30
|
+
console.warn(`[i18nIdReplaceLoaderConfig] Warning: No i18n data found in JSResource file: ${jsResourcePath}`);
|
|
31
|
+
return {};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return i18nData;
|
|
35
|
+
} catch (err) {
|
|
36
|
+
throw new Error(`Error reading JSResource file ${jsResourcePath}: ${err.message}`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function i18nIdReplaceLoaderConfig(options, webpackContext) {
|
|
41
|
+
let numericMapPath;
|
|
42
|
+
|
|
43
|
+
if (options.i18nIndexing && options.i18nIndexing.enable) {
|
|
44
|
+
numericMapPath = options.i18nIndexing.numericMapPath;
|
|
45
|
+
} else if (options.i18nChunkSplit && options.i18nChunkSplit.chunkSplitEnable && options.i18nChunkSplit.useNumericIndexing) {
|
|
46
|
+
numericMapPath = options.i18nChunkSplit.numericMapPath;
|
|
47
|
+
} else {
|
|
48
|
+
throw new Error('i18nIdReplaceLoader requires either i18nIndexing to be enabled or i18nChunkSplit with useNumericIndexing');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (!numericMapPath) {
|
|
52
|
+
throw new Error('numericMapPath is required in i18nIndexing or i18nChunkSplit config');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const allI18nData = loadJSResourcesOnce(options);
|
|
56
|
+
|
|
57
|
+
const i18nKeyReplaceLoaderPath = require.resolve('../loaders/i18nIdReplaceLoader.js');
|
|
58
|
+
|
|
59
|
+
const loaderOptions = {
|
|
60
|
+
allI18nData: allI18nData,
|
|
61
|
+
sourceMaps: false,
|
|
62
|
+
numericMapPath: numericMapPath,
|
|
63
|
+
includePaths: options.i18nIndexing?.loaderOptions?.includePaths || [],
|
|
64
|
+
excludePaths: options.i18nIndexing?.loaderOptions?.excludePaths || ['node_modules', 'tests']
|
|
65
|
+
};
|
|
66
|
+
return {
|
|
67
|
+
loader: i18nKeyReplaceLoaderPath,
|
|
68
|
+
options: loaderOptions
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
module.exports = {
|
|
73
|
+
i18nIdReplaceLoaderConfig
|
|
74
|
+
};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
|
|
5
|
+
const path = require('path');
|
|
6
|
+
|
|
7
|
+
const parser = require('@babel/parser');
|
|
8
|
+
|
|
9
|
+
const traverse = require('@babel/traverse').default;
|
|
10
|
+
|
|
11
|
+
const generator = require('@babel/generator').default;
|
|
12
|
+
|
|
13
|
+
const t = require('@babel/types');
|
|
14
|
+
|
|
15
|
+
const {
|
|
16
|
+
getOptions
|
|
17
|
+
} = require('loader-utils');
|
|
18
|
+
|
|
19
|
+
module.exports = function i18nIdReplaceLoader(source, map) {
|
|
20
|
+
const resourcePath = this.resourcePath;
|
|
21
|
+
this.cacheable && this.cacheable();
|
|
22
|
+
const options = getOptions(this) || {};
|
|
23
|
+
const callback = this.async();
|
|
24
|
+
|
|
25
|
+
if (options.excludePaths) {
|
|
26
|
+
const shouldExclude = options.excludePaths.some(excludePath => resourcePath.includes(excludePath));
|
|
27
|
+
|
|
28
|
+
if (shouldExclude) {
|
|
29
|
+
return callback(null, source, map);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (options.includePaths && options.includePaths.length > 0) {
|
|
34
|
+
const shouldInclude = options.includePaths.some(includePath => resourcePath.includes(includePath));
|
|
35
|
+
|
|
36
|
+
if (!shouldInclude) {
|
|
37
|
+
return callback(null, source, map);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (!options.allI18nData || Object.keys(options.allI18nData).length === 0) {
|
|
42
|
+
return callback(new Error(`i18nIdReplaceLoader: 'allI18nData' option is missing or empty`));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
let numericIdMap = null;
|
|
46
|
+
|
|
47
|
+
if (options.numericMapPath) {
|
|
48
|
+
try {
|
|
49
|
+
if (fs.existsSync(options.numericMapPath)) {
|
|
50
|
+
const fileContent = fs.readFileSync(options.numericMapPath, 'utf-8');
|
|
51
|
+
const parsedData = JSON.parse(fileContent);
|
|
52
|
+
|
|
53
|
+
if (parsedData && parsedData.originalKeyToNumericId) {
|
|
54
|
+
numericIdMap = parsedData.originalKeyToNumericId;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
} catch (err) {// ignore - optional file
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (!numericIdMap) {
|
|
62
|
+
return callback(null, source, map);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
const ast = parser.parse(source, {
|
|
67
|
+
sourceType: 'module',
|
|
68
|
+
plugins: ['jsx', 'typescript', 'classProperties', 'optionalChaining', 'nullishCoalescingOperator'],
|
|
69
|
+
sourceFilename: resourcePath
|
|
70
|
+
});
|
|
71
|
+
let hasTransformations = false;
|
|
72
|
+
traverse(ast, {
|
|
73
|
+
StringLiteral(path) {
|
|
74
|
+
const {
|
|
75
|
+
node
|
|
76
|
+
} = path;
|
|
77
|
+
|
|
78
|
+
if (!options.allI18nData.hasOwnProperty(node.value)) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (numericIdMap.hasOwnProperty(node.value)) {
|
|
83
|
+
const numericId = String(numericIdMap[node.value]);
|
|
84
|
+
path.replaceWith(t.stringLiteral(numericId));
|
|
85
|
+
hasTransformations = true;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
if (hasTransformations) {
|
|
92
|
+
const output = generator(ast, {
|
|
93
|
+
sourceMaps: !!options.sourceMaps,
|
|
94
|
+
sourceFileName: resourcePath,
|
|
95
|
+
retainLines: false,
|
|
96
|
+
comments: true
|
|
97
|
+
}, source);
|
|
98
|
+
callback(null, output.code, output.map);
|
|
99
|
+
} else {
|
|
100
|
+
callback(null, source, map);
|
|
101
|
+
}
|
|
102
|
+
} catch (err) {
|
|
103
|
+
callback(err);
|
|
104
|
+
}
|
|
105
|
+
};
|