@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.
Files changed (221) hide show
  1. package/lib/babel/babel-option-utils/babel-preset-react-option.js +0 -3
  2. package/lib/babel/cmjs-plugins-presets.js +1 -8
  3. package/lib/babel/es-plugins-presets.js +1 -8
  4. package/lib/common/buildEs.js +1 -0
  5. package/lib/common/getEntries.js +2 -13
  6. package/lib/common/getPublicPathConfig.js +0 -6
  7. package/lib/common/index.js +1 -6
  8. package/lib/common/runPreProcess.js +6 -15
  9. package/lib/common/splitChunks.js +2 -21
  10. package/lib/common/sslcertUpdater.js +7 -18
  11. package/lib/common/templateParameters.js +0 -2
  12. package/lib/common/testPattern.js +10 -21
  13. package/lib/common/valueReplacer.js +1 -16
  14. package/lib/configs/jest.config.js +1 -10
  15. package/lib/configs/libAlias.js +8 -14
  16. package/lib/configs/resolvers.js +4 -14
  17. package/lib/configs/webpack.component.umd.config.js +1 -6
  18. package/lib/configs/webpack.css.umd.config.js +6 -14
  19. package/lib/configs/webpack.dev.config.js +1 -14
  20. package/lib/configs/webpack.docs.config.js +1 -9
  21. package/lib/configs/webpack.impact.config.js +1 -8
  22. package/lib/configs/webpack.prod.config.js +4 -17
  23. package/lib/constants.js +9 -18
  24. package/lib/deprecationLogger.js +0 -7
  25. package/lib/hooks/docsProptypeHook.js +4 -8
  26. package/lib/jest/commitedFilesResult.js +4 -46
  27. package/lib/jest/coverageCollector.js +1 -12
  28. package/lib/jest/jsonMaker.js +0 -6
  29. package/lib/jest/preProcessors/cssPreprocessor.js +1 -9
  30. package/lib/jest/preProcessors/jsPreprocessor.js +1 -6
  31. package/lib/jest/preProcessors/otherFilesPreprocessor.js +1 -4
  32. package/lib/jest/result.js +1 -23
  33. package/lib/jest/run.js +7 -18
  34. package/lib/jest/setup.js +8 -60
  35. package/lib/loaderUtils/configsAssetsLoaders.js +2 -12
  36. package/lib/loaderUtils/getCSSLoaders.js +10 -22
  37. package/lib/loaderUtils/getDevJsLoaders.js +4 -13
  38. package/lib/loaderUtils/index.js +1 -4
  39. package/lib/loaderUtils/tests/windowsModification.test.js +0 -1
  40. package/lib/loaderUtils/windowsModification.js +2 -3
  41. package/lib/loaders/__test__/markdownLoader.spec.js +0 -1
  42. package/lib/loaders/composeLoader.js +13 -37
  43. package/lib/loaders/docsLoader.js +1 -12
  44. package/lib/loaders/docsPropsLoader.js +4 -8
  45. package/lib/loaders/enhancedReactLiveConverter.js +2 -23
  46. package/lib/loaders/fileBountryLoader.js +1 -3
  47. package/lib/loaders/fileLoader.js +12 -23
  48. package/lib/loaders/markdownLoader.js +14 -19
  49. package/lib/loaders/reactLiveConvertor.js +5 -15
  50. package/lib/loaders/scriptInstrumentLoader.js +7 -16
  51. package/lib/loaders/selectorMappingLoader.js +7 -26
  52. package/lib/loaders/workerLoader.js +9 -24
  53. package/lib/logger.js +0 -4
  54. package/lib/middlewares/HMRMiddleware.js +13 -27
  55. package/lib/middlewares/SSTMiddleware.js +1 -5
  56. package/lib/pluginUtils/configHtmlWebpackPlugins.js +1 -24
  57. package/lib/pluginUtils/getDevPlugins.js +9 -42
  58. package/lib/pluginUtils/getDocsPlugins.js +3 -13
  59. package/lib/pluginUtils/getLibraryImactPlugins.js +5 -6
  60. package/lib/pluginUtils/getLibraryPlugins.js +2 -8
  61. package/lib/pluginUtils/getProdPlugins.js +8 -47
  62. package/lib/pluginUtils/getServerPlugins.js +2 -8
  63. package/lib/pluginUtils/getUMDCSSPlugins.js +2 -10
  64. package/lib/pluginUtils/getUMDComponentPlugins.js +2 -10
  65. package/lib/pluginUtils/index.js +1 -9
  66. package/lib/plugins/CdnChangePlugin.js +2 -18
  67. package/lib/plugins/CleanupStatsPlugin.js +0 -5
  68. package/lib/plugins/CssOrderControlPlugin.js +3 -6
  69. package/lib/plugins/CustomAttributePlugin.js +14 -19
  70. package/lib/plugins/CustomScriptLoadingStrategyPlugin.js +3 -23
  71. package/lib/plugins/EFCPlugin.js +20 -34
  72. package/lib/plugins/EFCTemplatePlugin.js +19 -30
  73. package/lib/plugins/EfcResourceCleanupPlugin.js +0 -3
  74. package/lib/plugins/EventsHandlingPlugin.js +2 -4
  75. package/lib/plugins/I18NInjectIntoIndexPlugin.js +11 -37
  76. package/lib/plugins/I18nSplitPlugin/I18nDebugPlugin.js +10 -15
  77. package/lib/plugins/I18nSplitPlugin/I18nDependency.js +4 -10
  78. package/lib/plugins/I18nSplitPlugin/I18nDownlodLogic.js +12 -25
  79. package/lib/plugins/I18nSplitPlugin/I18nFilesEmitter.js +55 -133
  80. package/lib/plugins/I18nSplitPlugin/I18nKeysIdentifer.js +12 -23
  81. package/lib/plugins/I18nSplitPlugin/index.js +13 -24
  82. package/lib/plugins/I18nSplitPlugin/utils/applyMetaManifest.js +64 -58
  83. package/lib/plugins/I18nSplitPlugin/utils/collectI18nKeys.js +2 -12
  84. package/lib/plugins/I18nSplitPlugin/utils/createMetaManifest.js +10 -29
  85. package/lib/plugins/I18nSplitPlugin/utils/createRegularManifest.js +8 -19
  86. package/lib/plugins/I18nSplitPlugin/utils/getI18nFileUrlPathTemplate.js +0 -1
  87. package/lib/plugins/I18nSplitPlugin/utils/getI18nKeysFormModules.js +0 -5
  88. package/lib/plugins/I18nSplitPlugin/utils/hashUtils.js +1 -8
  89. package/lib/plugins/I18nSplitPlugin/utils/index.js +0 -4
  90. package/lib/plugins/I18nSplitPlugin/utils/propertiesUtils.js +0 -20
  91. package/lib/plugins/I18nSplitPlugin/utils/unicodeConversion.js +0 -1
  92. package/lib/plugins/ManifestPlugin.js +1 -18
  93. package/lib/plugins/MinifyPlugin.js +1 -10
  94. package/lib/plugins/ModuleStatsPlugin.js +1 -24
  95. package/lib/plugins/OptimizeJSPlugin.js +2 -10
  96. package/lib/plugins/PublicPathCallbackPlugin.js +1 -12
  97. package/lib/plugins/PublicPathChangePlugin.js +6 -39
  98. package/lib/plugins/ReportGeneratePlugin.js +5 -32
  99. package/lib/plugins/RequireVariablePublicPlugin.js +1 -8
  100. package/lib/plugins/ResourceHintsPlugin.js +4 -13
  101. package/lib/plugins/RtlSplitPlugin/OverwriteCssPathForRTL.js +12 -17
  102. package/lib/plugins/RtlSplitPlugin/RtlCssPlugin.js +10 -17
  103. package/lib/plugins/RtlSplitPlugin/replaceCssDirTemplate.js +2 -5
  104. package/lib/plugins/ScriptInstrumentPlugin.js +1 -8
  105. package/lib/plugins/SelectorPlugin.js +6 -32
  106. package/lib/plugins/ServiceWorkerPlugin.js +5 -22
  107. package/lib/plugins/ShadowDOMSupportPlugin.js +4 -41
  108. package/lib/plugins/SourceMapHookPlugin.js +2 -12
  109. package/lib/plugins/StatsPlugin.js +0 -14
  110. package/lib/plugins/TPHashMappingPlugin.js +3 -18
  111. package/lib/plugins/UnusedFilesFindPlugin.js +4 -39
  112. package/lib/plugins/VariableConversionCollector.js +15 -42
  113. package/lib/plugins/index.js +1 -20
  114. package/lib/plugins/libraryImpactPlugin.js +1 -33
  115. package/lib/plugins/newi18nsplitplugin/18nPlugin1.js +306 -0
  116. package/lib/plugins/newi18nsplitplugin/18nPlugin2.js +363 -0
  117. package/lib/plugins/newi18nsplitplugin/18nPlugin3.js +694 -0
  118. package/lib/plugins/newi18nsplitplugin/18nPlugin_hashed.js +1258 -0
  119. package/lib/plugins/newi18nsplitplugin/18nPlugin_working.js +542 -0
  120. package/lib/plugins/newi18nsplitplugin/18nplugin.js +974 -0
  121. package/lib/plugins/newi18nsplitplugin/ChunkManager.js +131 -0
  122. package/lib/plugins/newi18nsplitplugin/GenerateModuleIdToKeysMapPlugin.js +59 -0
  123. package/lib/plugins/newi18nsplitplugin/I18nDiffPlugin.js +262 -0
  124. package/lib/plugins/newi18nsplitplugin/I18nDownloadLogic.js +166 -0
  125. package/lib/plugins/newi18nsplitplugin/I18nPropertiesPlugin.js +111 -0
  126. package/lib/plugins/newi18nsplitplugin/KeyCollector.js +163 -0
  127. package/lib/plugins/newi18nsplitplugin/ManifestGenerator.js +88 -0
  128. package/lib/plugins/newi18nsplitplugin/UnicodeConversionPlugin.js +101 -0
  129. package/lib/plugins/newi18nsplitplugin/constants.js +162 -0
  130. package/lib/plugins/newi18nsplitplugin/utils/I18nKeyHasher.js +78 -0
  131. package/lib/plugins/newi18nsplitplugin/utils/getJsResourceKeys.js +22 -0
  132. package/lib/plugins/newi18nsplitplugin/utils/i18nChunkUtils.js +18 -0
  133. package/lib/plugins/newi18nsplitplugin/utils/manifestGenerator.js +580 -0
  134. package/lib/plugins/newi18nsplitplugin/utils/propertiesUtils.js +54 -0
  135. package/lib/plugins/utils/classHandling.js +0 -6
  136. package/lib/plugins/utils/fileHandling.js +6 -15
  137. package/lib/plugins/utils/tests/fileHandling.test.js +0 -4
  138. package/lib/plugins/variableConvertorUtils.js +14 -29
  139. package/lib/plugins/webpackwatchrunplugin.js +0 -5
  140. package/lib/postcss-plugins/EmptyPlugin.js +3 -4
  141. package/lib/postcss-plugins/ExcludePlugin.js +1 -5
  142. package/lib/postcss-plugins/IncludePlugin.js +1 -5
  143. package/lib/postcss-plugins/RTLSplitPlugin.js +14 -27
  144. package/lib/postcss-plugins/SelectorReplace.js +1 -16
  145. package/lib/postcss-plugins/ValueReplacer.js +7 -6
  146. package/lib/postcss-plugins/__test__/hoverActivePlugin.spec.js +0 -3
  147. package/lib/postcss-plugins/__test__/selectorReplace.test.js +6 -3
  148. package/lib/postcss-plugins/__test__/valueReplacer.spec.js +2 -5
  149. package/lib/postcss-plugins/hoverActivePlugin.js +31 -67
  150. package/lib/postcss-plugins/variableModificationPlugin/ErrorHandler.js +0 -7
  151. package/lib/postcss-plugins/variableModificationPlugin/index.js +28 -49
  152. package/lib/schemas/index.js +3 -9
  153. package/lib/servers/clusterHubServer.js +1 -11
  154. package/lib/servers/devBuild.js +14 -26
  155. package/lib/servers/docsServer.js +1 -3
  156. package/lib/servers/docsServerCore.js +1 -22
  157. package/lib/servers/getCliPath.js +0 -9
  158. package/lib/servers/helpServer.js +1 -6
  159. package/lib/servers/httpsOptions.js +2 -8
  160. package/lib/servers/impactServer.js +3 -35
  161. package/lib/servers/mockserver.js +1 -10
  162. package/lib/servers/nowatchserver.js +12 -37
  163. package/lib/servers/requireLocalOrGlobal.js +6 -17
  164. package/lib/servers/scrServer.js +14 -21
  165. package/lib/servers/server.js +6 -36
  166. package/lib/servers/ssServer.js +1 -17
  167. package/lib/templates/CoverageScriptTemplate.js +0 -14
  168. package/lib/templates/WMSTemplate.js +7 -13
  169. package/lib/templates/linterConstant.js +2 -4
  170. package/lib/utils/babelPresets.js +3 -7
  171. package/lib/utils/clean.js +3 -9
  172. package/lib/utils/copy.js +1 -7
  173. package/lib/utils/copyTimezones.js +1 -9
  174. package/lib/utils/createEventStream.js +1 -6
  175. package/lib/utils/cssClassNameGenerate.js +10 -30
  176. package/lib/utils/cssURLReplacer.js +1 -22
  177. package/lib/utils/dependencyPostPublish.js +1 -10
  178. package/lib/utils/deprecationSupport.js +7 -32
  179. package/lib/utils/fileUtils.js +1 -28
  180. package/lib/utils/folderIterator.js +2 -13
  181. package/lib/utils/getComponents.js +0 -21
  182. package/lib/utils/getCurrentBranch.js +0 -5
  183. package/lib/utils/getDependenciesImpactList.js +1 -22
  184. package/lib/utils/getFileType.js +2 -10
  185. package/lib/utils/getHash.js +1 -8
  186. package/lib/utils/getIp.js +0 -2
  187. package/lib/utils/getOptions.js +16 -53
  188. package/lib/utils/getServerURL.js +1 -10
  189. package/lib/utils/index.js +4 -51
  190. package/lib/utils/init.js +0 -1
  191. package/lib/utils/initPreCommitHook.js +7 -30
  192. package/lib/utils/jsonHelper.js +3 -22
  193. package/lib/utils/libraryImpactConfig.js +2 -5
  194. package/lib/utils/lint/addScripts.js +2 -5
  195. package/lib/utils/lint/checkExistingConfig.js +3 -12
  196. package/lib/utils/lint/copyConfigs.js +0 -3
  197. package/lib/utils/lint/index.js +0 -9
  198. package/lib/utils/lint/lintScripts.js +0 -1
  199. package/lib/utils/lint/lintSetup.js +3 -4
  200. package/lib/utils/lint/lintStagedPreCommitHook.js +0 -1
  201. package/lib/utils/lint/question.js +0 -7
  202. package/lib/utils/lintReporter.js +0 -20
  203. package/lib/utils/log.js +0 -1
  204. package/lib/utils/mailSender.js +1 -8
  205. package/lib/utils/object-manipulation.js +1 -17
  206. package/lib/utils/pullOrigin.js +0 -4
  207. package/lib/utils/reinstallDependencies.js +1 -29
  208. package/lib/utils/removeAttributes.js +1 -8
  209. package/lib/utils/repoClone.js +3 -28
  210. package/lib/utils/request.js +0 -12
  211. package/lib/utils/rtl.js +5 -17
  212. package/lib/utils/selectorReplacer.js +10 -16
  213. package/lib/utils/setEnvVariables.js +0 -2
  214. package/lib/utils/ssTestHack.js +1 -11
  215. package/lib/utils/switchBranch.js +0 -5
  216. package/lib/utils/typeCheck.js +0 -1
  217. package/lib/utils/urlConcat.js +0 -4
  218. package/lib/utils/useExitCleanup.js +9 -10
  219. package/lib/utils/variableConverter.js +22 -31
  220. package/{npm-shrinkwrap.json → package-lock.json} +2098 -1999
  221. package/package.json +1 -1
