MCPcopy
hub / github.com/freshframework/fresh / configureServer

Function configureServer

packages/plugin-vite/src/plugins/dev_server.ts:22–169  ·  view source on GitHub ↗
(server)

Source from the content-addressed store, hash-verified

20 publicDir = config.publicDir;
21 },
22 configureServer(server) {
23 // Build the ignore pattern accounting for the configured base path.
24 // Vite prefixes virtual module URLs with the base (e.g. /ui/@id/...),
25 // so we need to match both /@ and /base/@.
26 const base = server.config.base.replace(/\/$/, "");
27 const IGNORE_URLS = new RegExp(
28 `^(${base})?/(@(vite|fs|id)|\\.vite)/`,
29 );
30
31 server.middlewares.use(async (nodeReq, nodeRes, next) => {
32 const serverCfg = server.config.server;
33
34 const protocol = serverCfg.https ? "https" : "http";
35 const host = serverCfg.host ? serverCfg.host : "localhost";
36 const port = serverCfg.port;
37 const url = new URL(
38 `${protocol}://${host}:${port}${nodeReq.url ?? "/"}`,
39 );
40
41 // Don't cache in dev
42 url.searchParams.delete(ASSET_CACHE_BUST_KEY);
43
44 // Check if it's a vite url or a node_modules asset (e.g. fonts
45 // referenced from CSS in npm packages)
46 if (
47 IGNORE_URLS.test(url.pathname) ||
48 url.pathname.startsWith("/node_modules/") ||
49 server.environments.client.moduleGraph.urlToModuleMap.has(
50 url.pathname,
51 ) ||
52 server.environments.ssr.moduleGraph.urlToModuleMap.has(
53 url.pathname,
54 ) ||
55 url.pathname === "/.well-known/appspecific/com.chrome.devtools.json"
56 ) {
57 return next();
58 }
59
60 const decodedPathname = decodeURIComponent(url.pathname.slice(1));
61
62 // Check if it's a static file first.
63 // Vite handles publicDir (the first static dir) natively,
64 // but we also need to check extra static dirs.
65 const extraStaticDirs = freshConfig.staticDir.slice(1);
66 const allStaticDirs = [publicDir, ...extraStaticDirs];
67 let handledStatic = false;
68 for (const dir of allStaticDirs) {
69 const staticFilePath = path.join(dir, decodedPathname);
70 try {
71 const stat = await Deno.stat(staticFilePath);
72 if (stat.isFile) {
73 if (dir === publicDir) {
74 // Vite serves publicDir files natively
75 return next();
76 }
77 // Serve files from extra dirs manually
78 const file = await Deno.open(staticFilePath, { read: true });
79 const { ext } = path.parse(staticFilePath);

Callers

nothing calls this directly

Calls 9

nextFunction · 0.85
collectCssFunction · 0.85
useMethod · 0.80
openMethod · 0.80
readTextFileMethod · 0.80
getContentTypeFunction · 0.70
deleteMethod · 0.45
getMethod · 0.45
textMethod · 0.45

Tested by

no test coverage detected