@netlify/edge-bundler 14.9.5 → 14.9.7

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,4 +1,5 @@
1
1
  import { promises as fs } from 'fs';
2
+ import { builtinModules } from 'module';
2
3
  import path from 'path';
3
4
  import { fileURLToPath, pathToFileURL } from 'url';
4
5
  import commonPathPrefix from 'common-path-prefix';
@@ -17,25 +18,32 @@ export const bundle = async ({ buildID, deno, distDirectory, functions, importMa
17
18
  version: 1,
18
19
  };
19
20
  const entryPoints = functions.map((func) => func.path);
20
- // Find the common path prefix for all entry points. When using a single
21
- // entry point, `commonPathPrefix` returns an empty string, so we use
21
+ // Use deno info to get the module graph and identify which local files are actually needed.
22
+ // This avoids copying unnecessary files (like node_modules) that happen to be under commonPath.
23
+ // If module graph analysis fails, fall back to copying files from entry point directories.
24
+ const sourceFiles = await getRequiredSourceFiles(deno, entryPoints, importMap);
25
+ // Find the common path prefix for all source files (entry points + their local imports).
26
+ // This ensures imports to sibling directories (e.g., ../internal/) are included.
27
+ // When using a single file, `commonPathPrefix` returns an empty string, so we use
22
28
  // the path of the first entry point's directory.
23
- const commonPath = commonPathPrefix(entryPoints) || path.dirname(entryPoints[0]);
29
+ const commonPath = commonPathPrefix(sourceFiles) || path.dirname(entryPoints[0]);
24
30
  // Build the manifest mapping function names to their relative paths
25
31
  for (const func of functions) {
26
32
  const relativePath = path.relative(commonPath, func.path);
27
33
  manifest.functions[func.name] = getUnixPath(relativePath);
28
34
  }
29
- // Use deno info to get the module graph and identify which local files are actually needed.
30
- // This avoids copying unnecessary files (like node_modules) that happen to be under commonPath.
31
- // If module graph analysis fails, fall back to copying files from entry point directories.
32
- const sourceFiles = await getRequiredSourceFiles(deno, entryPoints, importMap, commonPath);
33
35
  for (const sourceFile of sourceFiles) {
34
36
  const relativePath = path.relative(commonPath, sourceFile);
35
37
  const destPath = path.join(bundleDir.path, relativePath);
36
38
  await fs.mkdir(path.dirname(destPath), { recursive: true });
37
39
  await fs.copyFile(sourceFile, destPath);
38
40
  }
41
+ // Rewrite bare specifier imports to their resolved URLs so they can be
42
+ // resolved by Deno's --vendor flag at runtime without needing the customer's import map.
43
+ // At runtime, Deno discovers config from /platform/deno.json (the bootstrap entry
44
+ // point), not /function/deno.json, so the customer's import map is unreachable.
45
+ // This is because we boot Deno before we've mounted the customer's edge-functions directory, so it can't be used to resolve imports during the initial bundle phase.
46
+ await rewriteBareSpecifiers(bundleDir.path, sourceFiles, commonPath, importMap);
39
47
  // Vendor all dependencies in the bundle directory
40
48
  await deno.run([
41
49
  'install',
@@ -106,6 +114,77 @@ export const bundle = async ({ buildID, deno, distDirectory, functions, importMa
106
114
  hash,
107
115
  };
108
116
  };
117
+ // Specifiers provided by the platform deno.json at runtime - no need to rewrite these.
118
+ const PLATFORM_SPECIFIERS = new Set(['@netlify/edge-functions', 'netlify:edge']);
119
+ // Source file extensions that may contain import statements.
120
+ const REWRITABLE_EXTENSIONS = new Set(['.ts', '.tsx', '.js', '.jsx', '.mjs', '.mts']);
121
+ /**
122
+ * Rewrites bare specifier imports in copied source files to their resolved URLs
123
+ * from the import map. This allows Deno's --vendor flag to resolve these imports
124
+ * at runtime without needing the customer's import map (which is unreachable
125
+ * because Deno discovers config from the platform bootstrap directory, not the
126
+ * function directory).
127
+ *
128
+ * Only rewrites specifiers that:
129
+ * - Are bare package specifiers (not relative, absolute, or URL imports)
130
+ * - Resolve to http/https or npm: URLs in the import map
131
+ * - Are NOT node builtins or platform-provided imports
132
+ */
133
+ async function rewriteBareSpecifiers(bundleDirPath, sourceFiles, commonPath, importMap) {
134
+ const contents = importMap.getContents();
135
+ const builtinSet = new Set(builtinModules);
136
+ // Collect bare specifiers that should be rewritten to URLs
137
+ const specifierEntries = Object.entries(contents.imports)
138
+ .filter(([specifier, url]) => {
139
+ // Skip node builtins
140
+ if (specifier.startsWith('node:') || builtinSet.has(specifier))
141
+ return false;
142
+ // Skip platform-provided specifiers (handled by platform deno.json)
143
+ if (PLATFORM_SPECIFIERS.has(specifier))
144
+ return false;
145
+ // Skip relative/absolute path specifiers
146
+ if (specifier.startsWith('.') || specifier.startsWith('/'))
147
+ return false;
148
+ // Only rewrite to http/https or npm: URLs
149
+ if (!url.startsWith('http://') && !url.startsWith('https://') && !url.startsWith('npm:'))
150
+ return false;
151
+ return true;
152
+ })
153
+ // Sort longest first so prefix mappings like "lodash/" match before "lodash"
154
+ .sort((a, b) => b[0].length - a[0].length);
155
+ for (const sourceFile of sourceFiles) {
156
+ if (!REWRITABLE_EXTENSIONS.has(path.extname(sourceFile)))
157
+ continue;
158
+ const relativePath = path.relative(commonPath, sourceFile);
159
+ const destPath = path.join(bundleDirPath, relativePath);
160
+ let source;
161
+ try {
162
+ source = await fs.readFile(destPath, 'utf-8');
163
+ }
164
+ catch {
165
+ continue;
166
+ }
167
+ let modified = source;
168
+ for (const [specifier, url] of specifierEntries) {
169
+ const escaped = specifier.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
170
+ // Escape $ in URL for use in replacement string
171
+ const safeUrl = url.replace(/\$/g, '$$$$');
172
+ for (const quote of ['"', "'"]) {
173
+ if (specifier.endsWith('/')) {
174
+ // Prefix mapping: "specifier/subpath" -> "url/subpath"
175
+ modified = modified.replace(new RegExp(`(\\bfrom\\s+|\\bimport\\s+|\\bimport\\s*\\(\\s*)${quote}${escaped}([^${quote}]*)${quote}`, 'g'), `$1${quote}${safeUrl}$2${quote}`);
176
+ }
177
+ else {
178
+ // Exact mapping: "specifier" -> "url"
179
+ modified = modified.replace(new RegExp(`(\\bfrom\\s+|\\bimport\\s+|\\bimport\\s*\\(\\s*)${quote}${escaped}${quote}`, 'g'), `$1${quote}${safeUrl}${quote}`);
180
+ }
181
+ }
182
+ }
183
+ if (modified !== source) {
184
+ await fs.writeFile(destPath, modified);
185
+ }
186
+ }
187
+ }
109
188
  /**
110
189
  * Uses deno info to get the module graph and extract only the local source files
111
190
  * that are actually needed by the entry points. This avoids copying unnecessary
@@ -114,8 +193,7 @@ export const bundle = async ({ buildID, deno, distDirectory, functions, importMa
114
193
  * If deno info fails, falls back to copying files from the directories containing
115
194
  * the entry points (not the entire common path).
116
195
  */
117
- async function getRequiredSourceFiles(deno, entryPoints, importMap, commonPath) {
118
- const commonPathUrl = pathToFileURL(commonPath + path.sep).href;
196
+ async function getRequiredSourceFiles(deno, entryPoints, importMap) {
119
197
  const localFiles = new Set();
120
198
  const importMapDataUrl = importMap.withNodeBuiltins().toDataURL();
121
199
  // Run deno info for each entry point and combine the results
@@ -129,10 +207,9 @@ async function getRequiredSourceFiles(deno, entryPoints, importMap, commonPath)
129
207
  pathToFileURL(entryPoint).href,
130
208
  ]);
131
209
  const graph = JSON.parse(stdout);
132
- // Extract local files from the module graph
210
+ // Extract all local files from the module graph
133
211
  for (const module of graph.modules) {
134
- // Only include local file:// URLs that are under the common path
135
- if (module.specifier.startsWith('file://') && module.specifier.startsWith(commonPathUrl)) {
212
+ if (module.specifier.startsWith('file://')) {
136
213
  const filePath = fileURLToPath(module.specifier);
137
214
  localFiles.add(filePath);
138
215
  }
@@ -142,11 +219,9 @@ async function getRequiredSourceFiles(deno, entryPoints, importMap, commonPath)
142
219
  // If deno info fails for this entry point, fall back to copying files
143
220
  // from its directory
144
221
  const dir = path.dirname(entryPoint);
145
- if (dir.startsWith(commonPath)) {
146
- const files = await listRecursively(dir);
147
- for (const file of files) {
148
- localFiles.add(file);
149
- }
222
+ const files = await listRecursively(dir);
223
+ for (const file of files) {
224
+ localFiles.add(file);
150
225
  }
151
226
  }
152
227
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/edge-bundler",
3
- "version": "14.9.5",
3
+ "version": "14.9.7",
4
4
  "description": "Intelligently prepare Netlify Edge Functions for deployment",
5
5
  "type": "module",
6
6
  "main": "./dist/node/index.js",
@@ -79,5 +79,5 @@
79
79
  "urlpattern-polyfill": "8.0.2",
80
80
  "uuid": "^11.0.0"
81
81
  },
82
- "gitHead": "76b0a932036a205140d5f2110f3ef7e90583fbb0"
82
+ "gitHead": "55f6d3568bcea071e679b4152c3273eb48223159"
83
83
  }