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

vite.config.ts.ejs

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