vite-plugin-vercel 11.0.0-beta.3 → 11.0.0-beta.5

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/types.d.ts CHANGED
@@ -17,6 +17,15 @@ interface ViteVercelConfig {
17
17
  * @default basic
18
18
  */
19
19
  bundleStrategy?: "basic" | "nf3";
20
+ /**
21
+ * Override vite build environments
22
+ * @experimental
23
+ */
24
+ viteEnvNames?: {
25
+ client?: string;
26
+ edge?: string | false;
27
+ node?: string;
28
+ };
20
29
  /**
21
30
  * How long Functions should be allowed to run for every request, in seconds.
22
31
  * If left empty, default value for your plan will be used.
package/dist/vite.js CHANGED
@@ -19,6 +19,16 @@ import { getNodeVersion } from "@vercel/build-utils";
19
19
  import { toPathToRegexpV6 } from "convert-route/path-to-regexp-v6";
20
20
  import { getTransformedRoutes, mergeRoutes, normalizeRoutes } from "@vercel/routing-utils";
21
21
 
22
+ //#region src/utils/buildEnvs.ts
23
+ function getBuildEnvNames(pluginConfig) {
24
+ return {
25
+ client: pluginConfig.viteEnvNames?.client ?? "vercel_client",
26
+ edge: pluginConfig.viteEnvNames?.edge ?? "vercel_edge",
27
+ node: pluginConfig.viteEnvNames?.node ?? "vercel_node"
28
+ };
29
+ }
30
+
31
+ //#endregion
22
32
  //#region src/utils/dedupeRoutes.ts
23
33
  /**
24
34
  * When multiple entries point to the same module, we can deploy them as a single function.
@@ -68,6 +78,7 @@ function entryDestination(root, entry, postfix) {
68
78
  //#endregion
69
79
  //#region src/plugins/api.ts
70
80
  function apiPlugin(pluginConfig) {
81
+ const envNames = getBuildEnvNames(pluginConfig);
71
82
  const outfiles = [];
72
83
  return {
73
84
  name: "vite-plugin-vercel:api",
@@ -75,7 +86,7 @@ function apiPlugin(pluginConfig) {
75
86
  return createAPI(outfiles, pluginConfig);
76
87
  },
77
88
  applyToEnvironment({ name }) {
78
- return name === "vercel_edge" || name === "vercel_node";
89
+ return name === envNames.edge || name === envNames.node;
79
90
  },
80
91
  writeBundle(_opts, bundle$2) {
81
92
  const root = this.environment.config.root ?? process.cwd();
@@ -153,7 +164,7 @@ const reactEdgePlugin$1 = {
153
164
  }
154
165
  }
155
166
  };
156
- function basicBundlePlugin() {
167
+ function basicBundlePlugin(pluginConfig) {
157
168
  const bundledAssets = /* @__PURE__ */ new Map();
158
169
  const bundledChunks = [];
159
170
  return [{
@@ -179,7 +190,7 @@ function basicBundlePlugin() {
179
190
  closeBundle: {
180
191
  order: "post",
181
192
  async handler() {
182
- if (!isVercelLastBuildStep(this.environment)) return;
193
+ if (!isVercelLastBuildStep(this.environment, pluginConfig)) return;
183
194
  this.environment.logger.info("Creating Vercel bundles...");
184
195
  const outfiles = getVercelAPI(this).getOutFiles();
185
196
  const filesToKeep = [];
@@ -205,7 +216,8 @@ async function bundle$1(pluginContext, bundledAssets, outfile) {
205
216
  const buildOptions = {};
206
217
  buildOptions.output = {
207
218
  format: "esm",
208
- legalComments: "none"
219
+ legalComments: "none",
220
+ inlineDynamicImports: true
209
221
  };
210
222
  buildOptions.checks = { pluginTimings: false };
211
223
  buildOptions.input = [source];
@@ -295,8 +307,9 @@ function joinAbsolutePosix(env_or_p0, p1, ...p) {
295
307
  if (path.isAbsolute(p1)) return path.posix.join(p1, ...p);
296
308
  return path.posix.join(typeof env_or_p0 === "string" ? env_or_p0 : env_or_p0.config.root, p1, ...p);
297
309
  }
298
- function isVercelLastBuildStep(env) {
299
- return (typeof env !== "string" ? env.name : env) === "vercel_node";
310
+ function isVercelLastBuildStep(env, pluginConfig) {
311
+ const envNames = getBuildEnvNames(pluginConfig);
312
+ return (typeof env !== "string" ? env.name : env) === envNames.node;
300
313
  }
301
314
  async function cleanup$1(filesToKeep, bundledChunks) {
302
315
  const toKeep = new Set(filesToKeep);
@@ -323,7 +336,8 @@ async function cleanup$1(filesToKeep, bundledChunks) {
323
336
 
324
337
  //#endregion
325
338
  //#region src/plugins/bundle/nf3.ts
326
- function nf3BundlePlugin() {
339
+ function nf3BundlePlugin(pluginConfig) {
340
+ const envNames = getBuildEnvNames(pluginConfig);
327
341
  const externalsPlugin = externals({});
328
342
  delete externalsPlugin.buildEnd;
329
343
  let buildOutput;
@@ -339,7 +353,7 @@ function nf3BundlePlugin() {
339
353
  return env.config.consumer !== "client";
340
354
  },
341
355
  async writeBundle(_, output) {
342
- const isEdge = this.environment.name === "vercel_edge";
356
+ const isEdge = this.environment.name === envNames.edge;
343
357
  const config = this.environment.config;
344
358
  const outDir$1 = normalizePath(path.isAbsolute(config.build.outDir) ? config.build.outDir : path.join(config.root, config.build.outDir));
345
359
  const entries = Object.entries(output).filter((e) => "isEntry" in e[1] && e[1].isEntry).map((e) => ({
@@ -429,12 +443,13 @@ function removeExtension(subject) {
429
443
  //#region src/plugins/clean-outdir.ts
430
444
  function vercelCleanupPlugin(pluginConfig) {
431
445
  let alreadyRun = false;
446
+ const envNames = getBuildEnvNames(pluginConfig);
432
447
  return {
433
448
  apply: "build",
434
449
  name: "vite-plugin-vercel:cleanup",
435
450
  enforce: "pre",
436
451
  applyToEnvironment(env) {
437
- return env.name === "vercel_client";
452
+ return env.name === envNames.client;
438
453
  },
439
454
  buildStart: {
440
455
  order: "pre",
@@ -476,6 +491,7 @@ function getVcConfig(pluginConfig, filename, options) {
476
491
  //#region src/plugins/loader.ts
477
492
  const re_DUMMY = /* @__PURE__ */ new RegExp(`__DUMMY__$`);
478
493
  function loaderPlugin(pluginConfig) {
494
+ const envNames = getBuildEnvNames(pluginConfig);
479
495
  let root;
480
496
  return [
481
497
  {
@@ -519,7 +535,7 @@ export default def;`;
519
535
  name: "vite-plugin-vercel:build-functions",
520
536
  apply: "build",
521
537
  applyToEnvironment(env) {
522
- return env.name === "vercel_node" || env.name === "vercel_edge";
538
+ return env.name === envNames.node || env.name === envNames.edge;
523
539
  },
524
540
  config: {
525
541
  order: "post",
@@ -530,8 +546,8 @@ export default def;`;
530
546
  configEnvironment: {
531
547
  order: "post",
532
548
  handler(name) {
533
- const isEdge = name === "vercel_edge";
534
- if (name === "vercel_node" || isEdge) {
549
+ const isEdge = name === envNames.edge;
550
+ if (name === envNames.node || isEdge) {
535
551
  const entries = dedupeRoutes().filter((e) => (e.vercel?.edge ?? false) === isEdge);
536
552
  return { build: { rollupOptions: {
537
553
  input: Object.fromEntries(entries.map((e) => [entryDestination(root ?? process.cwd(), e, ".func/index"), isEdge ? `${e.id}?edge` : e.id])),
@@ -541,17 +557,16 @@ export default def;`;
541
557
  }
542
558
  },
543
559
  async buildStart() {
544
- const isEdge = this.environment.name === "vercel_edge";
560
+ const isEdge = this.environment.name === envNames.edge;
545
561
  const nodeVersion = await getNodeVersion(process.cwd());
546
562
  const entries = dedupeRoutes();
547
563
  for (const entry of entries.filter((e) => (e.vercel?.edge ?? false) === isEdge)) {
548
- const isEdge$1 = this.environment.name === "vercel_edge";
549
564
  this.emitFile({
550
565
  type: "asset",
551
566
  fileName: entryDestination(root ?? process.cwd(), entry, ".func/.vc-config.json"),
552
- source: JSON.stringify(getVcConfig(pluginConfig, isEdge$1 ? "index.js" : "index.mjs", {
567
+ source: JSON.stringify(getVcConfig(pluginConfig, isEdge ? "index.js" : "index.mjs", {
553
568
  nodeVersion,
554
- edge: isEdge$1,
569
+ edge: isEdge,
555
570
  streaming: entry.vercel?.streaming
556
571
  }), void 0, 2)
557
572
  });
@@ -588,11 +603,12 @@ export default def;`;
588
603
 
589
604
  //#endregion
590
605
  //#region src/plugins/react-edge.ts
591
- function reactEdgePlugin() {
606
+ function reactEdgePlugin(pluginConfig) {
607
+ const envNames = getBuildEnvNames(pluginConfig);
592
608
  return {
593
609
  name: "vite-plugin-vercel:react-edge",
594
610
  applyToEnvironment(env) {
595
- return env.name === "vercel_edge";
611
+ return env.name === envNames.edge;
596
612
  },
597
613
  resolveId: {
598
614
  order: "pre",
@@ -662,19 +678,20 @@ const outDir = path.posix.join(process.cwd(), ".vercel/output");
662
678
  const DUMMY = "__DUMMY__";
663
679
  let injected = false;
664
680
  function setupEnvs(pluginConfig) {
681
+ const envNames = getBuildEnvNames(pluginConfig);
665
682
  return [
666
683
  {
667
684
  name: "vite-plugin-vercel:setup-envs",
668
685
  buildApp: {
669
686
  order: "post",
670
687
  async handler(builder) {
671
- try {
672
- await builder.build(builder.environments.vercel_client);
688
+ if (!builder.environments[envNames.client].isBuilt) try {
689
+ await builder.build(builder.environments[envNames.client]);
673
690
  } catch (e) {
674
691
  if (e instanceof Error && e.message.includes(`Could not resolve entry module "index.html"`)) {} else throw e;
675
692
  }
676
- await builder.build(builder.environments.vercel_edge);
677
- await builder.build(builder.environments.vercel_node);
693
+ if (envNames.edge !== false && !builder.environments[envNames.edge].isBuilt) await builder.build(builder.environments[envNames.edge]);
694
+ if (!builder.environments[envNames.node].isBuilt) await builder.build(builder.environments[envNames.node]);
678
695
  }
679
696
  },
680
697
  config: {
@@ -685,19 +702,19 @@ function setupEnvs(pluginConfig) {
685
702
  if (pluginConfig.entries) store.entries.push(...pluginConfig.entries);
686
703
  }
687
704
  const outDirOverride = pluginConfig.outDir ? { build: { outDir: pluginConfig.outDir } } : {};
688
- return {
689
- environments: {
690
- vercel_edge: createVercelEnvironmentOptions(outDirOverride),
691
- vercel_node: createVercelEnvironmentOptions(outDirOverride),
692
- vercel_client: {
693
- build: {
694
- outDir: path.join(pluginConfig.outDir ?? outDir, "static"),
695
- copyPublicDir: true,
696
- rollupOptions: { input: getDummyInput() }
697
- },
698
- consumer: "client"
699
- }
705
+ const environments = {};
706
+ if (envNames.client) environments[envNames.client] = {
707
+ build: {
708
+ outDir: path.join(pluginConfig.outDir ?? outDir, "static"),
709
+ copyPublicDir: true,
710
+ rollupOptions: { input: getDummyInput() }
700
711
  },
712
+ consumer: "client"
713
+ };
714
+ if (envNames.edge) environments[envNames.edge] = createVercelEnvironmentOptions(outDirOverride);
715
+ if (envNames.node) environments[envNames.node] = createVercelEnvironmentOptions(outDirOverride);
716
+ return {
717
+ environments,
701
718
  builder: {}
702
719
  };
703
720
  }
@@ -707,10 +724,10 @@ function setupEnvs(pluginConfig) {
707
724
  {
708
725
  name: "vite-plugin-vercel:setup-envs:vercel_edge",
709
726
  applyToEnvironment(env) {
710
- return env.name === "vercel_edge";
727
+ return env.name === envNames.edge;
711
728
  },
712
729
  configEnvironment(name, config, env) {
713
- if (name !== "vercel_edge") return;
730
+ if (name !== envNames.edge) return;
714
731
  return {
715
732
  resolve: {
716
733
  external: edgeExternal,
@@ -746,10 +763,10 @@ function setupEnvs(pluginConfig) {
746
763
  {
747
764
  name: "vite-plugin-vercel:setup-envs:vercel_node",
748
765
  applyToEnvironment(env) {
749
- return env.name === "vercel_node";
766
+ return env.name === envNames.node;
750
767
  },
751
768
  configEnvironment(name, config) {
752
- if (name !== "vercel_node") return;
769
+ if (name !== envNames.node) return;
753
770
  return { optimizeDeps: { ...config.optimizeDeps } };
754
771
  },
755
772
  generateBundle: {
@@ -768,7 +785,7 @@ function setupEnvs(pluginConfig) {
768
785
  {
769
786
  name: "vite-plugin-vercel:setup-envs:vercel_client",
770
787
  applyToEnvironment(env) {
771
- return env.name === "vercel_client";
788
+ return env.name === envNames.client;
772
789
  },
773
790
  generateBundle: { async handler(_opts, bundle$2) {
774
791
  cleanupDummy(bundle$2);
@@ -802,7 +819,9 @@ function createVercelEnvironmentOptions(overrides) {
802
819
  rollupOptions: {
803
820
  input: getDummyInput(),
804
821
  output: {
805
- sanitizeFileName: false,
822
+ sanitizeFileName: (filename) => {
823
+ return filename.replace("\0", "_");
824
+ },
806
825
  sourcemap: false
807
826
  }
808
827
  },
@@ -826,12 +845,12 @@ function cleanupDummy(bundle$2) {
826
845
  //#region src/plugins/index.ts
827
846
  function vercel(pluginConfig = {}) {
828
847
  return [
829
- reactEdgePlugin(),
830
- vercelCleanupPlugin(),
848
+ reactEdgePlugin(pluginConfig),
849
+ vercelCleanupPlugin(pluginConfig),
831
850
  apiPlugin(pluginConfig),
832
851
  ...setupEnvs(pluginConfig),
833
852
  ...loaderPlugin(pluginConfig),
834
- ...pluginConfig?.bundleStrategy === "nf3" ? nf3BundlePlugin() : basicBundlePlugin(),
853
+ ...pluginConfig?.bundleStrategy === "nf3" ? nf3BundlePlugin(pluginConfig) : basicBundlePlugin(pluginConfig),
835
854
  catchAll(),
836
855
  devServer()
837
856
  ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-vercel",
3
- "version": "11.0.0-beta.3",
3
+ "version": "11.0.0-beta.5",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",