Skip to content

Commit

Permalink
simply serve the static files
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtatranta committed Feb 26, 2025
1 parent 73a6167 commit 0708451
Showing 1 changed file with 10 additions and 29 deletions.
39 changes: 10 additions & 29 deletions packages/suite-web/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { viteCommonjs } from '@originjs/vite-plugin-commonjs';

Check failure on line 1 in packages/suite-web/vite.config.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

'@originjs/vite-plugin-commonjs' should be listed in the project's dependencies, not devDependencies
import react from '@vitejs/plugin-react';

Check failure on line 2 in packages/suite-web/vite.config.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

'@vitejs/plugin-react' should be listed in the project's dependencies, not devDependencies
import { execSync } from 'child_process';
import { createReadStream, readdirSync, statSync } from 'fs';
import { readdirSync } from 'fs';
import { resolve } from 'path';
import { Plugin, ViteDevServer, defineConfig } from 'vite';

Check failure on line 6 in packages/suite-web/vite.config.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

'vite' should be listed in the project's dependencies, not devDependencies
import { nodePolyfills } from 'vite-plugin-node-polyfills';

Check failure on line 7 in packages/suite-web/vite.config.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

'vite-plugin-node-polyfills' should be listed in the project's dependencies, not devDependencies
Expand All @@ -10,35 +10,15 @@ import wasm from 'vite-plugin-wasm';
import { suiteVersion } from '../suite/package.json';
import { assetPrefix, project } from '../suite-build/utils/env';

// Plugin to serve static files from suite-data/files with /static prefix
const staticDataPlugin = () => ({
name: 'static-data',
// Plugin to serve static files with /static prefix
const staticAliasPlugin = (): Plugin => ({
name: 'static-alias',
configureServer(server: ViteDevServer) {
// Middleware to handle /static requests
server.middlewares.use((req, res, next) => {
if (req.url?.startsWith('/static/')) {
const filePath = req.url.replace('/static/', '');
const fullPath = resolve(__dirname, '../suite-data/files', filePath);
try {
const stat = statSync(fullPath);
const ext = filePath.split('.').pop() as string;
if (stat.isFile()) {
const contentType =
{
webm: 'video/webm',
svg: 'image/svg+xml',
png: 'image/png',
jpg: 'image/jpeg',
jpeg: 'image/jpeg',
}[ext] || 'application/octet-stream';

res.setHeader('Content-Type', contentType);
createReadStream(fullPath).pipe(res);

return;
}
} catch {
/* empty */
}
// Rewrite the URL to access the file from the public directory
req.url = req.url.replace('/static/', '/');
}
next();
});
Expand Down Expand Up @@ -126,14 +106,16 @@ const commitId = execSync('git rev-parse HEAD').toString().trim();
export default defineConfig({
root: '.',
base: assetPrefix,
// Use suite-data/files as the public directory
publicDir: resolve(__dirname, '../suite-data/files'),
plugins: [
nodePolyfills({
// Whether to polyfill `node:` protocol imports.
protocolImports: true,
// Include specific polyfills
include: ['buffer', 'process', 'util'],
}),
staticDataPlugin(),
staticAliasPlugin(),
serveCorePlugin(),
viteCommonjs(),
workerPlugin(),
Expand Down Expand Up @@ -236,7 +218,6 @@ export default defineConfig({
],
},
},
publicDir: false,
build: {
rollupOptions: {
input: resolve(__dirname, 'index.html'),
Expand Down

0 comments on commit 0708451

Please sign in to comment.