Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom code - webpack #2816

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CustomComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Your pre-compiled CommonJS code
const React = require('react');

const MyComponent = () => {
return React.createElement('p', null, 'TEST123');
};

module.exports = MyComponent;
872 changes: 546 additions & 326 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@
"recoil": "^0.7.5",
"resize-observer-polyfill": "^1.5.1",
"rfc6902": "^5.0.1",
"url": "^0.11.0"
"url": "^0.11.0",
"webpack": "^5.90.3",
"webpack-cli": "^5.1.4"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.16.2",
Expand Down
82 changes: 81 additions & 1 deletion public/extensions/extensions.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,81 @@
[]
general:
resource:
kind: Application
group: applicationconnector.kyma-project.io
version: v1alpha1
urlPath: applications
scope: cluster
name: Applications
category: Integration
description: description
details:
header:
- name: headers.description
source: spec.description
widget: CustomCode
- name: headers.access-label
source: spec.accessLabel
body:
- widget: Table
source: spec.services
name: headers.services-and-events
children:
- source: $count($item.entries[type='API'])
name: headers.apis
- source: $count($item.entries[type='Events'])
name: headers.events
- source: $item.providerDisplayName
name: headers.provider
collapsibleTitle: $item.displayName
collapsible:
- widget: Plain
children:
- source: $item.longDescription
- widget: Table
name: headers.apis
source: $item.entries[type = 'API']
children:
- source: $item.name
name: headers.name
- source: $item.accessLabel
name: headers.access-label
- widget: ExternalLink
source: $item.centralGatewayUrl
name: headers.gateway-url
copyable: true
- widget: Table
name: headers.events
source: $item.entries[type = 'Events']
children:
- source: $item.name
name: headers.name
- source: $item.accessLabel
name: headers.access-label
- widget: ExternalLink
source: $item.centralGatewayUrl
name: headers.gateway-url
copyable: true
form:
- path: metadata.name
extraPaths:
- spec.accessLabel
- metadata.labels['app.kubernetes.io/name']
- path: spec.description
placeholder: placeholders.description
- path: spec.accessLabel
translations:
en:
description: >-
{{[Application](https://kyma-project.io/docs/kyma/latest/05-technical-reference/00-custom-resources/ac-01-application)}}
represents an external solution connected to Kyma.
headers:
access-label: Access Label
apis: APIs
description: Description
events: Events
gateway-url: Central Gateway URL
name: Name
provider: Provider
services-and-events: Provided Services and Events
placeholders:
description: Provide a description for your Application
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import DynamicGitHubComponent from './DynamicGitHubComponent';

export function CustomCode({
value,
structure,
schema,
singleRootResource,
embedResource,
...props
}) {
const githubUrl =
'https://raw.githubusercontent.com/mrCherry97/busola/custom-code-babel/CustomComponent.js';

return (
<div>
<DynamicGitHubComponent githubUrl={githubUrl} />
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Your React component
import React, { useEffect, useState } from 'react';
import path from 'path';
import webpack from 'webpack';
import fs from 'fs';

const DynamicGitHubComponent = ({ githubUrl }) => {
const [renderedComponent, setRenderedComponent] = useState(null);

useEffect(() => {
const fetchAndRenderComponent = async () => {
try {
// Dynamically generate Webpack configuration
const webpackConfig = {
mode: 'production',
entry: githubUrl,
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
libraryTarget: 'commonjs2',
},
module: {
rules: [
{
test: /\.js$/,
use: 'webpack-http-loader',
},
],
},
};

// Run Webpack
const outputPath = webpackConfig.output.path;
await webpack(webpackConfig);

// Determine whether the component is using CommonJS or ES6 module syntax
const isCommonJS = fs.existsSync(`${outputPath}/bundle.js`); // Assuming bundle.js for CommonJS

// Dynamically import and render the component
const { default: componentFactory } = await import(
`./${outputPath}/bundle${isCommonJS ? '' : '.mjs'}`
);

// Update the state with the rendered component
setRenderedComponent(React.createElement(componentFactory));
} catch (error) {
console.error(
`Error loading and rendering component from URL ${githubUrl}:`,
error,
);
}
};

fetchAndRenderComponent();
}, [githubUrl]);

return <div>{renderedComponent || <div>Loading...</div>}</div>;
};

export default DynamicGitHubComponent;
2 changes: 2 additions & 0 deletions src/components/Extensibility/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { FeaturedCard } from './FeaturedCard/FeaturedCard';
import { APIRuleHost } from './APIRules/APIRuleHost';

import { PendingWrapper } from './PendingWrapper';
import { CustomCode } from './CustomCode/CustomCodeRenderer';

export const widgets = {
Null: () => '',
Expand All @@ -48,6 +49,7 @@ export const widgets = {
Text,
Wizard,
FeaturedCard,
CustomCode,
};

export const valuePreprocessors = {
Expand Down
Loading