|
1 | 1 | const esbuild = require("esbuild"); |
| 2 | +const { nodeExternals, define } = require("./build/utils.cjs"); |
| 3 | +const { |
| 4 | + moduleReplacementPlugin, |
| 5 | + forbidImportsPlugin, |
| 6 | +} = require("./build/plugins.cjs"); |
2 | 7 |
|
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 | +]); |
11 | 14 |
|
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 | +}); |
23 | 22 |
|
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 | +}; |
41 | 36 | } |
42 | 37 | `, |
43 | | - }, |
44 | | - plugins: [browserifyPlugin], |
45 | | - }), |
| 38 | + }, |
| 39 | + plugins: [browserPlugin], |
| 40 | +}); |
46 | 41 |
|
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 | +}; |
60 | 54 | } |
61 | 55 | `, |
62 | | - }, |
63 | | - plugins: [browserifyPlugin], |
64 | | - }), |
| 56 | + }, |
| 57 | + plugins: [browserPlugin], |
| 58 | +}); |
65 | 59 |
|
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 | + */ |
78 | 63 |
|
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 | +}); |
88 | 84 |
|
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, |
102 | 135 | ]).catch(() => { |
103 | 136 | process.exit(1); |
104 | 137 | }); |
0 commit comments