@netlify/edge-bundler 8.19.1 → 8.20.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 +2 -2
- package/deno/lib/stage2.ts +21 -4
- package/dist/node/bundler.d.ts +7 -7
- package/dist/node/bundler.js +32 -2
- package/dist/node/bundler.test.js +25 -1
- package/dist/node/feature_flags.d.ts +2 -0
- package/dist/node/feature_flags.js +1 -0
- package/dist/node/formats/eszip.d.ts +2 -1
- package/dist/node/formats/eszip.js +9 -6
- package/dist/node/import_map.d.ts +6 -0
- package/dist/node/import_map.js +22 -0
- package/dist/node/import_map.test.js +18 -9
- package/dist/node/npm_dependencies.d.ts +22 -0
- package/dist/node/npm_dependencies.js +189 -0
- package/dist/shared/consts.d.ts +3 -0
- package/dist/shared/consts.js +3 -0
- package/dist/shared/stage2.d.ts +1 -0
- package/dist/test/util.d.ts +1 -1
- package/dist/test/util.js +5 -2
- package/package.json +2 -1
- package/shared/consts.ts +3 -0
- package/shared/stage2.ts +1 -0
package/deno/bundle.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { writeStage2 } from './lib/stage2.ts'
|
|
2
2
|
|
|
3
3
|
const [payload] = Deno.args
|
|
4
|
-
const { basePath, destPath, externals, functions, importMapData } = JSON.parse(payload)
|
|
4
|
+
const { basePath, destPath, externals, functions, importMapData, vendorDirectory } = JSON.parse(payload)
|
|
5
5
|
|
|
6
6
|
try {
|
|
7
|
-
await writeStage2({ basePath, destPath, externals, functions, importMapData })
|
|
7
|
+
await writeStage2({ basePath, destPath, externals, functions, importMapData, vendorDirectory })
|
|
8
8
|
} catch (error) {
|
|
9
9
|
if (error instanceof Error && error.message.includes("The module's source code could not be parsed")) {
|
|
10
10
|
delete error.stack
|
package/deno/lib/stage2.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { build, LoadResponse } from 'https://deno.land/x/eszip@v0.40.0/mod.ts'
|
|
|
3
3
|
import * as path from 'https://deno.land/std@0.177.0/path/mod.ts'
|
|
4
4
|
|
|
5
5
|
import type { InputFunction, WriteStage2Options } from '../../shared/stage2.ts'
|
|
6
|
-
import { importMapSpecifier, virtualRoot } from '../../shared/consts.ts'
|
|
6
|
+
import { importMapSpecifier, virtualRoot, virtualVendorRoot } from '../../shared/consts.ts'
|
|
7
7
|
import { LEGACY_PUBLIC_SPECIFIER, PUBLIC_SPECIFIER, STAGE2_SPECIFIER } from './consts.ts'
|
|
8
8
|
import { inlineModule, loadFromVirtualRoot, loadWithRetry } from './common.ts'
|
|
9
9
|
|
|
@@ -63,7 +63,13 @@ const getVirtualPath = (basePath: string, filePath: string) => {
|
|
|
63
63
|
return url
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
const stage2Loader = (
|
|
66
|
+
const stage2Loader = (
|
|
67
|
+
basePath: string,
|
|
68
|
+
functions: InputFunction[],
|
|
69
|
+
externals: Set<string>,
|
|
70
|
+
importMapData: string | undefined,
|
|
71
|
+
vendorDirectory?: string,
|
|
72
|
+
) => {
|
|
67
73
|
return async (specifier: string): Promise<LoadResponse | undefined> => {
|
|
68
74
|
if (specifier === STAGE2_SPECIFIER) {
|
|
69
75
|
const stage2Entry = getStage2Entry(basePath, functions)
|
|
@@ -91,13 +97,24 @@ const stage2Loader = (basePath: string, functions: InputFunction[], externals: S
|
|
|
91
97
|
return loadFromVirtualRoot(specifier, virtualRoot, basePath)
|
|
92
98
|
}
|
|
93
99
|
|
|
100
|
+
if (vendorDirectory !== undefined && specifier.startsWith(virtualVendorRoot)) {
|
|
101
|
+
return loadFromVirtualRoot(specifier, virtualVendorRoot, vendorDirectory)
|
|
102
|
+
}
|
|
103
|
+
|
|
94
104
|
return await loadWithRetry(specifier)
|
|
95
105
|
}
|
|
96
106
|
}
|
|
97
107
|
|
|
98
|
-
const writeStage2 = async ({
|
|
108
|
+
const writeStage2 = async ({
|
|
109
|
+
basePath,
|
|
110
|
+
destPath,
|
|
111
|
+
externals,
|
|
112
|
+
functions,
|
|
113
|
+
importMapData,
|
|
114
|
+
vendorDirectory,
|
|
115
|
+
}: WriteStage2Options) => {
|
|
99
116
|
const importMapURL = importMapData ? importMapSpecifier : undefined
|
|
100
|
-
const loader = stage2Loader(basePath, functions, new Set(externals), importMapData)
|
|
117
|
+
const loader = stage2Loader(basePath, functions, new Set(externals), importMapData, vendorDirectory)
|
|
101
118
|
const bytes = await build([STAGE2_SPECIFIER], loader, importMapURL)
|
|
102
119
|
const directory = path.dirname(destPath)
|
|
103
120
|
|
package/dist/node/bundler.d.ts
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
import { OnAfterDownloadHook, OnBeforeDownloadHook } from './bridge.js';
|
|
2
2
|
import { Declaration } from './declaration.js';
|
|
3
|
+
import { EdgeFunction } from './edge_function.js';
|
|
3
4
|
import { FeatureFlags } from './feature_flags.js';
|
|
4
5
|
import { LogFunction } from './logger.js';
|
|
5
|
-
interface BundleOptions {
|
|
6
|
+
export interface BundleOptions {
|
|
6
7
|
basePath?: string;
|
|
8
|
+
bootstrapURL?: string;
|
|
7
9
|
cacheDirectory?: string;
|
|
8
10
|
configPath?: string;
|
|
9
11
|
debug?: boolean;
|
|
10
12
|
distImportMapPath?: string;
|
|
11
13
|
featureFlags?: FeatureFlags;
|
|
12
14
|
importMapPaths?: (string | undefined)[];
|
|
15
|
+
internalSrcFolder?: string;
|
|
13
16
|
onAfterDownload?: OnAfterDownloadHook;
|
|
14
17
|
onBeforeDownload?: OnBeforeDownloadHook;
|
|
15
18
|
systemLogger?: LogFunction;
|
|
16
|
-
|
|
17
|
-
bootstrapURL?: string;
|
|
19
|
+
vendorDirectory?: string;
|
|
18
20
|
}
|
|
19
|
-
declare const bundle: (sourceDirectories: string[], distDirectory: string, tomlDeclarations?: Declaration[], { basePath: inputBasePath, cacheDirectory, configPath, debug, distImportMapPath, featureFlags: inputFeatureFlags, importMapPaths, onAfterDownload, onBeforeDownload, systemLogger,
|
|
20
|
-
functions:
|
|
21
|
+
export declare const bundle: (sourceDirectories: string[], distDirectory: string, tomlDeclarations?: Declaration[], { basePath: inputBasePath, cacheDirectory, configPath, debug, distImportMapPath, featureFlags: inputFeatureFlags, importMapPaths, internalSrcFolder, onAfterDownload, onBeforeDownload, systemLogger, vendorDirectory, }?: BundleOptions) => Promise<{
|
|
22
|
+
functions: EdgeFunction[];
|
|
21
23
|
manifest: import("./manifest.js").Manifest;
|
|
22
24
|
}>;
|
|
23
|
-
export { bundle };
|
|
24
|
-
export type { BundleOptions };
|
package/dist/node/bundler.js
CHANGED
|
@@ -13,8 +13,9 @@ import { bundle as bundleESZIP } from './formats/eszip.js';
|
|
|
13
13
|
import { ImportMap } from './import_map.js';
|
|
14
14
|
import { getLogger } from './logger.js';
|
|
15
15
|
import { writeManifest } from './manifest.js';
|
|
16
|
+
import { vendorNPMSpecifiers } from './npm_dependencies.js';
|
|
16
17
|
import { ensureLatestTypes } from './types.js';
|
|
17
|
-
const bundle = async (sourceDirectories, distDirectory, tomlDeclarations = [], { basePath: inputBasePath, cacheDirectory, configPath, debug, distImportMapPath, featureFlags: inputFeatureFlags, importMapPaths = [], onAfterDownload, onBeforeDownload, systemLogger,
|
|
18
|
+
export const bundle = async (sourceDirectories, distDirectory, tomlDeclarations = [], { basePath: inputBasePath, cacheDirectory, configPath, debug, distImportMapPath, featureFlags: inputFeatureFlags, importMapPaths = [], internalSrcFolder, onAfterDownload, onBeforeDownload, systemLogger, vendorDirectory, } = {}) => {
|
|
18
19
|
const logger = getLogger(systemLogger, debug);
|
|
19
20
|
const featureFlags = getFlags(inputFeatureFlags);
|
|
20
21
|
const options = {
|
|
@@ -46,6 +47,17 @@ const bundle = async (sourceDirectories, distDirectory, tomlDeclarations = [], {
|
|
|
46
47
|
const userFunctions = userSourceDirectories.length === 0 ? [] : await findFunctions(userSourceDirectories);
|
|
47
48
|
const internalFunctions = internalSrcFolder ? await findFunctions([internalSrcFolder]) : [];
|
|
48
49
|
const functions = [...internalFunctions, ...userFunctions];
|
|
50
|
+
const vendor = await safelyVendorNPMSpecifiers({
|
|
51
|
+
basePath,
|
|
52
|
+
featureFlags,
|
|
53
|
+
functions,
|
|
54
|
+
importMap,
|
|
55
|
+
logger,
|
|
56
|
+
vendorDirectory,
|
|
57
|
+
});
|
|
58
|
+
if (vendor) {
|
|
59
|
+
importMap.add(vendor.importMap);
|
|
60
|
+
}
|
|
49
61
|
const functionBundle = await bundleESZIP({
|
|
50
62
|
basePath,
|
|
51
63
|
buildID,
|
|
@@ -56,6 +68,7 @@ const bundle = async (sourceDirectories, distDirectory, tomlDeclarations = [], {
|
|
|
56
68
|
functions,
|
|
57
69
|
featureFlags,
|
|
58
70
|
importMap,
|
|
71
|
+
vendorDirectory: vendor === null || vendor === void 0 ? void 0 : vendor.directory,
|
|
59
72
|
});
|
|
60
73
|
// The final file name of the bundles contains a SHA256 hash of the contents,
|
|
61
74
|
// which we can only compute now that the files have been generated. So let's
|
|
@@ -86,6 +99,7 @@ const bundle = async (sourceDirectories, distDirectory, tomlDeclarations = [], {
|
|
|
86
99
|
importMap: importMapSpecifier,
|
|
87
100
|
layers: deployConfig.layers,
|
|
88
101
|
});
|
|
102
|
+
await (vendor === null || vendor === void 0 ? void 0 : vendor.cleanup());
|
|
89
103
|
if (distImportMapPath) {
|
|
90
104
|
await importMap.writeToFile(distImportMapPath);
|
|
91
105
|
}
|
|
@@ -133,4 +147,20 @@ const createFunctionConfig = ({ internalFunctionsWithConfig, declarations }) =>
|
|
|
133
147
|
[functionName]: addGeneratorFallback(mergedConfigFields),
|
|
134
148
|
};
|
|
135
149
|
}, {});
|
|
136
|
-
|
|
150
|
+
const safelyVendorNPMSpecifiers = async ({ basePath, featureFlags, functions, importMap, logger, vendorDirectory, }) => {
|
|
151
|
+
if (!featureFlags.edge_functions_npm_modules) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
try {
|
|
155
|
+
return await vendorNPMSpecifiers({
|
|
156
|
+
basePath,
|
|
157
|
+
directory: vendorDirectory,
|
|
158
|
+
functions: functions.map(({ path }) => path),
|
|
159
|
+
importMap,
|
|
160
|
+
logger,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
logger.system(error);
|
|
165
|
+
}
|
|
166
|
+
};
|
|
@@ -120,7 +120,7 @@ test('Prints a nice error message when user tries importing NPM module', async (
|
|
|
120
120
|
}
|
|
121
121
|
catch (error) {
|
|
122
122
|
expect(error).toBeInstanceOf(BundleError);
|
|
123
|
-
expect(error.message).toEqual(`It seems like you're trying to import an npm module. This is only supported via CDNs like esm.sh. Have you tried 'import mod from "https://esm.sh/
|
|
123
|
+
expect(error.message).toEqual(`It seems like you're trying to import an npm module. This is only supported via CDNs like esm.sh. Have you tried 'import mod from "https://esm.sh/parent-1"'?`);
|
|
124
124
|
}
|
|
125
125
|
finally {
|
|
126
126
|
await cleanup();
|
|
@@ -373,3 +373,27 @@ test('Handles imports with the `node:` prefix', async () => {
|
|
|
373
373
|
expect(func1).toBe('ok');
|
|
374
374
|
await cleanup();
|
|
375
375
|
});
|
|
376
|
+
test('Loads npm modules from bare specifiers with and without the `npm:` prefix', async () => {
|
|
377
|
+
const { basePath, cleanup, distPath } = await useFixture('imports_npm_module');
|
|
378
|
+
const sourceDirectory = join(basePath, 'functions');
|
|
379
|
+
const declarations = [
|
|
380
|
+
{
|
|
381
|
+
function: 'func1',
|
|
382
|
+
path: '/func1',
|
|
383
|
+
},
|
|
384
|
+
];
|
|
385
|
+
const vendorDirectory = await tmp.dir();
|
|
386
|
+
await bundle([sourceDirectory], distPath, declarations, {
|
|
387
|
+
basePath,
|
|
388
|
+
featureFlags: { edge_functions_npm_modules: true },
|
|
389
|
+
importMapPaths: [join(basePath, 'import_map.json')],
|
|
390
|
+
vendorDirectory: vendorDirectory.path,
|
|
391
|
+
});
|
|
392
|
+
const manifestFile = await readFile(resolve(distPath, 'manifest.json'), 'utf8');
|
|
393
|
+
const manifest = JSON.parse(manifestFile);
|
|
394
|
+
const bundlePath = join(distPath, manifest.bundles[0].asset);
|
|
395
|
+
const { func1 } = await runESZIP(bundlePath, vendorDirectory.path);
|
|
396
|
+
expect(func1).toBe(`<parent-1><child-1>JavaScript</child-1></parent-1>, <parent-2><child-2><grandchild-1>APIs<cwd>${process.cwd()}</cwd></grandchild-1></child-2></parent-2>, <parent-3><child-2><grandchild-1>Markup<cwd>${process.cwd()}</cwd></grandchild-1></child-2></parent-3>`);
|
|
397
|
+
await cleanup();
|
|
398
|
+
await rm(vendorDirectory.path, { force: true, recursive: true });
|
|
399
|
+
});
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
declare const defaultFlags: {
|
|
2
2
|
edge_functions_fail_unsupported_regex: boolean;
|
|
3
|
+
edge_functions_npm_modules: boolean;
|
|
3
4
|
};
|
|
4
5
|
type FeatureFlag = keyof typeof defaultFlags;
|
|
5
6
|
type FeatureFlags = Partial<Record<FeatureFlag, boolean>>;
|
|
6
7
|
declare const getFlags: (input?: Record<string, boolean>, flags?: {
|
|
7
8
|
edge_functions_fail_unsupported_regex: boolean;
|
|
9
|
+
edge_functions_npm_modules: boolean;
|
|
8
10
|
}) => FeatureFlags;
|
|
9
11
|
export { defaultFlags, getFlags };
|
|
10
12
|
export type { FeatureFlag, FeatureFlags };
|
|
@@ -13,6 +13,7 @@ interface BundleESZIPOptions {
|
|
|
13
13
|
featureFlags: FeatureFlags;
|
|
14
14
|
functions: EdgeFunction[];
|
|
15
15
|
importMap: ImportMap;
|
|
16
|
+
vendorDirectory?: string;
|
|
16
17
|
}
|
|
17
|
-
declare const bundleESZIP: ({ basePath, buildID, debug, deno, distDirectory, externals, functions, importMap, }: BundleESZIPOptions) => Promise<Bundle>;
|
|
18
|
+
declare const bundleESZIP: ({ basePath, buildID, debug, deno, distDirectory, externals, functions, importMap, vendorDirectory, }: BundleESZIPOptions) => Promise<Bundle>;
|
|
18
19
|
export { bundleESZIP as bundle };
|
|
@@ -1,26 +1,29 @@
|
|
|
1
1
|
import { join } from 'path';
|
|
2
2
|
import { pathToFileURL } from 'url';
|
|
3
|
-
import { virtualRoot } from '../../shared/consts.js';
|
|
3
|
+
import { virtualRoot, virtualVendorRoot } from '../../shared/consts.js';
|
|
4
4
|
import { BundleFormat } from '../bundle.js';
|
|
5
5
|
import { wrapBundleError } from '../bundle_error.js';
|
|
6
6
|
import { wrapNpmImportError } from '../npm_import_error.js';
|
|
7
7
|
import { getPackagePath } from '../package_json.js';
|
|
8
8
|
import { getFileHash } from '../utils/sha256.js';
|
|
9
|
-
const bundleESZIP = async ({ basePath, buildID, debug, deno, distDirectory, externals, functions, importMap, }) => {
|
|
9
|
+
const bundleESZIP = async ({ basePath, buildID, debug, deno, distDirectory, externals, functions, importMap, vendorDirectory, }) => {
|
|
10
10
|
const extension = '.eszip';
|
|
11
11
|
const destPath = join(distDirectory, `${buildID}${extension}`);
|
|
12
|
-
const { bundler, importMap: bundlerImportMap } = getESZIPPaths();
|
|
13
|
-
// Transforming all paths under `basePath` to use the virtual root prefix.
|
|
14
12
|
const importMapPrefixes = {
|
|
15
13
|
[`${pathToFileURL(basePath)}/`]: virtualRoot,
|
|
16
14
|
};
|
|
17
|
-
|
|
15
|
+
if (vendorDirectory !== undefined) {
|
|
16
|
+
importMapPrefixes[`${pathToFileURL(vendorDirectory)}/`] = virtualVendorRoot;
|
|
17
|
+
}
|
|
18
|
+
const { bundler, importMap: bundlerImportMap } = getESZIPPaths();
|
|
19
|
+
const importMapData = JSON.stringify(importMap.getContents(importMapPrefixes));
|
|
18
20
|
const payload = {
|
|
19
21
|
basePath,
|
|
20
22
|
destPath,
|
|
21
23
|
externals,
|
|
22
24
|
functions,
|
|
23
|
-
importMapData
|
|
25
|
+
importMapData,
|
|
26
|
+
vendorDirectory,
|
|
24
27
|
};
|
|
25
28
|
const flags = ['--allow-all', '--no-config', `--import-map=${bundlerImportMap}`];
|
|
26
29
|
if (!debug) {
|
|
@@ -14,6 +14,8 @@ export declare class ImportMap {
|
|
|
14
14
|
addFile(path: string, logger: Logger): Promise<void>;
|
|
15
15
|
addFiles(paths: (string | undefined)[], logger: Logger): Promise<void>;
|
|
16
16
|
static applyPrefixesToImports(imports: Imports, prefixes: Record<string, string>): Imports;
|
|
17
|
+
static convertImportsToURLObjects(imports: Imports): Record<string, URL>;
|
|
18
|
+
static convertScopesToURLObjects(scopes: Record<string, Imports>): Record<string, Record<string, URL>>;
|
|
17
19
|
static applyPrefixesToPath(path: string, prefixes: Record<string, string>): string;
|
|
18
20
|
filterImports(imports?: Record<string, URL | null>): Record<string, string>;
|
|
19
21
|
filterScopes(scopes?: ParsedImportMap['scopes']): Record<string, Imports>;
|
|
@@ -21,6 +23,10 @@ export declare class ImportMap {
|
|
|
21
23
|
imports: Imports;
|
|
22
24
|
scopes: {};
|
|
23
25
|
};
|
|
26
|
+
getContentsWithURLObjects(prefixes?: Record<string, string>): {
|
|
27
|
+
imports: Record<string, URL>;
|
|
28
|
+
scopes: Record<string, Record<string, URL>>;
|
|
29
|
+
};
|
|
24
30
|
static readFile(path: string, logger: Logger): Promise<ImportMapFile>;
|
|
25
31
|
resolve(source: ImportMapFile): {
|
|
26
32
|
imports: Record<string, string>;
|
package/dist/node/import_map.js
CHANGED
|
@@ -44,6 +44,18 @@ export class ImportMap {
|
|
|
44
44
|
[key]: ImportMap.applyPrefixesToPath(value, prefixes),
|
|
45
45
|
}), {});
|
|
46
46
|
}
|
|
47
|
+
static convertImportsToURLObjects(imports) {
|
|
48
|
+
return Object.entries(imports).reduce((acc, [key, value]) => ({
|
|
49
|
+
...acc,
|
|
50
|
+
[key]: new URL(value),
|
|
51
|
+
}), {});
|
|
52
|
+
}
|
|
53
|
+
static convertScopesToURLObjects(scopes) {
|
|
54
|
+
return Object.entries(scopes).reduce((acc, [key, value]) => ({
|
|
55
|
+
...acc,
|
|
56
|
+
[key]: ImportMap.convertImportsToURLObjects(value),
|
|
57
|
+
}), {});
|
|
58
|
+
}
|
|
47
59
|
// Applies a list of prefixes to a given path, returning the replaced path.
|
|
48
60
|
// For example, given a `path` of `file:///foo/bar/baz.js` and a `prefixes`
|
|
49
61
|
// object with `{"file:///foo/": "file:///hello/"}`, this method will return
|
|
@@ -122,6 +134,16 @@ export class ImportMap {
|
|
|
122
134
|
scopes: transformedScopes,
|
|
123
135
|
};
|
|
124
136
|
}
|
|
137
|
+
// The same as `getContents`, but the URLs are represented as URL objects
|
|
138
|
+
// instead of strings. This is compatible with the `ParsedImportMap` type
|
|
139
|
+
// from the `@import-maps/resolve` library.
|
|
140
|
+
getContentsWithURLObjects(prefixes = {}) {
|
|
141
|
+
const { imports, scopes } = this.getContents(prefixes);
|
|
142
|
+
return {
|
|
143
|
+
imports: ImportMap.convertImportsToURLObjects(imports),
|
|
144
|
+
scopes: ImportMap.convertScopesToURLObjects(scopes),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
125
147
|
static async readFile(path, logger) {
|
|
126
148
|
const baseURL = pathToFileURL(path);
|
|
127
149
|
try {
|
|
@@ -20,11 +20,16 @@ test('Handles import maps with full URLs without specifying a base URL', () => {
|
|
|
20
20
|
},
|
|
21
21
|
};
|
|
22
22
|
const map = new ImportMap([inputFile1, inputFile2]);
|
|
23
|
-
const
|
|
24
|
-
expect(imports['netlify:edge']).toBe('https://edge.netlify.com/v1/index.ts?v=legacy');
|
|
25
|
-
expect(imports['@netlify/edge-functions']).toBe('https://edge.netlify.com/v1/index.ts');
|
|
26
|
-
expect(imports['alias:jamstack']).toBe('https://jamstack.org/');
|
|
27
|
-
expect(imports['alias:pets']).toBe('https://petsofnetlify.com/');
|
|
23
|
+
const m1 = map.getContents();
|
|
24
|
+
expect(m1.imports['netlify:edge']).toBe('https://edge.netlify.com/v1/index.ts?v=legacy');
|
|
25
|
+
expect(m1.imports['@netlify/edge-functions']).toBe('https://edge.netlify.com/v1/index.ts');
|
|
26
|
+
expect(m1.imports['alias:jamstack']).toBe('https://jamstack.org/');
|
|
27
|
+
expect(m1.imports['alias:pets']).toBe('https://petsofnetlify.com/');
|
|
28
|
+
const m2 = map.getContentsWithURLObjects();
|
|
29
|
+
expect(m2.imports['netlify:edge']).toStrictEqual(new URL('https://edge.netlify.com/v1/index.ts?v=legacy'));
|
|
30
|
+
expect(m2.imports['@netlify/edge-functions']).toStrictEqual(new URL('https://edge.netlify.com/v1/index.ts'));
|
|
31
|
+
expect(m2.imports['alias:jamstack']).toStrictEqual(new URL('https://jamstack.org/'));
|
|
32
|
+
expect(m2.imports['alias:pets']).toStrictEqual(new URL('https://petsofnetlify.com/'));
|
|
28
33
|
});
|
|
29
34
|
test('Resolves relative paths to absolute paths if a base path is not provided', () => {
|
|
30
35
|
const basePath = join(cwd(), 'my-cool-site', 'import-map.json');
|
|
@@ -35,11 +40,15 @@ test('Resolves relative paths to absolute paths if a base path is not provided',
|
|
|
35
40
|
},
|
|
36
41
|
};
|
|
37
42
|
const map = new ImportMap([inputFile1]);
|
|
38
|
-
const { imports } = map.getContents();
|
|
39
43
|
const expectedPath = join(cwd(), 'my-cool-site', 'heart', 'pets');
|
|
40
|
-
|
|
41
|
-
expect(imports['
|
|
42
|
-
expect(imports['
|
|
44
|
+
const m1 = map.getContents();
|
|
45
|
+
expect(m1.imports['netlify:edge']).toBe('https://edge.netlify.com/v1/index.ts?v=legacy');
|
|
46
|
+
expect(m1.imports['@netlify/edge-functions']).toBe('https://edge.netlify.com/v1/index.ts');
|
|
47
|
+
expect(m1.imports['alias:pets']).toBe(`${pathToFileURL(expectedPath).toString()}/`);
|
|
48
|
+
const m2 = map.getContentsWithURLObjects();
|
|
49
|
+
expect(m2.imports['netlify:edge']).toStrictEqual(new URL('https://edge.netlify.com/v1/index.ts?v=legacy'));
|
|
50
|
+
expect(m2.imports['@netlify/edge-functions']).toStrictEqual(new URL('https://edge.netlify.com/v1/index.ts'));
|
|
51
|
+
expect(m2.imports['alias:pets']).toStrictEqual(new URL(`${pathToFileURL(expectedPath).toString()}/`));
|
|
43
52
|
});
|
|
44
53
|
describe('Returns the fully resolved import map', () => {
|
|
45
54
|
const inputFile1 = {
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { ParsedImportMap } from '@import-maps/resolve';
|
|
3
|
+
import { Plugin } from 'esbuild';
|
|
4
|
+
import { ImportMap } from './import_map.js';
|
|
5
|
+
import { Logger } from './logger.js';
|
|
6
|
+
export declare const getDependencyTrackerPlugin: (specifiers: Set<string>, importMap: ParsedImportMap, baseURL: URL) => Plugin;
|
|
7
|
+
interface VendorNPMSpecifiersOptions {
|
|
8
|
+
basePath: string;
|
|
9
|
+
directory?: string;
|
|
10
|
+
functions: string[];
|
|
11
|
+
importMap: ImportMap;
|
|
12
|
+
logger: Logger;
|
|
13
|
+
}
|
|
14
|
+
export declare const vendorNPMSpecifiers: ({ basePath, directory, functions, importMap, logger, }: VendorNPMSpecifiersOptions) => Promise<{
|
|
15
|
+
cleanup: () => Promise<void>;
|
|
16
|
+
directory: string;
|
|
17
|
+
importMap: {
|
|
18
|
+
baseURL: import("url").URL;
|
|
19
|
+
imports: Record<string, string>;
|
|
20
|
+
};
|
|
21
|
+
} | undefined>;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { promises as fs } from 'fs';
|
|
2
|
+
import { builtinModules, createRequire } from 'module';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { fileURLToPath, pathToFileURL } from 'url';
|
|
5
|
+
import { resolve } from '@import-maps/resolve';
|
|
6
|
+
import { build } from 'esbuild';
|
|
7
|
+
import tmp from 'tmp-promise';
|
|
8
|
+
import { nodePrefix, npmPrefix } from '../shared/consts.js';
|
|
9
|
+
const builtinModulesSet = new Set(builtinModules);
|
|
10
|
+
const require = createRequire(import.meta.url);
|
|
11
|
+
// Workaround for https://github.com/evanw/esbuild/issues/1921.
|
|
12
|
+
const banner = {
|
|
13
|
+
js: `
|
|
14
|
+
import {createRequire as ___nfyCreateRequire} from "module";
|
|
15
|
+
import {fileURLToPath as ___nfyFileURLToPath} from "url";
|
|
16
|
+
import {dirname as ___nfyPathDirname} from "path";
|
|
17
|
+
let __filename=___nfyFileURLToPath(import.meta.url);
|
|
18
|
+
let __dirname=___nfyPathDirname(___nfyFileURLToPath(import.meta.url));
|
|
19
|
+
let require=___nfyCreateRequire(import.meta.url);
|
|
20
|
+
`,
|
|
21
|
+
};
|
|
22
|
+
// esbuild plugin that will traverse the code and look for imports of external
|
|
23
|
+
// dependencies (i.e. Node modules). It stores the specifiers found in the Set
|
|
24
|
+
// provided.
|
|
25
|
+
export const getDependencyTrackerPlugin = (specifiers, importMap, baseURL) => ({
|
|
26
|
+
name: 'dependency-tracker',
|
|
27
|
+
setup(build) {
|
|
28
|
+
build.onResolve({ filter: /^(.*)$/ }, (args) => {
|
|
29
|
+
if (args.kind !== 'import-statement') {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const result = {};
|
|
33
|
+
let specifier = args.path;
|
|
34
|
+
// Start by checking whether the specifier matches any import map defined
|
|
35
|
+
// by the user.
|
|
36
|
+
const { matched, resolvedImport } = resolve(specifier, importMap, baseURL);
|
|
37
|
+
// If it does, the resolved import is the specifier we'll evaluate going
|
|
38
|
+
// forward.
|
|
39
|
+
if (matched) {
|
|
40
|
+
specifier = fileURLToPath(resolvedImport).replace(/\\/g, '/');
|
|
41
|
+
result.path = specifier;
|
|
42
|
+
}
|
|
43
|
+
// If the specifier is a Node.js built-in, we don't want to bundle it.
|
|
44
|
+
if (specifier.startsWith(nodePrefix) || builtinModulesSet.has(specifier)) {
|
|
45
|
+
return { external: true };
|
|
46
|
+
}
|
|
47
|
+
// If the specifier has the `npm:` prefix, strip it and use the rest of
|
|
48
|
+
// the specifier to resolve the module.
|
|
49
|
+
if (specifier.startsWith(npmPrefix)) {
|
|
50
|
+
const canonicalPath = specifier.slice(npmPrefix.length);
|
|
51
|
+
return build.resolve(canonicalPath, {
|
|
52
|
+
kind: args.kind,
|
|
53
|
+
resolveDir: args.resolveDir,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
const isLocalImport = specifier.startsWith(path.sep) || specifier.startsWith('.');
|
|
57
|
+
// If this is a local import, return so that esbuild visits that path.
|
|
58
|
+
if (isLocalImport) {
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
const isRemoteURLImport = specifier.startsWith('https://') || specifier.startsWith('http://');
|
|
62
|
+
if (isRemoteURLImport) {
|
|
63
|
+
return { external: true };
|
|
64
|
+
}
|
|
65
|
+
// At this point we know we're dealing with a bare specifier that should
|
|
66
|
+
// be treated as an external module. We first try to resolve it, because
|
|
67
|
+
// in the event that it doesn't exist (e.g. user is referencing a module
|
|
68
|
+
// that they haven't installed) we won't even attempt to bundle it. This
|
|
69
|
+
// lets the ESZIP bundler handle and report the missing import instead of
|
|
70
|
+
// esbuild, which is a better experience for the user.
|
|
71
|
+
try {
|
|
72
|
+
require.resolve(specifier, { paths: [args.resolveDir] });
|
|
73
|
+
specifiers.add(specifier);
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
// no-op
|
|
77
|
+
}
|
|
78
|
+
// Mark the specifier as external, because we don't want to traverse the
|
|
79
|
+
// entire module tree — i.e. if user code imports module `foo` and that
|
|
80
|
+
// imports `bar`, we only want to add `foo` to the list of specifiers,
|
|
81
|
+
// since the whole module — including its dependencies like `bar` —
|
|
82
|
+
// will be bundled.
|
|
83
|
+
return { external: true };
|
|
84
|
+
});
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
export const vendorNPMSpecifiers = async ({ basePath, directory, functions, importMap, logger, }) => {
|
|
88
|
+
const specifiers = new Set();
|
|
89
|
+
// The directories that esbuild will use when resolving Node modules. We must
|
|
90
|
+
// set these manually because esbuild will be operating from a temporary
|
|
91
|
+
// directory that will not live inside the project root, so the normal
|
|
92
|
+
// resolution logic won't work.
|
|
93
|
+
const nodePaths = [path.join(basePath, 'node_modules')];
|
|
94
|
+
// We need to create some files on disk, which we don't want to write to the
|
|
95
|
+
// project directory. If a custom directory has been specified, which happens
|
|
96
|
+
// only in tests, we use it. Otherwise, create a random temporary directory.
|
|
97
|
+
const temporaryDirectory = directory ? { path: directory } : await tmp.dir();
|
|
98
|
+
// Do a first pass at bundling to gather a list of specifiers that should be
|
|
99
|
+
// loaded as npm dependencies, because they either use the `npm:` prefix or
|
|
100
|
+
// they are bare specifiers. We'll collect them in `specifiers`.
|
|
101
|
+
try {
|
|
102
|
+
await build({
|
|
103
|
+
banner,
|
|
104
|
+
bundle: true,
|
|
105
|
+
entryPoints: functions,
|
|
106
|
+
logLevel: 'error',
|
|
107
|
+
nodePaths,
|
|
108
|
+
outdir: temporaryDirectory.path,
|
|
109
|
+
platform: 'node',
|
|
110
|
+
plugins: [getDependencyTrackerPlugin(specifiers, importMap.getContentsWithURLObjects(), pathToFileURL(basePath))],
|
|
111
|
+
write: false,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
logger.system('Could not track dependencies in edge function:', error);
|
|
116
|
+
logger.user('An error occurred when trying to scan your edge functions for npm modules, which is an experimental feature. If you are loading npm modules, please share the errors above in https://ntl.fyi/edge-functions-npm. If you are not loading npm modules, you can ignore this message.');
|
|
117
|
+
}
|
|
118
|
+
// If we found no specifiers, there's nothing left to do here.
|
|
119
|
+
if (specifiers.size === 0) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
logger.user('You are using npm modules in Edge Functions, which is an experimental feature. Learn more at https://ntl.fyi/edge-functions-npm.');
|
|
123
|
+
// To bundle an entire module and all its dependencies, we create a stub file
|
|
124
|
+
// where we re-export everything from that specifier. We do this for every
|
|
125
|
+
// specifier, and each of these files will be the entry points to esbuild.
|
|
126
|
+
const ops = await Promise.all([...specifiers].map(async (specifier, index) => {
|
|
127
|
+
const code = `import * as mod from "${specifier}"; export default mod.default; export * from "${specifier}";`;
|
|
128
|
+
const filePath = path.join(temporaryDirectory.path, `stub-${index}.js`);
|
|
129
|
+
await fs.writeFile(filePath, code);
|
|
130
|
+
return { filePath, specifier };
|
|
131
|
+
}));
|
|
132
|
+
const entryPoints = ops.map(({ filePath }) => filePath);
|
|
133
|
+
// Bundle each of the stub files we've created. We'll end up with a compiled
|
|
134
|
+
// version of each of the stub files, plus any chunks of shared code between
|
|
135
|
+
// stubs (such that a common module isn't bundled twice).
|
|
136
|
+
await build({
|
|
137
|
+
allowOverwrite: true,
|
|
138
|
+
banner,
|
|
139
|
+
bundle: true,
|
|
140
|
+
entryPoints,
|
|
141
|
+
format: 'esm',
|
|
142
|
+
logLevel: 'error',
|
|
143
|
+
nodePaths,
|
|
144
|
+
outdir: temporaryDirectory.path,
|
|
145
|
+
platform: 'node',
|
|
146
|
+
splitting: true,
|
|
147
|
+
target: 'es2020',
|
|
148
|
+
});
|
|
149
|
+
// Add all Node.js built-ins to the import map, so any unprefixed specifiers
|
|
150
|
+
// (e.g. `process`) resolve to the prefixed versions (e.g. `node:prefix`),
|
|
151
|
+
// which Deno can process.
|
|
152
|
+
const builtIns = builtinModules.reduce((acc, name) => ({
|
|
153
|
+
...acc,
|
|
154
|
+
[name]: `node:${name}`,
|
|
155
|
+
}), {});
|
|
156
|
+
// Creates an object that is compatible with the `imports` block of an import
|
|
157
|
+
// map, mapping specifiers to the paths of their bundled files on disk. Each
|
|
158
|
+
// specifier gets two entries in the import map, one with the `npm:` prefix
|
|
159
|
+
// and one without, such that both options are supported.
|
|
160
|
+
const newImportMap = {
|
|
161
|
+
baseURL: pathToFileURL(temporaryDirectory.path),
|
|
162
|
+
imports: ops.reduce((acc, op) => {
|
|
163
|
+
const url = pathToFileURL(op.filePath).toString();
|
|
164
|
+
return {
|
|
165
|
+
...acc,
|
|
166
|
+
[op.specifier]: url,
|
|
167
|
+
[npmPrefix + op.specifier]: url,
|
|
168
|
+
};
|
|
169
|
+
}, builtIns),
|
|
170
|
+
};
|
|
171
|
+
const cleanup = async () => {
|
|
172
|
+
// If a custom temporary directory was specified, we leave the cleanup job
|
|
173
|
+
// up to the caller.
|
|
174
|
+
if (directory) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
try {
|
|
178
|
+
await fs.rm(temporaryDirectory.path, { force: true, recursive: true });
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
// no-op
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
return {
|
|
185
|
+
cleanup,
|
|
186
|
+
directory: temporaryDirectory.path,
|
|
187
|
+
importMap: newImportMap,
|
|
188
|
+
};
|
|
189
|
+
};
|
package/dist/shared/consts.d.ts
CHANGED
package/dist/shared/consts.js
CHANGED
package/dist/shared/stage2.d.ts
CHANGED
package/dist/test/util.d.ts
CHANGED
|
@@ -7,5 +7,5 @@ declare const useFixture: (fixtureName: string) => Promise<{
|
|
|
7
7
|
distPath: string;
|
|
8
8
|
}>;
|
|
9
9
|
declare const getRouteMatcher: (manifest: Manifest) => (candidate: string) => import("../node/manifest.js").Route | undefined;
|
|
10
|
-
declare const runESZIP: (eszipPath: string) => Promise<any>;
|
|
10
|
+
declare const runESZIP: (eszipPath: string, vendorDirectory?: string) => Promise<any>;
|
|
11
11
|
export { fixturesDir, getRouteMatcher, testLogger, runESZIP, useFixture };
|
package/dist/test/util.js
CHANGED
|
@@ -48,7 +48,7 @@ const getRouteMatcher = (manifest) => (candidate) => manifest.routes.find((route
|
|
|
48
48
|
const isExcluded = excludedPatterns.some((pattern) => new RegExp(pattern).test(candidate));
|
|
49
49
|
return !isExcluded;
|
|
50
50
|
});
|
|
51
|
-
const runESZIP = async (eszipPath) => {
|
|
51
|
+
const runESZIP = async (eszipPath, vendorDirectory) => {
|
|
52
52
|
var _a, _b, _c;
|
|
53
53
|
const tmpDir = await tmp.dir({ unsafeCleanup: true });
|
|
54
54
|
// Extract ESZIP into temporary directory.
|
|
@@ -68,7 +68,10 @@ const runESZIP = async (eszipPath) => {
|
|
|
68
68
|
const importMapPath = join(virtualRootPath, '..', 'import-map');
|
|
69
69
|
for (const path of [importMapPath, stage2Path]) {
|
|
70
70
|
const file = await fs.readFile(path, 'utf8');
|
|
71
|
-
|
|
71
|
+
let normalizedFile = file.replace(/file:\/{3}root/g, pathToFileURL(virtualRootPath).toString());
|
|
72
|
+
if (vendorDirectory !== undefined) {
|
|
73
|
+
normalizedFile = normalizedFile.replace(/file:\/{3}vendor/g, pathToFileURL(vendorDirectory).toString());
|
|
74
|
+
}
|
|
72
75
|
await fs.writeFile(path, normalizedFile);
|
|
73
76
|
}
|
|
74
77
|
await fs.rename(stage2Path, `${stage2Path}.js`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@netlify/edge-bundler",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.20.0",
|
|
4
4
|
"description": "Intelligently prepare Netlify Edge Functions for deployment",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/node/index.js",
|
|
@@ -79,6 +79,7 @@
|
|
|
79
79
|
"better-ajv-errors": "^1.2.0",
|
|
80
80
|
"common-path-prefix": "^3.0.0",
|
|
81
81
|
"env-paths": "^3.0.0",
|
|
82
|
+
"esbuild": "0.19.2",
|
|
82
83
|
"execa": "^6.0.0",
|
|
83
84
|
"find-up": "^6.3.0",
|
|
84
85
|
"get-port": "^6.1.2",
|
package/shared/consts.ts
CHANGED