web/vite: expose libav.js files directly

This commit is contained in:
dumbmoron 2024-08-12 16:47:11 +00:00
parent 6a04312781
commit 75ef4604d8
No known key found for this signature in database
3 changed files with 71 additions and 13 deletions

View file

@ -149,6 +149,9 @@ importers:
eslint:
specifier: ^8.57.0
version: 8.57.0
glob:
specifier: ^10.4.5
version: 10.4.5
mdsvex:
specifier: ^0.11.2
version: 0.11.2(svelte@4.2.18)

View file

@ -33,6 +33,7 @@
"@types/node": "^20.14.10",
"compare-versions": "^6.1.0",
"eslint": "^8.57.0",
"glob": "^10.4.5",
"mdsvex": "^0.11.2",
"svelte": "^4.2.7",
"svelte-check": "^3.6.0",

View file

@ -1,22 +1,72 @@
import { defineConfig, searchForWorkspaceRoot, type PluginOption } from "vite";
import { sveltekit } from "@sveltejs/kit/vite";
import { defineConfig, searchForWorkspaceRoot } from "vite";
import basicSSL from "@vitejs/plugin-basic-ssl";
import { glob } from "glob";
import mime from "mime";
import { cp, readdir, mkdir } from "node:fs/promises";
import { createReadStream } from "node:fs";
import { join, basename } from "node:path";
const exposeLibAV: PluginOption = (() => {
const IMPUT_MODULE_DIR = join(__dirname, 'node_modules/@imput');
return {
name: "vite-libav.js",
configureServer(server) {
server.middlewares.use(async (req, res, next) => {
if (!req.url?.startsWith('/_libav/')) return next();
const filename = basename(req.url).split('?')[0];
if (!filename) return next();
const [ file ] = await glob(join(IMPUT_MODULE_DIR, '/**/dist/', filename));
if (!file) return next();
const fileType = mime.getType(filename);
if (!fileType) return next();
res.setHeader('Content-Type', fileType);
return createReadStream(file).pipe(res);
});
},
generateBundle: async (options) => {
if (!options.dir) {
return;
}
const assets = join(options.dir, '_libav');
await mkdir(assets, { recursive: true });
const modules = await readdir(IMPUT_MODULE_DIR).then(
modules => modules.filter(m => m.startsWith('libav.js'))
);
for (const module of modules) {
const distFolder = join(IMPUT_MODULE_DIR, module, 'dist/');
console.log(distFolder);
await cp(distFolder, assets, { recursive: true });
}
}
}
})();
const enableCOEP: PluginOption = {
name: "isolation",
configureServer(server) {
server.middlewares.use((_req, res, next) => {
res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
next();
})
}
};
export default defineConfig({
plugins: [
basicSSL(),
sveltekit(),
{
name: "isolation",
configureServer(server) {
server.middlewares.use((_req, res, next) => {
res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
next();
})
}
}
enableCOEP,
exposeLibAV
],
build: {
rollupOptions: {
@ -33,6 +83,10 @@ export default defineConfig({
}
},
server: {
headers: {
"Cross-Origin-Opener-Policy": "same-origin",
"Cross-Origin-Embedder-Policy": "require-corp"
},
fs: {
allow: [
searchForWorkspaceRoot(process.cwd())
@ -41,6 +95,6 @@ export default defineConfig({
proxy: {}
},
optimizeDeps: {
exclude: ["@imput/ffmpeg.wasm"]
exclude: [ "@imput/libav.js-remux-cli" ]
},
});