Skip to content
Source
Sign inSign up
TanStack/cli
Open History to see the last commit

vite.config.ts.ejs

110 lines4.0 KB

1/**
2 * This is the base config for vite.
3 * When building, the adapter config is used which loads this file and extends it.
4 */
5import { defineConfig, type UserConfig } from "vite";
6import { qwikVite } from "@builder.io/qwik/optimizer";
7import { qwikCity } from "@builder.io/qwik-city/vite";
8import tsconfigPaths from "vite-tsconfig-paths";
9import pkg from "./package.json";
10​
11type PkgDep = Record<string, string>;
12const { dependencies = {}, devDependencies = {} } = pkg as any as {
13 dependencies: PkgDep;
14 devDependencies: PkgDep;
15 [key: string]: unknown;
16};
17errorOnDuplicatesPkgDeps(devDependencies, dependencies);
18​
19<% if (tailwind) { %>
20import tailwindcss from '@tailwindcss/vite'
21<% } %>
22​
23/**
24 * Note that Vite normally starts from `index.html` but the qwikCity plugin makes start at `src/entry.ssr.tsx` instead.
25 */
26export default defineConfig(({ command, mode }): UserConfig => {
27 return {
28 plugins: [qwikCity(), qwikVite(), tsconfigPaths()<% if (tailwind) { %>, tailwindcss()<% } %>],
29 // This tells Vite which dependencies to pre-build in dev mode.
30 optimizeDeps: {
31 // Put problematic deps that break bundling here, mostly those with binaries.
32 // For example ['better-sqlite3'] if you use that in server functions.
33 exclude: [],
34 },
35​
36 /**
37 * This is an advanced setting. It improves the bundling of your server code. To use it, make sure you understand when your consumed packages are dependencies or dev dependencies. (otherwise things will break in production)
38 */
39 // ssr:
40 // command === "build" && mode === "production"
41 // ? {
42 // // All dev dependencies should be bundled in the server build
43 // noExternal: Object.keys(devDependencies),
44 // // Anything marked as a dependency will not be bundled
45 // // These should only be production binary deps (including deps of deps), CLI deps, and their module graph
46 // // If a dep-of-dep needs to be external, add it here
47 // // For example, if something uses `bcrypt` but you don't have it as a dep, you can write
48 // // external: [...Object.keys(dependencies), 'bcrypt']
49 // external: Object.keys(dependencies),
50 // }
51 // : undefined,
52​
53 server: {
54 headers: {
55 // Don't cache the server response in dev mode
56 "Cache-Control": "public, max-age=0",
57 },
58 },
59 preview: {
60 headers: {
61 // Do cache the server response in preview (non-adapter production build)
62 "Cache-Control": "public, max-age=600",
63 },
64 },
65 };
66});
67​
68// *** utils ***
69​
70/**
71 * Function to identify duplicate dependencies and throw an error
72 * @param {Object} devDependencies - List of development dependencies
73 * @param {Object} dependencies - List of production dependencies
74 */
75function errorOnDuplicatesPkgDeps(
76 devDependencies: PkgDep,
77 dependencies: PkgDep,
78) {
79 let msg = "";
80 // Create an array 'duplicateDeps' by filtering devDependencies.
81 // If a dependency also exists in dependencies, it is considered a duplicate.
82 const duplicateDeps = Object.keys(devDependencies).filter(
83 (dep) => dependencies[dep],
84 );
85​
86 // include any known qwik packages
87 const qwikPkg = Object.keys(dependencies).filter((value) =>
88 /qwik/i.test(value),
89 );
90​
91 // any errors for missing "qwik-city-plan"
92 // [PLUGIN_ERROR]: Invalid module "@qwik-city-plan" is not a valid package
93 msg = `Move qwik packages ${qwikPkg.join(", ")} to devDependencies`;
94​
95 if (qwikPkg.length > 0) {
96 throw new Error(msg);
97 }
98​
99 // Format the error message with the duplicates list.
100 // The `join` function is used to represent the elements of the 'duplicateDeps' array as a comma-separated string.
101 msg = `
102 Warning: The dependency "${duplicateDeps.join(", ")}" is listed in both "devDependencies" and "dependencies".
103 Please move the duplicated dependencies to "devDependencies" only and remove it from "dependencies"
104 `;
105​
106 // Throw an error with the constructed message.
107 if (duplicateDeps.length > 0) {
108 throw new Error(msg);
109 }