builder/lib/tasks/bundlers/generateStandaloneAppBundle.js

  1. const log = require("@ui5/logger").getLogger("builder:tasks:bundlers:generateStandaloneAppBundle");
  2. const moduleBundler = require("../../processors/bundlers/moduleBundler");
  3. function getBundleDefinition(config) {
  4. const bundleDefinition = {
  5. name: config.name,
  6. defaultFileTypes: [
  7. ".js",
  8. ".control.xml",
  9. ".fragment.html",
  10. ".fragment.json",
  11. ".fragment.xml",
  12. ".view.html",
  13. ".view.json",
  14. ".view.xml",
  15. ".properties"
  16. ],
  17. sections: []
  18. };
  19. // add raw section
  20. bundleDefinition.sections.push({
  21. // include all 'raw' modules that are needed for the UI5 loader
  22. mode: "raw",
  23. filters: config.filters,
  24. resolve: true, // dependencies for raw modules are taken from shims in .library files
  25. sort: true, // topological sort on raw modules is mandatory
  26. declareModules: false
  27. });
  28. // preload section is only relevant for sap-ui-custom.js
  29. if (config.preloadSection) {
  30. bundleDefinition.sections.push({
  31. mode: "preload",
  32. filters: [
  33. `${config.namespace || ""}/`,
  34. `${config.namespace || ""}/**/manifest.json`,
  35. `${config.namespace || ""}/changes/changes-bundle.json`,
  36. `${config.namespace || ""}/changes/flexibility-bundle.json`,
  37. `!${config.namespace || ""}/test/`,
  38. "sap/ui/core/Core.js"
  39. ],
  40. resolve: true,
  41. resolveConditional: true,
  42. renderer: true
  43. });
  44. }
  45. bundleDefinition.sections.push({
  46. mode: "require",
  47. filters: [
  48. "sap/ui/core/Core.js"
  49. ]
  50. });
  51. return bundleDefinition;
  52. }
  53. /**
  54. * Task for bundling standalone applications.
  55. *
  56. * @public
  57. * @alias module:@ui5/builder.tasks.generateStandaloneAppBundle
  58. * @param {object} parameters Parameters
  59. * @param {module:@ui5/fs.DuplexCollection} parameters.workspace DuplexCollection to read and write files
  60. * @param {module:@ui5/fs.AbstractReader} parameters.dependencies Reader or Collection to read dependency files
  61. * @param {module:@ui5/builder.tasks.TaskUtil|object} [parameters.taskUtil] TaskUtil
  62. * @param {object} parameters.options Options
  63. * @param {string} parameters.options.projectName Project name
  64. * @param {string} [parameters.options.namespace] Project namespace
  65. * @returns {Promise<undefined>} Promise resolving with <code>undefined</code> once data has been written
  66. */
  67. module.exports = async function({workspace, dependencies, taskUtil, options: {projectName, namespace}}) {
  68. if (!namespace) {
  69. log.warn(`Namespace of project ${projectName} is not known. Self contained bundling is currently ` +
  70. `unable to generate complete bundles for such projects.`);
  71. }
  72. // If an application does not have a namespace, its resources are located at the root. Otherwise in /resources
  73. // For dependencies, we do not want to search in their test-resources
  74. const results = await Promise.all([
  75. workspace.byGlob("/**/*.{js,json,xml,html,properties,library}"),
  76. dependencies.byGlob("/resources/**/*.{js,json,xml,html,properties,library}")
  77. ]);
  78. const resources = Array.prototype.concat.apply([], results);
  79. const isEvo = resources.find((resource) => {
  80. return resource.getPath() === "/resources/ui5loader.js";
  81. });
  82. let filters;
  83. if (isEvo) {
  84. filters = ["ui5loader-autoconfig.js"];
  85. } else {
  86. filters = ["jquery.sap.global.js"];
  87. }
  88. await Promise.all([
  89. moduleBundler({
  90. resources,
  91. options: {
  92. bundleDefinition: getBundleDefinition({
  93. name: "sap-ui-custom.js",
  94. filters,
  95. namespace,
  96. preloadSection: true
  97. })
  98. }
  99. }),
  100. moduleBundler({
  101. resources,
  102. options: {
  103. bundleDefinition: getBundleDefinition({
  104. name: "sap-ui-custom-dbg.js",
  105. filters,
  106. namespace
  107. }),
  108. bundleOptions: {
  109. optimize: false
  110. }
  111. }
  112. })
  113. ]).then((results) => {
  114. const bundles = Array.prototype.concat.apply([], results);
  115. return Promise.all(bundles.map((resource) => {
  116. if (taskUtil) {
  117. taskUtil.setTag(resource, taskUtil.STANDARD_TAGS.IsBundle);
  118. }
  119. return workspace.write(resource);
  120. }));
  121. });
  122. };