@ui5/webcomponents-tools 0.0.0-4e00e5559 → 0.0.0-51202adee

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 (87) hide show
  1. package/CHANGELOG.md +332 -0
  2. package/README.md +5 -6
  3. package/assets-meta.js +18 -1
  4. package/bin/dev.js +12 -1
  5. package/components-package/eslint.js +28 -0
  6. package/components-package/nps.js +95 -38
  7. package/components-package/postcss.themes.js +6 -3
  8. package/components-package/vite.config.js +12 -0
  9. package/components-package/wdio.js +127 -31
  10. package/components-package/wdio.sync.js +360 -0
  11. package/icons-collection/nps.js +48 -9
  12. package/lib/copy-and-watch/index.js +145 -0
  13. package/lib/copy-list/index.js +28 -0
  14. package/lib/create-icons/index.js +101 -54
  15. package/lib/create-illustrations/index.js +161 -0
  16. package/lib/create-new-component/index.js +21 -9
  17. package/lib/dev-server/dev-server.js +66 -0
  18. package/lib/dev-server/virtual-index-html-plugin.js +53 -0
  19. package/lib/esm-abs-to-rel/index.js +58 -0
  20. package/lib/generate-custom-elements-manifest/index.js +373 -0
  21. package/lib/generate-js-imports/illustrations.js +72 -0
  22. package/lib/generate-json-imports/i18n.js +75 -30
  23. package/lib/generate-json-imports/themes.js +65 -18
  24. package/lib/hbs2lit/index.js +2 -4
  25. package/lib/hbs2lit/src/compiler.js +26 -8
  26. package/lib/hbs2lit/src/includesReplacer.js +23 -17
  27. package/lib/hbs2lit/src/litVisitor2.js +90 -16
  28. package/lib/hbs2lit/src/svgProcessor.js +3 -3
  29. package/lib/hbs2ui5/RenderTemplates/LitRenderer.js +5 -5
  30. package/lib/hbs2ui5/index.js +52 -30
  31. package/lib/i18n/defaults.js +58 -57
  32. package/lib/i18n/toJSON.js +13 -12
  33. package/lib/jsdoc/config.json +1 -1
  34. package/lib/jsdoc/configTypescript.json +29 -0
  35. package/lib/jsdoc/plugin.js +65 -4
  36. package/lib/jsdoc/preprocess.js +146 -0
  37. package/lib/jsdoc/template/publish.js +32 -4
  38. package/lib/postcss-combine-duplicated-selectors/index.js +178 -0
  39. package/lib/postcss-css-to-esm/index.js +33 -18
  40. package/lib/postcss-css-to-json/index.js +31 -11
  41. package/lib/postcss-new-files/index.js +36 -0
  42. package/lib/postcss-p/postcss-p.mjs +14 -0
  43. package/lib/replace-global-core/index.js +25 -0
  44. package/lib/scoping/get-all-tags.js +37 -0
  45. package/lib/scoping/lint-src.js +31 -0
  46. package/lib/scoping/missing-dependencies.js +65 -0
  47. package/lib/scoping/report-tags-usage.js +28 -0
  48. package/lib/scoping/scope-test-pages.js +40 -0
  49. package/lib/test-runner/test-runner.js +63 -0
  50. package/package.json +48 -54
  51. package/bin/init-ui5-package.js +0 -3
  52. package/components-package/rollup.js +0 -134
  53. package/components-package/serve.json +0 -3
  54. package/lib/documentation/index.js +0 -143
  55. package/lib/documentation/templates/api-component-since.js +0 -3
  56. package/lib/documentation/templates/api-css-variables-section.js +0 -24
  57. package/lib/documentation/templates/api-events-section.js +0 -35
  58. package/lib/documentation/templates/api-methods-section.js +0 -26
  59. package/lib/documentation/templates/api-properties-section.js +0 -40
  60. package/lib/documentation/templates/api-slots-section.js +0 -28
  61. package/lib/documentation/templates/template.js +0 -38
  62. package/lib/init-package/index.js +0 -119
  63. package/lib/init-package/resources/.eslintignore +0 -3
  64. package/lib/init-package/resources/bundle.es5.js +0 -25
  65. package/lib/init-package/resources/bundle.esm.js +0 -34
  66. package/lib/init-package/resources/config/.eslintrc.js +0 -1
  67. package/lib/init-package/resources/config/postcss.components/postcss.config.js +0 -1
  68. package/lib/init-package/resources/config/postcss.themes/postcss.config.js +0 -1
  69. package/lib/init-package/resources/config/rollup.config.js +0 -1
  70. package/lib/init-package/resources/config/wdio.conf.js +0 -1
  71. package/lib/init-package/resources/package-scripts.js +0 -11
  72. package/lib/init-package/resources/src/Assets.js +0 -5
  73. package/lib/init-package/resources/src/Demo.hbs +0 -1
  74. package/lib/init-package/resources/src/Demo.js +0 -56
  75. package/lib/init-package/resources/src/i18n/messagebundle.properties +0 -2
  76. package/lib/init-package/resources/src/i18n/messagebundle_de.properties +0 -1
  77. package/lib/init-package/resources/src/i18n/messagebundle_en.properties +0 -1
  78. package/lib/init-package/resources/src/i18n/messagebundle_es.properties +0 -1
  79. package/lib/init-package/resources/src/i18n/messagebundle_fr.properties +0 -1
  80. package/lib/init-package/resources/src/themes/Demo.css +0 -11
  81. package/lib/init-package/resources/src/themes/sap_belize/parameters-bundle.css +0 -3
  82. package/lib/init-package/resources/src/themes/sap_belize_hcb/parameters-bundle.css +0 -3
  83. package/lib/init-package/resources/src/themes/sap_belize_hcw/parameters-bundle.css +0 -3
  84. package/lib/init-package/resources/src/themes/sap_fiori_3/parameters-bundle.css +0 -3
  85. package/lib/init-package/resources/src/themes/sap_fiori_3_dark/parameters-bundle.css +0 -3
  86. package/lib/init-package/resources/test/pages/index.html +0 -51
  87. package/lib/init-package/resources/test/specs/Demo.spec.js +0 -12
