Skip to content

Commit d3d329b

Browse files
committed
fix: Correctly export ESM modules
1 parent 2e30098 commit d3d329b

2 files changed

Lines changed: 55 additions & 2 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
}

internal/npm-integration-poc/scenarios/shared-config.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import commonjs from "@rollup/plugin-commonjs";
1010
import {mkdir, writeFile} from "fs/promises";
1111
import {dirname, join} from "path";
1212
import {fileURLToPath} from "url";
13+
import ui5AmdExportsPlugin from "../lib/plugins/rollup-plugin-ui5-amd-exports.js";
1314

1415
const __dirname = dirname(fileURLToPath(import.meta.url));
1516

@@ -49,7 +50,8 @@ export async function generateAMDBundle(config) {
4950
format: "amd",
5051
amd: {
5152
define: "sap.ui.define"
52-
}
53+
},
54+
plugins: [ui5AmdExportsPlugin()]
5355
});
5456

5557
return output[0].code;
@@ -68,7 +70,8 @@ export async function generateAMDBundleWithOutput(config) {
6870
format: "amd",
6971
amd: {
7072
define: "sap.ui.define"
71-
}
73+
},
74+
plugins: [ui5AmdExportsPlugin()]
7275
});
7376

7477
return output;

0 commit comments

Comments
 (0)