Skip to content

Commit 81f63e7

Browse files
new node client
1 parent 6f34b3c commit 81f63e7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+3904
-517
lines changed

build/plugins.cjs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module.exports.moduleReplacementPlugin = function moduleReplacementPlugin(
2+
replacements
3+
) {
4+
return {
5+
name: "module-replacement",
6+
setup(build) {
7+
for (const [key, value] of Object.entries(replacements)) {
8+
build.onResolve({ filter: new RegExp(`^${value}$`) }, (args) => {
9+
return { path: require.resolve(key) };
10+
});
11+
}
12+
},
13+
};
14+
};
15+
16+
module.exports.forbidImportsPlugin = function forbidImportsPlugin(imports) {
17+
return {
18+
name: "forbid-imports",
19+
setup(build) {
20+
for (const packageName of imports) {
21+
// catch `import ... from 'packageName'` **and** sub-paths `packageName/foo`
22+
const pkgFilter = new RegExp(`^${packageName}($|/)`);
23+
build.onResolve({ filter: pkgFilter }, (args) => {
24+
return {
25+
errors: [
26+
{
27+
text: `❌ Importing “${packageName}” is forbidden in this project.`,
28+
notes: [
29+
"If you really need it, talk to your team lead about an exception.",
30+
],
31+
},
32+
],
33+
};
34+
});
35+
}
36+
},
37+
};
38+
};

build/utils.cjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports.nodeExternals = [
2+
...Object.keys(require("../package.json").dependencies),
3+
...require("module").builtinModules,
4+
];
5+
6+
module.exports.define = {
7+
CSB_SDK_VERSION: `"${require("../package.json").version}"`,
8+
};

esbuild.cjs

Lines changed: 122 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,137 @@
11
const esbuild = require("esbuild");
2+
const { nodeExternals, define } = require("./build/utils.cjs");
3+
const {
4+
moduleReplacementPlugin,
5+
forbidImportsPlugin,
6+
} = require("./build/plugins.cjs");
27

3-
// Common plugin for module replacements
4-
const browserifyPlugin = {
5-
name: "alias",
6-
setup(build) {
7-
// Handle os module replacement
8-
build.onResolve({ filter: /^os$/ }, (args) => {
9-
return { path: require.resolve("os-browserify/browser") };
10-
});
8+
// Until pitcher-client is part of SDK we need to forbid these imports in
9+
// Node builds
10+
const preventPitcherClientImportsPlugin = forbidImportsPlugin([
11+
"@codesandbox/pitcher-protocol",
12+
"@codesandbox/pitcher-common",
13+
]);
1114

12-
// Handle path module replacement
13-
build.onResolve({ filter: /^path$/ }, (args) => {
14-
return { path: require.resolve("path-browserify") };
15-
});
16-
},
17-
};
18-
19-
const nodeExternals = [
20-
...Object.keys(require("./package.json").dependencies),
21-
...require("module").builtinModules,
22-
];
15+
/**
16+
* BROWSER CLIENT BUILD
17+
*/
18+
const browserPlugin = moduleReplacementPlugin({
19+
"os-browserify/browser": /^os$/,
20+
"path-browserify": /^path$/,
21+
});
2322

