Skip to content

Commit 6c934fa

Browse files
committed
don't pass shiki to client
1 parent 9ebcb95 commit 6c934fa

File tree

10 files changed

+75
-20
lines changed

10 files changed

+75
-20
lines changed

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
"@babel/generator": "^7.27.3",
4444
"@babel/types": "^7.27.3",
4545
"@clack/prompts": "^0.10.1",
46-
"@node-core/rehype-shiki": "^1.0.1-1815fa769361b836fa52cfab9c5bd4991f571c95",
47-
"@node-core/ui-components": "^1.0.1-f349d7e1aeb03ea35fd3eca134fd48e02e7bf717",
46+
"@node-core/rehype-shiki": "^1.0.1-601b74e6d7dfc706eb99e854fc96120bea42f03a",
47+
"@node-core/ui-components": "^1.0.1-6cb8b0a0c75c24f5ccc84bb07a1ea9b4b810abd2",
4848
"@orama/orama": "^3.1.6",
4949
"@orama/plugin-data-persistence": "^3.1.6",
5050
"@orama/react-components": "^0.8.0",

src/generators/web/client/components/CodeBox.jsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
import { useState } from 'react';
22
import BaseCodeBox from '@node-core/ui-components/Common/BaseCodeBox';
3-
import { getLanguageDisplayName } from '@node-core/rehype-shiki';
3+
import { staticData } from '../data.mjs';
4+
5+
const languageDisplayNameMap = new Map(staticData.shikiDisplayNameMap);
6+
7+
/**
8+
* Get the display name of a language
9+
* @param {string} language The language ID
10+
*/
11+
export const getLanguageDisplayName = language => {
12+
const entry = Array.from(languageDisplayNameMap.entries()).find(([aliases]) =>
13+
aliases.includes(language.toLowerCase())
14+
);
15+
16+
return entry?.[1] ?? language.toLowerCase();
17+
};
418

519
const MDXCodeBox = ({ className, ...props }) => {
620
const matches = className?.match(/language-(?<language>[a-zA-Z]+)/);

src/generators/web/client/data.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// eslint-disable-next-line no-undef
2+
export const staticData = __STATIC_DATA__;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createStaticData } from '../utils/staticData.mjs';
2+
3+
declare global {
4+
const __STATIC_DATA__: ReturnType<typeof createStaticData>;
5+
}

src/generators/web/index.mjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { estreeToBabel } from 'estree-to-babel';
66
import Mustache from 'mustache';
77

88
import { ESBUILD_RESOLVE_DIR } from './constants.mjs';
9-
import createASTBuilder from './utils/build.mjs';
9+
import createASTBuilder from './utils/astBuilder.mjs';
1010
import bundleCode from './utils/bundle.mjs';
1111

1212
/**
@@ -17,7 +17,9 @@ import bundleCode from './utils/bundle.mjs';
1717
*/
1818
async function executeServerCode(serverCode, require) {
1919
// Bundle the server code for execution
20-
const { js: bundledServer } = await bundleCode(serverCode, true);
20+
const { js: bundledServer } = await bundleCode(serverCode, {
21+
platform: 'node',
22+
});
2123

2224
// Create a safe execution context that returns the rendered content
2325
const executedFunction = new Function(

src/generators/web/utils/bundle.mjs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import stylePlugin from 'esbuild-style-plugin';
77
import postcssCalc from 'postcss-calc';
88

99
import { ESBUILD_RESOLVE_DIR } from '../constants.mjs';
10+
import staticData from './staticData.mjs';
1011

1112
const uiComponentsResolver = {
1213
name: 'ui-components-resolver',
@@ -53,25 +54,27 @@ const uiComponentsResolver = {
5354
/**
5455
* Bundles JavaScript code and returns JS/CSS content
5556
* @param {string} code - Source code to bundle
56-
* @param {boolean} server
57+
* @param {import('esbuild').BuildOptions} options
5758
* @returns {Promise<{js: string, css: string}>}
5859
*/
59-
export default async (code, server) => {
60+
export default async (code, options) => {
6061
const result = await esbuild.build({
6162
stdin: {
6263
contents: code,
6364
resolveDir: ESBUILD_RESOLVE_DIR,
6465
loader: 'jsx',
6566
},
6667
bundle: true,
67-
minify: false,
68-
sourcemap: 'inline',
68+
minify: true,
6969
format: 'iife',
7070
target: 'es2020',
71-
platform: server ? 'node' : 'browser',
71+
platform: 'browser',
7272
jsx: 'automatic',
7373
write: false,
74-
// This output file is a pseudo-file. It's never written to (`write: false`),
74+
define: {
75+
__STATIC_DATA__: staticData,
76+
},
77+
// This output file is a pseudo-file. It's never written (`write: false`),
7578
// but it gives ESLint a basename for the output.
7679
outfile: 'output.js',
7780
plugins: [
@@ -82,6 +85,7 @@ export default async (code, server) => {
8285
}),
8386
uiComponentsResolver,
8487
],
88+
...options,
8589
});
8690

8791
const [jsFile, cssFile] = result.outputFiles;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { shiki } from '@node-core/rehype-shiki';
2+
3+
/**
4+
* This creates the static data for the client. For example,
5+
* the map of language display names, without loading Shiki,
6+
* a very large module, on the client.
7+
*/
8+
export const createStaticData = () => {
9+
const shikiDisplayNameMap = [
10+
...new Map(
11+
shiki
12+
.getLoadedLanguages()
13+
.map(shiki.getLanguage)
14+
.map(({ name, _grammar: { aliases = [], displayName } }) => [
15+
name,
16+
[[...aliases, name], displayName],
17+
])
18+
).values(),
19+
];
20+
21+
return {
22+
/** @type {Array<[string[], string]>} */
23+
shikiDisplayNameMap,
24+
};
25+
};
26+
27+
const staticData = JSON.stringify(createStaticData());
28+
export default staticData;

src/utils/remark.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
import rehypeShikiji from '@node-core/rehype-shiki';
3+
import rehypeShikiji from '@node-core/rehype-shiki/plugin';
44
import recmaJsx from 'recma-jsx';
55
import rehypeRecma from 'rehype-recma';
66
import rehypeStringify from 'rehype-stringify';

0 commit comments

Comments
 (0)