@@ -0,0 +1,542 @@
1
+ "use strict";
2
+
3
+ //working size based approch
4
+ const path = require('path');
5
+ const fs = require('fs');
6
+ const {
7
+ getAllI18nGroupedBySection,
8
+ getPropertiesAsJSON
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 {
20
+ RawSource
21
+ } = require('webpack-sources');
22
+ const pluginName = 'I18nPlugin';
23
+ class I18nPlugin {
24
+ constructor(options = {}) {
25
+ const {
26
+ jsResourcePath,
27
+ mainChunkName,
28
+ propertiesFolder,
29
+ disableDefault,
30
+ jsResource
31
+ } = options;
32
+ if (!jsResourcePath) {
33
+ throw new Error('I18nPlugin: "jsResourcePath" option is required.');
34
+ }
35
+ if (!mainChunkName) {
36
+ throw new Error('I18nPlugin: "mainChunkName" option is required.');
37
+ }
38
+ this.jsResourcePath = jsResourcePath;
39
+ this.entryPointName = mainChunkName;
40
+ this.jsResourceI18nKeys = jsResource ? getPropertiesAsJSON(jsResource) : {}; // Use getAllI18nGroupedBySection to group keys by sections
41
+
42
+ this.allI18nKeys = getAllI18nGroupedBySection({
43
+ folderPath: propertiesFolder,
44
+ disableDefault,
45
+ jsResourceI18nKeys: this.jsResourceI18nKeys
46
+ });
47
+ console.log('Locales found:', Object.keys(this.allI18nKeys)); // Build keyToSectionMap
48
+
49
+ this.keyToSectionMap = {};
50
+ const locales = Object.keys(this.allI18nKeys);
51
+ if (locales.length > 0) {
52
+ // Use the first locale to build keyToSectionMap
53
+ const firstLocale = locales[0];
54
+ const sections = this.allI18nKeys[firstLocale];
55
+ console.log(`Sections found in properties files: ${Object.keys(sections).join(', ')}`);
56
+ for (const sectionName of Object.keys(sections)) {
57
+ const keysInSection = sections[sectionName];
58
+ for (const key of Object.keys(keysInSection)) {
59
+ this.keyToSectionMap[key] = sectionName;
60
+ }
61
+ }
62
+ }
63
+ this.moduleToKeysMap = new Map();
64
+ this.keyToModulesMap = new Map();
65
+ this.keyToI18nChunkIdMap = {};
66
+ }
67
+ defineI18nDependency(compilation) {
68
+ compilation.dependencyFactories.set(I18nDependency, new NullFactory());
69
+ compilation.dependencyTemplates.set(I18nDependency, new I18nDependencyTemplate());
70
+ }
71
+ apply(compiler) {
72
+ compiler.hooks.thisCompilation.tap(pluginName, (compilation, params) => {
73
+ this.defineI18nDependency(compilation); // Handler for parser
74
+
75
+ const handler = parser => {
76
+ parser.hooks.program.tap(pluginName, (ast, comments) => {
77
+ const {
78
+ module
79
+ } = parser.state;
80
+ if (!(module && /\.jsx?$/.test(module.resource))) {
81
+ return;
82
+ } // Collect i18n keys
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
+ // Store the keys for this module
89
+ this.moduleToKeysMap.set(module.resource, i18nKeys); // Map keys to modules
90
+
91
+ for (const key of i18nKeys) {
92
+ if (!this.keyToModulesMap.has(key)) {
93
+ this.keyToModulesMap.set(key, new Set());
94
+ }
95
+ this.keyToModulesMap.get(key).add(module.resource);
96
+ }
97
+ }
98
+ });
99
+ };
100
+ const factory = params.normalModuleFactory;
101
+ factory.hooks.parser.for('javascript/auto').tap(pluginName, handler);
102
+ factory.hooks.parser.for('javascript/dynamic').tap(pluginName, handler);
103
+ compilation.hooks.afterOptimizeChunks.tap(pluginName, () => {
104
+ this.processCollectedKeys(compilation);
105
+ });
106
+ });
107
+ }
108
+ processCollectedKeys(compilation) {
109
+ // Map to store chunkId to required sections
110
+ this.chunkIdToSectionsMap = {};
111
+ this.moduleChunkIdToModulesMap = {}; // Map module resource to chunk IDs
112
+
113
+ this.moduleResourceToChunkIds = new Map(); // Collect modules for the entry point
114
+
115
+ const entryPointChunk = compilation.namedChunks.get(this.entryPointName);
116
+ const entryPointModules = new Set();
117
+ if (entryPointChunk) {
118
+ for (const module of entryPointChunk.getModules()) {
119
+ if (module.resource) {
120
+ entryPointModules.add(module.resource);
121
+ }
122
+ }
123
+ } // Iterate over all chunks in the compilation
124
+
125
+ for (const chunk of compilation.chunks) {
126
+ const chunkId = chunk.name || chunk.id;
127
+ const chunkIdString = String(chunkId); // Collect all modules in the chunk
128
+
129
+ const modules = chunk.getModules();
130
+ let requiredSections = new Set();
131
+ let moduleResources = [];
132
+ for (const module of modules) {
133
+ const moduleResource = module.resource;
134
+ if (moduleResource) {
135
+ moduleResources.push(moduleResource);
136
+ const keys = this.moduleToKeysMap.get(moduleResource);
137
+ if (keys) {
138
+ for (const key of keys) {
139
+ const section = this.keyToSectionMap[key];
140
+ if (section) {
141
+ requiredSections.add(section);
142
+ }
143
+ }
144
+ } // Map module resource to chunk IDs
145
+
146
+ if (!this.moduleResourceToChunkIds.has(moduleResource)) {
147
+ this.moduleResourceToChunkIds.set(moduleResource, new Set());
148
+ }
149
+ this.moduleResourceToChunkIds.get(moduleResource).add(chunkIdString);
150
+ }
151
+ } // Store in the map if there are required sections
152
+
153
+ if (requiredSections.size > 0) {
154
+ this.chunkIdToSectionsMap[chunkIdString] = Array.from(requiredSections);
155
+ } // Map chunkId to module resources
156
+
157
+ if (moduleResources.length > 0) {
158
+ this.moduleChunkIdToModulesMap[chunkIdString] = moduleResources;
159
+ }
160
+ } // Collect all used keys
161
+
162
+ this.allUsedKeys = new Set();
163
+ for (const keys of this.moduleToKeysMap.values()) {
164
+ for (const key of keys) {
165
+ this.allUsedKeys.add(key);
166
+ }
167
+ } // Collect entry point keys
168
+
169
+ this.entryPointKeys = new Set();
170
+ for (const moduleResource of entryPointModules) {
171
+ const keys = this.moduleToKeysMap.get(moduleResource);
172
+ if (keys) {
173
+ for (const key of keys) {
174
+ this.entryPointKeys.add(key);
175
+ }
176
+ }
177
+ } // Collect unused keys
178
+
179
+ this.allKeysInProperties = new Set();
180
+ const firstLocale = Object.keys(this.allI18nKeys)[0];
181
+ const sections = this.allI18nKeys[firstLocale];
182
+ for (const section of Object.values(sections)) {
183
+ for (const key of Object.keys(section)) {
184
+ this.allKeysInProperties.add(key);
185
+ }
186
+ }
187
+ this.unusedKeys = new Set([...this.allKeysInProperties].filter(key => !this.allUsedKeys.has(key))); // Remove entry point keys from allUsedKeys
188
+
189
+ this.remainingKeys = new Set([...this.allUsedKeys].filter(key => !this.entryPointKeys.has(key))); // Emit the i18n chunks
190
+
191
+ this.emitI18nChunks(compilation);
192
+ }
193
+ getValueForKeyInLocale(key, locale) {
194
+ const sections = this.allI18nKeys[locale];
195
+ for (const sectionName in sections) {
196
+ const section = sections[sectionName];
197
+ if (key in section) {
198
+ return section[key];
199
+ }
200
+ }
201
+ return undefined;
202
+ }
203
+ emitI18nChunks(compilation) {
204
+ const manifest = {
205
+ moduleToI18nChunkMap: {},
206
+ locales: {}
207
+ }; // Get the list of locales
208
+
209
+ const locales = Object.keys(this.allI18nKeys);
210
+ const firstLocale = locales[0]; // Map from i18nChunkId to keys
211
+
212
+ const i18nChunkIdToKeysMap = {};
213
+ let i18nChunkIdCounter = 0; // **Chunk 0**: Entry Point Keys
214
+
215
+ const entryPointChunkId = i18nChunkIdCounter++;
216
+ i18nChunkIdToKeysMap[entryPointChunkId] = Array.from(this.entryPointKeys);
217
+ for (const key of this.entryPointKeys) {
218
+ this.keyToI18nChunkIdMap[key] = entryPointChunkId;
219
+ } // **Last Chunk**: Unused Keys
220
+
221
+ const unusedKeysChunkId = 'unused'; // Using a string to make it distinct
222
+
223
+ i18nChunkIdToKeysMap[unusedKeysChunkId] = Array.from(this.unusedKeys); // Remaining Keys: Group into chunks of ~30KB
224
+
225
+ let currentChunkKeys = [];
226
+ let currentChunkSize = 0;
227
+ const desiredChunkSize = 1 * 1024; // 30KB
228
+
229
+ for (const key of this.remainingKeys) {
230
+ const value = this.getValueForKeyInLocale(key, firstLocale);
231
+ const keySize = Buffer.byteLength(key, 'utf8') + Buffer.byteLength(value || '', 'utf8');
232
+ if (currentChunkSize + keySize > desiredChunkSize) {
233
+ // Assign current chunk
234
+ const i18nChunkId = i18nChunkIdCounter++;
235
+ i18nChunkIdToKeysMap[i18nChunkId] = currentChunkKeys; // Map keys to i18nChunkId
236
+
237
+ for (const chunkKey of currentChunkKeys) {
238
+ this.keyToI18nChunkIdMap[chunkKey] = i18nChunkId;
239
+ } // Start a new chunk
240
+
241
+ currentChunkKeys = [];
242
+ currentChunkSize = 0;
243
+ }
244
+ currentChunkKeys.push(key);
245
+ currentChunkSize += keySize;
246
+ } // Assign the remaining keys
247
+
248
+ if (currentChunkKeys.length > 0) {
249
+ const i18nChunkId = i18nChunkIdCounter++;
250
+ i18nChunkIdToKeysMap[i18nChunkId] = currentChunkKeys; // Map keys to i18nChunkId
251
+
252
+ for (const chunkKey of currentChunkKeys) {
253
+ this.keyToI18nChunkIdMap[chunkKey] = i18nChunkId;
254
+ }
255
+ } // Now, generate the i18n chunks for each locale
256
+
257
+ for (const locale of locales) {
258
+ manifest.locales[locale] = manifest.locales[locale] || {};
259
+ for (const [i18nChunkId, keys] of Object.entries(i18nChunkIdToKeysMap)) {
260
+ const i18nData = {};
261
+ for (const key of keys) {
262
+ const value = this.getValueForKeyInLocale(key, locale);
263
+ if (value !== undefined) {
264
+ i18nData[key] = value;
265
+ }
266
+ }
267
+ if (Object.keys(i18nData).length > 0) {
268
+ const moduleSource = `window.loadI18nChunk(${JSON.stringify(i18nData, null, 2)});`;
269
+ const filename = `i18n-chunks/${locale}/chunk-${i18nChunkId}.js`;
270
+ compilation.emitAsset(filename, new RawSource(moduleSource));
271
+ manifest.locales[locale][i18nChunkId] = filename;
272
+ console.log(`Emitted i18n chunk: ${filename}`);
273
+ }
274
+ }
275
+ } // Now, create moduleToI18nChunkMap
276
+
277
+ for (const [moduleChunkId, moduleResources] of Object.entries(this.moduleChunkIdToModulesMap)) {
278
+ const requiredI18nChunkIds = new Set();
279
+ for (const moduleResource of moduleResources) {
280
+ const keys = this.moduleToKeysMap.get(moduleResource);
281
+ if (keys) {
282
+ for (const key of keys) {
283
+ const i18nChunkId = this.keyToI18nChunkIdMap[key];
284
+ if (i18nChunkId !== undefined) {
285
+ requiredI18nChunkIds.add(i18nChunkId);
286
+ }
287
+ }
288
+ }
289
+ }
290
+ if (requiredI18nChunkIds.size > 0) {
291
+ manifest.moduleToI18nChunkMap[moduleChunkId] = Array.from(requiredI18nChunkIds);
292
+ } else {
293
+ console.log(`No required i18n chunks for module chunk ID ${moduleChunkId}`);
294
+ }
295
+ } // Ensure that the main chunk always requires chunk zero
296
+
297
+ manifest.moduleToI18nChunkMap[this.entryPointName] = manifest.moduleToI18nChunkMap[this.entryPointName] || [];
298
+ if (!manifest.moduleToI18nChunkMap[this.entryPointName].includes(entryPointChunkId)) {
299
+ manifest.moduleToI18nChunkMap[this.entryPointName].unshift(entryPointChunkId);
300
+ } // Emit the manifest file
301
+
302
+ const manifestSource = new RawSource(`window.i18n = window.i18n || {}; window.i18n.manifest = ${JSON.stringify(manifest, null, 2)};`);
303
+ const manifestFilename = 'i18n-chunks/manifest.js';
304
+ compilation.emitAsset(manifestFilename, manifestSource);
305
+ console.log(`Emitted i18n manifest: ${manifestFilename}`);
306
+ }
307
+ }
308
+ module.exports = I18nPlugin; //section based approch
309
+ // const path = require('path');
310
+ // const fs = require('fs');
311
+ // const {
312
+ // getAllI18nGroupedBySection,
313
+ // getPropertiesAsJSON,
314
+ // } = require('../I18nSplitPlugin/utils/propertiesUtils');
315
+ // const {
316
+ // collectI18nKeysfromAST,
317
+ // collectI18nKeysfromComments,
318
+ // } = require('../I18nSplitPlugin/utils/collectI18nKeys');
319
+ // const NullFactory = require('webpack/lib/NullFactory');
320
+ // const {
321
+ // I18nDependency,
322
+ // I18nDependencyTemplate,
323
+ // } = require('../I18nSplitPlugin/I18nDependency');
324
+ // const webpack = require('webpack');
325
+ // const { RawSource } = require('webpack-sources'); // Import RawSource from webpack-sources
326
+ // const pluginName = 'I18nPlugin';
327
+ // class I18nPlugin {
328
+ // constructor(options = {}) {
329
+ // const {
330
+ // jsResourcePath,
331
+ // mainChunkName,
332
+ // propertiesFolder,
333
+ // disableDefault,
334
+ // jsResource,
335
+ // } = options;
336
+ // if (!jsResourcePath) {
337
+ // throw new Error('I18nPlugin: "jsResourcePath" option is required.');
338
+ // }
339
+ // if (!mainChunkName) {
340
+ // throw new Error('I18nPlugin: "mainChunkName" option is required.');
341
+ // }
342
+ // this.jsResourcePath = jsResourcePath;
343
+ // this.entryPointName = mainChunkName;
344
+ // this.jsResourceI18nKeys = jsResource
345
+ // ? getPropertiesAsJSON(jsResource)
346
+ // : {};
347
+ // // Get all i18n keys grouped by sections
348
+ // this.allI18nKeys = getAllI18nGroupedBySection({
349
+ // folderPath: propertiesFolder,
350
+ // disableDefault,
351
+ // jsResourceI18nKeys: this.jsResourceI18nKeys,
352
+ // });
353
+ // console.log('All locales:', Object.keys(this.allI18nKeys));
354
+ // this.moduleToKeysMap = new Map();
355
+ // this.keyToModulesMap = new Map();
356
+ // this.keyToI18nChunkIdMap = {};
357
+ // }
358
+ // defineI18nDependency(compilation) {
359
+ // compilation.dependencyFactories.set(I18nDependency, new NullFactory());
360
+ // compilation.dependencyTemplates.set(
361
+ // I18nDependency,
362
+ // new I18nDependencyTemplate()
363
+ // );
364
+ // }
365
+ // addI18nDependency(module, i18nKeys) {
366
+ // const identifier = `i18n ${module.resource}`;
367
+ // const dep = new I18nDependency({ identifier, i18nKeys }, module.context, 0);
368
+ // module.addDependency(dep);
369
+ // }
370
+ // apply(compiler) {
371
+ // compiler.hooks.thisCompilation.tap(pluginName, (compilation, params) => {
372
+ // this.defineI18nDependency(compilation);
373
+ // // Handler for parser
374
+ // const handler = (parser) => {
375
+ // parser.hooks.program.tap(pluginName, (ast, comments) => {
376
+ // const { module } = parser.state;
377
+ // if (!(module && /\.jsx?$/.test(module.resource))) {
378
+ // return;
379
+ // }
380
+ // // Collect i18n keys
381
+ // let i18nKeys = collectI18nKeysfromAST(ast, this.jsResourceI18nKeys);
382
+ // let commentKeys = collectI18nKeysfromComments(
383
+ // comments,
384
+ // this.jsResourceI18nKeys
385
+ // );
386
+ // i18nKeys = i18nKeys.concat(commentKeys);
387
+ // if (i18nKeys.length) {
388
+ // // Store the keys for this module
389
+ // this.moduleToKeysMap.set(module.resource, i18nKeys);
390
+ // // Map keys to modules
391
+ // for (const key of i18nKeys) {
392
+ // if (!this.keyToModulesMap.has(key)) {
393
+ // this.keyToModulesMap.set(key, new Set());
394
+ // }
395
+ // this.keyToModulesMap.get(key).add(module.resource);
396
+ // }
397
+ // // Add I18nDependency to the module (optional, if needed)
398
+ // // this.addI18nDependency(module, i18nKeys);
399
+ // }
400
+ // });
401
+ // };
402
+ // // Tap into parser hooks
403
+ // const factory = params.normalModuleFactory;
404
+ // factory.hooks.parser.for('javascript/auto').tap(pluginName, handler);
405
+ // factory.hooks.parser.for('javascript/dynamic').tap(pluginName, handler);
406
+ // // After optimizing chunks, process collected keys
407
+ // compilation.hooks.afterOptimizeChunks.tap(pluginName, () => {
408
+ // this.processCollectedKeys(compilation);
409
+ // });
410
+ // });
411
+ // }
412
+ // processCollectedKeys(compilation) {
413
+ // console.log('Starting processCollectedKeys...');
414
+ // // Initialize mappings
415
+ // this.chunkIdToKeysMap = {};
416
+ // this.moduleChunkIdToModulesMap = {};
417
+ // // Iterate over all chunks in the compilation
418
+ // for (const chunk of compilation.chunks) {
419
+ // const chunkId = chunk.name || chunk.id;
420
+ // const chunkIdString = String(chunkId);
421
+ // console.log(`Processing chunk with ID: ${chunkIdString}`);
422
+ // // Collect all modules in the chunk
423
+ // const modules = chunk.getModules();
424
+ // let chunkKeys = new Set();
425
+ // let moduleResources = [];
426
+ // for (const module of modules) {
427
+ // const moduleResource = module.resource;
428
+ // if (moduleResource) {
429
+ // moduleResources.push(moduleResource);
430
+ // const keys = this.moduleToKeysMap.get(moduleResource);
431
+ // if (keys) {
432
+ // for (const key of keys) {
433
+ // chunkKeys.add(key);
434
+ // }
435
+ // }
436
+ // }
437
+ // }
438
+ // // Store in the map if there are keys
439
+ // chunkKeys = Array.from(chunkKeys);
440
+ // if (chunkKeys.length > 0) {
441
+ // this.chunkIdToKeysMap[chunkIdString] = chunkKeys;
442
+ // }
443
+ // // Map chunkId to module resources
444
+ // if (moduleResources.length > 0) {
445
+ // this.moduleChunkIdToModulesMap[chunkIdString] = moduleResources;
446
+ // }
447
+ // }
448
+ // // Emit the i18n chunks
449
+ // this.emitI18nChunks(compilation);
450
+ // }
451
+ // emitI18nChunks(compilation) {
452
+ // const manifest = {
453
+ // moduleToI18nChunkMap: {},
454
+ // locales: {},
455
+ // };
456
+ // // Get the list of locales
457
+ // const locales = Object.keys(this.allI18nKeys);
458
+ // // Initialize i18nChunkId counter
459
+ // let i18nChunkIdCounter = 0;
460
+ // const sectionToI18nChunkIdMap = {};
461
+ // // Assign each section to an i18n chunk
462
+ // // Assuming all locales have the same sections as the first locale
463
+ // const firstLocale = locales[0];
464
+ // const sections = Object.keys(this.allI18nKeys[firstLocale]);
465
+ // for (const sectionName of sections) {
466
+ // const i18nChunkId = i18nChunkIdCounter++;
467
+ // sectionToI18nChunkIdMap[sectionName] = i18nChunkId;
468
+ // // Collect keys for this section from the first locale
469
+ // const keys = Object.keys(this.allI18nKeys[firstLocale][sectionName] || {});
470
+ // // Map keys to i18nChunkId
471
+ // for (const key of keys) {
472
+ // this.keyToI18nChunkIdMap[key] = i18nChunkId;
473
+ // }
474
+ // // Generate the i18n chunk for each locale
475
+ // for (const locale of locales) {
476
+ // manifest.locales[locale] = manifest.locales[locale] || {};
477
+ // const i18nData = {};
478
+ // const localeSectionKeys = this.allI18nKeys[locale][sectionName] || {};
479
+ // for (const key of keys) {
480
+ // const value = localeSectionKeys[key];
481
+ // if (value !== undefined) {
482
+ // i18nData[key] = value;
483
+ // }
484
+ // }
485
+ // if (Object.keys(i18nData).length > 0) {
486
+ // const moduleSource = `window.loadI18nChunk(${JSON.stringify(i18nData, null, 2)});`;
487
+ // const filename = `i18n-chunks${locale}/${sectionName}chunk-${i18nChunkId}.js`;
488
+ // compilation.emitAsset(filename, new RawSource(moduleSource));
489
+ // manifest.locales[locale][i18nChunkId] = filename;
490
+ // }
491
+ // }
492
+ // }
493
+ // // Now, create moduleToI18nChunkMap
494
+ // for (const [moduleChunkId, moduleResources] of Object.entries(
495
+ // this.moduleChunkIdToModulesMap
496
+ // )) {
497
+ // const requiredI18nChunkIds = new Set();
498
+ // for (const moduleResource of moduleResources) {
499
+ // const keys = this.moduleToKeysMap.get(moduleResource);
500
+ // if (keys) {
501
+ // for (const key of keys) {
502
+ // const i18nChunkId = this.keyToI18nChunkIdMap[key];
503
+ // if (i18nChunkId !== undefined) {
504
+ // requiredI18nChunkIds.add(i18nChunkId);
505
+ // }
506
+ // }
507
+ // }
508
+ // }
509
+ // if (requiredI18nChunkIds.size > 0) {
510
+ // manifest.moduleToI18nChunkMap[moduleChunkId] = Array.from(
511
+ // requiredI18nChunkIds
512
+ // );
513
+ // } else {
514
+ // console.log(
515
+ // `No required i18n chunks for module chunk ID ${moduleChunkId}`
516
+ // );
517
+ // }
518
+ // }
519
+ // // Emit the manifest file
520
+ // const manifestSource = new RawSource(
521
+ // `window.i18n = window.i18n || {}; window.i18n.manifest = ${JSON.stringify(
522
+ // manifest,
523
+ // null,
524
+ // 2
525
+ // )};`
526
+ // );
527
+ // const manifestFilename = 'i18n-chunks/manifest.js';
528
+ // compilation.emitAsset(manifestFilename, manifestSource);
529
+ // console.log(`Emitted i18n manifest: ${manifestFilename}`);
530
+ // }
531
+ // }
532
+ // I18nPlugin.isChunkHasI18n = function (chunk) {
533
+ // for (const module of chunk.modulesIterable) {
534
+ // for (const dep of module.dependencies) {
535
+ // if (dep instanceof I18nDependency) {
536
+ // return true;
537
+ // }
538
+ // }
539
+ // }
540
+ // return false;
541
+ // };
542
+ // module.exports = I18nPlugin;