24-
// Build both CJS and ESM versions
25-
Promise.all([
26-
// Browser builds:
27-
// CommonJS build
28-
esbuild.build({
29-
entryPoints: ["src/browser/index.ts"],
30-
bundle: true,
31-
format: "cjs",
32-
// .cjs extension is required because "type": "module" is set in package.json
33-
outfile: "dist/cjs/browser.cjs",
34-
platform: "browser",
35-
// pitcher-common currently requires this, but breaks the first experience
36-
banner: {
37-
js: `if (typeof window !== "undefined" && !window.process) {
38-
window.process = {
39-
env: {},
40-
};
23+
const browserCjsBuild = esbuild.build({
24+
entryPoints: ["src/browser/index.ts"],
25+
bundle: true,
26+
format: "cjs",
27+
// .cjs extension is required because "type": "module" is set in package.json
28+
outfile: "dist/cjs/browser.cjs",
29+
platform: "browser",
30+
// pitcher-common currently requires this, but breaks the first experience
31+
banner: {
32+
js: `if (typeof window !== "undefined" && !window.process) {
33+
window.process = {
34+
env: {},
35+
};
4136
}
4237
`,
43-
},
44-
plugins: [browserifyPlugin],
45-
}),
38+
},
39+
plugins: [browserPlugin],
40+
});
4641

47-
// ESM build
48-
esbuild.build({
49-
entryPoints: ["src/browser/index.ts"],
50-
bundle: true,
51-
format: "esm",
52-
outfile: "dist/esm/browser.js",
53-
platform: "browser",
54-
// pitcher-common currently requires this, but breaks the first experience
55-
banner: {
56-
js: `if (typeof window !== "undefined" && !window.process) {
57-
window.process = {
58-
env: {},
59-
};
42+
const browserEsmBuild = esbuild.build({
43+
entryPoints: ["src/browser/index.ts"],
44+
bundle: true,
45+
format: "esm",
46+
outfile: "dist/esm/browser.js",
47+
platform: "browser",
48+
// pitcher-common currently requires this, but breaks the first experience
49+
banner: {
50+
js: `if (typeof window !== "undefined" && !window.process) {
51+
window.process = {
52+
env: {},
53+
};
6054
}
6155
`,
62-
},
63-
plugins: [browserifyPlugin],
64-
}),
56+
},
57+
plugins: [browserPlugin],
58+
});
6559

66-
// Index builds:
67-
// Node:
68-
// CommonJS build
69-
esbuild.build({
70-
entryPoints: ["src/index.ts"],
71-
bundle: true,
72-
format: "cjs",
73-
platform: "node",
74-
// .cjs extension is required because "type": "module" is set in package.json
75-
outfile: "dist/cjs/index.cjs",
76-
external: nodeExternals,
77-
}),
60+
/**
61+
* NODE CLIENT BUILD
62+
*/
7863

79-
// ESM build
80-
esbuild.build({
81-
entryPoints: ["src/index.ts"],
82-
bundle: true,
83-
format: "esm",
84-
platform: "node",
85-
outfile: "dist/esm/index.js",
86-
external: nodeExternals,
87-
}),
64+
const nodeClientCjsBuild = esbuild.build({
65+
entryPoints: ["src/node/index.ts"],
66+
bundle: true,
67+
format: "cjs",
68+
// .cjs extension is required because "type": "module" is set in package.json
69+
outfile: "dist/cjs/node.cjs",
70+
platform: "node",
71+
external: nodeExternals,
72+
plugins: [preventPitcherClientImportsPlugin],
73+
});
74+
75+
const nodeClientEsmBuild = esbuild.build({
76+
entryPoints: ["src/node/index.ts"],
77+
bundle: true,
78+
format: "esm",
79+
outfile: "dist/esm/node.js",
80+
platform: "node",
81+
external: nodeExternals,
82+
plugins: [preventPitcherClientImportsPlugin],
83+
});
8884

89-
// Bin builds:
90-
esbuild.build({
91-
entryPoints: ["src/bin/main.tsx"],
92-
outfile: "dist/bin/codesandbox.mjs",
93-
bundle: true,
94-
format: "esm",
95-
platform: "node",
96-
banner: {
97-
js: `#!/usr/bin/env node\n\n`,
98-
},
99-
// ORA is an ESM module so we have to include it in the build
100-
external: [...nodeExternals, "@codesandbox/sdk", "isbinaryfile"],
101-
}),
85+
/**
86+
* SDK BUILD
87+
*/
88+
const sdkCjsBuild = esbuild.build({
89+
entryPoints: ["src/index.ts"],
90+
bundle: true,
91+
format: "cjs",
92+
define,
93+
platform: "node",
94+
// .cjs extension is required because "type": "module" is set in package.json
95+
outfile: "dist/cjs/index.cjs",
96+
external: nodeExternals,
97+
});
98+
99+
const sdkEsmBuild = esbuild.build({
100+
entryPoints: ["src/index.ts"],
101+
bundle: true,
102+
format: "esm",
103+
define,
104+
platform: "node",
105+
outfile: "dist/esm/index.js",
106+
external: nodeExternals,
107+
plugins: [preventPitcherClientImportsPlugin],
108+
});
109+
110+
/**
111+
* CLI BUILD
112+
*/
113+
const cliBuild = esbuild.build({
114+
entryPoints: ["src/bin/main.tsx"],
115+
outfile: "dist/bin/codesandbox.mjs",
116+
bundle: true,
117+
define,
118+
format: "esm",
119+
platform: "node",
120+
banner: {
121+
js: `#!/usr/bin/env node\n\n`,
122+
},
123+
external: [...nodeExternals, "@codesandbox/sdk"],
124+
plugins: [preventPitcherClientImportsPlugin],
125+
});
126+
127+
Promise.all([
128+
browserCjsBuild,
129+
browserEsmBuild,
130+
nodeClientCjsBuild,
131+
nodeClientEsmBuild,
132+
sdkCjsBuild,
133+
sdkEsmBuild,
134+
cliBuild,
102135
]).catch(() => {
103136
process.exit(1);
104137
});

0 commit comments

Comments
 (0)