Skip to content

Commit

Permalink
dynamic ushell src
Browse files Browse the repository at this point in the history
  • Loading branch information
schreckstefan committed Jul 25, 2024
1 parent a369c8b commit ed9c37d
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 22 deletions.
26 changes: 26 additions & 0 deletions packages/preview-middleware-client/src/flp/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Calculates the script content for accessing the right sap/ushell/bootstrap sandbox.
*/
(window as any)['sap-ui-config'] = {
'open-ux-bootTask': ushellBootstrap
};

function ushellBootstrap(fnCallback: any) {
fetch('/resources/sap-ui-version.json')
.then((resp) => resp.json())
.then((json) => {
const version = json?.version;
const major = version ? parseInt(version.split('.')[0], 10) : 2;
const src =
major >= 2
? '/resources/sap/ushell/bootstrap/sandbox2.js'
: '/test-resources/sap/ushell/bootstrap/sandbox.js';
const shellBootstrap = document.getElementById('sap-ushell-bootstrap');
if (shellBootstrap) {
shellBootstrap.onload = () => {
(window as any)['sap-ui-config']['open-ux-bootTask'](fnCallback);
};
shellBootstrap.setAttribute('src', src);
}
});
}
23 changes: 16 additions & 7 deletions packages/preview-middleware-client/src/flp/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import IconPool from 'sap/ui/core/IconPool';
import ResourceBundle from 'sap/base/i18n/ResourceBundle';
import AppState from 'sap/ushell/services/AppState';
import { getManifestAppdescr } from '../adp/api-handler';
import VersionInfo from 'sap/ui/VersionInfo';