@@ -0,0 +1,53 @@
1
+ let path = require("path");
2
+
3
+ const virtualIndexPlugin = async () => {
4
+ const { globby } = await import("globby");
5
+ const files = await globby(["test/pages/**/*.html", "packages/*/test/pages/**/*.html"]);
6
+
7
+ const pagesPerFolder = {};
8
+ files.forEach(file => {
9
+ let folder = pagesPerFolder[path.dirname(file)] = pagesPerFolder[path.dirname(file)] || [];
10
+ folder.push(path.basename(file));
11
+ });
12
+
13
+ const rollupInput = {};
14
+
15
+ files.forEach(file => {
16
+ rollupInput[file] = path.resolve(process.cwd(), file);
17
+ })
18
+
19
+ return {
20
+ name: 'virtual-index-html',
21
+ config() {
22
+ return {
23
+ build: {
24
+ rollupOptions: {
25
+ input: rollupInput
26
+ }
27
+ }
28
+ }
29
+ },
30
+ configureServer(server) {
31
+ server.middlewares.use((req, res, next) => {
32
+ if (req.url === "/") {
33
+ const folders = Object.keys(pagesPerFolder);
34
+
35
+ res.statusCode = 200;
36
+ res.setHeader("Content-Type", "text/html; charset=utf-8");
37
+ res.end(`${folders.map(folder => {
38
+ const pages = pagesPerFolder[folder];
39
+ return `<h1>${folder}</h1>
40
+ ${pages.map(page => {
41
+ return `<li><a href='${folder}/${page}'>${page}</a></li>`
42
+ }).join("")}
43
+ `
44
+ }).join("")}`);
45
+ } else {
46
+ next();
47
+ }
48
+ })
49
+ },
50
+ }
51
+ };
52
+
53
+ module.exports = virtualIndexPlugin;
@@ -0,0 +1,58 @@
1
+ const esprima = require("esprima");
2
+ const escodegen = require("escodegen");
3
+
4
+ const fs = require("fs").promises;
5
+ const path = require("path");
6
+ const basePath = process.argv[2];
7
+
8
+ const convertImports = async (srcPath) => {
9
+ let changed = false;
10
+ // console.log("scanning imports of", srcPath);
11
+ let code = (await fs.readFile(srcPath)).toString();
12
+ const tree = esprima.parseModule(code);
13
+ const importer = srcPath.replace(basePath, "");
14
+ const importerDir = path.dirname(importer);
15
+ // console.log("-> ", importer);
16
+ tree.body.forEach(node => {
17
+ if (node.type === "ImportDeclaration") {
18
+ let importee = node.source.value;
19
+ if (importee.startsWith(".")) {
20
+ // add .js extension if missing
21
+ if (!importee.endsWith(".js")) {
22
+ node.source.value += ".js"
23
+ changed = true;
24
+ }
25
+ return;
26
+ }
27
+ let importeeDir = path.dirname(importee);
28
+ let importeeFile = path.basename(importee);
29
+ let relativePath = path.relative(importerDir, importeeDir);
30
+ if (relativePath.length === 0) {
31
+ relativePath = "."
32
+ }
33
+ if (!relativePath.startsWith(".")) {
34
+ relativePath = "./" + relativePath;
35
+ }
36
+
37
+ relativePath = relativePath.replace(/\\/g, "/"); // the browser expects unix paths
38
+ let relativeImport = `${relativePath}/${importeeFile}.js`;
39
+ // console.log(importee + " --> " + relativeImport);
40
+ node.source.value = relativeImport;
41
+ changed = true;
42
+ }
43
+ });
44
+
45
+ if (changed) {
46
+ return fs.writeFile(srcPath, escodegen.generate(tree));
47
+ }
48
+ }
49
+
50
+ const generate = async () => {
51
+ const { globby } = await import("globby");
52
+ const fileNames = await globby(basePath + "**/*.js");
53
+ return Promise.all(fileNames.map(convertImports).filter(x => !!x));
54
+ };
55
+
56
+ generate().then(() => {
57
+ console.log("Success: Converted absolute imports to relative for files in:", basePath);
58
+ });
@@ -0,0 +1,373 @@
1
+ const fs = require("fs").promises;
2
+ // https://github.com/webcomponents/custom-elements-manifest/blob/main/schema.json
3
+
4
+ const camelToKebabMap = new Map();
5
+ const apiIndex = new Map();
6
+ const processedApiIndex = new Set();
7
+ const forbiddenAttributeTypes = ["object", "array"];
8
+
9
+ const camelToKebabCase = string => {
10
+ if (!camelToKebabMap.has(string)) {
11
+ const result = string.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
12
+ camelToKebabMap.set(string, result);
13
+ }
14
+ return camelToKebabMap.get(string);
15
+ };
16
+
17
+ const generateJavaScriptExport = entity => {
18
+ return {
19
+ declaration: generateRefenrece(entity),
20
+ deprecated: !!entity.deprecated,
21
+ kind: "js",
22
+ name: "default",
23
+ };
24
+ };
25
+
26
+ const generateCustomElementExport = entity => {
27
+ return {
28
+ declaration: {
29
+ name: entity.basename,
30
+ module: `${entity.module}.js`,
31
+ },
32
+ deprecated: !!entity.deprecated,
33
+ kind: "custom-element-definition",
34
+ name: entity.tagname,
35
+ };
36
+ };
37
+
38
+ const generateJavaScriptModule = entity => {
39
+ return {
40
+ kind: "javascript-module",
41
+ path: `${entity.basename}.js`,
42
+ declarations: [
43
+ generateCustomElementDeclaration(entity),
44
+ ],
45
+ exports: [
46
+ generateJavaScriptExport(entity),
47
+ generateCustomElementExport(entity),
48
+ ],
49
+ };
50
+ };
51
+
52
+ const generateSingleClassField = classField => {
53
+ let generatedClassField = {
54
+ deprecated: !!classField.deprecated,
55
+ kind: "field",
56
+ name: classField.name,
57
+ privacy: classField.visibility,
58
+ static: !!classField.static,
59
+ type: generateType(classField.type),
60
+ };
61
+
62
+ if (classField.defaultValue) {
63
+ generatedClassField.default = classField.defaultValue;
64
+ }
65
+
66
+ if (classField.description) {
67
+ generatedClassField.description = classField.description;
68
+ }
69
+
70
+ return generatedClassField;
71
+ };
72
+
73
+ const generateSingleParameter = parameter => {
74
+ let generatedParameter = {
75
+ deprecated: !!parameter.deprecated,
76
+ name: parameter.name,
77
+ type: generateType(parameter.type),
78
+ };
79
+
80
+ if (parameter.description) {
81
+ generatedParameter.description = parameter.description;
82
+ }
83
+
84
+ if (parameter.optional) {
85
+ generatedParameter.optional = parameter.optional;
86
+ }
87
+
88
+ return generatedParameter;
89
+ };
90
+
91
+ const generateParameters = (parameters) => {
92
+ return parameters.reduce((newParametersArray, parameter) => {
93
+ newParametersArray.push(generateSingleParameter(parameter));
94
+
95
+ return newParametersArray;
96
+ }, []);
97
+ };
98
+
99
+ const generateSingleClassMethod = classMethod => {
100
+ let generatedClassMethod = {
101
+ deprecated: !!classMethod.deprecated,
102
+ kind: "method",
103
+ name: classMethod.name,
104
+ privacy: classMethod.visibility,
105
+ static: classMethod.static,
106
+ };
107
+
108
+ if (classMethod.description) {
109
+ generatedClassMethod.description = classMethod.description;
110
+ }
111
+
112
+ if (classMethod.parameters && classMethod.parameters.length) {
113
+ generatedClassMethod.parameters = generateParameters(classMethod.parameters);
114
+ }
115
+
116
+ if (classMethod.returnValue) {
117
+ generatedClassMethod.return = {
118
+ type: generateType(classMethod.returnValue.type),
119
+ };
120
+
121
+ if (classMethod.returnValue.description) {
122
+ generatedClassMethod.return.description = classMethod.returnValue.type;
123
+ }
124
+ }
125
+
126
+ return generatedClassMethod;
127
+ };
128
+
129
+ const generateClassFields = classFields => {
130
+ return classFields.reduce((newClassFieldsArray, classField) => {
131
+ newClassFieldsArray.push(generateSingleClassField(classField));
132
+
133
+ return newClassFieldsArray;
134
+ }, []);
135
+ };
136
+
137
+ const generateClassMethods = classMethods => {
138
+ return classMethods.reduce((newClassMethodsArray, classMethod) => {
139
+ newClassMethodsArray.push(generateSingleClassMethod(classMethod));
140
+
141
+ return newClassMethodsArray;
142
+ }, []);
143
+ };
144
+
145
+ const generateMembers = (classFields, classMethods) => {
146
+ return [...generateClassFields(classFields), ...generateClassMethods(classMethods)];
147
+ };
148
+
149
+ const generateType = type => {
150
+ const dataType = apiIndex.get(type);
151
+
152
+ return {
153
+ text: dataType && dataType.name.includes(".types.") ?
154
+ filterPublicApi(dataType.properties)
155
+ .map(prop => `"${prop.name}"`)
156
+ .join(" | ") : type,
157
+ };
158
+ };
159
+
160
+ const generateSingleAttribute = attribute => {
161
+ let generatedAttribute = {
162
+ default: attribute.defaultValue,
163
+ deprecated: !!attribute.deprecated,
164
+ fieldName: attribute.name,
165
+ name: camelToKebabCase(attribute.name),
166
+ type: generateType(attribute.type),
167
+ };
168
+
169
+ if (attribute.description) {
170
+ generatedAttribute.description = attribute.description;
171
+ }
172
+
173
+ return generatedAttribute;
174
+ };
175
+
176
+ const generateAttributes = attributes => {
177
+ attributes = attributes.reduce((newAttributesArray, attribute) => {
178
+ newAttributesArray.push(generateSingleAttribute(attribute));
179
+
180
+ return newAttributesArray;
181
+ }, []);
182
+
183
+ return attributes;
184
+ };
185
+
186
+ const generateSingleEvent = event => {
187
+ let generatedEvent = {
188
+ deprecated: !!event.deprecated,
189
+ name: event.name,
190
+ type: event.native === "true" ? "NativeEvent" : "CustomEvent",
191
+ };
192
+
193
+ if (event.description) {
194
+ generatedEvent.description = event.description;
195
+ }
196
+
197
+ return generatedEvent;
198
+ };
199
+
200
+ const generateEvents = events => {
201
+ events = events.reduce((newEventsArray, event) => {
202
+ newEventsArray.push(generateSingleEvent(event));
203
+
204
+ return newEventsArray;
205
+ }, []);
206
+
207
+ return events;
208
+ };
209
+
210
+ const generateSingleSlot = slot => {
211
+ return {
212
+ deprecated: !!slot.deprecated,
213
+ description: slot.description,
214
+ name: slot.name,
215
+ };
216
+ };
217
+
218
+ const generateSlots = slots => {
219
+ slots = slots.reduce((newSlotsArray, event) => {
220
+ newSlotsArray.push(generateSingleSlot(event));
221
+
222
+ return newSlotsArray;
223
+ }, []);
224
+
225
+ return slots;
226
+ };
227
+
228
+ const generateCustomElementDeclaration = entity => {
229
+ entity = generateFullComponentApi(entity);
230
+
231
+ let generatedCustomElementDeclaration = {
232
+ deprecated: !!entity.deprecated,
233
+ customElement: true,
234
+ kind: entity.basename,
235
+ name: entity.basename,
236
+ tagName: entity.tagname,
237
+ };
238
+
239
+ const slots = filterPublicApi(entity.slots);
240
+ const events = filterPublicApi(entity.events);
241
+ const classFields = filterPublicApi(entity.properties);
242
+ const classMethods = filterPublicApi(entity.methods);
243
+ const attributes = classFields.filter(property => {
244
+ return property.noattribute !== "true" && property.readonly !== "true" && !forbiddenAttributeTypes.includes(property.type.toLowerCase());
245
+ });
246
+
247
+ if (slots.length) {
248
+ generatedCustomElementDeclaration.slots = generateSlots(slots);
249
+ }
250
+
251
+ if (events.length) {
252
+ generatedCustomElementDeclaration.events = generateEvents(events);
253
+ }
254
+
255
+ if (attributes.length) {
256
+ generatedCustomElementDeclaration.attributes = generateAttributes(attributes);
257
+ }
258
+
259
+ if (entity.description) {
260
+ generatedCustomElementDeclaration.description = entity.description;
261
+ }
262
+
263
+ if (classFields.length || classMethods.length) {
264
+ generatedCustomElementDeclaration.members = generateMembers(classFields, classMethods);
265
+ }
266
+
267
+ if (entity.extends && entity.extends !== "HTMLElement") {
268
+ generatedCustomElementDeclaration.superclass = generateRefenrece(apiIndex.get(entity.extends));
269
+ }
270
+
271
+ return generatedCustomElementDeclaration;
272
+ };
273
+
274
+ const generateRefenrece = (entity) => {
275
+ let packageName;
276
+
277
+ if (!entity.name) {
278
+ throw new Error("JSDoc error: entity not found in api.json.");
279
+ }
280
+
281
+ if (entity.name.includes("sap.ui.webc.main")) {
282
+ packageName = "@ui5/webcomponents";
283
+ } else if (entity.name.includes("sap.ui.webc.fiori")) {
284
+ packageName = "@ui5/webcomponents-fiori";
285
+ } else if (entity.name.includes("sap.ui.webc.base")) {
286
+ packageName = "@ui5/webcomponents-base";
287
+ }
288
+
289
+ return {
290
+ module: `${entity.module}.js`,
291
+ name: `${entity.basename}`,
292
+ package: packageName,
293
+ };
294
+ };
295
+
296
+ const generateFullComponentApi = entity => {
297
+ const componentProps = ["properties", "slots", "events", "methods"];
298
+ let parent = apiIndex.get(entity.extends);
299
+
300
+ if (!parent) {
301
+ processedApiIndex.add(entity.name);
302
+
303
+ return entity;
304
+ }
305
+
306
+ parent = processedApiIndex.has(entity.extends) ? apiIndex.get(entity.extends) : generateFullComponentApi(parent);
307
+
308
+ componentProps.forEach(prop => {
309
+ if (parent[prop] && parent[prop].length) {
310
+ if (entity[prop] && entity[prop].length) {
311
+ const uniqueParentState = parent[prop].filter(pSlot => {
312
+ return !entity[prop].some(eSlot => eSlot.name === pSlot.name);
313
+ });
314
+
315
+ entity[prop] = entity[prop].concat(uniqueParentState);
316
+ } else {
317
+ entity[prop] = [...parent[prop]];
318
+ }
319
+ }
320
+ });
321
+
322
+ processedApiIndex.add(entity.name);
323
+
324
+ return entity;
325
+ };
326
+
327
+ const filterPublicApi = array => {
328
+ return (array || []).filter(el => el.visibility === "public");
329
+ };
330
+
331
+ const generate = async () => {
332
+ const apiFilesPaths = [
333
+ require.resolve("@ui5/webcomponents-base/dist/api.json"),
334
+ require.resolve("@ui5/webcomponents/dist/api.json"),
335
+ require.resolve("@ui5/webcomponents-fiori/dist/api.json"),
336
+ ];
337
+
338
+ let apiFiles = new Map();
339
+
340
+ await Promise.all(apiFilesPaths.map(async (apiFilePath) => {
341
+ const file = JSON.parse(await fs.readFile(apiFilePath));
342
+
343
+ apiFiles.set(apiFilePath, file);
344
+
345
+ file.symbols.forEach(symbol => {
346
+ apiIndex.set(symbol.name, symbol);
347
+ });
348
+ }));
349
+
350
+ await Promise.all(apiFilesPaths.map(async (apiFilePath) => {
351
+ if (apiFilePath.includes("base")) {
352
+ return;
353
+ }
354
+
355
+ let customElementsManifest = {
356
+ schemaVersion: "1.0.0",
357
+ readme: "",
358
+ modules: [],
359
+ };
360
+
361
+ apiFiles.get(apiFilePath).symbols.forEach(entity => {
362
+ if (entity.tagname) {
363
+ customElementsManifest.modules.push(generateJavaScriptModule(entity));
364
+ }
365
+ });
366
+
367
+ await fs.writeFile(apiFilePath.replace("api.json", "custom-elements.json"), JSON.stringify(customElementsManifest));
368
+ }));
369
+ };
370
+
371
+ generate().then(() => {
372
+ console.log("Custom elements manifest generated.");
373
+ });
@@ -0,0 +1,72 @@
1
+ const fs = require("fs").promises;
2
+ const path = require('path');
3
+
4
+ const generate = async () => {
5
+ const fioriInputFolder = path.normalize(process.argv[2]);
6
+ const tntInputFolder = path.normalize(process.argv[3]);
7
+ const outputFile = path.normalize(`${process.argv[4]}/Illustrations.js`);
8
+
9
+ const dir = await fs.readdir(fioriInputFolder);
10
+ const fioriIllustrationsOnFileSystem = dir.map(illustrationName => {
11
+ const fioriMatches = illustrationName.match(/.*\.js$/);
12
+ return fioriMatches ? illustrationName : undefined;
13
+ }).filter(key => !!key);
14
+
15
+ const tntDir = await fs.readdir(tntInputFolder);
16
+ const tntIllustrationsOnFileSystem = tntDir.map(illustrationName => {
17
+ const tntMatches = illustrationName.match(/.*\.js$/);
18
+ return tntMatches ? illustrationName : undefined;
19
+ }).filter(key => !!key);
20
+
21
+ // dynamic imports for Fiori illustrations
22
+ const fioriAvailableIllustrationsArray = `[${fioriIllustrationsOnFileSystem.filter(
23
+ // skipping the items starting with sapIllus-Dialog, sapIllus-Scene, sapIllus-Spot since they are included in the illustration's js file
24
+ line => !line.startsWith("sapIllus-Dialog") && !line.startsWith("sapIllus-Scene") && !line.startsWith("sapIllus-Spot") && !line.startsWith("AllIllustrations")).map(illustrationName => `"${illustrationName.replace('.js', '')}"`).join(", ")}]`;
25
+
26
+ const fioriDynamicImportLines = fioriIllustrationsOnFileSystem.map(illustrationName =>
27
+ `\t\tcase "${illustrationName.replace('.js', '')}": return (await import("../../illustrations/${illustrationName}")).default;`).filter(
28
+ // skipping the items starting with sapIllus-Dialog, sapIllus-Scene, sapIllus-Spot since they are included in the illustration's js file
29
+ line => !line.startsWith("\t\tcase \"sapIllus-Dialog") && !line.startsWith("\t\tcase \"sapIllus-Scene") && !line.startsWith("\t\tcase \"sapIllus-Spot") && !line.startsWith("\t\tcase \"AllIllustrations")).join("\n");
30
+
31
+ // dynamic imports for Tnt illustrations
32
+ const tntAvailableIllustrationsArray = `[${tntIllustrationsOnFileSystem.filter(
33
+ // skipping the items starting with tnt-Dialog, tnt-Scene, tnt-Spot since they are included in the illustration's js file
34
+ line => !line.startsWith("tnt-Dialog") && !line.startsWith("tnt-Scene") && !line.startsWith("tnt-Spot")).map(illustrationName => `"Tnt${illustrationName.replace('.js', '')}"`).join(", ")}]`;
35
+
36
+ const tntDynamicImportLines = tntIllustrationsOnFileSystem.map(illustrationName =>
37
+ `\t\tcase "Tnt${illustrationName.replace('.js', '')}": return (await import("../../illustrations/tnt/${illustrationName}")).default;`).filter(
38
+ // skipping the items starting with tnt-Dialog, tnt-Scene, tnt-Spot since they are included in the illustration's js file
39
+ line => !line.startsWith("\t\tcase \"Tnttnt-Dialog") && !line.startsWith("\t\tcase \"Tnttnt-Scene") && !line.startsWith("\t\tcase \"Tnttnt-Spot")).join("\n");
40
+
41
+
42
+ // dynamic imports file content
43
+ const contentDynamic = `import { registerIllustrationLoader } from "@ui5/webcomponents-base/dist/asset-registries/Illustrations.js";
44
+
45
+ const loadIllustration = async (illustrationName) => {
46
+ switch (illustrationName) {
47
+ ${fioriDynamicImportLines}
48
+ ${tntDynamicImportLines}
49
+ default: throw new Error("[Illustrations] Illustration not found: " + illustrationName);
50
+ }
51
+ };
52
+ const loadAndCheck = async (illustrationName) => {
53
+ const data = await loadIllustration(illustrationName);
54
+ return data;
55
+ }
56
+
57
+
58
+ ${fioriAvailableIllustrationsArray}.forEach(illustrationName => registerIllustrationLoader(illustrationName, loadAndCheck));
59
+ ${tntAvailableIllustrationsArray}.forEach(illustrationName => registerIllustrationLoader(illustrationName, loadAndCheck));`;
60
+
61
+
62
+ await fs.mkdir(path.dirname(outputFile), { recursive: true });
63
+ return Promise.all([fs.writeFile(outputFile, contentDynamic)]);
64
+ };
65
+
66
+ generate().then(() => {
67
+ console.log("Generated Illustrations.js");
68
+ })
69
+ .catch(err => {
70
+ console.error(err);
71
+ process.exit(1);
72
+ });
@@ -1,35 +1,38 @@
1
- const fs = require("fs");
1
+ const fs = require("fs").promises;
2
2
  const path = require('path');
