-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathesbuild.cjs
More file actions
160 lines (149 loc) · 4.3 KB
/
Copy pathesbuild.cjs
File metadata and controls
160 lines (149 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const { join } = require("path");
const esbuild = require("esbuild");
const { externalModules, define } = require("./build/utils.cjs");
const { moduleReplacementPlugin } = require("./build/plugins.cjs");
const devtoolsStubPlugin = {
name: "stub-react-devtools",
setup(build) {
// whenever someone does `import 'react-devtools-core'`, redirect to our empty.js
build.onResolve({ filter: /^react-devtools-core$/ }, () => ({
path: join(__dirname, "build/fakeReactDevtoolsCore.js"),
namespace: "file",
}));
},
};
/**
* BROWSER CLIENT BUILD
*/
const browserPlugin = moduleReplacementPlugin({
"os-browserify/browser": /^os$/,
"path-browserify": /^path$/,
});
const browserCjsBuild = esbuild.build({
entryPoints: ["src/browser/index.ts"],
bundle: true,
format: "cjs",
// .cjs extension is required because "type": "module" is set in package.json
outfile: "dist/cjs/browser.cjs",
platform: "browser",
// pitcher-common currently requires this, but breaks the first experience
banner: {
js: `if (typeof window !== "undefined" && !window.process) {
window.process = {
env: {},
};
}
`,
},
plugins: [browserPlugin],
// pitcher-client seems to have some dynamic dependency on @sentry/node
external: ["@sentry/node"],
});
const browserEsmBuild = esbuild.build({
entryPoints: ["src/browser/index.ts"],
bundle: true,
format: "esm",
outfile: "dist/esm/browser.js",
platform: "browser",
metafile: true,
// pitcher-common currently requires this, but breaks the first experience
banner: {
js: `if (typeof window !== "undefined" && !window.process) {
window.process = {
env: {},
};
}
`,
},
plugins: [browserPlugin],
// pitcher-client seems to have some dynamic dependency on @sentry/node
external: ["@sentry/node"],
});
/**
* NODE CLIENT BUILD
*/
const nodeClientCjsBuild = esbuild.build({
entryPoints: ["src/node/index.ts"],
bundle: true,
format: "cjs",
// .cjs extension is required because "type": "module" is set in package.json
outfile: "dist/cjs/node.cjs",
platform: "node",
external: externalModules,
});
const nodeClientEsmBuild = esbuild.build({
entryPoints: ["src/node/index.ts"],
bundle: true,
format: "esm",
outfile: "dist/esm/node.js",
// Handle dynamic requires (WS)
banner: {
js: `import { createRequire } from 'module'; const require = createRequire(import.meta.url);`,
},
platform: "node",
// These has to be optional deps, due to being native and only working in certain envs
external: externalModules.concat("bufferutil", "utf-8-validate"),
});
/**
* SDK BUILD
*/
const sdkCjsBuild = esbuild.build({
entryPoints: ["src/index.ts"],
bundle: true,
format: "cjs",
define,
platform: "node",
// .cjs extension is required because "type": "module" is set in package.json
outfile: "dist/cjs/index.cjs",
external: externalModules,
});
const sdkEsmBuild = esbuild.build({
entryPoints: ["src/index.ts"],
bundle: true,
format: "esm",
define,
platform: "node",
// Handle dynamic requires (WS)
banner: {
js: `import { createRequire } from 'module'; const require = createRequire(import.meta.url);`,
},
outfile: "dist/esm/index.js",
// These has to be optional deps, due to being native and only working in certain envs
external: externalModules.concat("bufferutil", "utf-8-validate"),
});
/**
* CLI BUILD
*/
const cliBuild = esbuild.build({
entryPoints: ["src/bin/main.tsx"],
outfile: "dist/bin/codesandbox.mjs",
bundle: true,
define,
format: "esm",
platform: "node",
banner: {
js: `#!/usr/bin/env node\n\nimport { createRequire } from "module";\nconst require = createRequire(import.meta.url);\n`,
},
external: [
// We have to bundle React and Ink into the bundle because Ink supports React 18 in v5 and React 19 in v6,
// but this breaks when running the CLI in the project folder as it might have either React version and we do not
// want users to manually install React and correct Ink version as peer dependencies
...externalModules.filter(
(mod) =>
mod !== "react" && mod !== "ink" && mod !== "@tanstack/react-query"
),
"@codesandbox/sdk",
],
plugins: [devtoolsStubPlugin],
});
Promise.all([
browserCjsBuild,
browserEsmBuild,
nodeClientCjsBuild,
nodeClientEsmBuild,
sdkCjsBuild,
sdkEsmBuild,
cliBuild,
]).catch(() => {
process.exit(1);
});