@netlify/edge-bundler 14.9.6 → 14.9.8

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';
@@ -71,6 +72,11 @@ export const bundle = async ({ buildID, deno, distDirectory, functions, importMa
71
72
  }
72
73
  // Map common path to relative paths
73
74
  prefixes[pathToFileURL(commonPath + path.sep).href] = './';
75
+ // Rewrite bare specifier imports to their resolved URLs so they can be
76
+ // resolved by Deno's --vendor flag at runtime without needing the customer's import map.
77
+ // At runtime, Deno discovers config from /platform/deno.json (the bootstrap entry
78
+ // point), not /function/deno.json, so the customer's import map is unreachable.
79
+ await rewriteBareSpecifiers(bundleDir.path, sourceFiles, commonPath, importMap, prefixes);
74
80
  // Get import map contents with file:// URLs transformed to relative paths
75
81
  const importMapContents = importMap.getContents(prefixes);
76
82
  // Create deno.json with import map contents for runtime resolution
@@ -107,6 +113,92 @@ export const bundle = async ({ buildID, deno, distDirectory, functions, importMa
107
113
  hash,
108
114
  };
109
115
  };
116
+ // Specifiers provided by the platform deno.json at runtime - no need to rewrite these.
117
+ const PLATFORM_SPECIFIERS = new Set(['@netlify/edge-functions', 'netlify:edge']);
118
+ // Source file extensions that may contain import statements.
119
+ const REWRITABLE_EXTENSIONS = new Set(['.ts', '.tsx', '.js', '.jsx', '.mjs', '.mts']);
120
+ /**
121
+ * Rewrites bare specifier imports in copied source files to their resolved URLs
122
+ * from the import map. This allows Deno's --vendor flag to resolve these imports
123
+ * at runtime without needing the customer's import map (which is unreachable
124
+ * because Deno discovers config from the platform bootstrap directory, not the
125
+ * function directory).
126
+ *
127
+ * Only rewrites specifiers that:
128
+ * - Are bare package specifiers (not relative, absolute, or URL imports)
129
+ * - Resolve to http/https or npm: URLs in the import map
130
+ * - Are NOT node builtins or platform-provided imports
131
+ */
132
+ async function rewriteBareSpecifiers(bundleDirPath, sourceFiles, commonPath, importMap, prefixes) {
133
+ const contents = importMap.getContents(prefixes);
134
+ const builtinSet = new Set(builtinModules);
135
+ // Collect bare specifiers that should be rewritten to URLs or relative paths
136
+ const specifierEntries = Object.entries(contents.imports)
137
+ .filter(([specifier, url]) => {
138
+ // Skip node builtins
139
+ if (specifier.startsWith('node:') || builtinSet.has(specifier))
140
+ return false;
141
+ // Skip platform-provided specifiers (handled by platform deno.json)
142
+ if (PLATFORM_SPECIFIERS.has(specifier))
143
+ return false;
144
+ // Skip relative/absolute path specifiers in the specifier itself
145
+ if (specifier.startsWith('.') || specifier.startsWith('/'))
146
+ return false;
147
+ // Rewrite http/https, npm:, or vendored npm modules (relative paths with .netlify-npm-vendor)
148
+ if (url.startsWith('http://') ||
149
+ url.startsWith('https://') ||
150
+ url.startsWith('npm:') ||
151
+ url.includes('.netlify-npm-vendor')) {
152
+ return true;
153
+ }
154
+ return false;
155
+ })
156
+ // Sort longest first so prefix mappings like "lodash/" match before "lodash"
157
+ .sort((a, b) => b[0].length - a[0].length);
158
+ for (const sourceFile of sourceFiles) {
159
+ if (!REWRITABLE_EXTENSIONS.has(path.extname(sourceFile)))
160
+ continue;
161
+ const relativePath = path.relative(commonPath, sourceFile);
162
+ const destPath = path.join(bundleDirPath, relativePath);
163
+ let source;
164
+ try {
165
+ source = await fs.readFile(destPath, 'utf-8');
166
+ }
167
+ catch {
168
+ continue;
169
+ }
170
+ let modified = source;
171
+ for (const [specifier, url] of specifierEntries) {
172
+ const escaped = specifier.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
173
+ // Convert bundle-root-relative paths to source-file-relative paths
174
+ let targetUrl = url;
175
+ if (url.startsWith('./') || url.startsWith('../')) {
176
+ // URL is relative to bundle root (e.g., "./.netlify-npm-vendor/bundled-parent-1.js")
177
+ // Convert to absolute path in bundle, then to relative from source file
178
+ const targetAbsolutePath = path.resolve(bundleDirPath, url);
179
+ const relativeImport = path.relative(path.dirname(destPath), targetAbsolutePath);
180
+ // Ensure forward slashes and ./ prefix for clarity
181
+ targetUrl = relativeImport.startsWith('.') ? relativeImport : `./${relativeImport}`;
182
+ targetUrl = targetUrl.replace(/\\/g, '/');
183
+ }
184
+ // Escape $ in URL for use in replacement string
185
+ const safeUrl = targetUrl.replace(/\$/g, '$$$$');
186
+ for (const quote of ['"', "'"]) {
187
+ if (specifier.endsWith('/')) {
188
+ // Prefix mapping: "specifier/subpath" -> "url/subpath"
189
+ modified = modified.replace(new RegExp(`(\\bfrom\\s+|\\bimport\\s+|\\bimport\\s*\\(\\s*)${quote}${escaped}([^${quote}]*)${quote}`, 'g'), `$1${quote}${safeUrl}$2${quote}`);
190
+ }
191
+ else {
192
+ // Exact mapping: "specifier" -> "url"
193
+ modified = modified.replace(new RegExp(`(\\bfrom\\s+|\\bimport\\s+|\\bimport\\s*\\(\\s*)${quote}${escaped}${quote}`, 'g'), `$1${quote}${safeUrl}${quote}`);
194
+ }
195
+ }
196
+ }
197
+ if (modified !== source) {
198
+ await fs.writeFile(destPath, modified);
199
+ }
200
+ }
201
+ }
110
202
  /**
111
203
  * Uses deno info to get the module graph and extract only the local source files
112
204
  * 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.8",
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": "4395795665bc891337d375d51b0a63f6551d7d83"
83
83
  }