|
| 1 | +/** |
| 2 | + * Rollup Plugin: UI5 AMD Exports |
| 3 | + * |
| 4 | + * Transforms Rollup's named exports pattern: |
| 5 | + * sap.ui.define(['exports'], function(exports) { exports.foo = ...; }) |
| 6 | + * |
| 7 | + * Into UI5-compatible pattern: |
| 8 | + * sap.ui.define([], function() { const exports = {}; ...; return exports; }) |
| 9 | + * |
| 10 | + * This is necessary because UI5's sap.ui.define doesn't support the 'exports' dependency. |
| 11 | + */ |
| 12 | + |
| 13 | +export default function ui5AmdExportsPlugin() { |
| 14 | + return { |
| 15 | + name: "ui5-amd-exports", |
| 16 | + renderChunk(code) { |
| 17 | + // Pattern: sap.ui.define(['exports'], (function (exports) { 'use strict'; |
| 18 | + const exportsPattern = /^(sap\.ui\.define\(\[)'exports'(\],\s*\(function\s*\()(\w+)(\)\s*\{\s*'use strict';)/; |
| 19 | + const match = code.match(exportsPattern); |
| 20 | + |
| 21 | + if (!match) { |
| 22 | + // No exports dependency, return as-is |
| 23 | + return null; |
| 24 | + } |
| 25 | + |
| 26 | + // Get the parameter name (usually 'exports', but could be mangled) |
| 27 | + const exportsVarName = match[3]; |
| 28 | + |
| 29 | + // Transform the code: |
| 30 | + // 1. Remove 'exports' from dependencies |
| 31 | + // 2. Remove parameter from function |
| 32 | + // 3. Add const declaration at the start |
| 33 | + // 4. Add return statement at the end |
| 34 | + |
| 35 | + let transformed = code; |
| 36 | + |
| 37 | + // Remove 'exports' dependency and parameter, add const declaration |
| 38 | + transformed = transformed.replace( |
| 39 | + exportsPattern, |
| 40 | + `$1$2$4\n\n const ${exportsVarName} = {};` |
| 41 | + ); |
| 42 | + |
| 43 | + // Add return statement before the closing })); |
| 44 | + const closingPattern = /(\s*)\}\)\);?\s*$/; |
| 45 | + transformed = transformed.replace(closingPattern, `\n$1 return ${exportsVarName};\n$1}));\n`); |
| 46 | + |
| 47 | + return {code: transformed, map: null}; |
| 48 | + } |
| 49 | + }; |
| 50 | +} |
0 commit comments