3
- const mkdirp = require("mkdirp");
4
3
 
5
- const packageName = JSON.parse(fs.readFileSync("package.json")).name;
4
+ const generate = async () => {
6
5
 
7
- const inputFolder = path.normalize(process.argv[2]);
8
- const outputFile = path.normalize(`${process.argv[3]}/i18n.js`);
6
+ const packageName = JSON.parse(await fs.readFile("package.json")).name;
7
+
8
+ const inputFolder = path.normalize(process.argv[2]);
9
+ const outputFile = path.normalize(`${process.argv[3]}/i18n-static.js`);
10
+ const outputFileDynamic = path.normalize(`${process.argv[3]}/i18n.js`);
9
11
 
10
12
  // All languages present in the file system
11
- const files = fs.readdirSync(inputFolder);
12
- const languages = files.map(file => {
13
- const matches = file.match(/messagebundle_(.+?).json$/);
14
- return matches ? matches[1] : undefined;
15
- }).filter(key => !!key);
13
+ const files = await fs.readdir(inputFolder);
14
+ const languages = files.map(file => {
15
+ const matches = file.match(/messagebundle_(.+?).json$/);
16
+ return matches ? matches[1] : undefined;
17
+ }).filter(key => !!key);
16
18
 
17
- let content;
19
+ let contentStatic, contentDynamic;
18
20
 
19
21
  // No i18n - just import dependencies, if any
20
- if (languages.length === 0) {
21
- content = ``;
22
+ if (languages.length === 0) {
23
+ contentStatic = "";
24
+ contentDynamic = "";
22
25
  // There is i18n - generate the full file
23
- } else {
24
-
25
- // Keys for the array
26
- const languagesKeysString = languages.map(key => `${key},`).join("\n\t");
26
+ } else {
27
+ // Keys for the array
28
+ const languagesKeysString = languages.map(key => `"${key}": _${key},`).join("\n\t");
29
+ const languagesKeysStringArray = languages.map(key => `"${key}",`).join("\n\t");
27
30
 
28
- // Actual imports for json assets
29
- const assetsImportsString = languages.map(key => `import ${key} from "../assets/i18n/messagebundle_${key}.json";`).join("\n");
31
+ // Actual imports for json assets
32
+ const assetsImportsString = languages.map(key => `import _${key} from "../assets/i18n/messagebundle_${key}.json";`).join("\n");
30
33
 
31
- // Resulting file content
32
- content = `import { registerI18nBundle } from "@ui5/webcomponents-base/dist/asset-registries/i18n.js";
34
+ // static imports
35
+ contentStatic = `import { registerI18nLoader } from "@ui5/webcomponents-base/dist/asset-registries/i18n.js";
33
36
 
34
37
  ${assetsImportsString}
35
38
 
@@ -37,17 +40,59 @@ const bundleMap = {
37
40
  ${languagesKeysString}
38
41
  };
39
42
 
40
- const allEntriesInlined = Object.entries(bundleMap).every(([_key, value]) => typeof (value) === "object");
41
-
42
- if (allEntriesInlined) {
43
- console.warn(\`Inefficient bundling detected: consider bundling i18n imports as URLs instead of inlining them.
44
- See rollup-plugin-url or webpack file-loader for more information.
45
- Suggested pattern: "assets\\\\\\/.*\\\\\\.json"\`);
43
+ const fetchMessageBundle = async (localeId) => {
44
+ if (typeof bundleMap[localeId] === "object") {
45
+ // inlined from build
46
+ throw new Error("[i18n] Inlined JSON not supported with static imports of assets. Use dynamic imports of assets or configure JSON imports as URLs")
47
+ }
48
+ return (await fetch(bundleMap[localeId])).json()
46
49
  }
47
50
 
48
- registerI18nBundle("${packageName}", bundleMap);
51
+ const localeIds = [${languagesKeysStringArray}];
52
+
53
+ localeIds.forEach(localeId => {
54
+ registerI18nLoader("${packageName}", localeId, fetchMessageBundle);
55
+ });
49
56
  `;
57
+
58
+ // Actual imports for json assets
59
+ const dynamicImportsString = languages.map(key => ` case "${key}": return (await import("../assets/i18n/messagebundle_${key}.json")).default;`).join("\n");
60
+
61
+ // Resulting file content
62
+ contentDynamic = `import { registerI18nLoader } from "@ui5/webcomponents-base/dist/asset-registries/i18n.js";
63
+
64
+ const importMessageBundle = async (localeId) => {
65
+ switch (localeId) {
66
+ ${dynamicImportsString}
67
+ default: throw "unknown locale"
68
+ }
69
+ }
70
+
71
+ const importAndCheck = async (localeId) => {
72
+ const data = await importMessageBundle(localeId);
73
+ if (typeof data === "string" && data.endsWith(".json")) {
74
+ throw new Error(\`[i18n] Invalid bundling detected - dynamic JSON imports bundled as URLs. Switch to inlining JSON files from the build or use 'import ".../Assets-static.js"'. Check the \"Assets\" documentation for more information.\`);
75
+ }
76
+ return data;
77
+ }
78
+
79
+ const localeIds = [${languagesKeysStringArray}];
80
+
81
+ localeIds.forEach(localeId => {
82
+ registerI18nLoader("${packageName}", localeId, importAndCheck);
83
+ });
84
+ `;
85
+
86
+
87
+ }
88
+
89
+ await fs.mkdir(path.dirname(outputFile), { recursive: true });
90
+ return Promise.all([
91
+ fs.writeFile(outputFile, contentStatic),
92
+ fs.writeFile(outputFileDynamic, contentDynamic),
93
+ ]);
50
94
  }
51
95
 
52
- mkdirp.sync(path.dirname(outputFile));
53
- fs.writeFileSync(outputFile, content);
96
+ generate().then(() => {
97
+ console.log("Generated i18n JSON imports.");
98
+ });