/**
* SAPUI5 delivered namespaces from https://ui5.sap.com/#/api/sap
Expand Down Expand Up @@ -244,14 +245,14 @@ export async function init({
const urlParams = new URLSearchParams(window.location.search);
const container = sap?.ushell?.Container ?? (sap.ui.require('sap/ushell/Container') as typeof sap.ushell.Container);
let scenario: string = '';
const { version } = (await VersionInfo.load()) as { version: string };
// Register RTA if configured
if (flex) {
const flexSettings = JSON.parse(flex) as FlexSettings;
scenario = flexSettings.scenario;
container.attachRendererCreatedEvent(async function () {
const lifecycleService = await container.getServiceAsync<AppLifeCycle>('AppLifeCycle');
lifecycleService.attachAppLoaded((event) => {
const version = sap.ui.version;
const minor = parseInt(version.split('.')[1], 10);
const view = event.getParameter('componentInstance');
const flexSettings = JSON.parse(flex) as FlexSettings;
Expand Down Expand Up @@ -304,15 +305,23 @@ export async function init({
const resourceBundle = await loadI18nResourceBundle(scenario as Scenario);
setI18nTitle(resourceBundle);
registerSAPFonts();
const renderer = await container.createRenderer(undefined, true);
const major = version ? parseInt(version.split('.')[0], 10) : 2;
const renderer =
major < 2
? await container.createRenderer(undefined, true)
: await (container as any).createRendererInternal(undefined, true);
renderer.placeAt('content');
}

const bootstrapConfig = document.getElementById('sap-ui-bootstrap');
if (bootstrapConfig) {
init({
appUrls: bootstrapConfig.getAttribute('data-open-ux-preview-libs-manifests'),
flex: bootstrapConfig.getAttribute('data-open-ux-preview-flex-settings'),
customInit: bootstrapConfig.getAttribute('data-open-ux-preview-customInit')
}).catch(() => Log.error('Sandbox initialization failed.'));
try {
init({
appUrls: bootstrapConfig.getAttribute('data-open-ux-preview-libs-manifests'),
flex: bootstrapConfig.getAttribute('data-open-ux-preview-flex-settings'),
customInit: bootstrapConfig.getAttribute('data-open-ux-preview-customInit')
});
} catch (e) {
Log.error('Sandbox initialization failed: ' + e.message);
}
}
13 changes: 8 additions & 5 deletions packages/preview-middleware-client/src/flp/initConnectors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import VersionInfo from 'sap/ui/VersionInfo';

/**
* Initializes UI5 connectors based on the current UI5 version.
*
Expand All @@ -7,13 +9,14 @@
*
* @example
* intiConnectors(); // Simply call the function without any arguments.
* @returns {void}
*/
export function initConnectors(): void {
const version = sap.ui.version;
const minor = parseInt(version.split('.')[1], 10);
export async function initConnectors(): Promise<void> {
const { version } = (await VersionInfo.load()) as { version: string };
const versionArray = version ? version.split('.') : ['2', '99'];
const minor = parseInt(versionArray[1], 10);
const major = parseInt(versionArray[0], 10);

if (minor < 72) {
if (major === 1 && minor < 72) {
sap.ui.require(['open/ux/preview/client/flp/enableFakeConnector'], function (enableFakeConnector: () => void) {
enableFakeConnector();
});
Expand Down
3 changes: 2 additions & 1 deletion packages/preview-middleware/templates/flp/sandbox.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
};
</script>

<script src="<%- basePath %>/test-resources/sap/ushell/bootstrap/sandbox.js" id="sap-ushell-bootstrap"></script>
<script src="/preview/client/flp/bootstrap.js" id="preview-bootstrap"></script>
<script id="sap-ushell-bootstrap"></script>
<!-- Bootstrap the UI5 core library. 'data-sap-ui-frameOptions="allow"'' is a NON-SECURE setting for test environments -->
<script id="sap-ui-bootstrap"
src="<%- basePath %>/resources/sap-ui-core.js"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ exports[`config generatePreviewFiles minimum settings 1`] = `
};
</script>
<script src=\\"../test-resources/sap/ushell/bootstrap/sandbox.js\\" id=\\"sap-ushell-bootstrap\\"></script>
<script src=\\"/preview/client/flp/bootstrap.js\\" id=\\"preview-bootstrap\\"></script>
<script id=\\"sap-ushell-bootstrap\\"></script>
<!-- Bootstrap the UI5 core library. 'data-sap-ui-frameOptions=\\"allow\\"'' is a NON-SECURE setting for test environments -->
<script id=\\"sap-ui-bootstrap\\"
src=\\"../resources/sap-ui-core.js\\"
Expand Down Expand Up @@ -156,7 +157,8 @@ Object {
};
</script>
<script src=\\"../test-resources/sap/ushell/bootstrap/sandbox.js\\" id=\\"sap-ushell-bootstrap\\"></script>
<script src=\\"/preview/client/flp/bootstrap.js\\" id=\\"preview-bootstrap\\"></script>
<script id=\\"sap-ushell-bootstrap\\"></script>
<!-- Bootstrap the UI5 core library. 'data-sap-ui-frameOptions=\\"allow\\"'' is a NON-SECURE setting for test environments -->
<script id=\\"sap-ui-bootstrap\\"
src=\\"../resources/sap-ui-core.js\\"
Expand Down Expand Up @@ -236,7 +238,8 @@ Object {
};
</script>
<script src=\\"../test-resources/sap/ushell/bootstrap/sandbox.js\\" id=\\"sap-ushell-bootstrap\\"></script>
<script src=\\"/preview/client/flp/bootstrap.js\\" id=\\"preview-bootstrap\\"></script>
<script id=\\"sap-ushell-bootstrap\\"></script>
<!-- Bootstrap the UI5 core library. 'data-sap-ui-frameOptions=\\"allow\\"'' is a NON-SECURE setting for test environments -->
<script id=\\"sap-ui-bootstrap\\"
src=\\"../resources/sap-ui-core.js\\"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,8 @@ exports[`FlpSandbox router editor with config 1`] = `
};
</script>
<script src=\\"../test-resources/sap/ushell/bootstrap/sandbox.js\\" id=\\"sap-ushell-bootstrap\\"></script>
<script src=\\"/preview/client/flp/bootstrap.js\\" id=\\"preview-bootstrap\\"></script>
<script id=\\"sap-ushell-bootstrap\\"></script>
<!-- Bootstrap the UI5 core library. 'data-sap-ui-frameOptions=\\"allow\\"'' is a NON-SECURE setting for test environments -->
<script id=\\"sap-ui-bootstrap\\"
src=\\"../resources/sap-ui-core.js\\"
Expand Down Expand Up @@ -586,7 +587,8 @@ exports[`FlpSandbox router rta 1`] = `
};
</script>
<script src=\\"../test-resources/sap/ushell/bootstrap/sandbox.js\\" id=\\"sap-ushell-bootstrap\\"></script>
<script src=\\"/preview/client/flp/bootstrap.js\\" id=\\"preview-bootstrap\\"></script>
<script id=\\"sap-ushell-bootstrap\\"></script>
<!-- Bootstrap the UI5 core library. 'data-sap-ui-frameOptions=\\"allow\\"'' is a NON-SECURE setting for test environments -->
<script id=\\"sap-ui-bootstrap\\"
src=\\"../resources/sap-ui-core.js\\"
Expand Down Expand Up @@ -694,7 +696,8 @@ exports[`FlpSandbox router rta with developerMode=true 2`] = `
};
</script>
<script src=\\"../test-resources/sap/ushell/bootstrap/sandbox.js\\" id=\\"sap-ushell-bootstrap\\"></script>
<script src=\\"/preview/client/flp/bootstrap.js\\" id=\\"preview-bootstrap\\"></script>
<script id=\\"sap-ushell-bootstrap\\"></script>
<!-- Bootstrap the UI5 core library. 'data-sap-ui-frameOptions=\\"allow\\"'' is a NON-SECURE setting for test environments -->
<script id=\\"sap-ui-bootstrap\\"
src=\\"../resources/sap-ui-core.js\\"
Expand Down Expand Up @@ -787,7 +790,8 @@ exports[`FlpSandbox router rta with developerMode=true and plugin 1`] = `
};
</script>
<script src=\\"../test-resources/sap/ushell/bootstrap/sandbox.js\\" id=\\"sap-ushell-bootstrap\\"></script>
<script src=\\"/preview/client/flp/bootstrap.js\\" id=\\"preview-bootstrap\\"></script>
<script id=\\"sap-ushell-bootstrap\\"></script>
<!-- Bootstrap the UI5 core library. 'data-sap-ui-frameOptions=\\"allow\\"'' is a NON-SECURE setting for test environments -->
<script id=\\"sap-ui-bootstrap\\"
src=\\"../resources/sap-ui-core.js\\"
Expand Down Expand Up @@ -880,7 +884,8 @@ exports[`FlpSandbox router test/flp.html 1`] = `
};
</script>
<script src=\\"../test-resources/sap/ushell/bootstrap/sandbox.js\\" id=\\"sap-ushell-bootstrap\\"></script>
<script src=\\"/preview/client/flp/bootstrap.js\\" id=\\"preview-bootstrap\\"></script>
<script id=\\"sap-ushell-bootstrap\\"></script>
<!-- Bootstrap the UI5 core library. 'data-sap-ui-frameOptions=\\"allow\\"'' is a NON-SECURE setting for test environments -->
<script id=\\"sap-ui-bootstrap\\"
src=\\"../resources/sap-ui-core.js\\"
Expand Down
2 changes: 1 addition & 1 deletion packages/ui5-proxy-middleware/src/base/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const ui5Proxy = (config: ProxyConfig, options?: Options, filter?: Filter
});
const today = new Date();
const etag = `W/"${config.version || 'ui5-latest-' + today.getDate() + today.getMonth() + today.getFullYear()}"`;
const ui5Ver = config.version ? `/${config.version}` : '';
const ui5Ver = config.version && !config.version.startsWith('2') ? `/${config.version}` : '';
const proxyConfig: Options = {
target: config.url,
changeOrigin: true,
Expand Down

0 comments on commit ed9c37d

Please sign in to comment.