Skip to content

Commit 0139a69

Browse files
committed
Prebundle Sandpack lint and theme
1 parent c8b31c6 commit 0139a69

File tree

7 files changed

+67014
-14
lines changed

7 files changed

+67014
-14
lines changed

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
"private": true,
55
"license": "CC",
66
"scripts": {
7-
"analyze": "ANALYZE=true next build --webpack",
8-
"dev": "node scripts/buildRscWorker.mjs && next dev",
7+
"analyze": "node scripts/buildRscWorker.mjs && node scripts/buildSandpackHooksLint.mjs && node scripts/buildSandpackTheme.mjs && ANALYZE=true next build --webpack",
8+
"dev": "node scripts/buildRscWorker.mjs && node scripts/buildSandpackHooksLint.mjs && node scripts/buildSandpackTheme.mjs && next dev",
99
"prebuild:rsc": "node scripts/buildRscWorker.mjs",
10-
"build": "node scripts/buildRscWorker.mjs && next build && node --experimental-modules ./scripts/downloadFonts.mjs",
10+
"build": "node scripts/buildRscWorker.mjs && node scripts/buildSandpackHooksLint.mjs && node scripts/buildSandpackTheme.mjs && next build && node --experimental-modules ./scripts/downloadFonts.mjs",
1111
"lint": "eslint .",
1212
"lint:fix": "eslint . --fix",
1313
"format:source": "prettier --config .prettierrc --write \"{plugins,src}/**/*.{js,ts,jsx,tsx,css}\"",
@@ -72,7 +72,6 @@
7272
"eslint-plugin-local-rules": "link:eslint-local-rules",
7373
"eslint-plugin-react": "7.x",
7474
"eslint-plugin-react-hooks": "^7.0.1",
75-
"eslint-plugin-react-hooks-browser": "npm:eslint-plugin-react-hooks@5.2.0",
7675
"fs-extra": "^9.0.1",
7776
"globby": "^11.0.1",
7877
"gray-matter": "^4.0.2",

scripts/buildSandpackHooksLint.mjs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import * as esbuild from 'esbuild';
9+
import fs from 'fs';
10+
import path from 'path';
11+
import {fileURLToPath} from 'url';
12+
13+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
14+
const root = path.resolve(__dirname, '..');
15+
const generatedHooksLintOutfile = path.resolve(
16+
root,
17+
'src/components/MDX/Sandpack/generatedHooksLint.ts'
18+
);
19+
20+
const stubbedModules = new Set([
21+
'fs',
22+
'path',
23+
'crypto',
24+
'util',
25+
'assert',
26+
'@babel/core',
27+
'@babel/parser',
28+
'hermes-parser',
29+
]);
30+
31+
const stubPlugin = {
32+
name: 'stub-sandpack-hooks-runtime-deps',
33+
setup(build) {
34+
build.onResolve({filter: /.*/}, (args) => {
35+
if (stubbedModules.has(args.path)) {
36+
return {
37+
path: args.path,
38+
namespace: 'sandpack-hooks-stub',
39+
};
40+
}
41+
});
42+
43+
build.onLoad(
44+
{filter: /.*/, namespace: 'sandpack-hooks-stub'},
45+
() => ({
46+
contents: `
47+
const stub = new Proxy(function () { return stub; }, {
48+
get(_target, prop) {
49+
if (prop === Symbol.toPrimitive) return () => '';
50+
if (prop === 'toString') return () => '';
51+
if (prop === 'valueOf') return () => '';
52+
if (prop === 'then') return undefined;
53+
return stub;
54+
},
55+
apply() { return stub; },
56+
construct() { return stub; },
57+
});
58+
59+
module.exports = stub;
60+
`,
61+
loader: 'js',
62+
})
63+
);
64+
},
65+
};
66+
67+
await esbuild.build({
68+
stdin: {
69+
contents: `
70+
const plugin = require('eslint-plugin-react-hooks');
71+
72+
const rulesOfHooks = plugin.rules['rules-of-hooks'];
73+
const exhaustiveDeps = plugin.rules['exhaustive-deps'];
74+
75+
export const reactHooksPlugin = {
76+
rules: {
77+
'rules-of-hooks': rulesOfHooks,
78+
'exhaustive-deps': exhaustiveDeps,
79+
},
80+
};
81+
`,
82+
resolveDir: root,
83+
sourcefile: 'sandpack-hooks-entry.js',
84+
},
85+
bundle: true,
86+
define: {
87+
'process.env.NODE_ENV': '"production"',
88+
},
89+
format: 'esm',
90+
minify: true,
91+
outfile: generatedHooksLintOutfile,
92+
platform: 'browser',
93+
plugins: [stubPlugin],
94+
});
95+
96+
const generatedSource = fs.readFileSync(generatedHooksLintOutfile, 'utf8');
97+
const header = `// @ts-nocheck
98+
/**
99+
* Copyright (c) Meta Platforms, Inc. and affiliates.
100+
*
101+
* This source code is licensed under the MIT license found in the
102+
* LICENSE file in the root directory of this source tree.
103+
*/
104+
105+
// This file is generated by scripts/buildSandpackHooksLint.mjs.
106+
107+
`;
108+
109+
fs.writeFileSync(generatedHooksLintOutfile, header + generatedSource);

scripts/buildSandpackTheme.mjs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import fs from 'fs';
9+
import path from 'path';
10+
import {createRequire} from 'module';
11+
import {fileURLToPath} from 'url';
12+
13+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
14+
const root = path.resolve(__dirname, '..');
15+
const require = createRequire(import.meta.url);
16+
17+
const generatedThemeOutfile = path.resolve(
18+
root,
19+
'src/components/MDX/Sandpack/generatedTheme.ts'
20+
);
21+
22+
const tailwindConfig = require(path.resolve(root, 'tailwind.config.js'));
23+
24+
const sandpackThemeFonts = {
25+
body: tailwindConfig.theme.extend.fontFamily.text
26+
.join(', ')
27+
.replace(/"/g, ''),
28+
mono: tailwindConfig.theme.extend.fontFamily.mono
29+
.join(', ')
30+
.replace(/"/g, ''),
31+
size: tailwindConfig.theme.extend.fontSize.code,
32+
};
33+
34+
const generatedThemeModule = `/**
35+
* Copyright (c) Meta Platforms, Inc. and affiliates.
36+
*
37+
* This source code is licensed under the MIT license found in the
38+
* LICENSE file in the root directory of this source tree.
39+
*/
40+
41+
// This file is generated by scripts/buildSandpackTheme.mjs.
42+
43+
export const sandpackThemeFonts = ${JSON.stringify(
44+
sandpackThemeFonts,
45+
null,
46+
2
47+
)} as const;
48+
`;
49+
50+
fs.writeFileSync(generatedThemeOutfile, generatedThemeModule);

src/components/MDX/Sandpack/Themes.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Copyright (c) Facebook, Inc. and its affiliates.
1010
*/
1111

12-
import tailwindConfig from '../../../../tailwind.config';
12+
import {sandpackThemeFonts} from './generatedTheme';
1313

1414
export const CustomTheme = {
1515
colors: {
@@ -38,13 +38,9 @@ export const CustomTheme = {
3838
string: 'inherit',
3939
},
4040
font: {
41-
body: tailwindConfig.theme.extend.fontFamily.text
42-
.join(', ')
43-
.replace(/"/gm, ''),
44-
mono: tailwindConfig.theme.extend.fontFamily.mono
45-
.join(', ')
46-
.replace(/"/gm, ''),
47-
size: tailwindConfig.theme.extend.fontSize.code,
41+
body: sandpackThemeFonts.body,
42+
mono: sandpackThemeFonts.mono,
43+
size: sandpackThemeFonts.size,
4844
lineHeight: '24px',
4945
},
5046
};

0 commit comments

Comments
 (0)