@ui5/webcomponents-tools 0.0.0-d9933bd82 → 0.0.0-d9b978d1d

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 (93) hide show
  1. package/CHANGELOG.md +871 -0
  2. package/README.md +6 -6
  3. package/assets-meta.js +29 -3
  4. package/bin/dev.js +12 -1
  5. package/components-package/eslint.js +35 -0
  6. package/components-package/nps.js +116 -43
  7. package/components-package/postcss.components.js +1 -21
  8. package/components-package/postcss.themes.js +1 -23
  9. package/components-package/vite.config.js +13 -0
  10. package/components-package/wdio.js +405 -297
  11. package/components-package/wdio.sync.js +368 -0
  12. package/icons-collection/nps.js +55 -12
  13. package/lib/copy-and-watch/index.js +30 -5
  14. package/lib/copy-list/index.js +28 -0
  15. package/lib/create-icons/index.js +124 -54
  16. package/lib/create-illustrations/index.js +175 -0
  17. package/lib/create-new-component/index.js +72 -93
  18. package/lib/create-new-component/jsFileContentTemplate.js +73 -0
  19. package/lib/create-new-component/tsFileContentTemplate.js +80 -0
  20. package/lib/css-processors/css-processor-components.mjs +77 -0
  21. package/lib/css-processors/css-processor-themes.mjs +79 -0
  22. package/lib/css-processors/scope-variables.mjs +46 -0
  23. package/lib/css-processors/shared.mjs +76 -0
  24. package/lib/dev-server/custom-hot-update-plugin.js +39 -0
  25. package/lib/dev-server/dev-server.js +66 -0
  26. package/lib/dev-server/virtual-index-html-plugin.js +52 -0
  27. package/lib/esm-abs-to-rel/index.js +61 -0
  28. package/lib/generate-custom-elements-manifest/index.js +327 -0
  29. package/lib/generate-js-imports/illustrations.js +86 -0
  30. package/lib/generate-json-imports/i18n.js +80 -30
  31. package/lib/generate-json-imports/themes.js +70 -18
  32. package/lib/hbs2lit/index.js +2 -4
  33. package/lib/hbs2lit/src/compiler.js +29 -13
  34. package/lib/hbs2lit/src/includesReplacer.js +21 -10
  35. package/lib/hbs2lit/src/litVisitor2.js +125 -26
  36. package/lib/hbs2lit/src/svgProcessor.js +12 -5
  37. package/lib/hbs2ui5/RenderTemplates/LitRenderer.js +35 -7
  38. package/lib/hbs2ui5/index.js +69 -30
  39. package/lib/i18n/defaults.js +65 -57
  40. package/lib/i18n/toJSON.js +13 -12
  41. package/lib/jsdoc/config.json +1 -1
  42. package/lib/jsdoc/configTypescript.json +29 -0
  43. package/lib/jsdoc/plugin.js +65 -4
  44. package/lib/jsdoc/preprocess.js +146 -0
  45. package/lib/jsdoc/template/publish.js +32 -4
  46. package/lib/postcss-combine-duplicated-selectors/index.js +185 -0
  47. package/lib/replace-global-core/index.js +25 -0
  48. package/lib/scoping/get-all-tags.js +37 -0
  49. package/lib/scoping/lint-src.js +31 -0
  50. package/lib/scoping/missing-dependencies.js +65 -0
  51. package/lib/scoping/report-tags-usage.js +28 -0
  52. package/lib/scoping/scope-test-pages.js +41 -0
  53. package/lib/test-runner/test-runner.js +71 -0
  54. package/package.json +52 -53
  55. package/bin/init-ui5-package.js +0 -3
  56. package/components-package/rollup.js +0 -134
  57. package/components-package/serve.json +0 -3
  58. package/lib/documentation/index.js +0 -143
  59. package/lib/documentation/templates/api-component-since.js +0 -3
  60. package/lib/documentation/templates/api-css-variables-section.js +0 -24
  61. package/lib/documentation/templates/api-events-section.js +0 -35
  62. package/lib/documentation/templates/api-methods-section.js +0 -26
  63. package/lib/documentation/templates/api-properties-section.js +0 -40
  64. package/lib/documentation/templates/api-slots-section.js +0 -28
  65. package/lib/documentation/templates/template.js +0 -38
  66. package/lib/init-package/index.js +0 -119
  67. package/lib/init-package/resources/.eslintignore +0 -3
  68. package/lib/init-package/resources/bundle.es5.js +0 -25
  69. package/lib/init-package/resources/bundle.esm.js +0 -34
  70. package/lib/init-package/resources/config/.eslintrc.js +0 -1
  71. package/lib/init-package/resources/config/postcss.components/postcss.config.js +0 -1
  72. package/lib/init-package/resources/config/postcss.themes/postcss.config.js +0 -1
  73. package/lib/init-package/resources/config/rollup.config.js +0 -1
  74. package/lib/init-package/resources/config/wdio.conf.js +0 -1
  75. package/lib/init-package/resources/package-scripts.js +0 -11
  76. package/lib/init-package/resources/src/Assets.js +0 -5
  77. package/lib/init-package/resources/src/Demo.hbs +0 -1
  78. package/lib/init-package/resources/src/Demo.js +0 -56
  79. package/lib/init-package/resources/src/i18n/messagebundle.properties +0 -2
  80. package/lib/init-package/resources/src/i18n/messagebundle_de.properties +0 -1
  81. package/lib/init-package/resources/src/i18n/messagebundle_en.properties +0 -1
  82. package/lib/init-package/resources/src/i18n/messagebundle_es.properties +0 -1
  83. package/lib/init-package/resources/src/i18n/messagebundle_fr.properties +0 -1
  84. package/lib/init-package/resources/src/themes/Demo.css +0 -11
  85. package/lib/init-package/resources/src/themes/sap_belize/parameters-bundle.css +0 -3
  86. package/lib/init-package/resources/src/themes/sap_belize_hcb/parameters-bundle.css +0 -3
  87. package/lib/init-package/resources/src/themes/sap_belize_hcw/parameters-bundle.css +0 -3
  88. package/lib/init-package/resources/src/themes/sap_fiori_3/parameters-bundle.css +0 -3
  89. package/lib/init-package/resources/src/themes/sap_fiori_3_dark/parameters-bundle.css +0 -3
  90. package/lib/init-package/resources/test/pages/index.html +0 -51
  91. package/lib/init-package/resources/test/specs/Demo.spec.js +0 -12
  92. package/lib/postcss-css-to-esm/index.js +0 -42
  93. package/lib/postcss-css-to-json/index.js +0 -27
