@cedarjs/vite 8.0.0-canary.3450 → 8.0.0-canary.3452

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"buildApp.d.ts","sourceRoot":"","sources":["../src/buildApp.ts"],"names":[],"mappings":"AAuEA,MAAM,WAAW,oBAAoB;IACnC,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,SAAS,CAAC,EAAE,MAAM,EAAE,CAAA;IACpB,EAAE,CAAC,EAAE,OAAO,CAAA;CACb;AAED;;;;;;;;GAQG;AACH,wBAAsB,aAAa,CAAC,EAClC,OAAe,EACf,SAA0B,EAC1B,EAAU,GACX,GAAE,oBAAyB,iBAyd3B"}
1
+ {"version":3,"file":"buildApp.d.ts","sourceRoot":"","sources":["../src/buildApp.ts"],"names":[],"mappings":"AAiEA,MAAM,WAAW,oBAAoB;IACnC,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,SAAS,CAAC,EAAE,MAAM,EAAE,CAAA;IACpB,EAAE,CAAC,EAAE,OAAO,CAAA;CACb;AAED;;;;;;;;GAQG;AACH,wBAAsB,aAAa,CAAC,EAClC,OAAe,EACf,SAA0B,EAC1B,EAAU,GACX,GAAE,oBAAyB,iBA8b3B"}
package/dist/buildApp.js CHANGED
@@ -26,11 +26,6 @@ import {
26
26
  getPaths
27
27
  } from "@cedarjs/project-config";
28
28
  import { generateDiffSourceMap } from "./lib/generateDiffSourceMap.js";
29
- import {
30
- copySandboxChildrenIntoVercelFunctions,
31
- findSandboxChildScripts,
32
- SANDBOX_CHILDREN_DIR
33
- } from "./lib/sandboxChildren.js";
34
29
  import { getWorkspacePackageAliases } from "./lib/workspacePackageAliases.js";
35
30
  import { cedarAutoImportsPlugin } from "./plugins/vite-plugin-cedar-auto-import.js";
36
31
  import { cedarDirectoryNamedImportPlugin } from "./plugins/vite-plugin-cedar-directory-named-import.js";
@@ -39,7 +34,6 @@ import { cedarGraphqlOptionsExtractPlugin } from "./plugins/vite-plugin-cedar-gr
39
34
  import { cedarImportDirPlugin } from "./plugins/vite-plugin-cedar-import-dir.js";
40
35
  import { cedarMockCellDataPlugin } from "./plugins/vite-plugin-cedar-mock-cell-data.js";
41
36
  import { cedarOtelWrappingPlugin } from "./plugins/vite-plugin-cedar-otel-wrapping.js";
42
- import { cedarSandboxChildrenPlugin } from "./plugins/vite-plugin-cedar-sandbox-children.js";
43
37
  import { cedarjsJobPathInjectorPlugin } from "./plugins/vite-plugin-cedarjs-job-path-injector.js";
44
38
  import { handlerAlsWrappingPlugin } from "./plugins/vite-plugin-handler-als-wrapping.js";
