@netlify/edge-bundler 1.13.0 → 1.14.0
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.
- package/deno/bundle.ts +1 -1
- package/deno/lib/common.ts +47 -0
- package/deno/lib/consts.ts +4 -0
- package/deno/lib/stage2.ts +81 -0
- package/package.json +1 -1
package/deno/bundle.ts
CHANGED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { load } from "https://deno.land/x/eszip@v0.18.0/loader.ts";
|
|
2
|
+
import { LoadResponse } from "https://deno.land/x/eszip@v0.18.0/mod.ts";
|
|
3
|
+
import * as path from "https://deno.land/std@0.127.0/path/mod.ts";
|
|
4
|
+
import { retryAsync } from "https://deno.land/x/retry@v2.0.0/mod.ts";
|
|
5
|
+
|
|
6
|
+
const inlineModule = (
|
|
7
|
+
specifier: string,
|
|
8
|
+
content: string,
|
|
9
|
+
): LoadResponse => {
|
|
10
|
+
return {
|
|
11
|
+
content,
|
|
12
|
+
headers: {
|
|
13
|
+
"content-type": "application/typescript",
|
|
14
|
+
},
|
|
15
|
+
kind: "module",
|
|
16
|
+
specifier,
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const loadFromVirtualRoot = async (
|
|
21
|
+
specifier: string,
|
|
22
|
+
virtualRoot: string,
|
|
23
|
+
basePath: string,
|
|
24
|
+
) => {
|
|
25
|
+
const basePathURL = path.toFileUrl(basePath).toString();
|
|
26
|
+
const filePath = specifier.replace(virtualRoot.slice(0, -1), basePathURL);
|
|
27
|
+
const file = await load(filePath);
|
|
28
|
+
|
|
29
|
+
if (file === undefined) {
|
|
30
|
+
throw new Error(`Could not find file: ${filePath}`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return { ...file, specifier };
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const loadWithRetry = (specifier: string, delay = 1000, maxTry = 3) => {
|
|
37
|
+
if (!specifier.startsWith("https://")) {
|
|
38
|
+
return load(specifier);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return retryAsync(() => load(specifier), {
|
|
42
|
+
delay,
|
|
43
|
+
maxTry,
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export { inlineModule, loadFromVirtualRoot, loadWithRetry };
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { build, LoadResponse } from 'https://deno.land/x/eszip@v0.18.0/mod.ts'
|
|
2
|
+
|
|
3
|
+
import * as path from 'https://deno.land/std@0.127.0/path/mod.ts'
|
|
4
|
+
|
|
5
|
+
import { PUBLIC_SPECIFIER, STAGE2_SPECIFIER, virtualRoot } from './consts.ts'
|
|
6
|
+
import { inlineModule, loadFromVirtualRoot, loadWithRetry } from './common.ts'
|
|
7
|
+
|
|
8
|
+
interface InputFunction {
|
|
9
|
+
name: string
|
|
10
|
+
path: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface WriteStage2Options {
|
|
14
|
+
basePath: string
|
|
15
|
+
destPath: string
|
|
16
|
+
functions: InputFunction[]
|
|
17
|
+
imports?: Record<string, string>
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const getFunctionReference = (basePath: string, func: InputFunction, index: number) => {
|
|
21
|
+
const importName = `func${index}`
|
|
22
|
+
const exportLine = `"${func.name}": ${importName}`
|
|
23
|
+
const url = getVirtualPath(basePath, func.path)
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
exportLine,
|
|
27
|
+
importLine: `import ${importName} from "${url}";`,
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const getStage2Entry = (basePath: string, functions: InputFunction[]) => {
|
|
32
|
+
const lines = functions.map((func, index) => getFunctionReference(basePath, func, index))
|
|
33
|
+
const importLines = lines.map(({ importLine }) => importLine).join('\n')
|
|
34
|
+
const exportLines = lines.map(({ exportLine }) => exportLine).join(', ')
|
|
35
|
+
const exportDeclaration = `export const functions = {${exportLines}};`
|
|
36
|
+
|
|
37
|
+
return [importLines, exportDeclaration].join('\n\n')
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const getVirtualPath = (basePath: string, filePath: string) => {
|
|
41
|
+
const relativePath = path.relative(basePath, filePath)
|
|
42
|
+
const url = new URL(relativePath, virtualRoot)
|
|
43
|
+
|
|
44
|
+
return url
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const stage2Loader = (basePath: string, functions: InputFunction[], imports: Record<string, string> = {}) => {
|
|
48
|
+
return async (specifier: string): Promise<LoadResponse | undefined> => {
|
|
49
|
+
if (specifier === STAGE2_SPECIFIER) {
|
|
50
|
+
const stage2Entry = getStage2Entry(basePath, functions)
|
|
51
|
+
|
|
52
|
+
return inlineModule(specifier, stage2Entry)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (specifier === PUBLIC_SPECIFIER) {
|
|
56
|
+
return {
|
|
57
|
+
kind: 'external',
|
|
58
|
+
specifier,
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (imports[specifier] !== undefined) {
|
|
63
|
+
return await loadWithRetry(imports[specifier])
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (specifier.startsWith(virtualRoot)) {
|
|
67
|
+
return loadFromVirtualRoot(specifier, virtualRoot, basePath)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return await loadWithRetry(specifier)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const writeStage2 = async ({ basePath, destPath, functions, imports }: WriteStage2Options) => {
|
|
75
|
+
const loader = stage2Loader(basePath, functions, imports)
|
|
76
|
+
const bytes = await build([STAGE2_SPECIFIER], loader)
|
|
77
|
+
|
|
78
|
+
return await Deno.writeFile(destPath, bytes)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export { writeStage2 }
|