builder/lib/tasks/bundlers/generateFlexChangesBundle.js

  1. const log = require("@ui5/logger").getLogger("builder:tasks:bundlers:generateFlexChangesBundle");
  2. const flexChangesBundler = require("../../processors/bundlers/flexChangesBundler");
  3. const semver = require("semver");
  4. /**
  5. * Task to create changesBundle.json file containing all changes stored in the /changes folder for easier consumption
  6. * at runtime.
  7. * If a change bundle is created, "sap.ui.fl" is added as a dependency to the manifest.json if not already present -
  8. * if the dependency is already listed but lazy-loaded, lazy loading is disabled.
  9. * If minUI5Version >= 1.73 flexibility-bundle.json will be create.
  10. * If there are control variants and minUI5Version < 1.73 build will break and throw an error.
  11. *
  12. * @public
  13. * @alias module:@ui5/builder.tasks.generateFlexChangesBundle
  14. * @param {object} parameters Parameters
  15. * @param {module:@ui5/fs.DuplexCollection} parameters.workspace DuplexCollection to read and write files
  16. * @param {module:@ui5/builder.tasks.TaskUtil|object} [parameters.taskUtil] TaskUtil
  17. * @param {object} [parameters.options] Options
  18. * @param {string} [parameters.options.namespace] Application Namespace
  19. * @returns {Promise<undefined>} Promise resolving with <code>undefined</code> once data has been written
  20. */
  21. module.exports = async function({workspace, taskUtil, options: {namespace}}) {
  22. // Use the given namespace if available, otherwise use no namespace
  23. // (e.g. in case no manifest.json is present)
  24. let pathPrefix = "";
  25. if (namespace) {
  26. pathPrefix = `/resources/${namespace}`;
  27. }
  28. function updateJson(data) {
  29. // ensure the existence of the libs section in the dependencies
  30. data["sap.ui5"] = data["sap.ui5"] || {};
  31. data["sap.ui5"].dependencies = data["sap.ui5"].dependencies || {};
  32. const mLibs = data["sap.ui5"].dependencies.libs = data["sap.ui5"].dependencies.libs || {};
  33. if (mLibs["sap.ui.fl"]) {
  34. log.verbose("sap.ui.fl found in manifest.json");
  35. if (mLibs["sap.ui.fl"].lazy) {
  36. log.verbose("sap.ui.fl 'lazy' attribute found in manifest.json, setting it to false...");
  37. mLibs["sap.ui.fl"].lazy = false;
  38. }
  39. } else {
  40. log.verbose("sap.ui.fl not found in manifest.json, inserting it...");
  41. mLibs["sap.ui.fl"] = {};
  42. }
  43. }
  44. async function updateFLdependency() {
  45. const manifestResource = await workspace.byPath(`${pathPrefix}/manifest.json`);
  46. const manifestContent = JSON.parse(await manifestResource.getString());
  47. updateJson(manifestContent);
  48. manifestResource.setString(JSON.stringify(manifestContent, null, "\t"));
  49. await workspace.write(manifestResource);
  50. }
  51. async function readManifestMinUI5Version() {
  52. const manifestResource = await workspace.byPath(`${pathPrefix}/manifest.json`);
  53. const manifestContent = JSON.parse(await manifestResource.getString());
  54. manifestContent["sap.ui5"] = manifestContent["sap.ui5"] || {};
  55. manifestContent["sap.ui5"].dependencies = manifestContent["sap.ui5"].dependencies || {};
  56. return manifestContent["sap.ui5"].dependencies.minUI5Version =
  57. manifestContent["sap.ui5"].dependencies.minUI5Version || "";
  58. }
  59. log.verbose("Collecting flexibility changes");
  60. const allResources = await workspace.byGlob(
  61. `${pathPrefix}/changes/*.{change,variant,ctrl_variant,ctrl_variant_change,ctrl_variant_management_change}`);
  62. if (allResources.length > 0) {
  63. const version = semver.coerce(await readManifestMinUI5Version());
  64. let hasFlexBundleVersion = false;
  65. let flexBundle = {};
  66. if (semver.compare(version, "1.73.0") >= 0) {
  67. hasFlexBundleVersion = true;
  68. const flexBundleResource = await workspace.byPath(`${pathPrefix}/changes/flexibility-bundle.json`);
  69. if (flexBundleResource) {
  70. flexBundle = JSON.parse(await flexBundleResource.getString());
  71. }
  72. }
  73. const processedResources = await flexChangesBundler({
  74. resources: allResources,
  75. options: {
  76. pathPrefix,
  77. hasFlexBundleVersion
  78. },
  79. existingFlexBundle: flexBundle
  80. });
  81. await Promise.all(processedResources.map((resource) => {
  82. log.verbose("Writing flexibility changes bundle");
  83. return workspace.write(resource);
  84. }));
  85. // Add the sap.ui.fl dependency if a bundle has been created
  86. if (processedResources.length > 0) {
  87. await updateFLdependency();
  88. }
  89. // Do not write bundled source files to build result
  90. if (taskUtil) {
  91. allResources.forEach((resource) => {
  92. taskUtil.setTag(resource, taskUtil.STANDARD_TAGS.OmitFromBuildResult);
  93. });
  94. }
  95. }
  96. };