Skip to content

Commit

Permalink
add vite replace html plugin for graphiql plugin explorer (#3742)
Browse files Browse the repository at this point in the history
* add vite replace html plugin for graphiql plugin explorer

* fix build
  • Loading branch information
dimaMachina authored Aug 20, 2024
1 parent e432fb5 commit ffaf5c9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
15 changes: 6 additions & 9 deletions packages/graphiql-plugin-explorer/example.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,27 @@
></script>

<script
src="https://unpkg.com/graphiql@4.0.0-alpha.5/dist/index.umd.js"
src="https://unpkg.com/graphiql/dist/index.umd.js"
crossorigin
></script>
<link
rel="stylesheet"
href="https://unpkg.com/graphiql@4.0.0-alpha.5/dist/style.css"
/>
<link rel="stylesheet" href="https://unpkg.com/graphiql/dist/style.css" />

<script
src="https://unpkg.com/@graphiql/plugin-explorer@4.0.0-alpha.0/dist/index.umd.js"
src="https://unpkg.com/@graphiql/plugin-explorer/dist/index.umd.js"
crossorigin
></script>
<link
rel="stylesheet"
href="https://unpkg.com/@graphiql/plugin-explorer@4.0.0-alpha.0/dist/style.css"
href="https://unpkg.com/@graphiql/plugin-explorer/dist/style.css"
/>
</head>

<body>
<div id="graphiql">
<div class="loading">Loading</div>
<div class="loading">Loading...</div>
</div>

<script>
<script type="module">
const fetcher = GraphiQL.createFetcher({
url: 'https://swapi-graphql.netlify.app/.netlify/functions/index',
});
Expand Down
41 changes: 38 additions & 3 deletions packages/graphiql-plugin-explorer/vite.config.mts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { defineConfig } from 'vite';
import { createRequire } from 'node:module';
import { defineConfig, PluginOption } from 'vite';
import react from '@vitejs/plugin-react';
import svgr from 'vite-plugin-svgr';
import packageJSON from './package.json';
import dts from 'vite-plugin-dts';
import packageJSON from './package.json';

const IS_UMD = process.env.UMD === 'true';

Expand All @@ -15,7 +16,7 @@ export default defineConfig({
titleProp: true,
},
}),
!IS_UMD && dts({ rollupTypes: true }),
!IS_UMD && [dts({ rollupTypes: true }), htmlPlugin()],
],
build: {
minify: IS_UMD
Expand Down Expand Up @@ -51,3 +52,37 @@ export default defineConfig({
},
},
});

function htmlPlugin(): PluginOption {
const require = createRequire(import.meta.url);

const graphiqlPath = require
.resolve('graphiql/package.json')
.replace('/package.json', '');

const htmlForVite = `<link rel="stylesheet" href="${graphiqlPath}/src/style.css" />
<script type="module">
import React from 'react';
import ReactDOM from 'react-dom/client';
import GraphiQL from '${graphiqlPath}/src/cdn';
import * as GraphiQLPluginExplorer from './src';
Object.assign(globalThis, { React, ReactDOM, GraphiQL, GraphiQLPluginExplorer });
</script>`;

return {
name: 'html-replace-umd-with-src',
transformIndexHtml: {
order: 'pre',
handler(html) {
const start = '</style>';
const end = '<body>';
const contentToReplace = html.slice(
html.indexOf(start) + start.length,
html.indexOf(end),
);
return html.replace(contentToReplace, htmlForVite);
},
},
};
}

0 comments on commit ffaf5c9

Please sign in to comment.