@netlify/edge-bundler 14.9.8 → 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,7 +37,8 @@ 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
43
  // Vendor all dependencies in the bundle directory
42
44
  await deno.run([
@@ -67,7 +69,8 @@ export const bundle = async ({ buildID, deno, distDirectory, functions, importMa
67
69
  const relativePath = path.relative(vendorDirectory, vendorFile);
68
70
  const destPath = path.join(bundleDir.path, npmVendorDir, relativePath);
69
71
  await fs.mkdir(path.dirname(destPath), { recursive: true });
70
- await fs.copyFile(vendorFile, destPath);
72
+ // Rewrite import assertions in vendored files as well
73
+ await rewriteImportAssertions(vendorFile, destPath);
71
74
  }
72
75
  }
73
76
  // Map common path to relative paths
@@ -241,3 +244,21 @@ async function getRequiredSourceFiles(deno, entryPoints, importMap) {
241
244
  }
242
245
  return Array.from(localFiles).sort();
243
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.8",
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": "4395795665bc891337d375d51b0a63f6551d7d83"
85
+ "gitHead": "63274005adc06616e05a0b6ba0c1b0d1e2f49dab"
83
86
  }