Skip to content

Commit 142f3f2

Browse files
authored
graphiql 5: reduce bundle size, import prettier dynamically to avoid bundling Prettier (#4069)
* upd * Apply suggestions from code review
1 parent ad8ff3a commit 142f3f2

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

.changeset/brave-points-bathe.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
'@graphiql/react': patch
3+
'graphiql': patch
4+
---
5+
6+
reduce bundle size, import `prettier` dynamically to avoid bundling Prettier
7+
8+
diff from vite example
9+
10+
```diff
11+
-dist/assets/index-BMgFrxsd.js 4,911.53 kB │ gzip: 1,339.77 kB
12+
+dist/assets/index-BlpzusGL.js 4,221.28 kB │ gzip: 1,145.58 kB
13+
```

packages/graphiql-react/src/constants.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
/* eslint-disable no-bitwise */
22
import { initializeMode } from 'monaco-graphql/esm/lite.js';
3-
// @ts-expect-error -- wrong types
4-
import { printers } from 'prettier/plugins/graphql'; // eslint-disable-line import-x/no-duplicates
5-
import { parsers } from 'prettier/parser-graphql'; // eslint-disable-line import-x/no-duplicates
6-
import prettier from 'prettier/standalone';
73
import type { DiagnosticSettings } from 'monaco-graphql';
84
import { KeyCode, KeyMod, languages } from './monaco-editor';
95
import type { EditorSlice } from './stores';
@@ -147,13 +143,21 @@ export const MONACO_GRAPHQL_API = initializeMode({
147143
diagnosticSettings: MONACO_GRAPHQL_DIAGNOSTIC_SETTINGS,
148144
});
149145

150-
export const DEFAULT_PRETTIFY_QUERY: EditorSlice['onPrettifyQuery'] = query =>
151-
prettier.format(query, {
152-
parser: 'graphql',
153-
plugins: [
154-
// Fix: Couldn't find plugin for AST format "graphql"
155-
{ printers },
156-
// @ts-expect-error -- Fix: Couldn't resolve parser "graphql"
157-
{ parsers },
158-
],
159-
});
146+
export const DEFAULT_PRETTIFY_QUERY: EditorSlice['onPrettifyQuery'] =
147+
async query => {
148+
// We don't need to load Prettier initially; it's only used when the 'Format Query' button or shortcut is triggered
149+
// @ts-expect-error -- wrong types
150+
const { printers } = await import('prettier/plugins/graphql');
151+
const { parsers } = await import('prettier/parser-graphql');
152+
const prettier = await import('prettier/standalone');
153+
154+
return prettier.format(query, {
155+
parser: 'graphql',
156+
plugins: [
157+
// Fix: Couldn't find plugin for AST format "graphql"
158+
{ printers },
159+
// @ts-expect-error -- Fix: Couldn't resolve parser "graphql"
160+
{ parsers },
161+
],
162+
});
163+
};

packages/graphiql-react/src/utility/jsonc.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import prettier from 'prettier/standalone';
2-
// @ts-expect-error -- wrong types
3-
import { printers } from 'prettier/plugins/estree';
4-
import { parsers } from 'prettier/parser-babel';
51
import {
62
parse as jsoncParse,
73
ParseError,
84
printParseErrorCode,
95
} from 'jsonc-parser';
106

11-
export function formatJSONC(content: string) {
7+
export async function formatJSONC(content: string): Promise<string> {
8+
// We don't need to load Prettier initially; it's only used when the 'Format Query' button or shortcut is triggered
9+
const prettier = await import('prettier/standalone');
10+
// @ts-expect-error -- wrong types
11+
const { printers } = await import('prettier/plugins/estree');
12+
const { parsers } = await import('prettier/parser-babel');
13+
1214
return prettier.format(content, {
1315
parser: 'jsonc',
1416
plugins: [

0 commit comments

Comments
 (0)