vite-plugin-vercel 4.0.2 → 5.0.1

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/index.cjs CHANGED
@@ -293,6 +293,8 @@ var vercelEndpointExports = import_zod3.z.object({
293
293
  // src/build.ts
294
294
  var import_magicast = require("magicast");
295
295
  var import_build_utils = require("@vercel/build-utils");
296
+ var import_nft = require("@vercel/nft");
297
+ var import_workspace_root = require("workspace-root");
296
298
  function getAdditionalEndpoints(resolvedConfig) {
297
299
  var _a;
298
300
  return (((_a = resolvedConfig.vercel) == null ? void 0 : _a.additionalEndpoints) ?? []).map((e) => ({
@@ -325,8 +327,8 @@ function getEntries(resolvedConfig) {
325
327
  return entryPoints;
326
328
  }, getAdditionalEndpoints(resolvedConfig));
327
329
  }
328
- var wasmPlugin = {
329
- name: "wasm",
330
+ var edgeWasmPlugin = {
331
+ name: "edge-wasm-vercel",
330
332
  setup(build2) {
331
333
  build2.onResolve({ filter: /\.wasm/ }, (args) => {
332
334
  return {
@@ -354,30 +356,33 @@ var vercelOgPlugin = (ctx) => {
354
356
  var standardBuildOptions = {
355
357
  bundle: true,
356
358
  target: "es2020",
357
- format: "cjs",
359
+ format: "esm",
358
360
  platform: "node",
359
361
  logLevel: "info",
360
362
  logOverride: {
361
363
  "ignored-bare-import": "verbose",
362
364
  "require-resolve-not-external": "verbose"
363
365
  },
364
- minify: true,
365
- plugins: [wasmPlugin]
366
+ minify: false,
367
+ plugins: []
366
368
  };
367
369
  async function buildFn(resolvedConfig, entry, buildOptions) {
370
+ var _a;
368
371
  assert(
369
372
  entry.destination.length > 0,
370
373
  `Endpoint ${typeof entry.source === "string" ? entry.source : "-"} does not have build destination`
371
374
  );
375
+ const options = Object.assign({}, standardBuildOptions);
376
+ if (buildOptions) {
377
+ Object.assign(options, buildOptions);
378
+ }
379
+ const filename = entry.edge || options.format === "cjs" ? "index.js" : "index.mjs";
372
380
  const outfile = import_path3.default.join(
373
381
  getOutput(resolvedConfig, "functions"),
374
382
  entry.destination,
375
- "index.js"
383
+ filename
376
384
  );
377
- const options = Object.assign({}, standardBuildOptions, { outfile });
378
- if (buildOptions) {
379
- Object.assign(options, buildOptions);
380
- }
385
+ Object.assign(options, { outfile });
381
386
  if (!options.stdin) {
382
387
  if (typeof entry.source === "string") {
383
388
  options.entryPoints = [entry.source];
@@ -401,29 +406,69 @@ async function buildFn(resolvedConfig, entry, buildOptions) {
401
406
  "import",
402
407
  "require"
403
408
  ];
409
+ (_a = options.plugins) == null ? void 0 : _a.push(edgeWasmPlugin);
404
410
  options.format = "esm";
411
+ } else if (options.format === "esm") {
412
+ options.banner = {
413
+ js: `import { createRequire } from 'node:module';
414
+ import path from 'node:path';
415
+ import url from 'node:url';
416
+ const require = createRequire(import.meta.url);
417
+ const __filename = url.fileURLToPath(import.meta.url);
418
+ const __dirname = path.dirname(__filename);
419
+ `
420
+ };
405
421
  }
406
422
  const ctx = { found: false, index: "" };
407
423
  options.plugins.push(vercelOgPlugin(ctx));
408
424
  const output = await (0, import_esbuild.build)(options);
409
- if (ctx.found && ctx.index) {
410
- const dir = (0, import_path3.dirname)(ctx.index);
411
- const externalFiles = await (0, import_fast_glob.default)(`${dir}/*.{ttf,wasm}`);
412
- for (const f of externalFiles) {
413
- await (0, import_promises2.copyFile)(
414
- f,
415
- import_path3.default.join(
416
- getOutput(resolvedConfig, "functions"),
417
- entry.destination,
418
- (0, import_path3.basename)(f)
419
- )
420
- );
425
+ if (typeof entry.source == "string") {
426
+ const base = (0, import_workspace_root.workspaceRootSync)(resolvedConfig.root) || resolvedConfig.root;
427
+ const { fileList, reasons } = await (0, import_nft.nodeFileTrace)([entry.source], {
428
+ base,
429
+ processCwd: resolvedConfig.root,
430
+ mixedModules: true,
431
+ ignore: [
432
+ "**/node_modules/react{,-dom,-dom-server-turbopack}/**/*.development.js",
433
+ "**/*.d.ts",
434
+ "**/*.map",
435
+ "**/node_modules/webpack5/**/*"
436
+ ],
437
+ async readFile(filepath) {
438
+ if (filepath.endsWith(".ts") || filepath.endsWith(".tsx")) {
439
+ const result = await (0, import_esbuild.build)({
440
+ ...standardBuildOptions,
441
+ entryPoints: [entry.source],
442
+ bundle: false,
443
+ write: false
444
+ });
445
+ return result.outputFiles[0].text;
446
+ }
447
+ return import_promises2.default.readFile(filepath, "utf-8");
448
+ }
449
+ });
450
+ for (const file of fileList) {
451
+ if (reasons.has(file) && reasons.get(file).type.includes("asset") && !file.endsWith(".js") && !file.endsWith(".cjs") && !file.endsWith(".mjs") && !file.endsWith("package.json")) {
452
+ await (0, import_promises2.copyFile)(
453
+ import_path3.default.join(base, file),
454
+ import_path3.default.join(
455
+ getOutput(resolvedConfig, "functions"),
456
+ entry.destination,
457
+ (0, import_path3.basename)(file)
458
+ )
459
+ );
460
+ }
421
461
  }
422
462
  }
423
- await writeVcConfig(resolvedConfig, entry.destination, Boolean(entry.edge));
463
+ await writeVcConfig(
464
+ resolvedConfig,
465
+ entry.destination,
466
+ Boolean(entry.edge),
467
+ filename
468
+ );
424
469
  return output;
425
470
  }
426
- async function writeVcConfig(resolvedConfig, destination, edge) {
471
+ async function writeVcConfig(resolvedConfig, destination, edge, filename) {
427
472
  var _a;
428
473
  const vcConfig = import_path3.default.join(
429
474
  getOutput(resolvedConfig, "functions"),
@@ -437,10 +482,10 @@ async function writeVcConfig(resolvedConfig, destination, edge) {
437
482
  vercelOutputVcConfigSchema.parse(
438
483
  edge ? {
439
484
  runtime: "edge",
440
- entrypoint: "index.js"
485
+ entrypoint: filename
441
486
  } : {
442
487
  runtime: nodeVersion.runtime,
443
- handler: "index.js",
488
+ handler: filename,
444
489
  maxDuration: (_a = resolvedConfig.vercel) == null ? void 0 : _a.defaultMaxDuration,
445
490
  launcherType: "Nodejs",
446
491
  shouldAddHelpers: true
@@ -476,6 +521,7 @@ async function extractExports(filepath) {
476
521
  const contents = await removeDefaultExport(filepath);
477
522
  const buildOptions = {
478
523
  ...standardBuildOptions,
524
+ format: "cjs",
479
525
  minify: false,
480
526
  write: false,
481
527
  legalComments: "none"
package/dist/index.js CHANGED
@@ -263,6 +263,8 @@ var vercelEndpointExports = z3.object({
263
263
  // src/build.ts
264
264
  import { generateCode, loadFile } from "magicast";
265
265
  import { getNodeVersion } from "@vercel/build-utils";
266
+ import { nodeFileTrace } from "@vercel/nft";
267
+ import { workspaceRootSync } from "workspace-root";
266
268
  function getAdditionalEndpoints(resolvedConfig) {
267
269
  var _a;
268
270
  return (((_a = resolvedConfig.vercel) == null ? void 0 : _a.additionalEndpoints) ?? []).map((e) => ({
@@ -295,8 +297,8 @@ function getEntries(resolvedConfig) {
295
297
  return entryPoints;
296
298
  }, getAdditionalEndpoints(resolvedConfig));
297
299
  }
298
- var wasmPlugin = {
299
- name: "wasm",
300
+ var edgeWasmPlugin = {
301
+ name: "edge-wasm-vercel",
300
302
  setup(build2) {
301
303
  build2.onResolve({ filter: /\.wasm/ }, (args) => {
302
304
  return {
@@ -324,30 +326,33 @@ var vercelOgPlugin = (ctx) => {
324
326
  var standardBuildOptions = {
325
327
  bundle: true,
326
328
  target: "es2020",
327
- format: "cjs",
329
+ format: "esm",
328
330
  platform: "node",
329
331
  logLevel: "info",
330
332
  logOverride: {
331
333
  "ignored-bare-import": "verbose",
332
334
  "require-resolve-not-external": "verbose"
333
335
  },
334
- minify: true,
335
- plugins: [wasmPlugin]
336
+ minify: false,
337
+ plugins: []
336
338
  };
337
339
  async function buildFn(resolvedConfig, entry, buildOptions) {
340
+ var _a;
338
341
  assert(
339
342
  entry.destination.length > 0,
340
343
  `Endpoint ${typeof entry.source === "string" ? entry.source : "-"} does not have build destination`
341
344
  );
345
+ const options = Object.assign({}, standardBuildOptions);
346
+ if (buildOptions) {
347
+ Object.assign(options, buildOptions);
348
+ }
349
+ const filename = entry.edge || options.format === "cjs" ? "index.js" : "index.mjs";
342
350
  const outfile = path3.join(
343
351
  getOutput(resolvedConfig, "functions"),
344
352
  entry.destination,
345
- "index.js"
353
+ filename
346
354
  );
347
- const options = Object.assign({}, standardBuildOptions, { outfile });
348
- if (buildOptions) {
349
- Object.assign(options, buildOptions);
350
- }
355
+ Object.assign(options, { outfile });
351
356
  if (!options.stdin) {
352
357
  if (typeof entry.source === "string") {
353
358
  options.entryPoints = [entry.source];
@@ -371,29 +376,69 @@ async function buildFn(resolvedConfig, entry, buildOptions) {
371
376
  "import",
372
377
  "require"
373
378
  ];
379
+ (_a = options.plugins) == null ? void 0 : _a.push(edgeWasmPlugin);
374
380
  options.format = "esm";
381
+ } else if (options.format === "esm") {
382
+ options.banner = {
383
+ js: `import { createRequire } from 'node:module';
384
+ import path from 'node:path';
385
+ import url from 'node:url';
386
+ const require = createRequire(import.meta.url);
387
+ const __filename = url.fileURLToPath(import.meta.url);
388
+ const __dirname = path.dirname(__filename);
389
+ `
390
+ };
375
391
  }
376
392
  const ctx = { found: false, index: "" };
377
393
  options.plugins.push(vercelOgPlugin(ctx));
378
394
  const output = await build(options);
379
- if (ctx.found && ctx.index) {
380
- const dir = dirname(ctx.index);
381
- const externalFiles = await glob(`${dir}/*.{ttf,wasm}`);
382
- for (const f of externalFiles) {
383
- await copyFile(
384
- f,
385
- path3.join(
386
- getOutput(resolvedConfig, "functions"),
387
- entry.destination,
388
- basename(f)
389
- )
390
- );
395
+ if (typeof entry.source == "string") {
396
+ const base = workspaceRootSync(resolvedConfig.root) || resolvedConfig.root;
397
+ const { fileList, reasons } = await nodeFileTrace([entry.source], {
398
+ base,
399
+ processCwd: resolvedConfig.root,
400
+ mixedModules: true,
401
+ ignore: [
402
+ "**/node_modules/react{,-dom,-dom-server-turbopack}/**/*.development.js",
403
+ "**/*.d.ts",
404
+ "**/*.map",
405
+ "**/node_modules/webpack5/**/*"
406
+ ],
407
+ async readFile(filepath) {
408
+ if (filepath.endsWith(".ts") || filepath.endsWith(".tsx")) {
409
+ const result = await build({
410
+ ...standardBuildOptions,
411
+ entryPoints: [entry.source],
412
+ bundle: false,
413
+ write: false
414
+ });
415
+ return result.outputFiles[0].text;
416
+ }
417
+ return fs2.readFile(filepath, "utf-8");
418
+ }
419
+ });
420
+ for (const file of fileList) {
421
+ if (reasons.has(file) && reasons.get(file).type.includes("asset") && !file.endsWith(".js") && !file.endsWith(".cjs") && !file.endsWith(".mjs") && !file.endsWith("package.json")) {
422
+ await copyFile(
423
+ path3.join(base, file),
424
+ path3.join(
425
+ getOutput(resolvedConfig, "functions"),
426
+ entry.destination,
427
+ basename(file)
428
+ )
429
+ );
430
+ }
391
431
  }
392
432
  }
393
- await writeVcConfig(resolvedConfig, entry.destination, Boolean(entry.edge));
433
+ await writeVcConfig(
434
+ resolvedConfig,
435
+ entry.destination,
436
+ Boolean(entry.edge),
437
+ filename
438
+ );
394
439
  return output;
395
440
  }
396
- async function writeVcConfig(resolvedConfig, destination, edge) {
441
+ async function writeVcConfig(resolvedConfig, destination, edge, filename) {
397
442
  var _a;
398
443
  const vcConfig = path3.join(
399
444
  getOutput(resolvedConfig, "functions"),
@@ -407,10 +452,10 @@ async function writeVcConfig(resolvedConfig, destination, edge) {
407
452
  vercelOutputVcConfigSchema.parse(
408
453
  edge ? {
409
454
  runtime: "edge",
410
- entrypoint: "index.js"
455
+ entrypoint: filename
411
456
  } : {
412
457
  runtime: nodeVersion.runtime,
413
- handler: "index.js",
458
+ handler: filename,
414
459
  maxDuration: (_a = resolvedConfig.vercel) == null ? void 0 : _a.defaultMaxDuration,
415
460
  launcherType: "Nodejs",
416
461
  shouldAddHelpers: true
@@ -446,6 +491,7 @@ async function extractExports(filepath) {
446
491
  const contents = await removeDefaultExport(filepath);
447
492
  const buildOptions = {
448
493
  ...standardBuildOptions,
494
+ format: "cjs",
449
495
  minify: false,
450
496
  write: false,
451
497
  legalComments: "none"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-vercel",
3
- "version": "4.0.2",
3
+ "version": "5.0.1",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
@@ -22,7 +22,7 @@
22
22
  "peerDependencies": {
23
23
  "vike": "*",
24
24
  "vite": "^4.4 || ^5.0.2",
25
- "@vite-plugin-vercel/vike": "4.0.2"
25
+ "@vite-plugin-vercel/vike": "5.0.1"
26
26
  },
27
27
  "peerDependenciesMeta": {
28
28
  "@vite-plugin-vercel/vike": {
@@ -34,23 +34,25 @@
34
34
  },
35
35
  "devDependencies": {
36
36
  "@types/node": "^16.18.76",
37
- "@typescript-eslint/eslint-plugin": "^7.1.1",
38
- "@typescript-eslint/parser": "^7.1.1",
37
+ "@typescript-eslint/eslint-plugin": "^7.2.0",
38
+ "@typescript-eslint/parser": "^7.2.0",
39
39
  "eslint": "^8.57.0",
40
40
  "tsup": "^8.0.2",
41
41
  "typescript": "^5.4.2",
42
42
  "vike": "^0.4.165",
43
43
  "vite": "^5.1.6",
44
- "@vite-plugin-vercel/vike": "4.0.2"
44
+ "@vite-plugin-vercel/vike": "5.0.1"
45
45
  },
46
46
  "dependencies": {
47
47
  "@brillout/libassert": "^0.5.8",
48
- "@vercel/build-utils": "^7.8.0",
48
+ "@vercel/build-utils": "^7.9.1",
49
+ "@vercel/nft": "^0.26.4",
49
50
  "@vercel/routing-utils": "^3.1.0",
50
- "esbuild": "^0.20.1",
51
+ "esbuild": "^0.20.2",
51
52
  "eval": "^0.1.8",
52
53
  "fast-glob": "^3.3.2",
53
54
  "magicast": "^0.3.3",
55
+ "workspace-root": "^3.2.0",
54
56
  "zod": "^3.22.4"
55
57
  },
56
58
  "scripts": {