@netlify/edge-bundler 14.9.6 → 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';
@@ -37,6 +38,12 @@ export const bundle = async ({ buildID, deno, distDirectory, functions, importMa
37
38
  await fs.mkdir(path.dirname(destPath), { recursive: true });
38
39
  await fs.copyFile(sourceFile, destPath);
39
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);
40
47
  // Vendor all dependencies in the bundle directory
41
48
  await deno.run([
42
49
  'install',
@@ -107,6 +114,77 @@ export const bundle = async ({ buildID, deno, distDirectory, functions, importMa
107
114
  hash,
108
115
  };
109
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
+ }
110
188
  /**
111
189
  * Uses deno info to get the module graph and extract only the local source files
112
190
  * that are actually needed by the entry points. This avoids copying unnecessary
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/edge-bundler",
3
- "version": "14.9.6",
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": "3b7249e4fd7ea006bf473b4953c17312369d1973"
82
+ "gitHead": "55f6d3568bcea071e679b4152c3273eb48223159"
83
83
  }