Skip to content

Commit f645119

Browse files
jeetissdanilowoz
andauthoredFeb 28, 2023
fix(sandpack-client): setup build with rollup (#758)
Co-authored-by: Danilo Woznica <danilowoz@gmail.com>
1 parent 979d61c commit f645119

File tree

12 files changed

+239
-1484
lines changed

12 files changed

+239
-1484
lines changed
 

‎package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@
3737
"@babel/preset-react": "^7.16.5",
3838
"@babel/preset-typescript": "^7.16.5",
3939
"@rollup/plugin-commonjs": "^24.0.0",
40+
"@rollup/plugin-node-resolve": "^15.0.1",
41+
"@rollup/plugin-replace": "^5.0.2",
42+
"@rollup/plugin-terser": "^0.4.0",
4043
"@rollup/plugin-typescript": "^10.0.1",
4144
"@types/jest": "^27.4.0",
4245
"@typescript-eslint/eslint-plugin": "^4.0.0",
4346
"@typescript-eslint/parser": "^4.0.0",
4447
"babel-eslint": "^10.0.0",
4548
"babel-jest": "^27.4.5",
46-
"esbuild": "^0.17.9",
4749
"eslint": "^7.5.0",
4850
"eslint-config-prettier": "^8.1.0",
4951
"eslint-config-react-app": "^6.0.0",
@@ -52,15 +54,14 @@
5254
"eslint-plugin-jsx-a11y": "^6.3.1",
5355
"eslint-plugin-react": "^7.20.3",
5456
"eslint-plugin-react-hooks": "^4.0.8",
55-
"@esbuild-plugins/node-globals-polyfill": "0.1.1",
5657
"jest": "^27.4.5",
5758
"lerna": "^4.0.0",
5859
"lerna-changelog": "^2.1.0",
5960
"lint-staged": "^10.5.4",
6061
"prettier": "^2.2.1",
6162
"react-test-renderer": "^18.1.0",
6263
"rollup": "^3.9.1",
63-
"rollup-plugin-replace": "^2.2.0",
64+
"rollup-plugin-string": "^3.0.0",
6465
"turbo": "^1.5.5"
6566
},
6667
"lint-staged": {

‎sandpack-client/build.js

-80
This file was deleted.

‎sandpack-client/package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@
1212
"sideEffects": false,
1313
"main": "./dist/index.js",
1414
"module": "./dist/index.mjs",
15+
"types": "./dist/index.d.ts",
1516
"exports": {
1617
"./clients/runtime": {
18+
"types": "./dist/clients/runtime/index.d.ts",
1719
"import": "./dist/clients/runtime/index.mjs",
1820
"require": "./dist/clients/runtime/index.js"
1921
},
2022
"./clients/node": {
23+
"types": "./dist/clients/node/index.d.ts",
2124
"import": "./dist/clients/node/index.mjs",
2225
"require": "./dist/clients/node/index.js"
2326
},
2427
".": {
28+
"types": "./dist/index.d.ts",
2529
"import": "./dist/index.mjs",
2630
"require": "./dist/index.js"
2731
}
@@ -31,8 +35,7 @@
3135
"prebuild": "yarn run clean",
3236
"test": "jest .",
3337
"lint": "eslint '**/*.ts?(x)' --fix",
34-
"build": "node build.js && yarn run build:types",
35-
"build:types": "tsc -p tsconfig.json",
38+
"build": "rollup -c --bundleConfigAsCjs",
3639
"build:publish": "yarn build && gulp",
3740
"build:bundler": "gulp",
3841
"format": "prettier --write '**/*.{ts,tsx,js,jsx}'",
@@ -47,6 +50,7 @@
4750
],
4851
"dependencies": {
4952
"@codesandbox/nodebox": "0.1.0",
53+
"buffer": "^6.0.3",
5054
"dequal": "^2.0.2",
5155
"outvariant": "1.3.0"
5256
},

‎sandpack-client/rollup.config.js

+61-32
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,68 @@
1-
const commonjs = require("@rollup/plugin-commonjs");
2-
const typescript = require("@rollup/plugin-typescript");
3-
const replace = require("rollup-plugin-replace");
1+
import commonjs from "@rollup/plugin-commonjs";
2+
import { nodeResolve } from "@rollup/plugin-node-resolve";
3+
import replace from "@rollup/plugin-replace";
4+
import terser from "@rollup/plugin-terser";
5+
import typescript from "@rollup/plugin-typescript";
6+
import { string } from "rollup-plugin-string";
47

5-
const pkg = require("./package.json");
8+
import pkg from "./package.json";
69

7-
const configBase = {
8-
input: "src/index.ts",
9-
output: [
10-
{
11-
file: pkg.main,
12-
exports: "named",
13-
format: "cjs",
14-
inlineDynamicImports: true,
15-
interop: "auto",
16-
},
17-
{
18-
file: pkg.module,
19-
exports: "named",
10+
const configs = [
11+
{
12+
input: "src/clients/node/inject-scripts/consoleHook.ts",
13+
output: {
14+
file: "src/clients/node/inject-scripts/dist/consoleHook.js",
2015
format: "es",
21-
inlineDynamicImports: true,
2216
},
23-
],
17+
plugins: [
18+
typescript({
19+
tsconfig: "./tsconfig.json",
20+
compilerOptions: { declaration: false },
21+
}),
22+
commonjs(),
23+
nodeResolve(),
24+
terser({ compress: { passes: 2 } }),
25+
],
26+
external: [],
27+
},
28+
29+
{
30+
input: {
31+
index: "src/index.ts",
32+
"clients/node/index": "src/clients/node/index.ts",
33+
"clients/runtime/index": "src/clients/runtime/index.ts",
34+
},
35+
output: [
36+
{
37+
dir: "dist",
38+
format: "cjs",
39+
},
40+
{
41+
dir: "dist",
42+
chunkFileNames: "[name]-[hash].mjs",
43+
entryFileNames: "[name].mjs",
44+
format: "es",
45+
},
46+
],
2447

25-
plugins: [
26-
typescript({ tsconfig: "./tsconfig.json" }),
27-
replace({
28-
"process.env.CODESANDBOX_ENV": `"${process.env.CODESANDBOX_ENV}"`,
29-
"process.env.PACKAGE_VERSION": `"${pkg.version}"`,
48+
plugins: [
49+
typescript({ tsconfig: "./tsconfig.json" }),
50+
string({ include: "**/dist/consoleHook.js" }),
51+
replace({
52+
preventAssignment: true,
53+
values: {
54+
global: "globalThis",
55+
"process.env.CODESANDBOX_ENV": `"${process.env.CODESANDBOX_ENV}"`,
56+
"process.env.PACKAGE_VERSION": `"${pkg.version}"`,
57+
},
58+
}),
59+
],
60+
external: Object.keys({
61+
...(pkg.dependencies || {}),
62+
...(pkg.devDependencies || {}),
63+
...(pkg.peerDependencies || {}),
3064
}),
31-
commonjs({ requireReturnsDefault: "preferred" }),
32-
],
33-
external: [
34-
...Object.keys(pkg.dependencies),
35-
...Object.keys(pkg.devDependencies),
36-
],
37-
};
65+
},
66+
];
3867

39-
module.exports = configBase;
68+
export default configs;

‎sandpack-client/src/clients/node/client.utils.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Buffer } from "buffer";
2+
13
import type { ShellCommandOptions } from "@codesandbox/nodebox/build/modules/shell";
24
import { invariant } from "outvariant";
35

‎sandpack-client/src/clients/node/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* eslint-disable no-console,@typescript-eslint/no-explicit-any,prefer-rest-params,@typescript-eslint/explicit-module-boundary-types */
22

3+
import { Buffer } from "buffer";
4+
35
import { PREVIEW_LOADED_MESSAGE_TYPE, Nodebox } from "@codesandbox/nodebox";
46
import type {
57
FilesMap,

‎sandpack-client/src/clients/node/inject-scripts/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { INJECT_MESSAGE_TYPE } from "@codesandbox/nodebox";
55

66
// get the bundled file, which contains all dependencies
77
// @ts-ignore
8-
import consoleHook from "./dist/consoleHook.txt";
8+
import consoleHook from "./dist/consoleHook.js";
99
import { setupHistoryListeners } from "./historyListener";
1010

1111
const scripts = [

‎sandpack-react/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
"prebuild": "yarn run clean",
2121
"test": "TEST_ENV=true jest . --transformIgnorePatterns \"node_modules/(?!@codemirror)/\" --modulePathIgnorePatterns \"e2e\"",
2222
"lint": "eslint '**/*.ts?(x)' --fix",
23-
"build": "rollup -c && yarn build:types",
24-
"build:types": "tsc -p tsconfig.json",
23+
"build": "rollup -c",
2524
"build:publish": "yarn build",
2625
"start": "tsc -p tsconfig.esm.json --watch",
2726
"dev": "start-storybook -p 6006 --quiet",

‎sandpack-react/rollup.config.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-var-requires */
22
const commonjs = require("@rollup/plugin-commonjs");
3+
const replace = require("@rollup/plugin-replace");
34
const typescript = require("@rollup/plugin-typescript");
4-
const replace = require("rollup-plugin-replace");
55

66
const pkg = require("./package.json");
77

@@ -25,11 +25,14 @@ const configBase = {
2525

2626
plugins: [
2727
typescript({ tsconfig: "./tsconfig.json" }),
28-
replace({ "process.env.TEST_ENV": "false" }),
28+
replace({
29+
preventAssignment: true,
30+
values: { "process.env.TEST_ENV": "false" },
31+
}),
2932
commonjs({ requireReturnsDefault: "preferred" }),
3033
],
3134
external: [
32-
'react/jsx-runtime',
35+
"react/jsx-runtime",
3336
...Object.keys(pkg.dependencies),
3437
...Object.keys(pkg.devDependencies),
3538
...Object.keys(pkg.peerDependencies),

‎sandpack-themes/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
"scripts": {
1717
"clean": "rm -rf dist",
1818
"prebuild": "yarn run clean",
19-
"build": "rollup -c && yarn build:types",
20-
"build:types": "tsc -p tsconfig.json",
19+
"build": "rollup -c",
2120
"build:publish": "yarn build",
2221
"typecheck": "tsc"
2322
},

‎sandpack-themes/rollup.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
12
const typescript = require("@rollup/plugin-typescript");
23

34
const pkg = require("./package.json");

‎yarn.lock

+154-1,359
Large diffs are not rendered by default.

1 commit comments

Comments
 (1)

vercel[bot] commented on Feb 28, 2023

@vercel[bot]

Successfully deployed to the following URLs:

sandpack-docs – ./website/docs

sandpack-docs-codesandbox1.vercel.app
sandpack-docs-git-main-codesandbox1.vercel.app
sandpack.vercel.app

Please sign in to comment.