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

[Feat][GRAFX-3857] Produce es module in addition to the "iife" for modern support #221

Merged
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
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,18 @@ If you need help generating a token or would like code samples, please see our [
<!-- div where studio ui will be constructed in -->
<div id="studio-ui-container"></div>

<!-- script to inject latest studio ui -->
<!-- 1. ES5 approach script to inject latest studio ui -->
<script src="https://studio-cdn.chiligrafx.com/studio-ui/latest/bundle.js"></script>

<!-- OR -->

<!-- 2. ES Modules approach (recommended one) script to inject latest studio ui -->
<script type="module">
import('https://studio-cdn.chiligrafx.com/studio-ui/latest/es-module/bundle.js').then((module) => {
window.StudioUI = module.default;
});
</script>

<!-- custom logic -->
<script>
/* your access token, should be gathered on the fly, is just here for demo purposes. */
Expand Down
11 changes: 10 additions & 1 deletion documentation/advanced-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,18 @@ Create a new html file, you can call it whatever you want, but in our case we ca
<!-- div where studio ui will be constructed in -->
<div id="studio-ui-container"></div>

<!-- script to inject latest studio ui -->
<!-- 1. ES5 approach script to inject latest studio ui -->
<script src="https://studio-cdn.chiligrafx.com/studio-ui/latest/bundle.js"></script>

<!-- OR -->

<!-- 2. ES Modules approach (recommended one) script to inject latest studio ui -->
<script type="module">
import('https://studio-cdn.chiligrafx.com/studio-ui/latest/es-module/bundle.js').then((module) => {
window.StudioUI = module.default;
});
</script>

<!-- custom logic in seperate js file -->
<script srs="./integration.js"></script>
</body>
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"license": "MIT",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"build": "tsc && npm run build:iife && npm run build:module",
"build:iife": "OUTPUT_FORMAT=iife vite build",
"build:module": "OUTPUT_FORMAT=module vite build",
"preview": "vite preview",
"testw": "jest --watch --silent",
"test": "jest",
Expand Down
3 changes: 2 additions & 1 deletion src/_dev-execution/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// This is an entry point when running standalone version of studio workspace in dev mode
// It's not going to be bundled to the main `bundle.js` file

import StudioUI from '../main';
import { TokenManager } from './token-manager';

(async () => {
Expand Down Expand Up @@ -64,7 +65,7 @@ import { TokenManager } from './token-manager';
} else {
authToken = await tokenManager.getAccessToken();
}
window.StudioUI.studioUILoaderConfig({
StudioUI.studioUILoaderConfig({
selector: 'sui-root',
projectId,
projectName: 'Dev Run',
Expand Down
3 changes: 3 additions & 0 deletions src/es-index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import StudioUI from './main';

export default StudioUI;
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import StudioUI from './main';

declare global {
interface Window {
StudioUI: typeof StudioUI;
}
}

// This is an entry file for 'iife' format
window.StudioUI = StudioUI;
33 changes: 12 additions & 21 deletions src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
import { ITheme } from '@chili-publish/grafx-shared-components';
import { AxiosResponse } from 'axios';
import React from 'react';
import ReactDOM from 'react-dom/client';
import { AxiosResponse } from 'axios';
import { ITheme } from '@chili-publish/grafx-shared-components';
import App from './App';
import { DemoDocumentLoader } from './DemoDocumentLoader';
import { StudioProjectLoader } from './StudioProjectLoader';
import './index.css';
import { ConnectorAuthenticationResult } from './types/ConnectorAuthenticationResult';
import {
defaultBackFn,
defaultOutputSettings,
defaultPlatformUiOptions,
defaultUiOptions,
DownloadLinkResult,
IStudioUILoaderConfig,
OutputSettings,
PaginatedResponse,
Project,
ProjectConfig,
StudioConfig,
UiOptions,
defaultBackFn,
defaultOutputSettings,
defaultPlatformUiOptions,
defaultUiOptions,
IStudioUILoaderConfig,
UserInterface,
PaginatedResponse,
UserInterfaceWithOutputSettings,
} from './types/types';
import { DemoDocumentLoader } from './DemoDocumentLoader';
import { StudioProjectLoader } from './StudioProjectLoader';
import './index.css';
import { ConnectorAuthenticationResult } from './types/ConnectorAuthenticationResult';

declare global {
interface Window {
StudioUI: typeof StudioUI;
}
}

export default class StudioUI {
constructor(selector: string, projectConfig: ProjectConfig) {
Expand Down Expand Up @@ -389,6 +383,3 @@ export default class StudioUI {
);
}
}

// Make this class accessible on window
window.StudioUI = StudioUI;
28 changes: 21 additions & 7 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import EnvironmentPlugin from 'vite-plugin-environment';

// https://vitejs.dev/config/
export default ({ mode }) => {
const outputFormat = process.env.OUTPUT_FORMAT;
if (outputFormat !== 'iife' && outputFormat !== 'module') {
process.exit(1);
}
return defineConfig({
plugins: [react(), EnvironmentPlugin(['NODE_ENV', 'PREVIEW_ERROR_URL'])],
server: {
Expand All @@ -21,21 +25,31 @@ export default ({ mode }) => {
build: {
emptyOutDir: true,
rollupOptions: {
preserveEntrySignatures: 'exports-only',
input:
mode === 'development'
? {
index: './src/main.tsx',
bootstrap: './src/_dev-execution/bootstrap.ts',
}
: {
index: './src/main.tsx',
index: outputFormat === 'iife' ? './src/index.ts' : './src/es-index.ts',
},
output:
outputFormat === 'iife'
? {
entryFileNames: 'bundle.js',
chunkFileNames: 'bundle.js',
assetFileNames: 'main.[ext]',
format: 'iife',
}
: {
entryFileNames: 'bundle.js',
chunkFileNames: 'chunk.[name].js',
assetFileNames: 'main.[ext]',
format: 'es',
dir: 'dist/es-module',
},
output: {
entryFileNames: 'bundle.js',
chunkFileNames: 'bundle.js',
assetFileNames: 'main.[ext]',
format: 'iife',
},
},
},
});
Expand Down
Loading