@zohodesk/react-cli 0.0.1-beta.98 → 0.0.1-betaa.138.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (139) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/README.md +149 -0
  3. package/bin/cli.js +34 -29
  4. package/files/eslintrc.js +57 -0
  5. package/files/prettierrc.js +3 -0
  6. package/lib/babel/cmjs-plugins-presets.js +16 -9
  7. package/lib/babel/es-plugins-presets.js +26 -14
  8. package/lib/common/getEntries.js +31 -22
  9. package/lib/common/getPublicPathConfig.js +27 -13
  10. package/lib/common/index.js +21 -25
  11. package/lib/common/splitChunks.js +62 -53
  12. package/lib/common/templateParameters.js +10 -8
  13. package/lib/configs/jest.config.js +15 -25
  14. package/lib/configs/webpack.component.umd.config.js +31 -37
  15. package/lib/configs/webpack.css.umd.config.js +44 -47
  16. package/lib/configs/webpack.dev.config.js +70 -52
  17. package/lib/configs/webpack.docs.config.js +106 -106
  18. package/lib/configs/webpack.prod.config.js +95 -75
  19. package/lib/hooks/docsProptypeHook.js +32 -38
  20. package/lib/jest/commitedFilesResult.js +103 -74
  21. package/lib/jest/coverageCollector.js +41 -21
  22. package/lib/jest/jsonMaker.js +15 -16
  23. package/lib/jest/preProcessors/cssPreprocessor.js +16 -18
  24. package/lib/jest/preProcessors/jsPreprocessor.js +3 -5
  25. package/lib/jest/preProcessors/otherFilesPreprocessor.js +5 -6
  26. package/lib/jest/result.js +90 -45
  27. package/lib/jest/run.js +43 -43
  28. package/lib/jest/setup.js +102 -95
  29. package/lib/loaderUtils/getCSSLoaders.js +21 -22
  30. package/lib/loaderUtils/getDevJsLoaders.js +25 -23
  31. package/lib/loaderUtils/index.js +11 -13
  32. package/lib/loaders/docsLoader.js +15 -15
  33. package/lib/loaders/docsPropsLoader.js +14 -17
  34. package/lib/loaders/fileLoader.js +33 -35
  35. package/lib/loaders/scriptInstrumentLoader.js +21 -20
  36. package/lib/loaders/workerLoader.js +136 -0
  37. package/lib/middlewares/HMRMiddleware.js +54 -45
  38. package/lib/middlewares/SSTMiddleware.js +8 -5
  39. package/lib/pluginUtils/getDevPlugins.js +114 -52
  40. package/lib/pluginUtils/getDocsPlugins.js +25 -29
  41. package/lib/pluginUtils/getLibraryPlugins.js +8 -10
  42. package/lib/pluginUtils/getProdPlugins.js +163 -74
  43. package/lib/pluginUtils/getServerPlugins.js +8 -11
  44. package/lib/pluginUtils/getUMDCSSPlugins.js +11 -15
  45. package/lib/pluginUtils/getUMDComponentPlugins.js +11 -15
  46. package/lib/pluginUtils/index.js +36 -43
  47. package/lib/plugins/CdnChangePlugin.js +63 -0
  48. package/lib/plugins/CleanupStatsPlugin.js +18 -32
  49. package/lib/plugins/CustomAttributePlugin.js +79 -0
  50. package/lib/plugins/CustomAttributePlugin.md +35 -0
  51. package/lib/plugins/EFCPlugin.js +185 -0
  52. package/lib/plugins/I18NInjectIntoIndexPlugin.js +118 -52
  53. package/lib/plugins/I18nSplitPlugin/I18nDebugPlugin.js +60 -0
  54. package/lib/plugins/I18nSplitPlugin/I18nDependency.js +44 -0
  55. package/lib/plugins/I18nSplitPlugin/I18nDownlodLogic.js +266 -0
  56. package/lib/plugins/I18nSplitPlugin/I18nFilesEmitter.js +183 -0
  57. package/lib/plugins/I18nSplitPlugin/I18nKeysIdentifer.js +100 -0
  58. package/lib/plugins/I18nSplitPlugin/I18nSplit.md +86 -0
  59. package/lib/plugins/I18nSplitPlugin/README.md +25 -0
  60. package/lib/plugins/I18nSplitPlugin/index.js +185 -0
  61. package/lib/plugins/I18nSplitPlugin/utils/collectI18nKeys.js +64 -0
  62. package/lib/plugins/I18nSplitPlugin/utils/getI18nFileUrlPathTemplate.js +13 -0
  63. package/lib/plugins/I18nSplitPlugin/utils/getI18nKeysFormModules.js +26 -0
  64. package/lib/plugins/I18nSplitPlugin/utils/hashUtils.js +40 -0
  65. package/lib/plugins/I18nSplitPlugin/utils/index.js +31 -0
  66. package/lib/plugins/I18nSplitPlugin/utils/propertiesUtils.js +112 -0
  67. package/lib/plugins/ManifestPlugin.js +52 -63
  68. package/lib/plugins/ModuleStatsPlugin.js +83 -173
  69. package/lib/plugins/OptimizeJSPlugin.js +24 -41
  70. package/lib/plugins/PublicPathCallbackPlugin.js +42 -116
  71. package/lib/plugins/PublicPathChangePlugin.js +175 -284
  72. package/lib/plugins/ReportGeneratePlugin.js +150 -151
  73. package/lib/plugins/ResourceHintsPlugin.js +53 -35
  74. package/lib/plugins/ScriptInstrumentPlugin.js +22 -37
  75. package/lib/plugins/ServiceWorkerPlugin.js +81 -57
  76. package/lib/plugins/ShadowDOMSupportPlugin.js +153 -234
  77. package/lib/plugins/SourceMapHookPlugin.js +21 -33
  78. package/lib/plugins/TPHashMappingPlugin.js +67 -0
  79. package/lib/plugins/UglifyCSSPlugin.js +23 -30
  80. package/lib/plugins/UnusedFilesFindPlugin.js +137 -152
  81. package/lib/plugins/index.js +100 -91
  82. package/lib/plugins/webpackwatchrunplugin.js +18 -30
  83. package/lib/postcss-plugins/ExcludeRTLPlugin.js +14 -17
  84. package/lib/schemas/index.js +336 -81
  85. package/lib/servers/clusterHubServer.js +22 -26
  86. package/lib/servers/docsServer.js +3 -5
  87. package/lib/servers/docsServerCore.js +84 -91
  88. package/lib/servers/helpServer.js +19 -21
  89. package/lib/servers/impactServer.js +99 -92
  90. package/lib/servers/mockserver.js +24 -24
  91. package/lib/servers/scrServer.js +78 -98
  92. package/lib/servers/server.js +101 -122
  93. package/lib/servers/ssServer.js +63 -61
  94. package/lib/sh/reportPublish.sh +1 -2
  95. package/lib/templates/CoverageScriptTemplate.js +45 -18
  96. package/lib/templates/WMSTemplate.js +17 -18
  97. package/lib/templates/linterConstant.js +6 -4
  98. package/lib/utils/babelPresets.js +12 -5
  99. package/lib/utils/clean.js +12 -11
  100. package/lib/utils/copy.js +12 -34
  101. package/lib/utils/copyTimezones.js +9 -16
  102. package/lib/utils/createEventStream.js +24 -19
  103. package/lib/utils/cssClassNameGenerate.js +44 -73
  104. package/lib/utils/cssURLReplacer.js +62 -56
  105. package/lib/utils/dependencyPostPublish.js +20 -19
  106. package/lib/utils/fileUtils.js +66 -54
  107. package/lib/utils/folderIterator.js +20 -21
  108. package/lib/utils/getComponents.js +70 -27
  109. package/lib/utils/getCurrentBranch.js +11 -17
  110. package/lib/utils/getDependenciesImpactList.js +114 -187
  111. package/lib/utils/getHash.js +9 -13
  112. package/lib/utils/getIp.js +9 -9
  113. package/lib/utils/getOptions.js +26 -29
  114. package/lib/utils/getServerURL.js +19 -10
  115. package/lib/utils/index.js +147 -126
  116. package/lib/utils/init.js +2 -2
  117. package/lib/utils/initPreCommitHook.js +40 -32
  118. package/lib/utils/jsonHelper.js +37 -21
  119. package/lib/utils/lint/addScripts.js +27 -0
  120. package/lib/utils/lint/checkExistingConfig.js +67 -0
  121. package/lib/utils/lint/copyConfigs.js +24 -0
  122. package/lib/utils/lint/index.js +54 -0
  123. package/lib/utils/lint/lintScripts.js +11 -0
  124. package/lib/utils/lint/lintSetup.js +31 -0
  125. package/lib/utils/lint/lintStagedPreCommitHook.js +7 -0
  126. package/lib/utils/lint/question.js +30 -0
  127. package/lib/utils/lintReporter.js +70 -56
  128. package/lib/utils/mailSender.js +12 -27
  129. package/lib/utils/pullOrigin.js +21 -18
  130. package/lib/utils/reinstallDependencies.js +76 -85
  131. package/lib/utils/removeAttributes.js +25 -23
  132. package/lib/utils/repoClone.js +47 -45
  133. package/lib/utils/request.js +64 -77
  134. package/lib/utils/rtl.js +22 -29
  135. package/lib/utils/setEnvVariables.js +5 -6
  136. package/lib/utils/ssTestHack.js +21 -19
  137. package/lib/utils/switchBranch.js +21 -20
  138. package/lib/utils/urlConcat.js +22 -0
  139. package/package.json +72 -67
