@netlify/edge-bundler 14.9.7 → 14.9.9

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.
@@ -15,4 +15,9 @@ interface BundleTarballOptions {
15
15
  vendorDirectory?: string;
16
16
  }
17
17
  export declare const bundle: ({ buildID, deno, distDirectory, functions, importMap, vendorDirectory, }: BundleTarballOptions) => Promise<Bundle>;
18
+ /**
19
+ * Rewrites import assert into import with in the bundle directory
20
+ * Defaults to copying the file in its current form
21
+ */
22
+ export declare function rewriteImportAssertions(sourceFile: string, destPath: string): Promise<void>;
18
23
  export {};
@@ -8,6 +8,7 @@ import tmp from 'tmp-promise';
8
8
  import { BundleFormat } from '../bundle.js';
9
9
  import { listRecursively } from '../utils/fs.js';
10
10
  import { getFileHash } from '../utils/sha256.js';
11
+ import { rewriteSourceImportAssertions } from '../utils/import_attributes.js';
11
12
  const TARBALL_EXTENSION = '.tar.gz';
12
13
  const getUnixPath = (input) => input.split(path.sep).join('/');
13
14
  export const bundle = async ({ buildID, deno, distDirectory, functions, importMap, vendorDirectory, }) => {
@@ -36,14 +37,9 @@ export const bundle = async ({ buildID, deno, distDirectory, functions, importMa
36
37
  const relativePath = path.relative(commonPath, sourceFile);
37
38
  const destPath = path.join(bundleDir.path, relativePath);
38
39
  await fs.mkdir(path.dirname(destPath), { recursive: true });
39
- await fs.copyFile(sourceFile, destPath);
40
+ // Deno 2.x dropped support for import for import assertions
41
+ await rewriteImportAssertions(sourceFile, destPath);
40
42
  }
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);
47
43
  // Vendor all dependencies in the bundle directory
48
44
  await deno.run([
49
45
  'install',
@@ -73,11 +69,17 @@ export const bundle = async ({ buildID, deno, distDirectory, functions, importMa
73
69
  const relativePath = path.relative(vendorDirectory, vendorFile);
74
70
  const destPath = path.join(bundleDir.path, npmVendorDir, relativePath);
75
71
  await fs.mkdir(path.dirname(destPath), { recursive: true });
76
- await fs.copyFile(vendorFile, destPath);
72
+ // Rewrite import assertions in vendored files as well
73
+ await rewriteImportAssertions(vendorFile, destPath);
77
74
  }
78
75
  }
79
76
  // Map common path to relative paths
80
77
  prefixes[pathToFileURL(commonPath + path.sep).href] = './';
78
+ // Rewrite bare specifier imports to their resolved URLs so they can be
79
+ // resolved by Deno's --vendor flag at runtime without needing the customer's import map.
80
+ // At runtime, Deno discovers config from /platform/deno.json (the bootstrap entry
81
+ // point), not /function/deno.json, so the customer's import map is unreachable.
82
+ await rewriteBareSpecifiers(bundleDir.path, sourceFiles, commonPath, importMap, prefixes);
81
83
  // Get import map contents with file:// URLs transformed to relative paths
82
84
  const importMapContents = importMap.getContents(prefixes);
83
85
  // Create deno.json with import map contents for runtime resolution
@@ -130,10 +132,10 @@ const REWRITABLE_EXTENSIONS = new Set(['.ts', '.tsx', '.js', '.jsx', '.mjs', '.m
130
132
  * - Resolve to http/https or npm: URLs in the import map
131
133
  * - Are NOT node builtins or platform-provided imports
132
134
  */
133
- async function rewriteBareSpecifiers(bundleDirPath, sourceFiles, commonPath, importMap) {
134
- const contents = importMap.getContents();
135
+ async function rewriteBareSpecifiers(bundleDirPath, sourceFiles, commonPath, importMap, prefixes) {
136
+ const contents = importMap.getContents(prefixes);
135
137
  const builtinSet = new Set(builtinModules);
136
- // Collect bare specifiers that should be rewritten to URLs
138
+ // Collect bare specifiers that should be rewritten to URLs or relative paths
137
139
  const specifierEntries = Object.entries(contents.imports)
138
140
  .filter(([specifier, url]) => {
139
141
  // Skip node builtins
@@ -142,13 +144,17 @@ async function rewriteBareSpecifiers(bundleDirPath, sourceFiles, commonPath, imp
142
144
  // Skip platform-provided specifiers (handled by platform deno.json)
143
145
  if (PLATFORM_SPECIFIERS.has(specifier))
144
146
  return false;
145
- // Skip relative/absolute path specifiers
147
+ // Skip relative/absolute path specifiers in the specifier itself
146
148
  if (specifier.startsWith('.') || specifier.startsWith('/'))
147
149
  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;
150
+ // Rewrite http/https, npm:, or vendored npm modules (relative paths with .netlify-npm-vendor)
151
+ if (url.startsWith('http://') ||
152
+ url.startsWith('https://') ||
153
+ url.startsWith('npm:') ||
154
+ url.includes('.netlify-npm-vendor')) {
155
+ return true;
156
+ }
157
+ return false;
152
158
  })
153
159
  // Sort longest first so prefix mappings like "lodash/" match before "lodash"
154
160
  .sort((a, b) => b[0].length - a[0].length);
@@ -167,8 +173,19 @@ async function rewriteBareSpecifiers(bundleDirPath, sourceFiles, commonPath, imp
167
173
  let modified = source;
168
174
  for (const [specifier, url] of specifierEntries) {
169
175
  const escaped = specifier.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
176
+ // Convert bundle-root-relative paths to source-file-relative paths
177
+ let targetUrl = url;
178
+ if (url.startsWith('./') || url.startsWith('../')) {
179
+ // URL is relative to bundle root (e.g., "./.netlify-npm-vendor/bundled-parent-1.js")
180
+ // Convert to absolute path in bundle, then to relative from source file
181
+ const targetAbsolutePath = path.resolve(bundleDirPath, url);
182
+ const relativeImport = path.relative(path.dirname(destPath), targetAbsolutePath);
183
+ // Ensure forward slashes and ./ prefix for clarity
184
+ targetUrl = relativeImport.startsWith('.') ? relativeImport : `./${relativeImport}`;
185
+ targetUrl = targetUrl.replace(/\\/g, '/');
186
+ }
170
187
  // Escape $ in URL for use in replacement string
171
- const safeUrl = url.replace(/\$/g, '$$$$');
188
+ const safeUrl = targetUrl.replace(/\$/g, '$$$$');
172
189
  for (const quote of ['"', "'"]) {
173
190
  if (specifier.endsWith('/')) {
174
191
  // Prefix mapping: "specifier/subpath" -> "url/subpath"
@@ -227,3 +244,21 @@ async function getRequiredSourceFiles(deno, entryPoints, importMap) {
227
244
  }
228
245
  return Array.from(localFiles).sort();
229
246
  }
247
+ /**
248
+ * Rewrites import assert into import with in the bundle directory
249
+ * Defaults to copying the file in its current form
250
+ */
251
+ export async function rewriteImportAssertions(sourceFile, destPath) {
252
+ if (!REWRITABLE_EXTENSIONS.has(path.extname(sourceFile))) {
253
+ await fs.copyFile(sourceFile, destPath);
254
+ return;
255
+ }
256
+ try {
257
+ const source = await fs.readFile(sourceFile, 'utf-8');
258
+ const modified = rewriteSourceImportAssertions(source);
259
+ await fs.writeFile(destPath, modified);
260
+ }
261
+ catch {
262
+ await fs.copyFile(sourceFile, destPath);
263
+ }
264
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Given source code rewrites import assert into import with
3
+ */
4
+ export declare function rewriteSourceImportAssertions(source: string): string;
@@ -0,0 +1,48 @@
1
+ import { Parser } from 'acorn';
2
+ import * as walk from 'acorn-walk';
3
+ import { tsPlugin } from '@sveltejs/acorn-typescript';
4
+ const acorn = Parser.extend(tsPlugin());
5
+ /**
6
+ * Given source code rewrites import assert into import with
7
+ */
8
+ export function rewriteSourceImportAssertions(source) {
9
+ if (!source.includes('assert')) {
10
+ return source;
11
+ }
12
+ let modified = source;
13
+ try {
14
+ const parsedAST = acorn.parse(source, {
15
+ ecmaVersion: 'latest',
16
+ sourceType: 'module',
17
+ });
18
+ walk.simple(parsedAST, {
19
+ ImportDeclaration(node) {
20
+ const statement = source.slice(node.source.end, node.end);
21
+ if (statement.includes('assert')) {
22
+ const newStatement = statement.replace('assert', 'with');
23
+ modified = modified.replace(statement, newStatement);
24
+ }
25
+ },
26
+ ImportExpression(node) {
27
+ const statement = source.slice(node.source.end, node.end);
28
+ if (statement.includes('assert')) {
29
+ const newStatement = statement.replace('assert', 'with');
30
+ modified = modified.replace(statement, newStatement);
31
+ }
32
+ },
33
+ ExportNamedDeclaration(node) {
34
+ if (!node.source)
35
+ return;
36
+ const statement = source.slice(node.source.end, node.end);
37
+ if (statement.includes('assert')) {
38
+ const newStatement = statement.replace('assert', 'with');
39
+ modified = modified.replace(statement, newStatement);
40
+ }
41
+ },
42
+ });
43
+ return modified;
44
+ }
45
+ catch {
46
+ return source;
47
+ }
48
+ }
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/edge-bundler",
3
- "version": "14.9.7",
3
+ "version": "14.9.9",
4
4
  "description": "Intelligently prepare Netlify Edge Functions for deployment",
5
5
  "type": "module",
6
6
  "main": "./dist/node/index.js",
@@ -59,6 +59,9 @@
59
59
  },
60
60
  "dependencies": {
61
61
  "@import-maps/resolve": "^2.0.0",
62
+ "@sveltejs/acorn-typescript": "^1.0.9",
63
+ "acorn": "^8.15.0",
64
+ "acorn-walk": "^8.3.4",
62
65
  "ajv": "^8.11.2",
63
66
  "ajv-errors": "^3.0.0",
64
67
  "better-ajv-errors": "^1.2.0",
@@ -79,5 +82,5 @@
79
82
  "urlpattern-polyfill": "8.0.2",
80
83
  "uuid": "^11.0.0"
81
84
  },
82
- "gitHead": "55f6d3568bcea071e679b4152c3273eb48223159"
85
+ "gitHead": "63274005adc06616e05a0b6ba0c1b0d1e2f49dab"
83
86
  }