Skip to content

Commit

Permalink
feat(core): Create build setup for GraphiQL pages
Browse files Browse the repository at this point in the history
  • Loading branch information
toBeOfUse committed Nov 30, 2024
1 parent c0e9fbc commit 38888e8
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 1 deletion.
13 changes: 13 additions & 0 deletions packages/core/build/copy-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import path from 'path';

const SCHEMAS_GLOB = '**/*.graphql';
const MESSAGES_GLOB = 'i18n/messages/**/*';
const GRAPHIQL_GLOB = 'graphiql/output/**/*';
const DEST_DIR = path.join(__dirname, '../dist');

function copyFiles(sourceGlob: string, destinationDir: string) {
Expand Down Expand Up @@ -42,17 +43,29 @@ function copyI18nMessages() {
}
}

function copyBundlededGraphiQLPage() {
try {
copyFiles(GRAPHIQL_GLOB, DEST_DIR);
console.log('Bundled GraphiQL page copied successfully!');
} catch (error) {
console.error('Error copying GraphiQL page:', error);
}
}

function build() {
copySchemas();
copyI18nMessages();
copyBundlededGraphiQLPage();
}

function watch() {
const watcher1 = chokidar.watch(SCHEMAS_GLOB, { cwd: path.join(__dirname, '../src') });
const watcher2 = chokidar.watch(MESSAGES_GLOB, { cwd: path.join(__dirname, '../src') });
const watcher3 = chokidar.watch(GRAPHIQL_GLOB, { cwd: path.join(__dirname, '../src') });

watcher1.on('change', copySchemas);
watcher2.on('change', copyI18nMessages);
watcher3.on('change', copyBundlededGraphiQLPage);

console.log('Watching for changes...');
}
Expand Down
4 changes: 3 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"scripts": {
"tsc:watch": "tsc -p ./build/tsconfig.build.json --watch",
"copy:watch": "ts-node build/copy-static.ts watch",
"build": "rimraf dist && tsc -p ./build/tsconfig.build.json && tsc -p ./build/tsconfig.cli.json && ts-node build/copy-static.ts build",
"build": "rimraf dist && tsc -p ./build/tsconfig.build.json && tsc -p ./build/tsconfig.cli.json && npm run build:graphiql && npm run build:copyfiles",
"build:copyfiles": "ts-node build/copy-static.ts build",
"build:graphiql": "webpack --config ./src/graphiql/webpack.config.js",
"watch": "concurrently npm:tsc:watch npm:copy:watch",
"lint": "eslint --fix .",
"test": "vitest --config vitest.config.mts --run",
Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/graphiql/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!doctype html>
<html lang="en">
<head>
<title>GraphiQL Playground</title>
<style>
body {
height: 100%;
margin: 0;
width: 100%;
overflow: hidden;
}

#root {
height: 100vh;
}
</style>
</head>
<body>
<div id="root"></div>
</body>
</html>
12 changes: 12 additions & 0 deletions packages/core/src/graphiql/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { createGraphiQLFetcher } from '@graphiql/toolkit';
import { GraphiQL } from 'graphiql';
import React from 'react';
import { createRoot } from 'react-dom/client';
import 'graphiql/graphiql.css';

// this is replaced when the bundled graphiql code is read in
// src/api/config/config-graphql-module.ts
const fetcher = createGraphiQLFetcher({ url: '__API_URL__' });

const root = createRoot(document.getElementById('root'));
root.render(React.createElement(GraphiQL, { fetcher: fetcher }));
45 changes: 45 additions & 0 deletions packages/core/src/graphiql/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const path = require('node:path');

const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlInlineScriptPlugin = require('html-inline-script-webpack-plugin');
const webpack = require('webpack');

/**
* @type {import('webpack').Configuration}
*/
module.exports = {
entry: path.resolve(__dirname, 'index.mjs'),
output: {
path: path.resolve(__dirname, 'dist'),
clean: true,
publicPath: '',
},
module: {
rules: [
{
test: /\.m?js$/,
loader: 'esbuild-loader',
options: {
target: 'es2015',
},
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
],
},
resolve: {
extensions: ['.js', '.json', '.jsx', '.css', '.mjs'],
},
plugins: [
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1,
}),
new HtmlWebpackPlugin({
template: path.join(__dirname, '/index.html'),
inject: 'body',
}),
new HtmlInlineScriptPlugin(),
],
};

0 comments on commit 38888e8

Please sign in to comment.