45
39
  function resolveWithExtensions(id) {
@@ -62,7 +56,6 @@ async function buildCedarApp({
62
56
  const cedarPaths = getPaths();
63
57
  const cedarConfig = getConfig();
64
58
  const environments = {};
65
- let sandboxChildren = {};
66
59
  if (workspace.includes("web")) {
67
60
  environments.client = {
68
61
  build: {
@@ -104,14 +97,15 @@ async function buildCedarApp({
104
97
  }
105
98
  };
106
99
  if (ud) {
107
- sandboxChildren = findSandboxChildScripts(cedarPaths.api.src);
108
100
  environments["ssr"] = {
109
101
  build: {
110
102
  ssr: true,
111
103
  outDir: path.join(cedarPaths.api.dist, "ud"),
112
104
  emptyOutDir: true,
113
105
  rollupOptions: {
114
- input: { index: catchAllEntry, ...sandboxChildren },
106
+ // Named, so a plugin in the app's Vite config can add entries
107
+ // of its own next to it, each in a file of its own
108
+ input: { index: catchAllEntry },
115
109
  output: {
116
110
  entryFileNames: "[name].js"
117
111
  },
@@ -209,12 +203,6 @@ async function buildCedarApp({
209
203
  ];
210
204
  if (ud) {
211
205
  plugins.push(catchAll());
212
- plugins.push(
213
- cedarSandboxChildrenPlugin({
214
- environment: "ssr",
215
- children: sandboxChildren
216
- })
217
- );
218
206
  plugins.push({
219
207
  name: "cedar-ud-verify-routes",
220
208
  configResolved() {
@@ -355,19 +343,7 @@ async function buildCedarApp({
355
343
  },
356
344
  plugins
357
345
  });
358
- await builder.buildApp();
359
- const vercelNode = builder.environments["vercel_node"];
360
- if (ud && vercelNode && Object.keys(sandboxChildren).length > 0) {
361
- const outDir = path.resolve(
362
- vercelNode.config.root,
363
- vercelNode.config.build.outDir
364
- );
365
- copySandboxChildrenIntoVercelFunctions({
366
- appRoot: cedarPaths.base,
367
- childrenDir: path.join(cedarPaths.api.dist, "ud", SANDBOX_CHILDREN_DIR),
368
- functionsDir: path.join(outDir, "functions")
369
- });
370
- }
346
+ return builder.buildApp();
371
347
  }
372
348
  export {
373
349
  buildCedarApp
@@ -3,5 +3,5 @@
3
3
  * buildFeServer -> buildRscFeServer -> rscBuildClient
4
4
  * Generate the client bundle
5
5
  */
6
- export declare function rscBuildClient(clientEntryFiles: Record<string, string>): Promise<[import("rollup").OutputChunk, ...(import("rollup").OutputAsset | import("rollup").OutputChunk)[]]>;
6
+ export declare function rscBuildClient(clientEntryFiles: Record<string, string>): Promise<[import("rollup").OutputChunk, ...(import("rollup").OutputChunk | import("rollup").OutputAsset)[]]>;
7
7
  //# sourceMappingURL=rscBuildClient.d.ts.map
@@ -3,5 +3,5 @@
3
3
  * buildFeServer -> buildRscFeServer -> rscBuildForServer
4
4
  * Generate the output to be used by the rsc worker (not the actual server!)
5
5
  */
6
- export declare function rscBuildForServer(clientEntryFiles: Record<string, string>, serverEntryFiles: Record<string, string>, customModules: Record<string, string>): Promise<[import("rollup").OutputChunk, ...(import("rollup").OutputAsset | import("rollup").OutputChunk)[]]>;
6
+ export declare function rscBuildForServer(clientEntryFiles: Record<string, string>, serverEntryFiles: Record<string, string>, customModules: Record<string, string>): Promise<[import("rollup").OutputChunk, ...(import("rollup").OutputChunk | import("rollup").OutputAsset)[]]>;
7
7
  //# sourceMappingURL=rscBuildForServer.d.ts.map
@@ -5,5 +5,5 @@
5
5
  export declare function rscBuildForSsr({ clientEntryFiles, verbose, }: {
6
6
  clientEntryFiles: Record<string, string>;
7
7
  verbose?: boolean;
8
- }): Promise<[import("rollup").OutputChunk, ...(import("rollup").OutputAsset | import("rollup").OutputChunk)[]]>;
8
+ }): Promise<[import("rollup").OutputChunk, ...(import("rollup").OutputChunk | import("rollup").OutputAsset)[]]>;
9
9
  //# sourceMappingURL=rscBuildForSsr.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cedarjs/vite",
3
- "version": "8.0.0-canary.3450",
3
+ "version": "8.0.0-canary.3452",
4
4
  "description": "Vite configuration package for CedarJS",
5
5
  "repository": {
6
6
  "type": "git",
@@ -54,17 +54,17 @@
54
54
  "@babel/preset-typescript": "7.29.7",
55
55
  "@babel/traverse": "7.29.10",
56
56
  "@babel/types": "7.29.8",
57
- "@cedarjs/api": "8.0.0-canary.3450",
58
- "@cedarjs/api-server": "8.0.0-canary.3450",
59
- "@cedarjs/babel-config": "8.0.0-canary.3450",
60
- "@cedarjs/context": "8.0.0-canary.3450",
61
- "@cedarjs/graphql-server": "8.0.0-canary.3450",
62
- "@cedarjs/internal": "8.0.0-canary.3450",
63
- "@cedarjs/jobs": "8.0.0-canary.3450",
64
- "@cedarjs/project-config": "8.0.0-canary.3450",
65
- "@cedarjs/server-store": "8.0.0-canary.3450",
66
- "@cedarjs/structure": "8.0.0-canary.3450",
67
- "@cedarjs/testing": "8.0.0-canary.3450",
57
+ "@cedarjs/api": "8.0.0-canary.3452",
58
+ "@cedarjs/api-server": "8.0.0-canary.3452",
59
+ "@cedarjs/babel-config": "8.0.0-canary.3452",
60
+ "@cedarjs/context": "8.0.0-canary.3452",
61
+ "@cedarjs/graphql-server": "8.0.0-canary.3452",
62
+ "@cedarjs/internal": "8.0.0-canary.3452",
63
+ "@cedarjs/jobs": "8.0.0-canary.3452",
64
+ "@cedarjs/project-config": "8.0.0-canary.3452",
65
+ "@cedarjs/server-store": "8.0.0-canary.3452",
66
+ "@cedarjs/structure": "8.0.0-canary.3452",
67
+ "@cedarjs/testing": "8.0.0-canary.3452",
68
68
  "@swc/core": "1.16.18",
69
69
  "@universal-deploy/store": "^0.3.0",
70
70
  "@universal-deploy/vite": "^0.2.0",
@@ -94,10 +94,10 @@
94
94
  "yargs-parser": "21.1.1"
95
95
  },
96
96
  "devDependencies": {
97
- "@cedarjs/auth": "8.0.0-canary.3450",
98
- "@cedarjs/framework-tools": "8.0.0-canary.3450",
99
- "@cedarjs/router": "8.0.0-canary.3450",
100
- "@cedarjs/web": "8.0.0-canary.3450",
97
+ "@cedarjs/auth": "8.0.0-canary.3452",
98
+ "@cedarjs/framework-tools": "8.0.0-canary.3452",
99
+ "@cedarjs/router": "8.0.0-canary.3452",
100
+ "@cedarjs/web": "8.0.0-canary.3452",
101
101
  "@dr.pogodin/react-helmet": "2.0.4",
102
102
  "@hyrious/esbuild-plugin-commonjs": "0.2.6",
103
103
  "@jridgewell/trace-mapping": "0.3.31",
@@ -119,9 +119,9 @@
119
119
  "vitest": "4.1.11"
120
120
  },
121
121
  "peerDependencies": {
122
- "@cedarjs/auth": "8.0.0-canary.3450",
123
- "@cedarjs/router": "8.0.0-canary.3450",
124
- "@cedarjs/web": "8.0.0-canary.3450",
122
+ "@cedarjs/auth": "8.0.0-canary.3452",
123
+ "@cedarjs/router": "8.0.0-canary.3452",
124
+ "@cedarjs/web": "8.0.0-canary.3452",
125
125
  "react": "19.3.0",
126
126
  "react-dom": "19.3.0"
127
127
  },
@@ -1,58 +0,0 @@
1
- /**
2
- * The folder in `api/dist/ud` a bundled API build puts every sandboxed
3
- * child script in, as `<name>.child.js`, with a `node_modules` holding the
4
- * packages they import. `findChildScript()` in `@cedarjs/api/sandbox`
5
- * looks for `api/dist/ud/sandbox-children` from its caller's folder up.
6
- */
7
- export declare const SANDBOX_CHILDREN_DIR = "sandbox-children";
8
- /**
9
- * Every sandboxed child script in `apiSrc` (`**\/*.child.{ts,js,mts,mjs}`),
10
- * as `{ 'sandbox-children/<name>.child': file }`, for a bundled build's
11
- * `input`. Throws when two have the same name: in a bundle they all go in
12
- * one folder, and `findChildScript()` finds them by name.
13
- */
14
- export declare function findSandboxChildScripts(apiSrc: string): Record<string, string>;
15
- export interface SandboxChildImports {
16
- /** The child script's source file, which its imports resolve from */
17
- source: string;
18
- /** What it imports: package names and Node builtins */
19
- imports: string[];
20
- }
21
- /**
22
- * Copies the packages the child scripts import, and everything those
23
- * depend on, into `<childrenDir>/node_modules`, so the folder runs on its
24
- * own wherever it's deployed: a serverless function's file tracing doesn't
25
- * follow a child script (nothing imports it), and it would miss what
26
- * native packages load at run time anyway (sharp's libvips).
27
- *
28
- * Follows each package's `dependencies`, and the `optionalDependencies`
29
- * and `peerDependencies` that are installed, which is how native packages
30
- * ship their platform's binaries (`@img/sharp-linux-x64`,
31
- * `@img/sharp-libvips-linux-x64`).
32
- *
33
- * The copy keeps each package's choices, so it finds the same versions
34
- * it does in the app: a dependency nested in a package's own
35
- * `node_modules` is copied into the copy's `node_modules`; one beside it
36
- * in a `node_modules` (pnpm's layout), or shadowed in the copy by another
37
- * version, goes next to the dependent's copy; one in the app's own
38
- * `node_modules` folders goes at the top. A package already reachable from
39
- * the dependent's copy is reused. When two versions would still need the
40
- * same place, it throws instead of letting one overwrite the other. So
41
- * does a package or regular dependency that isn't installed: the child
42
- * couldn't import it either.
43
- */
44
- export declare function vendorSandboxChildPackages(childrenDir: string, children: SandboxChildImports[]): void;
45
- /**
46
- * Copies the bundled build's sandboxed children (and their `node_modules`)
47
- * into every Node function in a Vercel Build Output API folder
48
- * (`.vercel/output/functions/*.func`), at the same path as in the app
49
- * (`api/dist/ud/sandbox-children`). Each Vercel function is a single
50
- * bundled file with nothing else from the app, and `findChildScript()`
51
- * looks for the children from the function's folder up.
52
- */
53
- export declare function copySandboxChildrenIntoVercelFunctions({ appRoot, childrenDir, functionsDir, }: {
54
- appRoot: string;
55
- childrenDir: string;
56
- functionsDir: string;
57
- }): string[];
58
- //# sourceMappingURL=sandboxChildren.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"sandboxChildren.d.ts","sourceRoot":"","sources":["../../src/lib/sandboxChildren.ts"],"names":[],"mappings":"AAMA;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,qBAAqB,CAAA;AAEtD;;;;;GAKG;AACH,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,MAAM,GACb,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAyBxB;AA6CD,MAAM,WAAW,mBAAmB;IAClC,qEAAqE;IACrE,MAAM,EAAE,MAAM,CAAA;IACd,uDAAuD;IACvD,OAAO,EAAE,MAAM,EAAE,CAAA;CAClB;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,0BAA0B,CACxC,WAAW,EAAE,MAAM,EACnB,QAAQ,EAAE,mBAAmB,EAAE,QA+GhC;AAgBD;;;;;;;GAOG;AACH,wBAAgB,sCAAsC,CAAC,EACrD,OAAO,EACP,WAAW,EACX,YAAY,GACb,EAAE;IACD,OAAO,EAAE,MAAM,CAAA;IACf,WAAW,EAAE,MAAM,CAAA;IACnB,YAAY,EAAE,MAAM,CAAA;CACrB,YAgCA"}
@@ -1,157 +0,0 @@
1
- import fs from "node:fs";
2
- import { isBuiltin } from "node:module";
3
- import path from "node:path";
4
- import fg from "fast-glob";
5
- const SANDBOX_CHILDREN_DIR = "sandbox-children";
6
- function findSandboxChildScripts(apiSrc) {
7
- const files = fg.sync("**/*.child.{ts,js,mts,mjs}", {
8
- cwd: apiSrc,
9
- absolute: true,
10
- ignore: ["**/node_modules/**", "**/__tests__/**"]
11
- }).sort();
12
- const entries = {};
13
- for (const file of files) {
14
- const name = path.basename(file).replace(/\.(ts|js|mts|mjs)$/, "");
15
- const key = `${SANDBOX_CHILDREN_DIR}/${name}`;
16
- if (entries[key]) {
17
- throw new Error(
18
- `Two sandboxed child scripts are named ${name}: ${path.relative(apiSrc, entries[key])} and ${path.relative(apiSrc, file)}. A bundled build puts every child script in one folder, so their names must be unique.`
19
- );
20
- }
21
- entries[key] = path.normalize(file);
22
- }
23
- return entries;
24
- }
25
- const packageName = (specifier) => {
26
- const parts = specifier.split("/");
27
- return specifier.startsWith("@") ? parts.slice(0, 2).join("/") : parts[0];
28
- };
29
- const readPackageJson = (dir) => {
30
- const parsed = JSON.parse(
31
- fs.readFileSync(path.join(dir, "package.json"), "utf8")
32
- );
33
- return parsed && typeof parsed === "object" ? parsed : {};
34
- };
35
- const findPackage = (name, dir) => {
36
- let current = dir;
37
- for (; ; ) {
38
- const candidate = path.join(current, "node_modules", name);
39
- if (fs.existsSync(path.join(candidate, "package.json"))) {
40
- return { realDir: fs.realpathSync(candidate), foundIn: current };
41
- }
42
- const parent = path.dirname(current);
43
- if (parent === current) {
44
- return null;
45
- }
46
- current = parent;
47
- }
48
- };
49
- function vendorSandboxChildPackages(childrenDir, children) {
50
- const copyOf = /* @__PURE__ */ new Map();
51
- const occupied = /* @__PURE__ */ new Map();
52
- const inNodeModules = (dir) => dir.split(path.sep).includes("node_modules");
53
- const reachableFrom = (copy, name) => {
54
- let dir = copy;
55
- for (; ; ) {
56
- const there = occupied.get(path.join(dir, "node_modules", name));
57
- if (there || dir === childrenDir) {
58
- return there?.realDir;
59
- }
60
- dir = path.dirname(dir);
61
- }
62
- };
63
- const visit = (name, fromDir, dependentCopy, required, dependent) => {
64
- const found = findPackage(name, fromDir);
65
- if (!found) {
66
- if (required) {
67
- throw new Error(
68
- `Can't find package "${name}", which ${dependent} imports, in a node_modules folder above ${fromDir}`
69
- );
70
- }
71
- return;
72
- }
73
- const { realDir, foundIn } = found;
74
- const reachable = reachableFrom(dependentCopy, name);
75
- if (reachable === realDir) {
76
- return;
77
- }
78
- const copiedFolder = copyOf.get(foundIn);
79
- const besideDependent = path.join(dependentCopy, "node_modules", name);
80
- const target = copiedFolder ? path.join(copiedFolder, "node_modules", name) : inNodeModules(foundIn) || reachable ? besideDependent : path.join(childrenDir, "node_modules", name);
81
- const there = occupied.get(target);
82
- if (there) {
83
- throw new Error(
84
- `Sandboxed child scripts need two versions of "${name}" in one place: ${there.realDir} (for ${there.neededBy}) and ${realDir} (for ${dependent}). Install a single version of it, or move the children so their imports resolve the same way.`
85
- );
86
- }
87
- occupied.set(target, { realDir, neededBy: dependent });
88
- if (!copyOf.has(realDir)) {
89
- copyOf.set(realDir, target);
90
- }
91
- fs.cpSync(realDir, target, {
92
- recursive: true,
93
- dereference: true,
94
- // Its own node_modules are copied as dependencies, when they're needed
95
- filter: (src) => src !== path.join(realDir, "node_modules")
96
- });
97
- const pkg = readPackageJson(realDir);
98
- const optional = /* @__PURE__ */ new Set([
99
- ...Object.keys(pkg.optionalDependencies ?? {}),
100
- ...Object.keys(pkg.peerDependencies ?? {})
101
- ]);
102
- for (const dep of Object.keys(pkg.dependencies ?? {})) {
103
- visit(dep, realDir, target, !optional.has(dep), name);
104
- }
105
- for (const dep of optional) {
106
- visit(dep, realDir, target, false, name);
107
- }
108
- };
109
- for (const { source, imports } of children) {
110
- for (const specifier of imports) {
111
- if (isBuiltin(specifier) || specifier.startsWith("#")) {
112
- continue;
113
- }
114
- visit(
115
- packageName(specifier),
116
- fs.realpathSync(path.dirname(source)),
117
- childrenDir,
118
- true,
119
- path.basename(source)
120
- );
121
- }
122
- }
123
- }
124
- const findVercelFunctionDirs = (dir) => fs.existsSync(dir) ? fs.readdirSync(dir, { withFileTypes: true }).filter((entry) => entry.isDirectory()).flatMap((entry) => {
125
- const entryPath = path.join(dir, entry.name);
126
- return entry.name.endsWith(".func") ? [entryPath] : findVercelFunctionDirs(entryPath);
127
- }) : [];
128
- function copySandboxChildrenIntoVercelFunctions({
129
- appRoot,
130
- childrenDir,
131
- functionsDir
132
- }) {
133
- const childrenDirInApp = path.relative(appRoot, childrenDir);
134
- if (!fs.existsSync(childrenDir)) {
135
- return [];
136
- }
137
- const functionDirs = findVercelFunctionDirs(functionsDir).filter((dir) => {
138
- const config = path.join(dir, ".vc-config.json");
139
- if (!fs.existsSync(config)) {
140
- return false;
141
- }
142
- const parsed = JSON.parse(fs.readFileSync(config, "utf8"));
143
- return !!parsed && typeof parsed === "object" && "runtime" in parsed && typeof parsed.runtime === "string" && parsed.runtime.startsWith("nodejs");
144
- });
145
- for (const dir of functionDirs) {
146
- fs.cpSync(childrenDir, path.join(dir, childrenDirInApp), {
147
- recursive: true
148
- });
149
- }
150
- return functionDirs;
151
- }
152
- export {
153
- SANDBOX_CHILDREN_DIR,
154
- copySandboxChildrenIntoVercelFunctions,
155
- findSandboxChildScripts,
156
- vendorSandboxChildPackages
157
- };
@@ -1,20 +0,0 @@
1
- import type { Plugin } from 'vite';
2
- /**
3
- * For the bundled API build (`cedar build --ud`): the sandboxed child
4
- * scripts in api/src are extra entries of the build (see
5
- * `findSandboxChildScripts()`), so each comes out as
6
- * `sandbox-children/<name>.child.js`. This plugin checks that each child
7
- * stands alone, importing only packages and Node builtins (it can read
8
- * nothing of the app), and then copies the packages they import into
9
- * `sandbox-children/node_modules`.
10
- *
11
- * `findChildScript()` from `@cedarjs/api/sandbox` finds the children there
12
- * from code in the bundle.
13
- */
14
- export declare function cedarSandboxChildrenPlugin({ environment, children, }: {
15
- /** The bundled build's environment */
16
- environment: string;
17
- /** The child scripts' entries: `{ 'sandbox-children/x.child': file }` */
18
- children: Record<string, string>;
19
- }): Plugin;
20
- //# sourceMappingURL=vite-plugin-cedar-sandbox-children.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"vite-plugin-cedar-sandbox-children.d.ts","sourceRoot":"","sources":["../../src/plugins/vite-plugin-cedar-sandbox-children.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AASlC;;;;;;;;;;;GAWG;AACH,wBAAgB,0BAA0B,CAAC,EACzC,WAAW,EACX,QAAQ,GACT,EAAE;IACD,sCAAsC;IACtC,WAAW,EAAE,MAAM,CAAA;IACnB,yEAAyE;IACzE,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CACjC,GAAG,MAAM,CAmDT"}
@@ -1,58 +0,0 @@
1
- import path from "node:path";
2
- import { normalizePath } from "vite";
3
- import {
4
- SANDBOX_CHILDREN_DIR,
5
- vendorSandboxChildPackages
6
- } from "../lib/sandboxChildren.js";
7
- function cedarSandboxChildrenPlugin({
8
- environment,
9
- children
10
- }) {
11
- const sources = new Set(
12
- Object.values(children).map((file) => normalizePath(file))
13
- );
14
- let imports = [];
15
- return {
16
- name: "cedar-sandbox-children",
17
- apply: "build",
18
- applyToEnvironment(env) {
19
- return env.name === environment;
20
- },
21
- generateBundle(_options, bundle) {
22
- imports = [];
23
- for (const chunk of Object.values(bundle)) {
24
- if (chunk.type !== "chunk" || !chunk.facadeModuleId) {
25
- continue;
26
- }
27
- const source = normalizePath(chunk.facadeModuleId);
28
- if (!sources.has(source)) {
29
- continue;
30
- }
31
- const bundled = [...chunk.imports, ...chunk.dynamicImports].filter(
32
- (id) => id in bundle
33
- );
34
- if (bundled.length > 0) {
35
- this.error(
36
- `${path.basename(source)} imports your app's own code (${bundled.join(", ")}). A sandboxed child script may import only packages and Node builtins: it runs in a process that can't read the rest of your app.`
37
- );
38
- }
39
- imports.push({
40
- source,
41
- imports: [...chunk.imports, ...chunk.dynamicImports]
42
- });
43
- }
44
- },
45
- writeBundle(options) {
46
- if (imports.length === 0 || !options.dir) {
47
- return;
48
- }
49
- vendorSandboxChildPackages(
50
- path.join(options.dir, SANDBOX_CHILDREN_DIR),
51
- imports
52
- );
53
- }
54
- };
55
- }
56
- export {
57
- cedarSandboxChildrenPlugin
58
- };