@@ -0,0 +1,327 @@
1
+ const fs = require("fs").promises;
2
+ const path = require("path");
3
+ // https://github.com/webcomponents/custom-elements-manifest/blob/main/schema.json
4
+
5
+ const inputDir = process.argv[2];
6
+ const outputDir = process.argv[3];
7
+
8
+ const camelToKebabMap = new Map();
9
+ const apiIndex = new Map();
10
+ const forbiddenAttributeTypes = ["object", "array"];
11
+
12
+ const camelToKebabCase = string => {
13
+ if (!camelToKebabMap.has(string)) {
14
+ const result = string.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
15
+ camelToKebabMap.set(string, result);
16
+ }
17
+ return camelToKebabMap.get(string);
18
+ };
19
+
20
+ const generateJavaScriptExport = entity => {
21
+ return {
22
+ declaration: generateRefenrece(entity.name),
23
+ deprecated: !!entity.deprecated,
24
+ kind: "js",
25
+ name: "default",
26
+ };
27
+ };
28
+
29
+ const generateCustomElementExport = entity => {
30
+ return {
31
+ declaration: {
32
+ name: entity.basename,
33
+ module: `${entity.module}.js`,
34
+ },
35
+ deprecated: !!entity.deprecated,
36
+ kind: "custom-element-definition",
37
+ name: entity.tagname,
38
+ };
39
+ };
40
+
41
+ const generateJavaScriptModule = entity => {
42
+ return {
43
+ kind: "javascript-module",
44
+ path: `${entity.basename}.js`,
45
+ declarations: [
46
+ generateCustomElementDeclaration(entity),
47
+ ],
48
+ exports: [
49
+ generateJavaScriptExport(entity),
50
+ generateCustomElementExport(entity),
51
+ ],
52
+ };
53
+ };
54
+
55
+ const generateSingleClassField = classField => {
56
+ let generatedClassField = {
57
+ deprecated: !!classField.deprecated,
58
+ kind: "field",
59
+ name: classField.name,
60
+ privacy: classField.visibility,
61
+ static: !!classField.static,
62
+ type: generateType(classField.type),
63
+ };
64
+
65
+ if (classField.defaultValue) {
66
+ generatedClassField.default = classField.defaultValue;
67
+ }
68
+
69
+ if (classField.description) {
70
+ generatedClassField.description = classField.description;
71
+ }
72
+
73
+ return generatedClassField;
74
+ };
75
+
76
+ const generateSingleParameter = parameter => {
77
+ let generatedParameter = {
78
+ deprecated: !!parameter.deprecated,
79
+ name: parameter.name,
80
+ type: generateType(parameter.type),
81
+ };
82
+
83
+ if (parameter.description) {
84
+ generatedParameter.description = parameter.description;
85
+ }
86
+
87
+ if (parameter.optional) {
88
+ generatedParameter.optional = parameter.optional;
89
+ }
90
+
91
+ return generatedParameter;
92
+ };
93
+
94
+ const generateParameters = (parameters) => {
95
+ return parameters.reduce((newParametersArray, parameter) => {
96
+ newParametersArray.push(generateSingleParameter(parameter));
97
+
98
+ return newParametersArray;
99
+ }, []);
100
+ };
101
+
102
+ const generateSingleClassMethod = classMethod => {
103
+ let generatedClassMethod = {
104
+ deprecated: !!classMethod.deprecated,
105
+ kind: "method",
106
+ name: classMethod.name,
107
+ privacy: classMethod.visibility,
108
+ static: classMethod.static,
109
+ };
110
+
111
+ if (classMethod.description) {
112
+ generatedClassMethod.description = classMethod.description;
113
+ }
114
+
115
+ if (classMethod.parameters && classMethod.parameters.length) {
116
+ generatedClassMethod.parameters = generateParameters(classMethod.parameters);
117
+ }
118
+
119
+ if (classMethod.returnValue) {
120
+ generatedClassMethod.return = {
121
+ type: generateType(classMethod.returnValue.type),
122
+ };
123
+
124
+ if (classMethod.returnValue.description) {
125
+ generatedClassMethod.return.description = classMethod.returnValue.type;
126
+ }
127
+ }
128
+
129
+ return generatedClassMethod;
130
+ };
131
+
132
+ const generateClassFields = classFields => {
133
+ return classFields.reduce((newClassFieldsArray, classField) => {
134
+ newClassFieldsArray.push(generateSingleClassField(classField));
135
+
136
+ return newClassFieldsArray;
137
+ }, []);
138
+ };
139
+
140
+ const generateClassMethods = classMethods => {
141
+ return classMethods.reduce((newClassMethodsArray, classMethod) => {
142
+ newClassMethodsArray.push(generateSingleClassMethod(classMethod));
143
+
144
+ return newClassMethodsArray;
145
+ }, []);
146
+ };
147
+
148
+ const generateMembers = (classFields, classMethods) => {
149
+ return [...generateClassFields(classFields), ...generateClassMethods(classMethods)];
150
+ };
151
+
152
+ const generateType = type => {
153
+ const dataType = apiIndex.get(type);
154
+
155
+ return {
156
+ text: dataType && dataType.name.includes(".types.") ?
157
+ filterPublicApi(dataType.properties)
158
+ .map(prop => `"${prop.name}"`)
159
+ .join(" | ") : type,
160
+ };
161
+ };
162
+
163
+ const generateSingleAttribute = attribute => {
164
+ let generatedAttribute = {
165
+ default: attribute.defaultValue,
166
+ deprecated: !!attribute.deprecated,
167
+ fieldName: attribute.name,
168
+ name: camelToKebabCase(attribute.name),
169
+ type: generateType(attribute.type),
170
+ };
171
+
172
+ if (attribute.description) {
173
+ generatedAttribute.description = attribute.description;
174
+ }
175
+
176
+ return generatedAttribute;
177
+ };
178
+
179
+ const generateAttributes = attributes => {
180
+ attributes = attributes.reduce((newAttributesArray, attribute) => {
181
+ newAttributesArray.push(generateSingleAttribute(attribute));
182
+
183
+ return newAttributesArray;
184
+ }, []);
185
+
186
+ return attributes;
187
+ };
188
+
189
+ const generateSingleEvent = event => {
190
+ let generatedEvent = {
191
+ deprecated: !!event.deprecated,
192
+ name: event.name,
193
+ type: event.native === "true" ? "NativeEvent" : "CustomEvent",
194
+ };
195
+
196
+ if (event.description) {
197
+ generatedEvent.description = event.description;
198
+ }
199
+
200
+ return generatedEvent;
201
+ };
202
+
203
+ const generateEvents = events => {
204
+ events = events.reduce((newEventsArray, event) => {
205
+ newEventsArray.push(generateSingleEvent(event));
206
+
207
+ return newEventsArray;
208
+ }, []);
209
+
210
+ return events;
211
+ };
212
+
213
+ const generateSingleSlot = slot => {
214
+ return {
215
+ deprecated: !!slot.deprecated,
216
+ description: slot.description,
217
+ name: slot.name,
218
+ };
219
+ };
220
+
221
+ const generateSlots = slots => {
222
+ slots = slots.reduce((newSlotsArray, event) => {
223
+ newSlotsArray.push(generateSingleSlot(event));
224
+
225
+ return newSlotsArray;
226
+ }, []);
227
+
228
+ return slots;
229
+ };
230
+
231
+ const generateCustomElementDeclaration = entity => {
232
+ let generatedCustomElementDeclaration = {
233
+ deprecated: !!entity.deprecated,
234
+ customElement: true,
235
+ kind: entity.basename,
236
+ name: entity.basename,
237
+ tagName: entity.tagname,
238
+ };
239
+
240
+ const slots = filterPublicApi(entity.slots);
241
+ const events = filterPublicApi(entity.events);
242
+ const classFields = filterPublicApi(entity.properties);
243
+ const classMethods = filterPublicApi(entity.methods);
244
+ const attributes = classFields.filter(property => {
245
+ return property.noattribute !== "true" && property.readonly !== "true" && !forbiddenAttributeTypes.includes(property.type.toLowerCase());
246
+ });
247
+
248
+ if (slots.length) {
249
+ generatedCustomElementDeclaration.slots = generateSlots(slots);
250
+ }
251
+
252
+ if (events.length) {
253
+ generatedCustomElementDeclaration.events = generateEvents(events);
254
+ }
255
+
256
+ if (attributes.length) {
257
+ generatedCustomElementDeclaration.attributes = generateAttributes(attributes);
258
+ }
259
+
260
+ if (entity.description) {
261
+ generatedCustomElementDeclaration.description = entity.description;
262
+ }
263
+
264
+ if (classFields.length || classMethods.length) {
265
+ generatedCustomElementDeclaration.members = generateMembers(classFields, classMethods);
266
+ }
267
+
268
+ if (entity.extends && entity.extends !== "HTMLElement") {
269
+ generatedCustomElementDeclaration.superclass = generateRefenrece(entity.extends);
270
+ }
271
+
272
+ return generatedCustomElementDeclaration;
273
+ };
274
+
275
+ const generateRefenrece = (entityName) => {
276
+ let packageName;
277
+ let basename;
278
+
279
+ if (!entityName) {
280
+ throw new Error("JSDoc error: entity not found in api.json.");
281
+ }
282
+
283
+ if (entityName.includes(".")) {
284
+ basename = entityName.split(".").pop();
285
+ } else {
286
+ basename = entityName
287
+ }
288
+
289
+ if (entityName.includes("sap.ui.webc.main")) {
290
+ packageName = "@ui5/webcomponents";
291
+ } else if (entityName.includes("sap.ui.webc.fiori")) {
292
+ packageName = "@ui5/webcomponents-fiori";
293
+ } else if (entityName.includes("sap.ui.webc.base")) {
294
+ packageName = "@ui5/webcomponents-base";
295
+ }
296
+
297
+ return {
298
+ module: `${basename}.js`,
299
+ name: `${basename}`,
300
+ package: packageName,
301
+ };
302
+ };
303
+
304
+ const filterPublicApi = array => {
305
+ return (array || []).filter(el => el.visibility === "public");
306
+ };
307
+
308
+ const generate = async () => {
309
+ const file = JSON.parse(await fs.readFile(path.join(inputDir, "api.json")));
310
+ let customElementsManifest = {
311
+ schemaVersion: "1.0.0",
312
+ readme: "",
313
+ modules: [],
314
+ };
315
+
316
+ file.symbols.forEach(entity => {
317
+ if (entity.tagname || entity.kind === "class") {
318
+ customElementsManifest.modules.push(generateJavaScriptModule(entity));
319
+ }
320
+ });
321
+
322
+ await fs.writeFile(path.join(outputDir, "custom-elements.json"), JSON.stringify(customElementsManifest));
323
+ };
324
+
325
+ generate().then(() => {
326
+ console.log("Custom elements manifest generated.");
327
+ });
@@ -0,0 +1,86 @@
1
+ const fs = require("fs").promises;
2
+ const path = require("path");
3
+
4
+ const generateDynamicImportLines = async (fileNames, location, exclusionPatterns = []) => {
5
+ const packageName = JSON.parse(await fs.readFile("package.json")).name;
6
+ return fileNames
7
+ .filter((fileName) => !exclusionPatterns.some((pattern) => fileName.startsWith(pattern)))
8
+ .map((fileName) => {
9
+ const illustrationName = fileName.replace(".js", "");
10
+ const illustrationPath = `${location}/${illustrationName}`;
11
+ return `\t\tcase "${fileName.replace('.js', '')}": return (await import(/* webpackChunkName: "${packageName.replace("@", "").replace("/", "-")}-${illustrationName.toLowerCase()}" */ "${illustrationPath}.js")).default;`;
12
+ })
13
+ .join("\n");
14
+ };
15
+
16
+ const generateAvailableIllustrationsArray = (fileNames, exclusionPatterns = []) => {
17
+ return JSON.stringify(
18
+ fileNames
19
+ .filter((fileName) => !exclusionPatterns.some((pattern) => fileName.startsWith(pattern)))
20
+ .map((fileName) => fileName.replace(".js", ""))
21
+ );
22
+ };
23
+
24
+ const generateDynamicImportsFileContent = (dynamicImports, availableIllustrations, collection, prefix = "") => {
25
+ return `// @ts-nocheck
26
+ import { registerIllustrationLoader } from "@ui5/webcomponents-base/dist/asset-registries/Illustrations.js";
27
+
28
+ export const loadIllustration = async (illustrationName) => {
29
+ const collectionAndPrefix = "${collection}/${prefix}";
30
+ const cleanIllustrationName = illustrationName.startsWith(collectionAndPrefix) ? illustrationName.replace(collectionAndPrefix, "") : illustrationName;
31
+ switch (cleanIllustrationName) {
32
+ ${dynamicImports}
33
+ default:
34
+ throw new Error("[Illustrations] Illustration not found: " + illustrationName);
35
+ }
36
+ };
37
+
38
+ const loadAndCheck = async (illustrationName) => {
39
+ const data = await loadIllustration(illustrationName);
40
+ return data;
41
+ };
42
+
43
+ ${availableIllustrations}.forEach((illustrationName) =>
44
+ registerIllustrationLoader(\`${collection}/${prefix}\${illustrationName}\`, loadAndCheck)
45
+ );
46
+ `;
47
+ };
48
+
49
+ const getMatchingFiles = async (folder, pattern) => {
50
+ const dir = await fs.readdir(folder);
51
+ return dir.filter((fileName) => fileName.match(pattern));
52
+ };
53
+
54
+ const generateIllustrations = async (config) => {
55
+ const { inputFolder, outputFile, collection, location, prefix, filterOut } = config;
56
+
57
+ const normalizedInputFolder = path.normalize(inputFolder);
58
+ const normalizedOutputFile = path.normalize(outputFile);
59
+
60
+ const illustrations = await getMatchingFiles(normalizedInputFolder, /^.*\.js$/);
61
+
62
+ const dynamicImports = await generateDynamicImportLines(illustrations, location, filterOut);
63
+ const availableIllustrations = generateAvailableIllustrationsArray(illustrations, filterOut);
64
+
65
+ const contentDynamic = generateDynamicImportsFileContent(dynamicImports, availableIllustrations, collection, prefix);
66
+
67
+ await fs.mkdir(path.dirname(normalizedOutputFile), { recursive: true });
68
+ await fs.writeFile(normalizedOutputFile, contentDynamic);
69
+
70
+ console.log(`Generated ${normalizedOutputFile}`);
71
+ };
72
+
73
+ // Parse configuration from command-line arguments
74
+ const config = {
75
+ inputFolder: process.argv[2],
76
+ outputFile: process.argv[3],
77
+ collection: process.argv[4],
78
+ location: process.argv[5],
79
+ prefix: process.argv[6],
80
+ filterOut: process.argv.slice(7),
81
+ };
82
+
83
+ // Run the generation process
84
+ generateIllustrations(config).catch((error) => {
85
+ console.error("Error generating illustrations:", error);
86
+ });
@@ -1,35 +1,42 @@
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 isTypeScript = process.env.UI5_TS;
5
+ const ext = isTypeScript ? 'ts' : 'js';
6
6
 
