|
| 1 | +/** |
| 2 | + * UI5-Tooling-Modules Bundler Wrapper |
| 3 | + * |
| 4 | + * Wraps ui5-tooling-modules to provide a consistent API for comparison |
| 5 | + * with our custom Rollup-based bundler. |
| 6 | + */ |
| 7 | + |
| 8 | +import {createRequire} from "module"; |
| 9 | +import {mkdir, writeFile, readFile} from "fs/promises"; |
| 10 | +import {join, dirname} from "path"; |
| 11 | +import {fileURLToPath} from "url"; |
| 12 | + |
| 13 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 14 | +const require = createRequire(import.meta.url); |
| 15 | + |
| 16 | +// Read the package.json for proper projectInfo |
| 17 | +const pkgJsonPath = join(__dirname, "../package.json"); |
| 18 | +let pkgJson; |
| 19 | +try { |
| 20 | + pkgJson = JSON.parse(await readFile(pkgJsonPath, "utf-8")); |
| 21 | +} catch { |
| 22 | + pkgJson = { |
| 23 | + name: "npm-integration-poc", |
| 24 | + version: "1.0.0" |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +// Create a proper projectInfo for the util module |
| 29 | +const projectInfo = { |
| 30 | + name: "npm-integration-poc", |
| 31 | + namespace: "npm/integration/poc", |
| 32 | + type: "application", |
| 33 | + framework: { |
| 34 | + name: "SAPUI5", |
| 35 | + version: "1.120.0" // Minimum version for WebComponents support |
| 36 | + }, |
| 37 | + pkgJson // ui5-tooling-modules needs this! |
| 38 | +}; |
| 39 | + |
| 40 | +// Minimal logger |
| 41 | +const log = { |
| 42 | + verbose: () => {}, |
| 43 | + info: (msg) => console.log(` [ui5-tooling-modules] ${msg}`), |
| 44 | + warn: (msg) => console.warn(` [ui5-tooling-modules] ⚠️ ${msg}`), |
| 45 | + error: (msg) => console.error(` [ui5-tooling-modules] ❌ ${msg}`) |
| 46 | +}; |
| 47 | + |
| 48 | +/** |
| 49 | + * Bundle a module using ui5-tooling-modules |
| 50 | + * |
| 51 | + * @param {string|string[]} moduleNames - Module(s) to bundle |
| 52 | + * @param {object} options - Configuration options |
| 53 | + * @returns {Promise<object>} Bundle info with code and metadata |
| 54 | + */ |
| 55 | +export async function bundleWithUI5ToolingModules(moduleNames, options = {}) { |
| 56 | + // Load the util module (CommonJS) |
| 57 | + const utilFactory = require("ui5-tooling-modules/lib/util.js"); |
| 58 | + const util = utilFactory(log, projectInfo); |
| 59 | + |
| 60 | + // Ensure moduleNames is an array |
| 61 | + const modules = Array.isArray(moduleNames) ? moduleNames : [moduleNames]; |
| 62 | + |
| 63 | + try { |
| 64 | + // Use getBundleInfo which handles caching and returns structured output |
| 65 | + const bundleInfo = await util.getBundleInfo(modules, { |
| 66 | + skipCache: true, |
| 67 | + pluginOptions: options.pluginOptions || {}, |
| 68 | + ...options |
| 69 | + }, { |
| 70 | + cwd: options.cwd || process.cwd() |
| 71 | + }); |
| 72 | + |
| 73 | + if (bundleInfo.error) { |
| 74 | + throw new Error(`Bundling failed: ${bundleInfo.error.message || bundleInfo.error}`); |
| 75 | + } |
| 76 | + |
| 77 | + return bundleInfo; |
| 78 | + } catch (error) { |
| 79 | + console.error(` [ui5-tooling-modules] Error bundling ${modules.join(", ")}:`, error.message); |
| 80 | + throw error; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Write ui5-tooling-modules bundle output to dist folder |
| 86 | + * |
| 87 | + * @param {string} scenarioName - Scenario folder name |
| 88 | + * @param {object} bundleInfo - Bundle info from ui5-tooling-modules |
| 89 | + * @returns {Promise<string[]>} Array of written file paths |
| 90 | + */ |
| 91 | +export async function writeUI5ToolingOutput(scenarioName, bundleInfo) { |
| 92 | + const outputDir = join(__dirname, `../dist/${scenarioName}/ui5-tooling-modules`); |
| 93 | + const writtenFiles = []; |
| 94 | + |
| 95 | + await mkdir(outputDir, {recursive: true}); |
| 96 | + |
| 97 | + // Write all entries (modules and chunks) |
| 98 | + for (const entry of bundleInfo.getEntries()) { |
| 99 | + if (entry.code) { |
| 100 | + const fileName = entry.name.endsWith(".js") ? entry.name : `${entry.name}.js`; |
| 101 | + const filePath = join(outputDir, fileName); |
| 102 | + |
| 103 | + await mkdir(dirname(filePath), {recursive: true}); |
| 104 | + await writeFile(filePath, entry.code, "utf-8"); |
| 105 | + writtenFiles.push({ |
| 106 | + path: filePath, |
| 107 | + name: entry.name, |
| 108 | + size: entry.code.length, |
| 109 | + type: entry.type |
| 110 | + }); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return writtenFiles; |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Get the main bundle code from bundleInfo |
| 119 | + * |
| 120 | + * @param {object} bundleInfo - Bundle info from ui5-tooling-modules |
| 121 | + * @param {string} moduleName - Module name to get code for |
| 122 | + * @returns {string} Bundle code |
| 123 | + */ |
| 124 | +export function getMainBundleCode(bundleInfo, moduleName) { |
| 125 | + const entry = bundleInfo.getEntry(moduleName); |
| 126 | + return entry?.code || ""; |
| 127 | +} |
| 128 | + |
| 129 | +/** |
| 130 | + * Print ui5-tooling-modules bundle summary |
| 131 | + * |
| 132 | + * @param {object} bundleInfo - Bundle info from ui5-tooling-modules |
| 133 | + * @param {string} label - Label for the summary |
| 134 | + */ |
| 135 | +export function printUI5ToolingSummary(bundleInfo, label = "ui5-tooling-modules") { |
| 136 | + const entries = bundleInfo.getEntries(); |
| 137 | + const modules = bundleInfo.getModules(); |
| 138 | + const chunks = bundleInfo.getChunks(); |
| 139 | + |
| 140 | + let totalSize = 0; |
| 141 | + for (const entry of entries) { |
| 142 | + if (entry.code) { |
| 143 | + totalSize += entry.code.length; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + console.log(`\n📦 ${label} Output:`); |
| 148 | + console.log(` Modules: ${modules.length}`); |
| 149 | + console.log(` Chunks: ${chunks.length}`); |
| 150 | + console.log(` Total size: ${(totalSize / 1024).toFixed(2)} KB`); |
| 151 | + |
| 152 | + // List files |
| 153 | + for (const entry of entries) { |
| 154 | + if (entry.code) { |
| 155 | + const sizeKB = (entry.code.length / 1024).toFixed(2); |
| 156 | + const typeIcon = entry.type === "module" ? "📄" : "📦"; |
| 157 | + console.log(` ${typeIcon} ${entry.name}.js (${sizeKB} KB)`); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments