@netlify/edge-bundler 10.1.0 → 10.1.2
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/dist/node/bundler.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Buffer } from 'buffer';
|
|
1
2
|
import { access, readdir, readFile, rm, writeFile } from 'fs/promises';
|
|
2
3
|
import { join, resolve } from 'path';
|
|
3
4
|
import process from 'process';
|
|
@@ -430,3 +431,27 @@ test('Loads JSON modules', async () => {
|
|
|
430
431
|
await cleanup();
|
|
431
432
|
await rm(vendorDirectory.path, { force: true, recursive: true });
|
|
432
433
|
});
|
|
434
|
+
test('Supports TSX and process.env', async () => {
|
|
435
|
+
const { basePath, cleanup, distPath } = await useFixture('tsx');
|
|
436
|
+
const sourceDirectory = join(basePath, 'functions');
|
|
437
|
+
const declarations = [
|
|
438
|
+
{
|
|
439
|
+
function: 'func1',
|
|
440
|
+
path: '/func1',
|
|
441
|
+
},
|
|
442
|
+
];
|
|
443
|
+
const vendorDirectory = await tmp.dir();
|
|
444
|
+
await bundle([sourceDirectory], distPath, declarations, {
|
|
445
|
+
basePath,
|
|
446
|
+
vendorDirectory: vendorDirectory.path,
|
|
447
|
+
});
|
|
448
|
+
const manifestFile = await readFile(resolve(distPath, 'manifest.json'), 'utf8');
|
|
449
|
+
const manifest = JSON.parse(manifestFile);
|
|
450
|
+
const bundlePath = join(distPath, manifest.bundles[0].asset);
|
|
451
|
+
process.env.FOO = 'bar';
|
|
452
|
+
const { func1 } = await runESZIP(bundlePath, vendorDirectory.path);
|
|
453
|
+
expect(Buffer.from(func1, 'base64').toString()).toBe(`hippedy hoppedy, createElement is now a production property. Here, take this env var: FOO=bar`);
|
|
454
|
+
await cleanup();
|
|
455
|
+
await rm(vendorDirectory.path, { force: true, recursive: true });
|
|
456
|
+
delete process.env.FOO;
|
|
457
|
+
});
|
|
@@ -7,10 +7,10 @@ interface VendorNPMSpecifiersOptions {
|
|
|
7
7
|
functions: string[];
|
|
8
8
|
importMap: ImportMap;
|
|
9
9
|
logger: Logger;
|
|
10
|
-
|
|
10
|
+
environment: 'production' | 'development';
|
|
11
11
|
rootPath?: string;
|
|
12
12
|
}
|
|
13
|
-
export declare const vendorNPMSpecifiers: ({ basePath, directory, functions, importMap,
|
|
13
|
+
export declare const vendorNPMSpecifiers: ({ basePath, directory, functions, importMap, environment, rootPath, }: VendorNPMSpecifiersOptions) => Promise<{
|
|
14
14
|
cleanup: () => Promise<void>;
|
|
15
15
|
directory: string;
|
|
16
16
|
importMap: {
|
|
@@ -9,7 +9,7 @@ import { findUp } from 'find-up';
|
|
|
9
9
|
import getPackageName from 'get-package-name';
|
|
10
10
|
import tmp from 'tmp-promise';
|
|
11
11
|
import { pathsBetween } from './utils/fs.js';
|
|
12
|
-
const TYPESCRIPT_EXTENSIONS = new Set(['.ts', '.cts', '.mts']);
|
|
12
|
+
const TYPESCRIPT_EXTENSIONS = new Set(['.ts', '.tsx', '.cts', '.ctsx', '.mts', '.mtsx']);
|
|
13
13
|
const slugifyPackageName = (specifier) => {
|
|
14
14
|
if (!specifier.startsWith('@'))
|
|
15
15
|
return specifier;
|
|
@@ -66,19 +66,26 @@ const safelyDetectTypes = async (packageJsonPath) => {
|
|
|
66
66
|
// Workaround for https://github.com/evanw/esbuild/issues/1921.
|
|
67
67
|
const banner = {
|
|
68
68
|
js: `
|
|
69
|
+
import __nfyProcess from "node:process";
|
|
70
|
+
import {setImmediate as __nfySetImmediate, clearImmediate as __nfyClearImmediate} from "node:timers";
|
|
71
|
+
import {Buffer as __nfyBuffer} from "node:buffer";
|
|
69
72
|
import {createRequire as ___nfyCreateRequire} from "node:module";
|
|
70
73
|
import {fileURLToPath as ___nfyFileURLToPath} from "node:url";
|
|
71
74
|
import {dirname as ___nfyPathDirname} from "node:path";
|
|
72
75
|
let __filename=___nfyFileURLToPath(import.meta.url);
|
|
73
76
|
let __dirname=___nfyPathDirname(___nfyFileURLToPath(import.meta.url));
|
|
74
77
|
let require=___nfyCreateRequire(import.meta.url);
|
|
78
|
+
globalThis.process = __nfyProcess;
|
|
79
|
+
globalThis.setImmediate = __nfySetImmediate;
|
|
80
|
+
globalThis.clearImmediate = __nfyClearImmediate;
|
|
81
|
+
globalThis.Buffer = __nfyBuffer;
|
|
75
82
|
`,
|
|
76
83
|
};
|
|
77
84
|
/**
|
|
78
85
|
* Parses a set of functions and returns a list of specifiers that correspond
|
|
79
86
|
* to npm modules.
|
|
80
87
|
*/
|
|
81
|
-
const getNPMSpecifiers = async ({ basePath, functions, importMap,
|
|
88
|
+
const getNPMSpecifiers = async ({ basePath, functions, importMap, environment, rootPath }) => {
|
|
82
89
|
const baseURL = pathToFileURL(basePath);
|
|
83
90
|
const { reasons } = await nodeFileTrace(functions, {
|
|
84
91
|
base: rootPath,
|
|
@@ -153,7 +160,7 @@ const getNPMSpecifiers = async ({ basePath, functions, importMap, referenceTypes
|
|
|
153
160
|
if (isDirectDependency) {
|
|
154
161
|
npmSpecifiers.push({
|
|
155
162
|
specifier: packageName,
|
|
156
|
-
types:
|
|
163
|
+
types: environment === 'development' ? await safelyDetectTypes(path.join(basePath, filePath)) : undefined,
|
|
157
164
|
});
|
|
158
165
|
}
|
|
159
166
|
}
|
|
@@ -162,7 +169,7 @@ const getNPMSpecifiers = async ({ basePath, functions, importMap, referenceTypes
|
|
|
162
169
|
npmSpecifiersWithExtraneousFiles: [...npmSpecifiersWithExtraneousFiles],
|
|
163
170
|
};
|
|
164
171
|
};
|
|
165
|
-
export const vendorNPMSpecifiers = async ({ basePath, directory, functions, importMap,
|
|
172
|
+
export const vendorNPMSpecifiers = async ({ basePath, directory, functions, importMap, environment, rootPath = basePath, }) => {
|
|
166
173
|
// The directories that esbuild will use when resolving Node modules. We must
|
|
167
174
|
// set these manually because esbuild will be operating from a temporary
|
|
168
175
|
// directory that will not live inside the project root, so the normal
|
|
@@ -176,7 +183,7 @@ export const vendorNPMSpecifiers = async ({ basePath, directory, functions, impo
|
|
|
176
183
|
basePath,
|
|
177
184
|
functions,
|
|
178
185
|
importMap: importMap.getContentsWithURLObjects(),
|
|
179
|
-
|
|
186
|
+
environment,
|
|
180
187
|
rootPath,
|
|
181
188
|
});
|
|
182
189
|
// If we found no specifiers, there's nothing left to do here.
|
|
@@ -210,6 +217,11 @@ export const vendorNPMSpecifiers = async ({ basePath, directory, functions, impo
|
|
|
210
217
|
splitting: true,
|
|
211
218
|
target: 'es2020',
|
|
212
219
|
write: false,
|
|
220
|
+
define: environment === 'production'
|
|
221
|
+
? {
|
|
222
|
+
'process.env.NODE_ENV': '"production"',
|
|
223
|
+
}
|
|
224
|
+
: undefined,
|
|
213
225
|
});
|
|
214
226
|
await Promise.all(outputFiles.map(async (file) => {
|
|
215
227
|
var _a;
|