refactor: change vite config
This commit is contained in:
@@ -10,9 +10,9 @@ import pkg from "./package.json";
|
|||||||
|
|
||||||
type PkgDep = Record<string, string>;
|
type PkgDep = Record<string, string>;
|
||||||
const { dependencies = {}, devDependencies = {} } = pkg as any as {
|
const { dependencies = {}, devDependencies = {} } = pkg as any as {
|
||||||
dependencies: PkgDep;
|
dependencies: PkgDep;
|
||||||
devDependencies: PkgDep;
|
devDependencies: PkgDep;
|
||||||
[key: string]: unknown;
|
[key: string]: unknown;
|
||||||
};
|
};
|
||||||
errorOnDuplicatesPkgDeps(devDependencies, dependencies);
|
errorOnDuplicatesPkgDeps(devDependencies, dependencies);
|
||||||
|
|
||||||
@@ -20,45 +20,47 @@ errorOnDuplicatesPkgDeps(devDependencies, dependencies);
|
|||||||
* Note that Vite normally starts from `index.html` but the qwikCity plugin makes start at `src/entry.ssr.tsx` instead.
|
* Note that Vite normally starts from `index.html` but the qwikCity plugin makes start at `src/entry.ssr.tsx` instead.
|
||||||
*/
|
*/
|
||||||
export default defineConfig(({ command, mode }): UserConfig => {
|
export default defineConfig(({ command, mode }): UserConfig => {
|
||||||
return {
|
return {
|
||||||
plugins: [qwikCity(), qwikVite(), tsconfigPaths()],
|
plugins: [qwikCity(), qwikVite(), tsconfigPaths()],
|
||||||
// This tells Vite which dependencies to pre-build in dev mode.
|
// This tells Vite which dependencies to pre-build in dev mode.
|
||||||
optimizeDeps: {
|
optimizeDeps: {
|
||||||
// Put problematic deps that break bundling here, mostly those with binaries.
|
// Put problematic deps that break bundling here, mostly those with binaries.
|
||||||
// For example ['better-sqlite3'] if you use that in server functions.
|
// For example ['better-sqlite3'] if you use that in server functions.
|
||||||
exclude: [],
|
exclude: [],
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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 depencies. (otherwise things will break in production)
|
* 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 depencies. (otherwise things will break in production)
|
||||||
*/
|
*/
|
||||||
// ssr:
|
// ssr:
|
||||||
// command === "build" && mode === "production"
|
// command === "build" && mode === "production"
|
||||||
// ? {
|
// ? {
|
||||||
// // All dev dependencies should be bundled in the server build
|
// // All dev dependencies should be bundled in the server build
|
||||||
// noExternal: Object.keys(devDependencies),
|
// noExternal: Object.keys(devDependencies),
|
||||||
// // Anything marked as a dependency will not be bundled
|
// // Anything marked as a dependency will not be bundled
|
||||||
// // These should only be production binary deps (including deps of deps), CLI deps, and their module graph
|
// // These should only be production binary deps (including deps of deps), CLI deps, and their module graph
|
||||||
// // If a dep-of-dep needs to be external, add it here
|
// // If a dep-of-dep needs to be external, add it here
|
||||||
// // For example, if something uses `bcrypt` but you don't have it as a dep, you can write
|
// // For example, if something uses `bcrypt` but you don't have it as a dep, you can write
|
||||||
// // external: [...Object.keys(dependencies), 'bcrypt']
|
// // external: [...Object.keys(dependencies), 'bcrypt']
|
||||||
// external: Object.keys(dependencies),
|
// external: Object.keys(dependencies),
|
||||||
// }
|
// }
|
||||||
// : undefined,
|
// : undefined,
|
||||||
|
|
||||||
server: {
|
server: {
|
||||||
headers: {
|
headers: {
|
||||||
// Don't cache the server response in dev mode
|
// Don't cache the server response in dev mode
|
||||||
"Cache-Control": "public, max-age=0",
|
"Cache-Control": "public, max-age=0",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
preview: {
|
preview: {
|
||||||
headers: {
|
host: true,
|
||||||
// Do cache the server response in preview (non-adapter production build)
|
port: 3000,
|
||||||
"Cache-Control": "public, max-age=600",
|
headers: {
|
||||||
},
|
// Do cache the server response in preview (non-adapter production build)
|
||||||
},
|
"Cache-Control": "public, max-age=600",
|
||||||
};
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
// *** utils ***
|
// *** utils ***
|
||||||
@@ -69,38 +71,38 @@ export default defineConfig(({ command, mode }): UserConfig => {
|
|||||||
* @param {Object} dependencies - List of production dependencies
|
* @param {Object} dependencies - List of production dependencies
|
||||||
*/
|
*/
|
||||||
function errorOnDuplicatesPkgDeps(
|
function errorOnDuplicatesPkgDeps(
|
||||||
devDependencies: PkgDep,
|
devDependencies: PkgDep,
|
||||||
dependencies: PkgDep,
|
dependencies: PkgDep,
|
||||||
) {
|
) {
|
||||||
let msg = "";
|
let msg = "";
|
||||||
// Create an array 'duplicateDeps' by filtering devDependencies.
|
// Create an array 'duplicateDeps' by filtering devDependencies.
|
||||||
// If a dependency also exists in dependencies, it is considered a duplicate.
|
// If a dependency also exists in dependencies, it is considered a duplicate.
|
||||||
const duplicateDeps = Object.keys(devDependencies).filter(
|
const duplicateDeps = Object.keys(devDependencies).filter(
|
||||||
(dep) => dependencies[dep],
|
(dep) => dependencies[dep],
|
||||||
);
|
);
|
||||||
|
|
||||||
// include any known qwik packages
|
// include any known qwik packages
|
||||||
const qwikPkg = Object.keys(dependencies).filter((value) =>
|
const qwikPkg = Object.keys(dependencies).filter((value) =>
|
||||||
/qwik/i.test(value),
|
/qwik/i.test(value),
|
||||||
);
|
);
|
||||||
|
|
||||||
// any errors for missing "qwik-city-plan"
|
// any errors for missing "qwik-city-plan"
|
||||||
// [PLUGIN_ERROR]: Invalid module "@qwik-city-plan" is not a valid package
|
// [PLUGIN_ERROR]: Invalid module "@qwik-city-plan" is not a valid package
|
||||||
msg = `Move qwik packages ${qwikPkg.join(", ")} to devDependencies`;
|
msg = `Move qwik packages ${qwikPkg.join(", ")} to devDependencies`;
|
||||||
|
|
||||||
if (qwikPkg.length > 0) {
|
if (qwikPkg.length > 0) {
|
||||||
throw new Error(msg);
|
throw new Error(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format the error message with the duplicates list.
|
// Format the error message with the duplicates list.
|
||||||
// The `join` function is used to represent the elements of the 'duplicateDeps' array as a comma-separated string.
|
// The `join` function is used to represent the elements of the 'duplicateDeps' array as a comma-separated string.
|
||||||
msg = `
|
msg = `
|
||||||
Warning: The dependency "${duplicateDeps.join(", ")}" is listed in both "devDependencies" and "dependencies".
|
Warning: The dependency "${duplicateDeps.join(", ")}" is listed in both "devDependencies" and "dependencies".
|
||||||
Please move the duplicated dependencies to "devDependencies" only and remove it from "dependencies"
|
Please move the duplicated dependencies to "devDependencies" only and remove it from "dependencies"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
// Throw an error with the constructed message.
|
// Throw an error with the constructed message.
|
||||||
if (duplicateDeps.length > 0) {
|
if (duplicateDeps.length > 0) {
|
||||||
throw new Error(msg);
|
throw new Error(msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user