How to exclude files from Vite's dependency-analysis... #4697
-
Hello. This feels like a very "niche" question so I'm not sure how, or where, to ask. A discussion feels most appropriate, please be gentle. Given: Project structure
UtilsSome shareables between client/server. One file in particular, needs to be excluded from the client:
esbuild: {
exclude: ["winston"]
}, import {readFile} from 'fs/promises';
const {dependencies} = JSON.parse((await readFile(new URL('./package.json', import.meta.url))).toString());
const entries = Object.keys(dependencies)
.filter(value => !value.includes("winston"))
.map(value => {
let path = `./node_modules/${value}`;
console.log(path);
return path;
});
optimizeDeps: {
entries,
exclude: [ // never worked even a little bit. And no matter the "path".
"../node_modules/winston",
"../node_modules/winston-daily-rotate-file"
],
...
} server: {
force: true,
watch: {
ignored: "winston" // tried ./node_modules/winston* and several variations therein.
}
}, Some variations of the above 3 would allow the page to load and render, but then when Vite connects, it "optimizes imports" and pulls in Rabbit Trail: ...
optimizeDeps: {
esbuildOptions: {
plugins: [
NodeGlobalsPolyfillPlugin({
process: true, // only needed for `./src/utils/logger.ts`, but that file shouldn't be imported.
}),
]
}
}
... This gets through undefined ServerThe server works fine: Client
// ./svelte.config.js
import preprocess from 'svelte-preprocess';
import less from 'svelte-preprocess';
import adapter from '@sveltejs/adapter-node';
import {NodeGlobalsPolyfillPlugin} from "@esbuild-plugins/node-globals-polyfill";
import * as path from "path";
/** @type {import('@sveltejs/kit').Config} */
export default {
// Consult https://github.com/sveltejs/svelte-preprocess for more information about preprocessors
preprocess: preprocess(less({})),
kit: {
adapter: adapter({
out: 'build/client',
precompress: false,
}),
files: {
assets: 'src/client/static',
lib: 'src/client/lib',
routes: 'src/client/routes',
template: 'src/client/app.html',
},
vite: {
root: path.join(process.cwd(), "../"),
server: {
force: true,
},
optimizeDeps: {
esbuildOptions: {
plugins: [
NodeGlobalsPolyfillPlugin({
process: true,
}),
]
}
},
clearScreen: true,
}
},
}; So. How do I exclude a file/files from |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
The only way to exclude from Vite's dep analysis is to not import it, and it seems like down the module graph you imported Let me know if that works for you, otherwise it would be easier to debug with a repro/codebase available to check out. |
Beta Was this translation helpful? Give feedback.
The only way to exclude from Vite's dep analysis is to not import it, and it seems like down the module graph you imported
src/utils/logger.ts
fromsrc/utils/index.ts
? Ifsrc/utils/index.ts
exports both client and server code, that may be the issue. It'd be best if you importsrc/utils/logger.ts
directly when using it from a server-side code. That way each file works in client or server exclusively.Let me know if that works for you, otherwise it would be easier to debug with a repro/codebase available to check out.