builder/lib/tasks/bundlers/generateBundle.js

  1. import moduleBundler from "../../processors/bundlers/moduleBundler.js";
  2. import createModuleNameMapping from "./utils/createModuleNameMapping.js";
  3. import ReaderCollectionPrioritized from "@ui5/fs/ReaderCollectionPrioritized";
  4. /**
  5. * @public
  6. * @module @ui5/builder/tasks/bundlers/generateBundle
  7. */
  8. /* eslint-disable max-len */
  9. /**
  10. * Generates a bundle based on the given bundle definition
  11. *
  12. * @public
  13. * @function default
  14. * @static
  15. *
  16. * @param {object} parameters Parameters
  17. * @param {@ui5/fs/DuplexCollection} parameters.workspace DuplexCollection to read and write files
  18. * @param {@ui5/fs/ReaderCollection} parameters.dependencies Collection to read dependency files
  19. * @param {@ui5/project/build/helpers/TaskUtil|object} [parameters.taskUtil] TaskUtil
  20. * @param {object} parameters.options Options
  21. * @param {string} parameters.options.projectName Project name
  22. * @param {module:@ui5/builder/processors/bundlers/moduleBundler~ModuleBundleDefinition} parameters.options.bundleDefinition Module
  23. * bundle definition
  24. * @param {module:@ui5/builder/processors/bundlers/moduleBundler~ModuleBundleOptions} [parameters.options.bundleOptions] Module
  25. * bundle options
  26. * @returns {Promise} Promise resolving with <code>undefined</code> once data has been written
  27. */
  28. /* eslint-enable max-len */
  29. export default async function({
  30. workspace, dependencies, taskUtil, options: {projectName, bundleDefinition, bundleOptions}
  31. }) {
  32. let combo = new ReaderCollectionPrioritized({
  33. name: `generateBundle - prioritize workspace over dependencies: ${projectName}`,
  34. readers: [workspace, dependencies]
  35. });
  36. const optimize = !bundleOptions || bundleOptions.optimize !== false;
  37. if (taskUtil) {
  38. /* Scenarios
  39. 1. Optimize bundle with minification already done
  40. Workspace:
  41. * /resources/my/lib/Control.js [ui5:HasDebugVariant]
  42. * /resources/my/lib/Control.js.map [ui5:HasDebugVariant]
  43. * /resources/my/lib/Control-dbg.js [ui5:IsDebugVariant]
  44. Bundler input:
  45. * /resources/my/lib/Control.js
  46. * /resources/my/lib/Control.js.map
  47. => Filter out debug resources
  48. 2. Optimize bundle with no minification
  49. * /resources/my/lib/Control.js
  50. => No action necessary
  51. 3. Debug-bundle with minification already done
  52. Workspace:
  53. * /resources/my/lib/Control.js [ui5:HasDebugVariant]
  54. * /resources/my/lib/Control.js.map [ui5:HasDebugVariant]
  55. * /resources/my/lib/Control-dbg.js [ui5:IsDebugVariant]
  56. Bundler input:
  57. * /resources/my/lib/Control-dbg.js
  58. * moduleNameMapping: [{"/resources/my/lib/Control-dbg.js": "my/lib/Control.js"}]
  59. => Filter out minified-resources (tagged as "HasDebugVariant", incl. source maps) and rename debug-files
  60. 4. Debug-bundle with no minification
  61. * /resources/my/lib/Control.js
  62. => No action necessary
  63. 5. Bundle with external input (optimize or not), e.g. TS-project
  64. Workspace:
  65. * /resources/my/lib/Control.ts
  66. * /resources/my/lib/Control.js
  67. * /resources/my/lib/Control.js.map
  68. Bundler input:
  69. * /resources/my/lib/Control.js
  70. * /resources/my/lib/Control.js.map
  71. */
  72. // Omit -dbg files for optimize bundles and vice versa
  73. const filterTag = optimize ?
  74. taskUtil.STANDARD_TAGS.IsDebugVariant : taskUtil.STANDARD_TAGS.HasDebugVariant;
  75. combo = taskUtil.resourceFactory.createFilterReader({
  76. reader: combo,
  77. callback: function(resource) {
  78. return !taskUtil.getTag(resource, filterTag);
  79. }
  80. });
  81. }
  82. return combo.byGlob("/resources/**/*.{js,json,xml,html,properties,library,js.map}").then((resources) => {
  83. const options = {bundleDefinition, bundleOptions};
  84. if (!optimize && taskUtil) {
  85. options.moduleNameMapping = createModuleNameMapping({resources, taskUtil});
  86. }
  87. return moduleBundler({options, resources}).then((bundles) => {
  88. return Promise.all(bundles.map(({bundle, sourceMap} = {}) => {
  89. if (!bundle) {
  90. // Skip empty bundles
  91. return;
  92. }
  93. if (taskUtil) {
  94. taskUtil.setTag(bundle, taskUtil.STANDARD_TAGS.IsBundle);
  95. if (sourceMap) {
  96. // Clear tag that might have been set by the minify task, in cases where
  97. // the bundle name is identical to a source file
  98. taskUtil.clearTag(sourceMap, taskUtil.STANDARD_TAGS.OmitFromBuildResult);
  99. }
  100. }
  101. const writes = [workspace.write(bundle)];
  102. if (sourceMap) {
  103. writes.push(workspace.write(sourceMap));
  104. }
  105. return Promise.all(writes);
  106. }));
  107. });
  108. });
  109. }