@zohodesk/react-cli 1.1.29-exp.1 → 1.1.29-exp.2
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/lib/babel/babel-option-utils/babel-preset-react-option.js +0 -3
- package/lib/babel/cmjs-plugins-presets.js +1 -8
- package/lib/babel/es-plugins-presets.js +1 -8
- package/lib/common/buildEs.js +1 -0
- package/lib/common/getEntries.js +2 -13
- package/lib/common/getPublicPathConfig.js +0 -6
- package/lib/common/index.js +1 -6
- package/lib/common/runPreProcess.js +6 -15
- package/lib/common/splitChunks.js +2 -21
- package/lib/common/sslcertUpdater.js +7 -18
- package/lib/common/templateParameters.js +0 -2
- package/lib/common/testPattern.js +10 -21
- package/lib/common/valueReplacer.js +1 -16
- package/lib/configs/jest.config.js +1 -10
- package/lib/configs/libAlias.js +8 -14
- package/lib/configs/resolvers.js +4 -14
- package/lib/configs/webpack.component.umd.config.js +1 -6
- package/lib/configs/webpack.css.umd.config.js +6 -14
- package/lib/configs/webpack.dev.config.js +1 -14
- package/lib/configs/webpack.docs.config.js +1 -9
- package/lib/configs/webpack.impact.config.js +1 -8
- package/lib/configs/webpack.prod.config.js +4 -17
- package/lib/constants.js +9 -18
- package/lib/deprecationLogger.js +0 -7
- package/lib/hooks/docsProptypeHook.js +4 -8
- package/lib/jest/commitedFilesResult.js +4 -46
- package/lib/jest/coverageCollector.js +1 -12
- package/lib/jest/jsonMaker.js +0 -6
- package/lib/jest/preProcessors/cssPreprocessor.js +1 -9
- package/lib/jest/preProcessors/jsPreprocessor.js +1 -6
- package/lib/jest/preProcessors/otherFilesPreprocessor.js +1 -4
- package/lib/jest/result.js +1 -23
- package/lib/jest/run.js +7 -18
- package/lib/jest/setup.js +8 -60
- package/lib/loaderUtils/configsAssetsLoaders.js +2 -12
- package/lib/loaderUtils/getCSSLoaders.js +10 -22
- package/lib/loaderUtils/getDevJsLoaders.js +4 -13
- package/lib/loaderUtils/index.js +1 -4
- package/lib/loaderUtils/tests/windowsModification.test.js +0 -1
- package/lib/loaderUtils/windowsModification.js +2 -3
- package/lib/loaders/__test__/markdownLoader.spec.js +0 -1
- package/lib/loaders/composeLoader.js +13 -37
- package/lib/loaders/docsLoader.js +1 -12
- package/lib/loaders/docsPropsLoader.js +4 -8
- package/lib/loaders/enhancedReactLiveConverter.js +2 -23
- package/lib/loaders/fileBountryLoader.js +1 -3
- package/lib/loaders/fileLoader.js +12 -23
- package/lib/loaders/markdownLoader.js +14 -19
- package/lib/loaders/reactLiveConvertor.js +5 -15
- package/lib/loaders/scriptInstrumentLoader.js +7 -16
- package/lib/loaders/selectorMappingLoader.js +7 -26
- package/lib/loaders/workerLoader.js +9 -24
- package/lib/logger.js +0 -4
- package/lib/middlewares/HMRMiddleware.js +13 -27
- package/lib/middlewares/SSTMiddleware.js +1 -5
- package/lib/pluginUtils/configHtmlWebpackPlugins.js +1 -24
- package/lib/pluginUtils/getDevPlugins.js +9 -42
- package/lib/pluginUtils/getDocsPlugins.js +3 -13
- package/lib/pluginUtils/getLibraryImactPlugins.js +5 -6
- package/lib/pluginUtils/getLibraryPlugins.js +2 -8
- package/lib/pluginUtils/getProdPlugins.js +8 -47
- package/lib/pluginUtils/getServerPlugins.js +2 -8
- package/lib/pluginUtils/getUMDCSSPlugins.js +2 -10
- package/lib/pluginUtils/getUMDComponentPlugins.js +2 -10
- package/lib/pluginUtils/index.js +1 -9
- package/lib/plugins/CdnChangePlugin.js +2 -18
- package/lib/plugins/CleanupStatsPlugin.js +0 -5
- package/lib/plugins/CssOrderControlPlugin.js +3 -6
- package/lib/plugins/CustomAttributePlugin.js +14 -19
- package/lib/plugins/CustomScriptLoadingStrategyPlugin.js +3 -23
- package/lib/plugins/EFCPlugin.js +20 -34
- package/lib/plugins/EFCTemplatePlugin.js +19 -30
- package/lib/plugins/EfcResourceCleanupPlugin.js +0 -3
- package/lib/plugins/EventsHandlingPlugin.js +2 -4
- package/lib/plugins/I18NInjectIntoIndexPlugin.js +11 -37
- package/lib/plugins/I18nSplitPlugin/I18nDebugPlugin.js +10 -15
- package/lib/plugins/I18nSplitPlugin/I18nDependency.js +4 -10
- package/lib/plugins/I18nSplitPlugin/I18nDownlodLogic.js +12 -25
- package/lib/plugins/I18nSplitPlugin/I18nFilesEmitter.js +55 -133
- package/lib/plugins/I18nSplitPlugin/I18nKeysIdentifer.js +12 -23
- package/lib/plugins/I18nSplitPlugin/index.js +13 -24
- package/lib/plugins/I18nSplitPlugin/utils/applyMetaManifest.js +64 -58
- package/lib/plugins/I18nSplitPlugin/utils/collectI18nKeys.js +2 -12
- package/lib/plugins/I18nSplitPlugin/utils/createMetaManifest.js +10 -29
- package/lib/plugins/I18nSplitPlugin/utils/createRegularManifest.js +8 -19
- package/lib/plugins/I18nSplitPlugin/utils/getI18nFileUrlPathTemplate.js +0 -1
- package/lib/plugins/I18nSplitPlugin/utils/getI18nKeysFormModules.js +0 -5
- package/lib/plugins/I18nSplitPlugin/utils/hashUtils.js +1 -8
- package/lib/plugins/I18nSplitPlugin/utils/index.js +0 -4
- package/lib/plugins/I18nSplitPlugin/utils/propertiesUtils.js +0 -20
- package/lib/plugins/I18nSplitPlugin/utils/unicodeConversion.js +0 -1
- package/lib/plugins/ManifestPlugin.js +1 -18
- package/lib/plugins/MinifyPlugin.js +1 -10
- package/lib/plugins/ModuleStatsPlugin.js +1 -24
- package/lib/plugins/OptimizeJSPlugin.js +2 -10
- package/lib/plugins/PublicPathCallbackPlugin.js +1 -12
- package/lib/plugins/PublicPathChangePlugin.js +6 -39
- package/lib/plugins/ReportGeneratePlugin.js +5 -32
- package/lib/plugins/RequireVariablePublicPlugin.js +1 -8
- package/lib/plugins/ResourceHintsPlugin.js +4 -13
- package/lib/plugins/RtlSplitPlugin/OverwriteCssPathForRTL.js +12 -17
- package/lib/plugins/RtlSplitPlugin/RtlCssPlugin.js +10 -17
- package/lib/plugins/RtlSplitPlugin/replaceCssDirTemplate.js +2 -5
- package/lib/plugins/ScriptInstrumentPlugin.js +1 -8
- package/lib/plugins/SelectorPlugin.js +6 -32
- package/lib/plugins/ServiceWorkerPlugin.js +5 -22
- package/lib/plugins/ShadowDOMSupportPlugin.js +4 -41
- package/lib/plugins/SourceMapHookPlugin.js +2 -12
- package/lib/plugins/StatsPlugin.js +0 -14
- package/lib/plugins/TPHashMappingPlugin.js +3 -18
- package/lib/plugins/UnusedFilesFindPlugin.js +4 -39
- package/lib/plugins/VariableConversionCollector.js +15 -42
- package/lib/plugins/index.js +1 -20
- package/lib/plugins/libraryImpactPlugin.js +1 -33
- package/lib/plugins/newi18nsplitplugin/18nPlugin1.js +306 -0
- package/lib/plugins/newi18nsplitplugin/18nPlugin2.js +363 -0
- package/lib/plugins/newi18nsplitplugin/18nPlugin3.js +694 -0
- package/lib/plugins/newi18nsplitplugin/18nPlugin_hashed.js +1258 -0
- package/lib/plugins/newi18nsplitplugin/18nPlugin_working.js +542 -0
- package/lib/plugins/newi18nsplitplugin/18nplugin.js +974 -0
- package/lib/plugins/newi18nsplitplugin/ChunkManager.js +131 -0
- package/lib/plugins/newi18nsplitplugin/GenerateModuleIdToKeysMapPlugin.js +59 -0
- package/lib/plugins/newi18nsplitplugin/I18nDiffPlugin.js +262 -0
- package/lib/plugins/newi18nsplitplugin/I18nDownloadLogic.js +166 -0
- package/lib/plugins/newi18nsplitplugin/I18nPropertiesPlugin.js +111 -0
- package/lib/plugins/newi18nsplitplugin/KeyCollector.js +163 -0
- package/lib/plugins/newi18nsplitplugin/ManifestGenerator.js +88 -0
- package/lib/plugins/newi18nsplitplugin/UnicodeConversionPlugin.js +101 -0
- package/lib/plugins/newi18nsplitplugin/constants.js +162 -0
- package/lib/plugins/newi18nsplitplugin/utils/I18nKeyHasher.js +78 -0
- package/lib/plugins/newi18nsplitplugin/utils/getJsResourceKeys.js +22 -0
- package/lib/plugins/newi18nsplitplugin/utils/i18nChunkUtils.js +18 -0
- package/lib/plugins/newi18nsplitplugin/utils/manifestGenerator.js +580 -0
- package/lib/plugins/newi18nsplitplugin/utils/propertiesUtils.js +54 -0
- package/lib/plugins/utils/classHandling.js +0 -6
- package/lib/plugins/utils/fileHandling.js +6 -15
- package/lib/plugins/utils/tests/fileHandling.test.js +0 -4
- package/lib/plugins/variableConvertorUtils.js +14 -29
- package/lib/plugins/webpackwatchrunplugin.js +0 -5
- package/lib/postcss-plugins/EmptyPlugin.js +3 -4
- package/lib/postcss-plugins/ExcludePlugin.js +1 -5
- package/lib/postcss-plugins/IncludePlugin.js +1 -5
- package/lib/postcss-plugins/RTLSplitPlugin.js +14 -27
- package/lib/postcss-plugins/SelectorReplace.js +1 -16
- package/lib/postcss-plugins/ValueReplacer.js +7 -6
- package/lib/postcss-plugins/__test__/hoverActivePlugin.spec.js +0 -3
- package/lib/postcss-plugins/__test__/selectorReplace.test.js +6 -3
- package/lib/postcss-plugins/__test__/valueReplacer.spec.js +2 -5
- package/lib/postcss-plugins/hoverActivePlugin.js +31 -67
- package/lib/postcss-plugins/variableModificationPlugin/ErrorHandler.js +0 -7
- package/lib/postcss-plugins/variableModificationPlugin/index.js +28 -49
- package/lib/schemas/index.js +3 -9
- package/lib/servers/clusterHubServer.js +1 -11
- package/lib/servers/devBuild.js +14 -26
- package/lib/servers/docsServer.js +1 -3
- package/lib/servers/docsServerCore.js +1 -22
- package/lib/servers/getCliPath.js +0 -9
- package/lib/servers/helpServer.js +1 -6
- package/lib/servers/httpsOptions.js +2 -8
- package/lib/servers/impactServer.js +3 -35
- package/lib/servers/mockserver.js +1 -10
- package/lib/servers/nowatchserver.js +12 -37
- package/lib/servers/requireLocalOrGlobal.js +6 -17
- package/lib/servers/scrServer.js +14 -21
- package/lib/servers/server.js +6 -36
- package/lib/servers/ssServer.js +1 -17
- package/lib/templates/CoverageScriptTemplate.js +0 -14
- package/lib/templates/WMSTemplate.js +7 -13
- package/lib/templates/linterConstant.js +2 -4
- package/lib/utils/babelPresets.js +3 -7
- package/lib/utils/clean.js +3 -9
- package/lib/utils/copy.js +1 -7
- package/lib/utils/copyTimezones.js +1 -9
- package/lib/utils/createEventStream.js +1 -6
- package/lib/utils/cssClassNameGenerate.js +10 -30
- package/lib/utils/cssURLReplacer.js +1 -22
- package/lib/utils/dependencyPostPublish.js +1 -10
- package/lib/utils/deprecationSupport.js +7 -32
- package/lib/utils/fileUtils.js +1 -28
- package/lib/utils/folderIterator.js +2 -13
- package/lib/utils/getComponents.js +0 -21
- package/lib/utils/getCurrentBranch.js +0 -5
- package/lib/utils/getDependenciesImpactList.js +1 -22
- package/lib/utils/getFileType.js +2 -10
- package/lib/utils/getHash.js +1 -8
- package/lib/utils/getIp.js +0 -2
- package/lib/utils/getOptions.js +16 -53
- package/lib/utils/getServerURL.js +1 -10
- package/lib/utils/index.js +4 -51
- package/lib/utils/init.js +0 -1
- package/lib/utils/initPreCommitHook.js +7 -30
- package/lib/utils/jsonHelper.js +3 -22
- package/lib/utils/libraryImpactConfig.js +2 -5
- package/lib/utils/lint/addScripts.js +2 -5
- package/lib/utils/lint/checkExistingConfig.js +3 -12
- package/lib/utils/lint/copyConfigs.js +0 -3
- package/lib/utils/lint/index.js +0 -9
- package/lib/utils/lint/lintScripts.js +0 -1
- package/lib/utils/lint/lintSetup.js +3 -4
- package/lib/utils/lint/lintStagedPreCommitHook.js +0 -1
- package/lib/utils/lint/question.js +0 -7
- package/lib/utils/lintReporter.js +0 -20
- package/lib/utils/log.js +0 -1
- package/lib/utils/mailSender.js +1 -8
- package/lib/utils/object-manipulation.js +1 -17
- package/lib/utils/pullOrigin.js +0 -4
- package/lib/utils/reinstallDependencies.js +1 -29
- package/lib/utils/removeAttributes.js +1 -8
- package/lib/utils/repoClone.js +3 -28
- package/lib/utils/request.js +0 -12
- package/lib/utils/rtl.js +5 -17
- package/lib/utils/selectorReplacer.js +10 -16
- package/lib/utils/setEnvVariables.js +0 -2
- package/lib/utils/ssTestHack.js +1 -11
- package/lib/utils/switchBranch.js +0 -5
- package/lib/utils/typeCheck.js +0 -1
- package/lib/utils/urlConcat.js +0 -4
- package/lib/utils/useExitCleanup.js +9 -10
- package/lib/utils/variableConverter.js +22 -31
- package/{npm-shrinkwrap.json → package-lock.json} +2098 -1999
- package/package.json +1 -1
@@ -0,0 +1,306 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.default = void 0;
|
7
|
+
|
8
|
+
// // plugins/I18nPlugin.js
|
9
|
+
// const path = require('path');
|
10
|
+
// const fs = require('fs');
|
11
|
+
// const getJsResourceKeys = require('./utils/getJsResourceKeys');
|
12
|
+
// const { collectI18nKeysfromAST, collectI18nKeysfromComments } = require('../I18nSplitPlugin/utils/collectI18nKeys');
|
13
|
+
// const NullFactory = require('webpack/lib/NullFactory');
|
14
|
+
// const { I18nDependency, I18nDependencyTemplate } = require('../I18nSplitPlugin/I18nDependency');
|
15
|
+
// class I18nPlugin {
|
16
|
+
// constructor(options = {}) {
|
17
|
+
// const { jsResourcePath } = options;
|
18
|
+
// if (!jsResourcePath) {
|
19
|
+
// throw new Error('I18nPlugin: "jsResourcePath" option is required.');
|
20
|
+
// }
|
21
|
+
// this.jsResourcePath = jsResourcePath;
|
22
|
+
// this.jsresourceKeys = getJsResourceKeys(jsResourcePath);
|
23
|
+
// this.moduleToKeysMap = new Map();
|
24
|
+
// }
|
25
|
+
// identifyEntryPoints(compilation) {
|
26
|
+
// this.entryPoints = new Set();
|
27
|
+
// const entryModules = new Set();
|
28
|
+
// compilation.entrypoints.forEach((entrypoint) => {
|
29
|
+
// entrypoint.getEntrypointChunk().getModules().forEach((module) => {
|
30
|
+
// if (module.resource) {
|
31
|
+
// entryModules.add(module.resource);
|
32
|
+
// console.log(entryModules)
|
33
|
+
// }
|
34
|
+
// });
|
35
|
+
// });
|
36
|
+
// this.entryPoints = entryModules;
|
37
|
+
// }
|
38
|
+
// apply(compiler) {
|
39
|
+
// compiler.hooks.thisCompilation.tap('I18nPlugin', (compilation, params) => {
|
40
|
+
// // Define dependency factories and templates for I18nDependency
|
41
|
+
// this.defineI18nDependency(compilation);
|
42
|
+
// // Handler for parsing modules
|
43
|
+
// const handler = (parser) => {
|
44
|
+
// parser.hooks.program.tap('I18nPlugin', (ast, comments) => {
|
45
|
+
// const { module } = parser.state;
|
46
|
+
// if (!module || !module.resource || !/\.(js|jsx)$/.test(module.resource)) {
|
47
|
+
// return;
|
48
|
+
// }
|
49
|
+
// const moduleResource = module.resource;
|
50
|
+
// // Collect i18n keys from AST and comments
|
51
|
+
// let i18nKeys = collectI18nKeysfromAST(ast, this.jsresourceKeys);
|
52
|
+
// i18nKeys = i18nKeys.concat(collectI18nKeysfromComments(comments, this.jsresourceKeys));
|
53
|
+
// if (i18nKeys.length > 0) {
|
54
|
+
// // Store the keys for this module
|
55
|
+
// this.moduleToKeysMap.set(moduleResource, i18nKeys);
|
56
|
+
// // Add I18nDependency to the module
|
57
|
+
// this.addI18nDependency(module, i18nKeys);
|
58
|
+
// console.log(`Module: ${moduleResource}`);
|
59
|
+
// // console.log(`I18n Keys: ${i18nKeys.join(', ')}`);
|
60
|
+
// }
|
61
|
+
// });
|
62
|
+
// };
|
63
|
+
// // Attach the handler to JavaScript parsers
|
64
|
+
// const normalModuleFactory = params.normalModuleFactory;
|
65
|
+
// normalModuleFactory.hooks.parser.for('javascript/auto').tap('I18nPlugin', handler);
|
66
|
+
// normalModuleFactory.hooks.parser.for('javascript/dynamic').tap('I18nPlugin', handler);
|
67
|
+
// normalModuleFactory.hooks.parser.for('javascript/esm').tap('I18nPlugin', handler);
|
68
|
+
// // After modules are built, process the collected keys
|
69
|
+
// compilation.hooks.finishModules.tap('I18nPlugin', (modules) => {
|
70
|
+
// this.identifyEntryPoints(compilation);
|
71
|
+
// this.processCollectedKeys(compilation);
|
72
|
+
// });
|
73
|
+
// });
|
74
|
+
// }
|
75
|
+
// defineI18nDependency(compilation) {
|
76
|
+
// compilation.dependencyFactories.set(I18nDependency, new NullFactory());
|
77
|
+
// compilation.dependencyTemplates.set(I18nDependency, new I18nDependencyTemplate());
|
78
|
+
// }
|
79
|
+
// addI18nDependency(module, i18nKeys) {
|
80
|
+
// const identifier = `i18n ${module.resource}`;
|
81
|
+
// const dep = new I18nDependency({ identifier, i18nKeys }, module.context, 0);
|
82
|
+
// module.addDependency(dep);
|
83
|
+
// }
|
84
|
+
// processCollectedKeys(compilation) {
|
85
|
+
// // Implement logic to process collected keys
|
86
|
+
// // For now, we'll just console log all the modules and their keys
|
87
|
+
// for (const [moduleResource, keys] of this.moduleToKeysMap.entries()) {
|
88
|
+
// // console.log(`Module: ${moduleResource} has keys: ${keys.join(', ')}`);
|
89
|
+
// }
|
90
|
+
// // Next steps:
|
91
|
+
// // - Identify entry point modules
|
92
|
+
// // - Assign keys to chunks (Chunk Zero for entry points, last chunk for unused keys)
|
93
|
+
// // - Implement the chunking logic as per your requirements
|
94
|
+
// }
|
95
|
+
// }
|
96
|
+
// module.exports = I18nPlugin;
|
97
|
+
// plugins/I18nPlugin.js
|
98
|
+
const path = require('path');
|
99
|
+
const fs = require('fs');
|
100
|
+
const getPropertiesAsJSON = require('./utils/getJsResourceKeys');
|
101
|
+
const {
|
102
|
+
collectI18nKeysfromAST,
|
103
|
+
collectI18nKeysfromComments
|
104
|
+
} = require('../I18nSplitPlugin/utils/collectI18nKeys');
|
105
|
+
const NullFactory = require('webpack/lib/NullFactory');
|
106
|
+
const {
|
107
|
+
I18nDependency,
|
108
|
+
I18nDependencyTemplate
|
109
|
+
} = require('../I18nSplitPlugin/I18nDependency');
|
110
|
+
const pluginName = 'I18nPlugin';
|
111
|
+
class I18nPlugin {
|
112
|
+
constructor(options = {}) {
|
113
|
+
const {
|
114
|
+
jsResourcePath,
|
115
|
+
mainChunkName
|
116
|
+
} = options;
|
117
|
+
if (!jsResourcePath) {
|
118
|
+
throw new Error('I18nPlugin: "jsResourcePath" option is required.');
|
119
|
+
}
|
120
|
+
if (!mainChunkName) {
|
121
|
+
throw new Error('I18nPlugin: "mainChunkName" option is required.');
|
122
|
+
}
|
123
|
+
this.jsResourcePath = jsResourcePath;
|
124
|
+
this.entryPointName = mainChunkName;
|
125
|
+
this.allI18nKeys = getPropertiesAsJSON(this.jsResourcePath);
|
126
|
+
console.log("allI18nKeys ", this.allI18nKeys.size);
|
127
|
+
this.moduleToKeysMap = new Map();
|
128
|
+
this.keyToModulesMap = new Map();
|
129
|
+
}
|
130
|
+
defineI18nDependency(compilation) {
|
131
|
+
compilation.dependencyFactories.set(I18nDependency, new NullFactory());
|
132
|
+
compilation.dependencyTemplates.set(I18nDependency, new I18nDependencyTemplate());
|
133
|
+
}
|
134
|
+
addI18nDependency(module, i18nKeys) {
|
135
|
+
const identifier = `i18n ${module.resource}`;
|
136
|
+
const dep = new I18nDependency({
|
137
|
+
identifier,
|
138
|
+
i18nKeys
|
139
|
+
}, module.context, 0);
|
140
|
+
module.addDependency(dep);
|
141
|
+
}
|
142
|
+
apply(compiler) {
|
143
|
+
compiler.hooks.thisCompilation.tap(pluginName, (compilation, params) => {
|
144
|
+
this.defineI18nDependency(compilation); // const handler = parser => {
|
145
|
+
// parser.hooks.program.tap(pluginName, (ast, commmets) => {
|
146
|
+
// const { module } = parser.state;
|
147
|
+
// //
|
148
|
+
// if (!(module && /\.jsx?$/.test(module.resource))) {
|
149
|
+
// return;
|
150
|
+
// }
|
151
|
+
// // i18n grep proper contract regex
|
152
|
+
// let i18nKeys = collectI18nKeysfromAST(ast, this.allI18nKeys );
|
153
|
+
// i18nKeys = i18nKeys.concat(
|
154
|
+
// // i18n grep proper contract regex
|
155
|
+
// collectI18nKeysfromComments(commmets, this.allI18nKeys)
|
156
|
+
// );
|
157
|
+
// if (i18nKeys.length) {
|
158
|
+
// console.log(i18nKeys)
|
159
|
+
// this.addI18nDependency(module, i18nKeys);
|
160
|
+
// }
|
161
|
+
// });
|
162
|
+
// };
|
163
|
+
|
164
|
+
const handler = parser => {
|
165
|
+
// Initialize counters for each type of log
|
166
|
+
let moduleLogCount = 0;
|
167
|
+
let astKeysLogCount = 0;
|
168
|
+
let commentKeysLogCount = 0;
|
169
|
+
let combinedKeysLogCount = 0;
|
170
|
+
let dependencyLogCount = 0;
|
171
|
+
parser.hooks.program.tap(pluginName, (ast, comments) => {
|
172
|
+
const {
|
173
|
+
module
|
174
|
+
} = parser.state;
|
175
|
+
if (moduleLogCount < 20) {
|
176
|
+
console.log("Processing module:", module ? module.resource : "No module found");
|
177
|
+
moduleLogCount++;
|
178
|
+
} // Check if the module and its resource match the .js or .jsx regex
|
179
|
+
|
180
|
+
if (!(module && /\.jsx?$/.test(module.resource))) {
|
181
|
+
if (moduleLogCount < 20) {
|
182
|
+
console.warn("Skipping non-JS/JSX module or missing module resource.");
|
183
|
+
moduleLogCount++;
|
184
|
+
}
|
185
|
+
return;
|
186
|
+
}
|
187
|
+
try {
|
188
|
+
// Collect i18n keys from AST
|
189
|
+
let i18nKeys = collectI18nKeysfromAST(ast, this.allI18nKeys);
|
190
|
+
if (astKeysLogCount < 20) {
|
191
|
+
console.log("i18n keys from AST:", i18nKeys);
|
192
|
+
astKeysLogCount++;
|
193
|
+
} // Collect i18n keys from comments
|
194
|
+
|
195
|
+
const commentKeys = collectI18nKeysfromComments(comments, this.allI18nKeys);
|
196
|
+
if (commentKeysLogCount < 20) {
|
197
|
+
console.log("i18n keys from comments:", commentKeys);
|
198
|
+
commentKeysLogCount++;
|
199
|
+
} // Combine keys from AST and comments
|
200
|
+
|
201
|
+
i18nKeys = i18nKeys.concat(commentKeys);
|
202
|
+
if (combinedKeysLogCount < 20) {
|
203
|
+
console.log("Combined i18n keys:", i18nKeys);
|
204
|
+
combinedKeysLogCount++;
|
205
|
+
} // If we have collected any keys, add them as dependencies
|
206
|
+
|
207
|
+
if (i18nKeys.length) {
|
208
|
+
this.addI18nDependency(module, i18nKeys);
|
209
|
+
if (dependencyLogCount < 20) {
|
210
|
+
console.log(`Added ${i18nKeys.length} i18n keys as dependencies.`);
|
211
|
+
dependencyLogCount++;
|
212
|
+
}
|
213
|
+
} else {
|
214
|
+
if (dependencyLogCount < 20) {
|
215
|
+
console.warn("No i18n keys found to add as dependencies.");
|
216
|
+
dependencyLogCount++;
|
217
|
+
}
|
218
|
+
}
|
219
|
+
} catch (error) {
|
220
|
+
console.error("Error processing module:", module.resource, error);
|
221
|
+
}
|
222
|
+
});
|
223
|
+
};
|
224
|
+
const normalModuleFactory = params.normalModuleFactory;
|
225
|
+
normalModuleFactory.hooks.parser.for('javascript/auto').tap(pluginName, handler);
|
226
|
+
normalModuleFactory.hooks.parser.for('javascript/dynamic').tap(pluginName, handler);
|
227
|
+
normalModuleFactory.hooks.parser.for('javascript/esm').tap(pluginName, handler);
|
228
|
+
compilation.hooks.afterOptimizeChunks.tap(pluginName, () => {
|
229
|
+
this.identifyEntryPoints(compilation); // this.processCollectedKeys(compilation);
|
230
|
+
});
|
231
|
+
});
|
232
|
+
}
|
233
|
+
identifyEntryPoints(compilation) {
|
234
|
+
this.entryPoints = new Set();
|
235
|
+
const entryModules = new Set();
|
236
|
+
console.log("Starting identifyEntryPoints..."); // Iterate over each entrypoint in the compilation
|
237
|
+
|
238
|
+
compilation.entrypoints.forEach((entrypoint, name) => {
|
239
|
+
console.log(`Processing entrypoint: ${name}`); // Logs the entrypoint name
|
240
|
+
|
241
|
+
if (name === "main") {
|
242
|
+
console.log(`Found the "main" entrypoint.`); // Debug log if "main" is found
|
243
|
+
} // Process all chunks associated with the entrypoint
|
244
|
+
|
245
|
+
entrypoint.chunks.forEach(chunk => {
|
246
|
+
// console.log(`Processing chunk: ${chunk.name}`);
|
247
|
+
chunk.getModules().forEach(module => {
|
248
|
+
if (module.resource) {
|
249
|
+
entryModules.add(module.resource); // console.log(`Added module: ${module.resource}`);
|
250
|
+
}
|
251
|
+
});
|
252
|
+
});
|
253
|
+
});
|
254
|
+
this.entryPoints = entryModules; // console.log("Entry Points Identified:", Array.from(this.entryPoints));
|
255
|
+
}
|
256
|
+
processCollectedKeys(compilation) {
|
257
|
+
// Map keys to chunk IDs
|
258
|
+
this.keyToChunkIdMap = {};
|
259
|
+
const unusedKeys = new Set(); // Assign keys used in entry points to Chunk Zero
|
260
|
+
|
261
|
+
for (const moduleResource of this.entryPoints) {
|
262
|
+
const keys = this.moduleToKeysMap.get(moduleResource);
|
263
|
+
if (keys) {
|
264
|
+
for (const key of keys) {
|
265
|
+
this.keyToChunkIdMap[key] = 0; // Assign to Chunk Zero
|
266
|
+
}
|
267
|
+
}
|
268
|
+
} // Assign other keys to their respective modules
|
269
|
+
|
270
|
+
let chunkIdCounter = 1; // Start from 1 since 0 is for entry points
|
271
|
+
|
272
|
+
for (const [key, modules] of this.keyToModulesMap.entries()) {
|
273
|
+
if (!this.keyToChunkIdMap.hasOwnProperty(key)) {
|
274
|
+
this.keyToChunkIdMap[key] = chunkIdCounter++;
|
275
|
+
}
|
276
|
+
} // Collect unused keys (keys not found in jsresource.properties)
|
277
|
+
|
278
|
+
for (const key of Object.keys(this.allI18nKeys)) {
|
279
|
+
if (!this.keyToModulesMap.has(key)) {
|
280
|
+
unusedKeys.add(key);
|
281
|
+
}
|
282
|
+
} // Assign unused keys to the last chunk
|
283
|
+
|
284
|
+
const lastChunkId = chunkIdCounter;
|
285
|
+
for (const key of unusedKeys) {
|
286
|
+
this.keyToChunkIdMap[key] = lastChunkId;
|
287
|
+
} // Store total number of chunks
|
288
|
+
|
289
|
+
this.totalChunks = lastChunkId + 1; // Output key-to-chunk mapping for use in i18n build
|
290
|
+
|
291
|
+
const outputPath = compilation.options.output.path;
|
292
|
+
const mappingFilePath = path.join(outputPath, 'i18n-key-chunk-mapping.json');
|
293
|
+
fs.writeFileSync(mappingFilePath, JSON.stringify(this.keyToChunkIdMap, null, 2), 'utf8');
|
294
|
+
}
|
295
|
+
}
|
296
|
+
exports.default = I18nPlugin;
|
297
|
+
I18nPlugin.isChunkHasI18n = function (chunk) {
|
298
|
+
for (const module of chunk.modulesIterable) {
|
299
|
+
for (const dep of module.dependencies) {
|
300
|
+
if (dep instanceof I18nDependency) {
|
301
|
+
return true;
|
302
|
+
}
|
303
|
+
}
|
304
|
+
}
|
305
|
+
return false;
|
306
|
+
};
|
@@ -0,0 +1,363 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
const path = require('path');
|
4
|
+
const fs = require('fs');
|
5
|
+
const {
|
6
|
+
getAllI18n,
|
7
|
+
getPropertiesAsJSON,
|
8
|
+
getAllI18nGroupedBySection
|
9
|
+
} = require('../I18nSplitPlugin/utils/propertiesUtils');
|
10
|
+
const {
|
11
|
+
collectI18nKeysfromAST,
|
12
|
+
collectI18nKeysfromComments
|
13
|
+
} = require('../I18nSplitPlugin/utils/collectI18nKeys');
|
14
|
+
const NullFactory = require('webpack/lib/NullFactory');
|
15
|
+
const {
|
16
|
+
I18nDependency,
|
17
|
+
I18nDependencyTemplate
|
18
|
+
} = require('../I18nSplitPlugin/I18nDependency');
|
19
|
+
const webpack = require('webpack');
|
20
|
+
const {
|
21
|
+
RawSource
|
22
|
+
} = require('webpack-sources'); // Import RawSource from webpack-sources
|
23
|
+
|
24
|
+
const pluginName = 'I18nPlugin';
|
25
|
+
class I18nPlugin {
|
26
|
+
constructor(options = {}) {
|
27
|
+
const {
|
28
|
+
jsResourcePath,
|
29
|
+
mainChunkName,
|
30
|
+
propertiesFolder,
|
31
|
+
disableDefault,
|
32
|
+
jsResource
|
33
|
+
} = options;
|
34
|
+
if (!jsResourcePath) {
|
35
|
+
throw new Error('I18nPlugin: "jsResourcePath" option is required.');
|
36
|
+
}
|
37
|
+
if (!mainChunkName) {
|
38
|
+
throw new Error('I18nPlugin: "mainChunkName" option is required.');
|
39
|
+
}
|
40
|
+
this.jsResourcePath = jsResourcePath;
|
41
|
+
this.entryPointName = mainChunkName;
|
42
|
+
this.jsResourceI18nKeys = jsResource ? (0, getPropertiesAsJSON)(jsResource) : {};
|
43
|
+
this.allI18nKeys = (0, getAllI18n)({
|
44
|
+
folderPath: propertiesFolder,
|
45
|
+
disableDefault,
|
46
|
+
jsResourceI18nKeys: this.jsResourceI18nKeys
|
47
|
+
});
|
48
|
+
console.log("allI18nKeys count:", Object.keys(this.allI18nKeys).length);
|
49
|
+
this.moduleToKeysMap = new Map();
|
50
|
+
this.keyToModulesMap = new Map();
|
51
|
+
this.keyToI18nChunkIdMap = {};
|
52
|
+
this.sectionedI18nKeys = getAllI18nGroupedBySection({
|
53
|
+
folderPath: propertiesFolder,
|
54
|
+
disableDefault,
|
55
|
+
jsResourceI18nKeys: this.jsResourceI18nKeys
|
56
|
+
});
|
57
|
+
}
|
58
|
+
defineI18nDependency(compilation) {
|
59
|
+
compilation.dependencyFactories.set(I18nDependency, new NullFactory());
|
60
|
+
compilation.dependencyTemplates.set(I18nDependency, new I18nDependencyTemplate());
|
61
|
+
}
|
62
|
+
addI18nDependency(module, i18nKeys) {
|
63
|
+
const identifier = `i18n ${module.resource}`;
|
64
|
+
const dep = new I18nDependency({
|
65
|
+
identifier,
|
66
|
+
i18nKeys
|
67
|
+
}, module.context, 0);
|
68
|
+
module.addDependency(dep);
|
69
|
+
}
|
70
|
+
apply(compiler) {
|
71
|
+
compiler.hooks.thisCompilation.tap(pluginName, (compilation, params) => {
|
72
|
+
this.defineI18nDependency(compilation); // handler for parser
|
73
|
+
|
74
|
+
const handler = parser => {
|
75
|
+
parser.hooks.program.tap(pluginName, (ast, comments) => {
|
76
|
+
const {
|
77
|
+
module
|
78
|
+
} = parser.state; //
|
79
|
+
|
80
|
+
if (!(module && /\.jsx?$/.test(module.resource))) {
|
81
|
+
return;
|
82
|
+
} // i18n grep proper contract regex
|
83
|
+
|
84
|
+
let i18nKeys = collectI18nKeysfromAST(ast, this.jsResourceI18nKeys);
|
85
|
+
let commentKeys = collectI18nKeysfromComments(comments, this.jsResourceI18nKeys);
|
86
|
+
i18nKeys = i18nKeys.concat(commentKeys);
|
87
|
+
if (i18nKeys.length) {
|
88
|
+
// console.log(`Collected i18n keys for module ${module.resource}:`, i18nKeys);
|
89
|
+
// Store the keys for this module
|
90
|
+
this.moduleToKeysMap.set(module.resource, i18nKeys); // Map keys to modules
|
91
|
+
|
92
|
+
for (const key of i18nKeys) {
|
93
|
+
if (!this.keyToModulesMap.has(key)) {
|
94
|
+
this.keyToModulesMap.set(key, new Set());
|
95
|
+
}
|
96
|
+
this.keyToModulesMap.get(key).add(module.resource);
|
97
|
+
} // Add I18nDependency to the module
|
98
|
+
}
|
99
|
+
});
|
100
|
+
}; // this below hook was tapped for collect I18n Key from files and add as Deppenensy
|
101
|
+
|
102
|
+
const factory = params.normalModuleFactory;
|
103
|
+
factory.hooks.parser.for('javascript/auto').tap(pluginName, handler);
|
104
|
+
factory.hooks.parser.for('javascript/dynamic').tap(pluginName, handler);
|
105
|
+
compilation.hooks.afterOptimizeChunks.tap(pluginName, () => {
|
106
|
+
this.identifyEntryPoints(compilation);
|
107
|
+
this.processCollectedKeys(compilation);
|
108
|
+
});
|
109
|
+
});
|
110
|
+
}
|
111
|
+
identifyEntryPoints(compilation) {
|
112
|
+
this.entryPoints = new Set();
|
113
|
+
const entryModules = new Set();
|
114
|
+
console.log("Starting identifyEntryPoints..."); // Iterate over each entrypoint in the compilation
|
115
|
+
|
116
|
+
compilation.entrypoints.forEach((entrypoint, name) => {
|
117
|
+
console.log(`Processing entrypoint: ${name}`); // Logs the entrypoint name
|
118
|
+
|
119
|
+
if (name === "main") {
|
120
|
+
console.log(`Found the "main" entrypoint.`); // Debug log if "main" is found
|
121
|
+
} // Process all chunks associated with the entrypoint
|
122
|
+
|
123
|
+
entrypoint.chunks.forEach(chunk => {
|
124
|
+
// console.log(`Processing chunk: ${chunk.name}`);
|
125
|
+
chunk.getModules().forEach(module => {
|
126
|
+
if (module.resource) {
|
127
|
+
entryModules.add(module.resource); // console.log(`Added module: ${module.resource}`);
|
128
|
+
}
|
129
|
+
});
|
130
|
+
});
|
131
|
+
});
|
132
|
+
this.entryPoints = entryModules; // console.log("Entry Points Identified:", Array.from(this.entryPoints));
|
133
|
+
}
|
134
|
+
processCollectedKeys(compilation) {
|
135
|
+
console.log("Starting processCollectedKeys..."); // Map to store chunkId to i18n keys
|
136
|
+
|
137
|
+
this.chunkIdToKeysMap = {};
|
138
|
+
this.moduleChunkIdToModulesMap = new Map(); // Iterate over all chunks in the compilation
|
139
|
+
|
140
|
+
for (const chunk of compilation.chunks) {
|
141
|
+
const chunkId = chunk.name || chunk.id; // console.log(`Processing chunk with ID: ${chunkId}`);
|
142
|
+
// Collect all modules in the chunk
|
143
|
+
|
144
|
+
const modules = chunk.getModules(); // console.log(`Modules in chunk ${chunkId}:`, modules.map(m => m.resource).filter(Boolean));
|
145
|
+
// Collect i18n keys used in these modules
|
146
|
+
|
147
|
+
let chunkKeys = new Set();
|
148
|
+
let moduleResources = [];
|
149
|
+
for (const module of modules) {
|
150
|
+
const moduleResource = module.resource;
|
151
|
+
if (moduleResource) {
|
152
|
+
moduleResources.push(moduleResource);
|
153
|
+
const keys = this.moduleToKeysMap.get(moduleResource);
|
154
|
+
if (keys) {
|
155
|
+
for (const key of keys) {
|
156
|
+
chunkKeys.add(key);
|
157
|
+
}
|
158
|
+
} else {// console.warn(`Module without resource in chunk ${chunkId}:`, module);
|
159
|
+
}
|
160
|
+
}
|
161
|
+
} // Convert the set to an array
|
162
|
+
|
163
|
+
chunkKeys = Array.from(chunkKeys); // Store in the map if there are keys
|
164
|
+
|
165
|
+
if (chunkKeys.length > 0) {
|
166
|
+
this.chunkIdToKeysMap[chunkId] = chunkKeys;
|
167
|
+
} // Map chunkId to module resources
|
168
|
+
|
169
|
+
if (moduleResources.length > 0) {
|
170
|
+
this.moduleChunkIdToModulesMap.set(chunkId, moduleResources);
|
171
|
+
}
|
172
|
+
} // console.log('moduleChunkIdToModulesMap:', this.moduleChunkIdToModulesMap);
|
173
|
+
// Emit the i18n chunks
|
174
|
+
|
175
|
+
this.emitI18nChunks(compilation);
|
176
|
+
} // emitI18nChunks(compilation) {
|
177
|
+
// const manifest = {
|
178
|
+
// moduleToI18nChunkMap: {},
|
179
|
+
// locales: {},
|
180
|
+
// };
|
181
|
+
// // Get the list of locales
|
182
|
+
// const locales = Object.keys(this.allI18nKeys);
|
183
|
+
// // Initialize i18nChunkId counter
|
184
|
+
// let i18nChunkIdCounter = 0;
|
185
|
+
// // Map of i18nChunkId to keys
|
186
|
+
// const i18nChunkIdToKeysMap = {};
|
187
|
+
// // For simplicity, assign all keys to chunks based on size
|
188
|
+
// const desiredChunkSize = 100 * 1024; // 100KB
|
189
|
+
// let currentChunkKeys = [];
|
190
|
+
// let currentChunkSize = 0;
|
191
|
+
// // Use the first locale to estimate key sizes
|
192
|
+
// const firstLocale = locales[0];
|
193
|
+
// const allKeys = Object.keys(this.allI18nKeys[firstLocale]);
|
194
|
+
// for (const key of allKeys) {
|
195
|
+
// const value = this.allI18nKeys[firstLocale][key] || '';
|
196
|
+
// const keySize = Buffer.byteLength(key, 'utf8') + Buffer.byteLength(value, 'utf8');
|
197
|
+
// if (currentChunkSize + keySize > desiredChunkSize) {
|
198
|
+
// // Assign current chunk
|
199
|
+
// const i18nChunkId = i18nChunkIdCounter++;
|
200
|
+
// i18nChunkIdToKeysMap[i18nChunkId] = currentChunkKeys;
|
201
|
+
// // Map keys to i18nChunkId
|
202
|
+
// for (const chunkKey of currentChunkKeys) {
|
203
|
+
// this.keyToI18nChunkIdMap[chunkKey] = i18nChunkId;
|
204
|
+
// }
|
205
|
+
// currentChunkKeys = [];
|
206
|
+
// currentChunkSize = 0;
|
207
|
+
// }
|
208
|
+
// currentChunkKeys.push(key);
|
209
|
+
// currentChunkSize += keySize;
|
210
|
+
// }
|
211
|
+
// // Assign the remaining keys in the current chunk
|
212
|
+
// if (currentChunkKeys.length > 0) {
|
213
|
+
// const i18nChunkId = i18nChunkIdCounter++;
|
214
|
+
// i18nChunkIdToKeysMap[i18nChunkId] = currentChunkKeys;
|
215
|
+
// // Map keys to i18nChunkId
|
216
|
+
// for (const chunkKey of currentChunkKeys) {
|
217
|
+
// this.keyToI18nChunkIdMap[chunkKey] = i18nChunkId;
|
218
|
+
// }
|
219
|
+
// }
|
220
|
+
// // console.log('keyToI18nChunkIdMap:', this.keyToI18nChunkIdMap);
|
221
|
+
// // Now, generate the i18n chunks
|
222
|
+
// for (const locale of locales) {
|
223
|
+
// manifest.locales[locale] = {};
|
224
|
+
// for (const [i18nChunkId, keys] of Object.entries(i18nChunkIdToKeysMap)) {
|
225
|
+
// const i18nData = {};
|
226
|
+
// for (const key of keys) {
|
227
|
+
// const value = this.allI18nKeys[locale][key];
|
228
|
+
// if (value !== undefined) {
|
229
|
+
// i18nData[key] = value;
|
230
|
+
// }
|
231
|
+
// }
|
232
|
+
// // Skip if no keys are available for this locale
|
233
|
+
// if (Object.keys(i18nData).length === 0) {
|
234
|
+
// console.log(`Locale ${locale}, i18n chunk ${i18nChunkId} has no keys, skipping.`);
|
235
|
+
// continue;
|
236
|
+
// }
|
237
|
+
// // Generate the JavaScript code
|
238
|
+
// const moduleSource = `window.i18nData = window.i18nData || {}; window.i18nData[${i18nChunkId}] = ${JSON.stringify(i18nData, null, 2)};`;
|
239
|
+
// // Define the filename and output path
|
240
|
+
// const filename = `${locale}/chunk-${i18nChunkId}.js`;
|
241
|
+
// // Emit the asset
|
242
|
+
// compilation.emitAsset(
|
243
|
+
// filename,
|
244
|
+
// new RawSource(moduleSource)
|
245
|
+
// );
|
246
|
+
// // console.log(`Emitted i18n chunk: ${filename}`);
|
247
|
+
// // Update the manifest
|
248
|
+
// manifest.locales[locale][i18nChunkId] = filename;
|
249
|
+
// }
|
250
|
+
// }
|
251
|
+
// // Now, create moduleToI18nChunkMap
|
252
|
+
// for (const [moduleChunkId, moduleResources] of this.moduleChunkIdToModulesMap.entries()) {
|
253
|
+
// // console.log(`Processing module chunk ID: ${moduleChunkId}`);
|
254
|
+
// // console.log(`Module resources:`, moduleResources);
|
255
|
+
// const requiredI18nChunkIds = new Set();
|
256
|
+
// for (const moduleResource of moduleResources) {
|
257
|
+
// const keys = this.moduleToKeysMap.get(moduleResource);
|
258
|
+
// if (keys) {
|
259
|
+
// for (const key of keys) {
|
260
|
+
// const i18nChunkId = this.keyToI18nChunkIdMap[key];
|
261
|
+
// if (i18nChunkId !== undefined) {
|
262
|
+
// requiredI18nChunkIds.add(i18nChunkId);
|
263
|
+
// }
|
264
|
+
// }
|
265
|
+
// }
|
266
|
+
// }
|
267
|
+
// if (requiredI18nChunkIds.size > 0) {
|
268
|
+
// manifest.moduleToI18nChunkMap[moduleChunkId] = Array.from(requiredI18nChunkIds);
|
269
|
+
// // console.log(`Mapped module chunk ID ${moduleChunkId} to i18n chunks:`, Array.from(requiredI18nChunkIds));
|
270
|
+
// }else{
|
271
|
+
// console.log(`No required i18n chunks for module chunk ID ${moduleChunkId}`);
|
272
|
+
// }
|
273
|
+
// }
|
274
|
+
// // Emit the manifest file
|
275
|
+
// const manifestSource = new RawSource(`window.i18n = window.i18n || {}; window.i18n.manifest = ${JSON.stringify(manifest, null, 2)};`);
|
276
|
+
// const manifestFilename = 'i18n-chunks/manifest.js';
|
277
|
+
// compilation.emitAsset(
|
278
|
+
// manifestFilename,
|
279
|
+
// manifestSource
|
280
|
+
// );
|
281
|
+
// console.log(`Emitted i18n manifest: ${manifestFilename}`);
|
282
|
+
// }
|
283
|
+
|
284
|
+
emitI18nChunks(compilation) {
|
285
|
+
const manifest = {
|
286
|
+
moduleToI18nChunkMap: {},
|
287
|
+
locales: {}
|
288
|
+
}; // Get the list of locales
|
289
|
+
|
290
|
+
const locales = Object.keys(this.allI18nKeys); // Initialize i18nChunkId counter
|
291
|
+
|
292
|
+
let i18nChunkIdCounter = 0;
|
293
|
+
const sectionToI18nChunkIdMap = {}; // Assign each section to an i18n chunk
|
294
|
+
// Assuming all locales have the same sections
|
295
|
+
|
296
|
+
const firstLocaleSections = Object.keys(this.allI18nKeys[locales[0]]);
|
297
|
+
for (const sectionName of firstLocaleSections) {
|
298
|
+
const i18nChunkId = i18nChunkIdCounter++;
|
299
|
+
sectionToI18nChunkIdMap[sectionName] = i18nChunkId; // Collect keys for this section
|
300
|
+
|
301
|
+
const keys = Object.keys(this.allI18nKeys[locales[0]][sectionName] || {}); // Map keys to i18nChunkId
|
302
|
+
|
303
|
+
for (const key of keys) {
|
304
|
+
this.keyToI18nChunkIdMap[key] = i18nChunkId;
|
305
|
+
} // Generate the i18n chunk for each locale
|
306
|
+
|
307
|
+
for (const locale of locales) {
|
308
|
+
manifest.locales[locale] = manifest.locales[locale] || {};
|
309
|
+
const i18nData = {};
|
310
|
+
const localeSectionKeys = this.allI18nKeys[locale][sectionName] || {};
|
311
|
+
for (const key of keys) {
|
312
|
+
const value = localeSectionKeys[key];
|
313
|
+
if (value !== undefined) {
|
314
|
+
i18nData[key] = value;
|
315
|
+
}
|
316
|
+
}
|
317
|
+
if (Object.keys(i18nData).length > 0) {
|
318
|
+
const moduleSource = `window.i18nData = window.i18nData || {}; window.i18nData[${i18nChunkId}] = ${JSON.stringify(i18nData, null, 2)};`;
|
319
|
+
const filename = `${locale}/chunk-${i18nChunkId}.js`;
|
320
|
+
compilation.emitAsset(filename, new RawSource(moduleSource));
|
321
|
+
manifest.locales[locale][i18nChunkId] = filename;
|
322
|
+
}
|
323
|
+
}
|
324
|
+
} // Now, create moduleToI18nChunkMap
|
325
|
+
|
326
|
+
for (const [moduleChunkId, moduleResources] of Object.entries(this.moduleChunkIdToModulesMap)) {
|
327
|
+
const requiredI18nChunkIds = new Set();
|
328
|
+
for (const moduleResource of moduleResources) {
|
329
|
+
const keys = this.moduleToKeysMap.get(moduleResource);
|
330
|
+
if (keys) {
|
331
|
+
for (const key of keys) {
|
332
|
+
const i18nChunkId = this.keyToI18nChunkIdMap[key];
|
333
|
+
if (i18nChunkId !== undefined) {
|
334
|
+
requiredI18nChunkIds.add(i18nChunkId);
|
335
|
+
}
|
336
|
+
}
|
337
|
+
}
|
338
|
+
}
|
339
|
+
if (requiredI18nChunkIds.size > 0) {
|
340
|
+
manifest.moduleToI18nChunkMap[moduleChunkId] = Array.from(requiredI18nChunkIds);
|
341
|
+
} else {
|
342
|
+
// console.log(
|
343
|
+
`No required i18n chunks for module chunk ID ${moduleChunkId}`; // );
|
344
|
+
}
|
345
|
+
} // Emit the manifest file
|
346
|
+
|
347
|
+
const manifestSource = new RawSource(`window.i18n = window.i18n || {}; window.i18n.manifest = ${JSON.stringify(manifest, null, 2)};`);
|
348
|
+
const manifestFilename = 'i18n-chunks/manifest.js';
|
349
|
+
compilation.emitAsset(manifestFilename, manifestSource);
|
350
|
+
console.log(`Emitted i18n manifest: ${manifestFilename}`);
|
351
|
+
}
|
352
|
+
}
|
353
|
+
I18nPlugin.isChunkHasI18n = function (chunk) {
|
354
|
+
for (const module of chunk.modulesIterable) {
|
355
|
+
for (const dep of module.dependencies) {
|
356
|
+
if (dep instanceof I18nDependency) {
|
357
|
+
return true;
|
358
|
+
}
|
359
|
+
}
|
360
|
+
}
|
361
|
+
return false;
|
362
|
+
};
|
363
|
+
module.exports = I18nPlugin;
|