mirror of
https://github.com/storytold/spark.git
synced 2026-10-18 07:40:38 +00:00
Consolidate all vite aliases in a single one. Maps node_modules to /examples/js/vendor/ to use same paths in development and production
This commit is contained in:
+22
-48
@@ -32,62 +32,36 @@ export default defineConfig(({ mode }) => {
|
|||||||
}),
|
}),
|
||||||
|
|
||||||
dts({ outDir: "dist/types" }),
|
dts({ outDir: "dist/types" }),
|
||||||
|
|
||||||
{
|
{
|
||||||
name: "serve-three-alias",
|
name: "serve-node-modules-alias",
|
||||||
configureServer(server) {
|
configureServer(server) {
|
||||||
const urlPath = "/examples/js/vendor/three/";
|
const baseUrlPath = "/examples/js/vendor/";
|
||||||
|
|
||||||
const filePath = path.resolve("node_modules/three/");
|
server.middlewares.use((req, res, next) => {
|
||||||
server.middlewares.use(urlPath, (req, res, next) => {
|
if (!req.url.startsWith(baseUrlPath)) return next();
|
||||||
if (fs.existsSync(filePath)) {
|
|
||||||
res.setHeader("Content-Type", "application/javascript");
|
const relModulePath = req.url.slice(baseUrlPath.length); // safe substring
|
||||||
fs.createReadStream(filePath).pipe(res);
|
const absPath = path.resolve("node_modules", relModulePath);
|
||||||
|
|
||||||
|
if (fs.existsSync(absPath) && fs.statSync(absPath).isFile()) {
|
||||||
|
const ext = path.extname(absPath);
|
||||||
|
const contentType =
|
||||||
|
{
|
||||||
|
".js": "application/javascript",
|
||||||
|
".mjs": "application/javascript",
|
||||||
|
".css": "text/css",
|
||||||
|
".json": "application/json",
|
||||||
|
}[ext] || "application/octet-stream";
|
||||||
|
|
||||||
|
res.setHeader("Content-Type", contentType);
|
||||||
|
fs.createReadStream(absPath).pipe(res);
|
||||||
} else {
|
} else {
|
||||||
res.statusCode = 404;
|
res.statusCode = 404;
|
||||||
res.end("three.module.js not found");
|
res.end(`Not found: ${relModulePath}`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(`📦 Dev alias active: ${urlPath} → ${filePath}`);
|
console.log(`📦 Dev alias active: ${baseUrlPath} → node_modules/*`);
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "serve-lil-gui-alias",
|
|
||||||
configureServer(server) {
|
|
||||||
const urlPath = "/examples/js/vendor/stats.js/";
|
|
||||||
|
|
||||||
const filePath = path.resolve("node_modules/lil-gui/");
|
|
||||||
server.middlewares.use(urlPath, (req, res, next) => {
|
|
||||||
if (fs.existsSync(filePath)) {
|
|
||||||
res.setHeader("Content-Type", "application/javascript");
|
|
||||||
fs.createReadStream(filePath).pipe(res);
|
|
||||||
} else {
|
|
||||||
res.statusCode = 404;
|
|
||||||
res.end("three.module.js not found");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log(`📦 Dev alias active: ${urlPath} → ${filePath}`);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "serve-statsjs-alias",
|
|
||||||
configureServer(server) {
|
|
||||||
const urlPath = "/examples/js/vendor/stats.js/";
|
|
||||||
|
|
||||||
const filePath = path.resolve("node_modules/stats.js/");
|
|
||||||
server.middlewares.use(urlPath, (req, res, next) => {
|
|
||||||
if (fs.existsSync(filePath)) {
|
|
||||||
res.setHeader("Content-Type", "application/javascript");
|
|
||||||
fs.createReadStream(filePath).pipe(res);
|
|
||||||
} else {
|
|
||||||
res.statusCode = 404;
|
|
||||||
res.end("three.module.js not found");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log(`📦 Dev alias active: ${urlPath} → ${filePath}`);
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
|||||||
Reference in New Issue
Block a user