7
- const inputFolder = path.normalize(process.argv[2]);
8
- const outputFile = path.normalize(`${process.argv[3]}/i18n.js`);
7
+ const generate = async () => {
8
+
9
+ const packageName = JSON.parse(await fs.readFile("package.json")).name;
10
+
11
+ const inputFolder = path.normalize(process.argv[2]);
12
+ const outputFile = path.normalize(`${process.argv[3]}/i18n-static.${ext}`);
13
+ const outputFileDynamic = path.normalize(`${process.argv[3]}/i18n.${ext}`);
9
14
 
10
15
  // 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);
16
+ const files = await fs.readdir(inputFolder);
17
+ const languages = files.map(file => {
18
+ const matches = file.match(/messagebundle_(.+?).json$/);
19
+ return matches ? matches[1] : undefined;
20
+ }).filter(key => !!key);
16
21
 
17
- let content;
22
+ let contentStatic, contentDynamic;
18
23
 
19
24
  // No i18n - just import dependencies, if any
20
- if (languages.length === 0) {
21
- content = ``;
25
+ if (languages.length === 0) {
26
+ contentStatic = "";
27
+ contentDynamic = "";
22
28
  // There is i18n - generate the full file
23
- } else {
29
+ } else {
30
+ // Keys for the array
31
+ const languagesKeysString = languages.map(key => `"${key}": _${key},`).join("\n\t");
32
+ const languagesKeysStringArray = languages.map(key => `"${key}",`).join("\n\t");
24
33
 
25
- // Keys for the array
26
- const languagesKeysString = languages.map(key => `${key},`).join("\n\t");
34
+ // Actual imports for json assets
35
+ const assetsImportsString = languages.map(key => `import _${key} from "../assets/i18n/messagebundle_${key}.json";`).join("\n");
27
36
 
28
- // Actual imports for json assets
29
- const assetsImportsString = languages.map(key => `import ${key} from "../assets/i18n/messagebundle_${key}.json";`).join("\n");
30
-
31
- // Resulting file content
32
- content = `import { registerI18nBundle } from "@ui5/webcomponents-base/dist/asset-registries/i18n.js";
37
+ // static imports
38
+ contentStatic = `// @ts-nocheck
39
+ import { registerI18nLoader } from "@ui5/webcomponents-base/dist/asset-registries/i18n.js";
33
40
 
34
41
  ${assetsImportsString}
35
42
 
@@ -37,17 +44,60 @@ const bundleMap = {
37
44
  ${languagesKeysString}
38
45
  };
39
46
 
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"\`);
47
+ const fetchMessageBundle = async (localeId) => {
48
+ if (typeof bundleMap[localeId] === "object") {
49
+ // inlined from build
50
+ throw new Error("[i18n] Inlined JSON not supported with static imports of assets. Use dynamic imports of assets or configure JSON imports as URLs")
51
+ }
52
+ return (await fetch(bundleMap[localeId])).json()
46
53
  }
47
54
 
48
- registerI18nBundle("${packageName}", bundleMap);
55
+ const localeIds = [${languagesKeysStringArray}];
56
+
57
+ localeIds.forEach(localeId => {
58
+ registerI18nLoader("${packageName}", localeId, fetchMessageBundle);
59
+ });
49
60
  `;
61
+
62
+ // Actual imports for json assets
63
+ const dynamicImportsString = languages.map(key => ` case "${key}": return (await import(/* webpackChunkName: "${packageName.replace("@", "").replace("/", "-")}-messagebundle-${key}" */ "../assets/i18n/messagebundle_${key}.json")).default;`).join("\n");
64
+
65
+ // Resulting file content
66
+ contentDynamic = `// @ts-nocheck
67
+ import { registerI18nLoader } from "@ui5/webcomponents-base/dist/asset-registries/i18n.js";
68
+
69
+ const importMessageBundle = async (localeId) => {
70
+ switch (localeId) {
71
+ ${dynamicImportsString}
72
+ default: throw "unknown locale"
73
+ }
74
+ }
75
+
76
+ const importAndCheck = async (localeId) => {
77
+ const data = await importMessageBundle(localeId);
78
+ if (typeof data === "string" && data.endsWith(".json")) {
79
+ 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.\`);
80
+ }
81
+ return data;
82
+ }
83
+
84
+ const localeIds = [${languagesKeysStringArray}];
85
+
86
+ localeIds.forEach(localeId => {
87
+ registerI18nLoader("${packageName}", localeId, importAndCheck);
88
+ });
89
+ `;
90
+
91
+
92
+ }
93
+
94
+ await fs.mkdir(path.dirname(outputFile), { recursive: true });
95
+ return Promise.all([
96
+ fs.writeFile(outputFile, contentStatic),
97
+ fs.writeFile(outputFileDynamic, contentDynamic),
98
+ ]);
50
99
  }
51
100
 
52
- mkdirp.sync(path.dirname(outputFile));
53
- fs.writeFileSync(outputFile, content);
101
+ generate().then(() => {
102
+ console.log("Generated i18n JSON imports.");
103
+ });
@@ -1,33 +1,85 @@
1
- const fs = require("fs");
1
+ const fs = require("fs").promises;
2
2
  const path = require('path');
3
- const mkdirp = require("mkdirp");
4
3
  const assets = require("../../assets-meta.js");
5
4
 
6
- const outputFile = path.normalize(`${process.argv[2]}/Themes.js`);
5
+ const isTypeScript = process.env.UI5_TS;
6
+ const ext = isTypeScript ? 'ts' : 'js';
7
7
 
8
- const optionalThemes = assets.themes.all.filter(theme => theme !== assets.themes.default);
8
+ const generate = async () => {
9
+ const inputFolder = path.normalize(process.argv[2]);
10
+ const outputFile = path.normalize(`${process.argv[3]}/Themes-static.${ext}`);
11
+ const outputFileDynamic = path.normalize(`${process.argv[3]}/Themes.${ext}`);
9
12
 
10
- const packageName = JSON.parse(fs.readFileSync("package.json")).name;
13
+ // All supported optional themes
14
+ const allThemes = assets.themes.all;
11
15
 
12
- const importLines = optionalThemes.map(theme => `import ${theme} from "../assets/themes/${theme}/parameters-bundle.css.json";`).join("\n");
13
- const isInlinedCondition = optionalThemes.map(theme => `isInlined(${theme})`).join(" || ");
14
- const registerLines = optionalThemes.map(theme => `registerThemeProperties("${packageName}", "${theme}", ${theme});`).join("\n");
16
+ // All themes present in the file system
17
+ const dirs = await fs.readdir(inputFolder);
18
+ const themesOnFileSystem = dirs.map(dir => {
19
+ const matches = dir.match(/sap_.*$/);
20
+ return matches ? dir : undefined;
21
+ }).filter(key => !!key && allThemes.includes(key));
15
22
 
16
- // Resulting file content
17
- const content = `import { registerThemeProperties } from "@ui5/webcomponents-base/dist/asset-registries/Themes.js";
23
+ const packageName = JSON.parse(await fs.readFile("package.json")).name;
24
+
25
+ const importLines = themesOnFileSystem.map(theme => `import ${theme} from "../assets/themes/${theme}/parameters-bundle.css.json";`).join("\n");
26
+ const themeUrlsByName = "{\n" + themesOnFileSystem.join(",\n") + "\n}";
27
+ const availableThemesArray = `[${themesOnFileSystem.map(theme => `"${theme}"`).join(", ")}]`;
28
+ const dynamicImportLines = themesOnFileSystem.map(theme => `\t\tcase "${theme}": return (await import(/* webpackChunkName: "${packageName.replace("@", "").replace("/", "-")}-${theme.replace("_", "-")}-parameters-bundle" */"../assets/themes/${theme}/parameters-bundle.css.json")).default;`).join("\n");
29
+
30
+
31
+ // static imports file content
32
+ const contentStatic = `// @ts-nocheck
33
+ import { registerThemePropertiesLoader } from "@ui5/webcomponents-base/dist/asset-registries/Themes.js";
18
34
 
19
35
  ${importLines}
20
36
 
37
+ const themeUrlsByName = ${themeUrlsByName};
21
38
  const isInlined = obj => typeof (obj) === "object";
22
39
 
23
- if (${isInlinedCondition}) {
24
- console.warn(\`Inefficient bundling detected: consider bundling theme properties imports as URLs instead of inlining them.
25
- See rollup-plugin-url or webpack file-loader for more information.
26
- Suggested pattern: "assets\\\\\\/.*\\\\\\.json"\`);
27
- }
40
+ const loadThemeProperties = async (themeName) => {
41
+ if (typeof themeUrlsByName[themeName] === "object") {
42
+ // inlined from build
43
+ throw new Error("[themes] Inlined JSON not supported with static imports of assets. Use dynamic imports of assets or configure JSON imports as URLs");
44
+ }
45
+ return (await fetch(themeUrlsByName[themeName])).json();
46
+ };
47
+
48
+ ${availableThemesArray}
49
+ .forEach(themeName => registerThemePropertiesLoader("${packageName}", themeName, loadThemeProperties));
50
+ `;
51
+
52
+
53
+ // dynamic imports file content
54
+ const contentDynamic = `// @ts-nocheck
55
+ import { registerThemePropertiesLoader } from "@ui5/webcomponents-base/dist/asset-registries/Themes.js";
56
+
57
+ const loadThemeProperties = async (themeName) => {
58
+ switch (themeName) {
59
+ ${dynamicImportLines}
60
+ default: throw "unknown theme"
61
+ }
62
+ };
28
63
 
29
- ${registerLines};
64
+ const loadAndCheck = async (themeName) => {
65
+ const data = await loadThemeProperties(themeName);
66
+ if (typeof data === "string" && data.endsWith(".json")) {
67
+ throw new Error(\`[themes] 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.\`);
68
+ }
69
+ return data;
70
+ };
71
+
72
+ ${availableThemesArray}
73
+ .forEach(themeName => registerThemePropertiesLoader("${packageName}", themeName, loadAndCheck));
30
74
  `;
31
75
 
32
- mkdirp.sync(path.dirname(outputFile));
33
- fs.writeFileSync(outputFile, content);
76
+ await fs.mkdir(path.dirname(outputFile), { recursive: true });
77
+ return Promise.all([
78
+ fs.writeFile(outputFile, contentStatic),
79
+ fs.writeFile(outputFileDynamic, contentDynamic)
80
+ ]);
81
+ };
82
+
83
+ generate().then(() => {
84
+ console.log("Generated themes JSON imports.");
85
+ });
@@ -1,5 +1,3 @@
1
- const compiler = require("./src/compiler");
1
+ const hbs2lit = require("./src/compiler");
2
2
 
3
- module.exports = {
4
- compileString: compiler.compileString
5
- };
3
+ module.exports = hbs2lit;