@@ -0,0 +1,266 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _webpack = require("webpack");
9
+
10
+ var _I18nKeysIdentifer = _interopRequireDefault(require("./I18nKeysIdentifer"));
11
+
12
+ var _hashUtils = require("./utils/hashUtils");
13
+
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+
16
+ /**
17
+ * this plugin for the download i18n files
18
+ * So, if need to downoad i18n in browser this plugin handle that
19
+ *
20
+ *
21
+ * this plugin works
22
+ * how it works?
23
+ * this plugin hook the requireEnsure method and we write logic for current compiled file download logic
24
+ *
25
+ * hinds:-
26
+ * requireEnsure method was loader for all js chunk's ,
27
+ * which means if we use dynamic import then this function will create script tag for that dynamic resource
28
+ * so every dynamic js chunk will load by this function,
29
+ * So we update this function with when dynamic jschunk call the nour code also execute
30
+ *
31
+ * some thing missing in this definetion
32
+ *
33
+ */
34
+ // import { REGEXP_HASH, REGEXP_CHUNKHASH } from './utils/hashUtils';
35
+ const pluginName = 'DownloadLogicOfI18n'; // const pluginName = 'i18n-plugin';
36
+
37
+ const MODULE_TYPE = 'json/i18n';
38
+
39
+ const ltype = locale => `${MODULE_TYPE}/${locale}`;
40
+
41
+ function hashesWithLength(hashes, length) {
42
+ const shortChunkHashMap = {};
43
+
44
+ for (let key of Object.keys(hashes)) {
45
+ if (typeof hashes[key] === 'string') {
46
+ shortChunkHashMap[key] = hashes.slice(0, length);
47
+ }
48
+ }
49
+
50
+ return shortChunkHashMap;
51
+ }
52
+
53
+ function getContentHashMaxLength(filenameTemplate) {
54
+ let length = 0;
55
+ let matches = filenameTemplate.match(_hashUtils.REGEXP_CONTENTHASH);
56
+
57
+ for (let match of matches) {
58
+ let tem = match.match(/\d+/);
59
+
60
+ if (tem) {
61
+ length = Math.max(parseInt(tem[0]), length);
62
+ } else {
63
+ length = 0;
64
+ break;
65
+ }
66
+ }
67
+
68
+ return length;
69
+ }
70
+
71
+ class I18nDownlodLogic {
72
+ constructor({
73
+ filenameTemplate,
74
+ publicPath,
75
+ localeVarName = 'document.documentElement.lang',
76
+ locales
77
+ }) {
78
+ this.locales = locales;
79
+ this.publicPath = publicPath;
80
+ this.options = {
81
+ filenameTemplate,
82
+ localeVarName
83
+ };
84
+ }
85
+
86
+ getShortContentHashes(chunk, hashLength) {
87
+ let allContenHashes = {};
88
+ const chunkMaps = chunk.getChunkMaps();
89
+ this.locales.forEach(locale => {
90
+ allContenHashes[locale] = chunkMaps.contentHash[ltype(locale)];
91
+ });
92
+
93
+ if (!length) {
94
+ return allContenHashes;
95
+ }
96
+
97
+ let shortContentHashMap = {};
98
+
99
+ for (const locale of this.locales) {
100
+ shortContentHashMap[locale] = hashesWithLength(allContenHashes[locale], hashLength);
101
+ }
102
+
103
+ return shortContentHashMap;
104
+ }
105
+
106
+ getI18nChunkObject(mainChunk) {
107
+ const obj = {};
108
+
109
+ for (const chunk of mainChunk.getAllAsyncChunks()) {
110
+ if (_I18nKeysIdentifer.default.isChunkHasI18n(chunk)) {
111
+ obj[chunk.id] = 1;
112
+ }
113
+ }
114
+
115
+ return obj;
116
+ }
117
+
118
+ addDownloadLogicOfI18nInMainTemplate(mainTemplate) {
119
+ mainTemplate.hooks.localVars.tap(pluginName, (source, mainChunk) => {
120
+ const chunkMap = this.getI18nChunkObject(mainChunk);
121
+
122
+ if (!Object.keys(chunkMap).length) {
123
+ return source;
124
+ }
125
+
126
+ return _webpack.Template.asString([source, '', '// object to store loaded I18N chunks', 'var installedI18nChunks = {', _webpack.Template.indent(mainChunk.ids.map(id => `${JSON.stringify(id)}: 0`).join(',\n')), '};']);
127
+ });
128
+ mainTemplate.hooks.requireEnsure.tap(pluginName, (source, mainChunk, hash) => {
129
+ /**
130
+ * for Information this is tapped in mainTemplate ,
131
+ * So this hooks argument chunk is main chunk means entry chunk mostly.
132
+ * if any chunk has i18n then we must write our download i18n logic,
133
+ */
134
+ const chunkMap = this.getI18nChunkObject(mainChunk);
135
+
136
+ if (!Object.keys(chunkMap).length) {
137
+ return source;
138
+ }
139
+ /**
140
+ * chunkMaps has
141
+ * @property {Object} hash [it has key as chunk id and value as chunkHash ]
142
+ * @property {Object} name [it has key as chunk id and value as chunk name ]
143
+ */
144
+
145
+
146
+ const chunkMaps = mainChunk.getChunkMaps();
147
+ const {
148
+ crossOriginLoading
149
+ } = mainTemplate.outputOptions;
150
+ const {
151
+ filenameTemplate,
152
+ localeVarName = 'document.documentElement.lang'
153
+ } = this.options;
154
+ const i18nSrcPath = mainTemplate.getAssetPath(JSON.stringify(filenameTemplate).replace(/\[locale\]/gi, '"+ locale +"'), // "i18n-chunk/[locale]/[name].[chunkhash].js"
155
+ {
156
+ hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
157
+ hashWithLength: length => `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`,
158
+ chunk: {
159
+ id: '" + chunkId + "',
160
+ hash: `" + ${JSON.stringify(chunkMaps.hash)}[chunkId] + "`,
161
+
162
+ hashWithLength(length) {
163
+ const shortChunkHashMap = hashesWithLength(chunkMaps.hash, length);
164
+ return `" + ${JSON.stringify(shortChunkHashMap)}[chunkId] + "`;
165
+ },
166
+
167
+ // contentHash: contentHashMap,
168
+ // contentHashWithLength: contentHashLengthMap,
169
+ contentHash: {
170
+ [MODULE_TYPE]: '" + contentHash + "'
171
+ },
172
+ contentHashWithLength: {
173
+ [MODULE_TYPE]: length => `" + contentHash.slice(0,${length}) + "`
174
+ },
175
+ name: `" + (${JSON.stringify(chunkMaps.name)}[chunkId]||chunkId) + "`
176
+ },
177
+ contentHashType: MODULE_TYPE
178
+ });
179
+ /** IDEA:
180
+ * as my suggestion ignore this `jsop` approch,
181
+ * Because json parse is faster then js parse.
182
+ * my suggestion is make ajax or fetch request
183
+ */
184
+
185
+ const buf = [];
186
+ /*
187
+ if (REGEXP_HASH.test(filenameTemplate)) {
188
+ buf.push(
189
+ `var hash = ${mainTemplate.renderCurrentHashCode(hash)};`
190
+ );
191
+ }
192
+ if (REGEXP_CHUNKHASH.test(filenameTemplate)) {
193
+ buf.push(`var chunkHashes = ${JSON.stringify(chunkMaps.hash)};`);
194
+ }
195
+ */
196
+
197
+ if ((0, _hashUtils.hasContentHash)(filenameTemplate)) {
198
+ const hashLength = getContentHashMaxLength(filenameTemplate);
199
+ const contentHashes = this.getShortContentHashes(mainChunk, hashLength);
200
+ buf.push(_webpack.Template.asString(['// all i18n files contentHash ', `var i18nContentHashes = ${JSON.stringify(contentHashes)};`, '', 'if (!i18nContentHashes[locale]) {', ' console.warn("this locale is "+locale + " not has support")', ' return;', '}', 'if (!i18nContentHashes[locale][chunkId]) {', ' console.warn("this chunkId is " + chunkId + " does not have \'"+locale+"\' locale chunk")', ' return;', '}']));
201
+ buf.push('var contentHash = i18nContentHashes[locale][chunkId]');
202
+ }
203
+
204
+ const hashUtils = _webpack.Template.asString(buf);
205
+
206
+ const publicPath = this.publicPath ? JSON.stringify(this.publicPath) : `${mainTemplate.requireFn}.p`;
207
+ return _webpack.Template.asString([source, `
208
+ (function() {
209
+ // ${pluginName} I18N loading
210
+ var i18nChunks = ${JSON.stringify(chunkMap)};
211
+ var locale = ${localeVarName};
212
+ // ${pluginName} I18N hasehes
213
+ ${hashUtils}
214
+ if(installedI18nChunks[chunkId]) {
215
+ promises.push(installedI18nChunks[chunkId]);
216
+ } else if(installedI18nChunks[chunkId] !== 0 && i18nChunks[chunkId]) {
217
+ promises.push(installedI18nChunks[chunkId] = new Promise(function(resolve, reject) {
218
+ var srcPath = (${i18nSrcPath});
219
+ ${'' // TODO: I think this is the area for download chunk of i18n we can
220
+ // change mainTemplate.requireFn as our cdn server urls !!!
221
+ // and we can change the i18n download logic here
222
+ }
223
+ var fullsrcPath = ${publicPath} + srcPath;
224
+ var existingLinkTags = document.getElementsByTagName("script");
225
+ for(var i = 0; i < existingLinkTags.length; i++) {
226
+ var tag = existingLinkTags[i];
227
+ var dataSrc = tag.getAttribute("data-src") || tag.getAttribute("src");
228
+ if(dataSrc === srcPath || dataSrc === fullsrcPath){ return resolve();}
229
+ }
230
+ var scriptTag = document.createElement("script");
231
+ scriptTag.onload = resolve;
232
+ scriptTag.onerror = function(event) {
233
+ var request = event && event.target && event.target.src || fullsrcPath;
234
+ var err = new Error("Loading I18N chunk " + chunkId + " failed.\\n(" + request + ")");
235
+ err.code = "I18N_CHUNK_LOAD_FAILED";
236
+ err.request = request;
237
+ delete installedI18nChunks[chunkId];
238
+ scriptTag.parentNode.removeChild(scriptTag);
239
+ reject(err);
240
+ };
241
+ ${mainTemplate.requireFn}.nc && scriptTag.setAttribute("nonce", ${mainTemplate.requireFn}.nc);
242
+ scriptTag.src = fullsrcPath;
243
+ ${crossOriginLoading ? `if (scriptTag.src.indexOf(window.location.origin + '/') !== 0) {
244
+ scriptTag.crossOrigin = ${JSON.stringify(crossOriginLoading)};
245
+ }` : ''}
246
+ document.body.appendChild(scriptTag);
247
+ }).then(function() {
248
+ installedI18nChunks[chunkId] = 0;
249
+ }));
250
+ }
251
+ })()`]);
252
+ });
253
+ }
254
+
255
+ apply(compiler) {
256
+ compiler.hooks.thisCompilation.tap(pluginName, compilation => {
257
+ const {
258
+ mainTemplate
259
+ } = compilation;
260
+ this.addDownloadLogicOfI18nInMainTemplate(mainTemplate);
261
+ });
262
+ }
263
+
264
+ }
265
+
266
+ exports.default = I18nDownlodLogic;
@@ -0,0 +1,183 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _webpackSources = require("webpack-sources");
9
+
10
+ var _utils = require("./utils");
11
+
12
+ var _getI18nKeysFormModules = _interopRequireDefault(require("./utils/getI18nKeysFormModules"));
13
+
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+
16
+ /**
17
+ * this plugin was creating the chunk specific i18n chunk's.
18
+ */
19
+ // this plugin's kick starter is apply method
20
+ const pluginName = 'I18nFilesEmitter';
21
+ const MODULE_TYPE = 'json/i18n';
22
+
23
+ const i18ntype = locale => `${MODULE_TYPE}/${locale}`;
24
+
25
+ class I18nFilesEmitter {
26
+ constructor({
27
+ allI18nObject,
28
+ i18nManifestFileName,
29
+ filenameTemplate,
30
+ locales,
31
+ jsonpFunc
32
+ }) {
33
+ this.locales = locales;
34
+ this.allI18nObject = allI18nObject;
35
+ this.filenameTemplate = filenameTemplate; // this.filenameTemplate = "i18n-chunk/[locale]/[name].js"
36
+
37
+ this.jsonpFunc = jsonpFunc;
38
+ this.prevHashes = {};
39
+ this.i18nManifestFileName = i18nManifestFileName;
40
+ }
41
+
42
+ emitAsset(compilation, filename, source, info) {
43
+ // NOTE: info support available since webpack v4.40.0 https://webpack.js.org/api/stats/#asset-objects
44
+ if (!compilation.emitAsset) {
45
+ compilation.assets[filename] = source;
46
+ } else {
47
+ compilation.emitAsset(filename, source, info);
48
+ }
49
+ } // this method for create object and assign value for i18nkeys
50
+
51
+
52
+ getI18nObjectByLocale(i18nKeys, locale) {
53
+ const data = {};
54
+ i18nKeys.forEach(key => {
55
+ data[key] = this.allI18nObject[locale][key];
56
+ });
57
+ return data;
58
+ }
59
+
60
+ getTemplateString(i18nKeys, locale) {
61
+ let i18nObject = this.getI18nObjectByLocale(i18nKeys, locale);
62
+ return `${this.jsonpFunc}(${(0, _utils.jsonToString)(i18nObject)});`; // return `${this.jsonpFunc}(JSON.parse(${JSON.stringify(jsonToString(i18nObject))}));`;
63
+ }
64
+
65
+ renderI18nLocaleChunk(chunk, locale, i18nKeys) {
66
+ // renderI18nLocaleChunk(chunk, locale, i18nKeys, compilation) {
67
+ const filenameTemplate = this.filenameTemplate.replace(/\[locale\]/g, locale);
68
+ const pathOptions = {
69
+ chunk,
70
+ contentHashType: i18ntype(locale)
71
+ };
72
+ return {
73
+ render: () => new _webpackSources.RawSource(this.getTemplateString(i18nKeys, locale)),
74
+ filenameTemplate,
75
+ pathOptions,
76
+ identifier: `${pluginName}.${chunk.id}.${locale}`,
77
+ hash: chunk.contentHash[i18ntype(locale)]
78
+ };
79
+ }
80
+
81
+ emitI18nFilesForChunk(chunk, compilation) {
82
+ const i18nKeys = (0, _getI18nKeysFormModules.default)(chunk.modulesIterable);
83
+ const manifestForChunk = {
84
+ assets: {},
85
+ hasI18n: false,
86
+ hasChanges: false
87
+ };
88
+
89
+ if (!i18nKeys.length) {
90
+ // why it is not checked as 0
91
+ return manifestForChunk;
92
+ }
93
+
94
+ manifestForChunk.hasI18n = true; // const locales = ['en_US'];
95
+
96
+ this.locales.forEach(locale => {
97
+ const result = this.renderI18nLocaleChunk(chunk, locale, i18nKeys, compilation);
98
+ const {
99
+ filenameTemplate,
100
+ pathOptions,
101
+ render,
102
+ identifier,
103
+ hash
104
+ } = result;
105
+ const filePath = compilation.getPath(filenameTemplate, pathOptions);
106
+ manifestForChunk.assets[locale] = filePath;
107
+
108
+ if (this.prevHashes[identifier] === hash) {
109
+ return;
110
+ }
111
+
112
+ manifestForChunk.hasChanges = true;
113
+ this.prevHashes[identifier] = hash;
114
+ this.emitAsset(compilation, filePath, render(), {
115
+ locale,
116
+ chunkId: chunk.id,
117
+ chunkName: chunk.name
118
+ }); // compilation.assets[filePath] = render();
119
+ });
120
+ return manifestForChunk;
121
+ }
122
+
123
+ emitI18nAssert(compilation) {
124
+ let manifest = {};
125
+ let needEmitManifest = false;
126
+ compilation.chunks.forEach(chunk => {
127
+ const manifestForChunk = this.emitI18nFilesForChunk(chunk, compilation);
128
+
129
+ if (manifestForChunk.hasI18n) {
130
+ manifest[chunk.id] = manifestForChunk.assets;
131
+ needEmitManifest = needEmitManifest || manifestForChunk.hasChanges;
132
+ }
133
+ });
134
+
135
+ if (needEmitManifest) {
136
+ const manifestSource = new _webpackSources.RawSource(JSON.stringify(manifest));
137
+ this.emitAsset(compilation, this.i18nManifestFileName, manifestSource, {
138
+ desc: 'this file is has chunks-locales-assets mapping'
139
+ });
140
+ }
141
+ }
142
+
143
+ apply(compiler) {
144
+ /*
145
+ compiler.hooks.thisCompilation.tap(pluginName, compilation => {
146
+ compilation.mainTemplate.hooks.renderManifest.tap(
147
+ pluginName,
148
+ (result, { chunk }) => {
149
+ this.renderChunk(result, compilation, chunk);
150
+ }
151
+ );
152
+ compilation.chunkTemplate.hooks.renderManifest.tap(
153
+ pluginName,
154
+ (result, { chunk }) => {
155
+ this.renderChunk(result, compilation, chunk);
156
+ }
157
+ );
158
+ });
159
+ */
160
+ // this below hook was tapped for create asssets(file) for chunk specific i18nChunk Files
161
+ compiler.hooks.emit.tap(pluginName, compilation => {
162
+ this.emitI18nAssert(compilation);
163
+ });
164
+ }
165
+ /*
166
+ renderChunk(result, compilation, chunk) {
167
+ const i18nKeys = getI18nKeysFormModules(chunk.modulesIterable);
168
+ // TODO: need to discuss with vimal ji can we chose this place for add i18n locale files ????
169
+ // IDEA: we chose this place for add i18n locale files ????
170
+ if (i18nKeys.length > 0) {
171
+ Object.keys(this.allI18nObject).forEach(locale => {
172
+ result.push(
173
+ this.renderI18nLocaleChunk(chunk, locale, i18nKeys, compilation)
174
+ );
175
+ });
176
+ }
177
+ }
178
+ */
179
+
180
+
181
+ }
182
+
183
+ exports.default = I18nFilesEmitter;
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _utils = require("./utils");
9
+
10
+ var _I18nDependency = require("./I18nDependency");
11
+
12
+ var _NullFactory = _interopRequireDefault(require("webpack/lib/NullFactory"));
13
+
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+
16
+ const pluginName = 'I18nKeysIdentifer';
17
+ /**
18
+ * this plugin search the i18 keys and make it as I18nDependency.
19
+ * how it works?
20
+ * this plugin get jsResourceI18nKeys in constructor.
21
+ * we walk throught the js files AST
22
+ * if any string literal in that AST is in jsResourceI18nKeys then we consider that string as I18n key
23
+ * after the walk of AST we get some I18n keys
24
+ * then we make it as I18nDependency.
25
+ */
26
+
27
+ class I18nKeysIdentifer {
28
+ constructor(jsResourceI18nKeys) {
29
+ this.jsResourceI18nKeys = jsResourceI18nKeys;
30
+ }
31
+
32
+ defineConfigarationForI18nDependency(compilation) {
33
+ compilation.dependencyFactories.set(_I18nDependency.I18nDependency, new _NullFactory.default());
34
+ compilation.dependencyTemplates.set(_I18nDependency.I18nDependency, new _I18nDependency.I18nDependencyTemplate());
35
+ }
36
+
37
+ addI18nDependency(module, i18nKeys) {
38
+ const identifier = `i18n ${module.resource}`;
39
+ const dep = new _I18nDependency.I18nDependency({
40
+ identifier,
41
+ i18nKeys
42
+ }, module.context, 0);
43
+ module.addDependency(dep); // console.log('module', i18nKeys, identifier);
44
+ }
45
+
46
+ apply(compiler) {
47
+ compiler.hooks.thisCompilation.tap(pluginName, (compilation, params) => {
48
+ // this below hook was tapped for we use I18nDependency
49
+ // so we must define module factory for I18nDependency
50
+ // and we must define how it import statement look like I18nDependencyTemplate
51
+ this.defineConfigarationForI18nDependency(compilation); // handler for parser
52
+
53
+ let handler = parser => {
54
+ parser.hooks.program.tap(pluginName, (ast, commmets) => {
55
+ const {
56
+ module
57
+ } = parser.state; //
58
+
59
+ if (!(module && /\.jsx?$/.test(module.resource))) {
60
+ return;
61
+ } // i18n grep proper contract regex
62
+
63
+
64
+ let i18nKeys = (0, _utils.collectI18nKeysfromAST)(ast, this.jsResourceI18nKeys);
65
+ i18nKeys = i18nKeys.concat( // i18n grep proper contract regex
66
+ (0, _utils.collectI18nKeysfromComments)(commmets, this.jsResourceI18nKeys));
67
+
68
+ if (i18nKeys.length) {
69
+ // console.log(
70
+ // 'i18nKeys.length',
71
+ // module.resource,
72
+ // i18nKeys.length < 5 ? i18nKeys : i18nKeys.length
73
+ // );
74
+ this.addI18nDependency(module, i18nKeys);
75
+ }
76
+ });
77
+ }; // this below hook was tapped for collect I18n Key from files and add as Deppenensy
78
+
79
+
80
+ const factory = params.normalModuleFactory;
81
+ factory.hooks.parser.for('javascript/auto').tap(pluginName, handler);
82
+ factory.hooks.parser.for('javascript/dynamic').tap(pluginName, handler);
83
+ });
84
+ }
85
+
86
+ }
87
+
88
+ exports.default = I18nKeysIdentifer;
89
+
90
+ I18nKeysIdentifer.isChunkHasI18n = function (chunk) {
91
+ for (const module of chunk.modulesIterable) {
92
+ for (const dep of module.dependencies) {
93
+ if (dep instanceof _I18nDependency.I18nDependency) {
94
+ return true;
95
+ }
96
+ }
97
+ }
98
+
99
+ return false;
100
+ };
@@ -0,0 +1,86 @@
1
+ # I18n Split As Chunks Plugin
2
+
3
+ generaly we have manage our all I18n keys and values as language specific i18n files and then we download all i18n in initial page load.
4
+
5
+ ### what is the problem with this?.
6
+ the problem is i18n file keep get's grown it will affect the the inital page load and critial rendering path.
7
+ So, We have decide to create a plugin for split i18n per chunk's of js vise.
8
+
9
+ ### what is i18n split?
10
+ it is like code split for i18n.
11
+ we will collect i18n from js chunk and we will create separate i18n chunk per js chunk,
12
+ with this we already load Js chunks are on demand and now we also download i18n also ondemand with this the initial and forthere actions.
13
+ when js download's we will download i18n with it.
14
+
15
+ ### How to we going to i18n split?
16
+ we will read the js chunk and collect I18n keys form it,
17
+ then we will create i18n chunk files with these used keys.
18
+
19
+ ### how do you collect i18n keys from js files?
20
+ we will traverse the js file's AST (static analysis) and find i18 keys,
21
+ we will concider all string which is in jsResource file as i18n keys.
22
+ and you can tell dynamic i18n key by js comments,
23
+ Like Below:-
24
+ ```js
25
+ fetch("tickets?view=view1").then(data => {
26
+ // I18n support.ticketsEmpty
27
+ // I18n support.viewNotFount
28
+ // I18n support.permissionDenied
29
+ let text = getI18nValue(data.i18nkey);
30
+ })
31
+ ```
32
+
33
+ ### is there any posiblity for unwanted keys in some chunk?
34
+ Yes, there is two posiblity
35
+ 1. js comment , if you write js comment but it is not needed then it will be add into i18n chunk
36
+ `To Resolve` we currently do not have perfect idea for this, But we can check this by chunk size limit.
37
+ `idea's will be welcome`
38
+ 2. like we said "we will concider all string which is in jsResource file as i18n keys",
39
+ So if you use string not for i18n but it was in jsResource then it will be add into i18n chunk.
40
+ `To Resolve` this problem we can use some kinda prefix or something.
41
+
42
+ ### how do we downlowd and give to app?
43
+ we have over write defualt webpack requireEnsure (every dynamic chunk requests are done by that function) function.
44
+ and we parlarly dowload i18n files. and we ask jsonFunction, to our plugin.
45
+ So we send i18nkeys , when it was download,
46
+ you must store all i18n keys, we give asycrnsly by that function download in that patticular.
47
+ so you must store and update (like append or assign) everytime that function call.
48
+
49
+ ### how to use this feature?
50
+ to use this feature use have give the below oprtions
51
+ `package.json`
52
+ ```json
53
+ {
54
+ /// ...some things
55
+ "react-cli": {
56
+ // ...some things
57
+ "i18n": {
58
+ "chunkSplitEnable": true,
59
+ "jsResource": "./deskapp/properties/JSResources.properties",
60
+ "localeVarName": "window.userInfo.langCode",
61
+ "jsonpFunc": "window.loadI18nChunk",
62
+ "templateLabel": "{{--user-locale}}",// this is for html template i18n file path locate template
63
+ "propertiesFolder": "./deskapp/properties"
64
+ },
65
+ // ...some things
66
+ }
67
+ }
68
+ ```
69
+
70
+
71
+ <!-- not need below -->
72
+ <!--
73
+
74
+ ### is there any problems with static analysis?
75
+ Yes,
76
+
77
+ ### how do we use dynamic i18n key?
78
+
79
+ and we will download i18n file with
80
+
81
+ and when js chunk download's then we will download i18n as need.
82
+ we all know in our app we loading i18n as one single big file for each locale(language).
83
+ we are going to split i18n as per js chunk, and when js chunk download's then we will download i18n as need.
84
+
85
+
86
+ -->
@@ -0,0 +1,25 @@
1
+ # I18n Split As Chunks Plugin
2
+
3
+ generaly we have manage our all I18n keys and values as language specific i18n files and then we download all i18n in initial page load.
4
+
5
+ Ok, what is the problem with this?.
6
+ the problem is i18n file keep get's grown it will affect the the inital page load and critial rendering path.
7
+ So, We have decide to create a plugin for split i18n per chunk's of js vise, Which mean we diside to create for each js chunk has its own i18n chunks, with this we already load Js chunks are on demand and now we also download i18n also ondemand with this the initial and forthere actions.
8
+
9
+ Ok, How to you do this thing?
10
+ we have read the js chunk and collect I18n keys what are used in that chunk and we will create i18n chunk files with only used key.
11
+
12
+ Ok, How do you collect that keys?
13
+ we now have two expremental statrge to collect I18n keys.
14
+
15
+ 1. we read jsResource.properties (or all keys I18n file) then we walk this AST of js and we collect all string which is jsResource.properties.
16
+ 2. for dynamic we discide to use comment some thing like
17
+ `// I18n support.tickekt.email.notvalid`
18
+
19
+ ok, how do you downlowd and give to app?
20
+ we have over write defualt webpack require ensure function.
21
+ So every dynamic chunk requset are done by that function.
22
+ So we parlarly dowload i18n files. and we ask jsonFunction, to our plugin.
23
+ So we send i18nkeys , when it was download,
24
+ you must store all i18n keys, we give asycrnsly by that function download in that patticular.
25
+ so you must store and update (like append or assign) everytime that function call.