tibi-docs/esbuild.config.js

133 lines
3.6 KiB
JavaScript
Raw Permalink Normal View History

2024-01-27 19:58:35 +01:00
const fs = require("fs")
2023-02-21 13:36:06 +01:00
const resolvePlugin = {
name: "resolvePlugin",
setup(build) {
let path = require("path")
// url in css does not resolve via esbuild-svelte correctly
build.onResolve({ filter: /.*/, namespace: "fakecss" }, (args) => {
// console.log(args)
if (args.path.match(/^\./)) return { path: path.dirname(args.importer) + "/" + args.path }
// return { path: path.join(args.resolveDir, "public", args.path) }
})
},
}
////////////////////////// esbuild-svelte
const sveltePlugin = require("esbuild-svelte")
const frontendDir = "./frontend"
const distDir = frontendDir + "/dist"
// console.log("copy public dir...")
// const copydir = require("copy-dir")
// copydir.sync(__dirname + "/public", __dirname + "/" + distDir)
/*copydir.sync(
__dirname + "/public/index.html",
__dirname + "/" + distDir + "/template.html"
)*/
const svelteConfig = require("./svelte.config")
const esbuildSvelte = sveltePlugin({
compilerOptions: {
2024-04-09 16:47:44 +02:00
css: "external",
2023-02-21 13:36:06 +01:00
hydratable: true,
dev: (process.argv?.length > 2 ? process.argv[2] : "build") !== "build",
},
preprocess: svelteConfig.preprocess,
cache: true,
filterWarnings: (warning) => {
// filter out a11y
if (warning.code.match(/^a11y/)) return false
return true
},
})
const options = {
logLevel: "info",
color: true,
entryPoints: ["./frontend/src/index.ts"],
outfile: distDir + "/index.mjs",
metafile: true,
format: "esm",
minify: process.argv[2] == "build",
bundle: true,
splitting: false,
plugins: [esbuildSvelte, resolvePlugin],
loader: {
".woff2": "file",
".woff": "file",
".eot": "file",
".svg": "file",
".ttf": "file",
},
sourcemap: true,
target: ["es2020", "chrome61", "firefox60", "safari11", "edge18"],
}
const bsMiddleware = []
if (process.argv[2] == "start") {
const { createProxyMiddleware } = require("http-proxy-middleware")
2024-01-27 19:58:35 +01:00
const dotEnv = fs.readFileSync(__dirname + "/.env", "utf8")
const TIBI_NAMESPACE = dotEnv.match(/TIBI_NAMESPACE=(.*)/)[1]
const apiBase = process.env.API_BASE || "http://localhost:8080/api/v1/_/" + TIBI_NAMESPACE
2023-02-21 13:36:06 +01:00
bsMiddleware.push(
2024-04-09 16:47:44 +02:00
createProxyMiddleware({
pathFilter: "/api",
2023-02-21 13:36:06 +01:00
target: apiBase,
pathRewrite: { "^/api": "" },
changeOrigin: true,
logLevel: "debug",
})
)
2024-01-27 19:58:35 +01:00
if (process.env.SSR) {
bsMiddleware.push(
2024-04-09 16:47:44 +02:00
createProxyMiddleware({
pathFilter: function (path, req) {
2024-01-27 19:58:35 +01:00
return !path.match(/\./)
},
2024-04-09 16:47:44 +02:00
target: apiBase,
changeOrigin: true,
logLevel: "debug",
pathRewrite: function (path, req) {
console.log(path)
return "/ssr?url=" + encodeURIComponent(path)
},
})
2024-01-27 19:58:35 +01:00
)
}
2023-02-21 13:36:06 +01:00
}
module.exports = {
sveltePlugin: sveltePlugin,
resolvePlugin: resolvePlugin,
options: options,
2024-01-27 19:58:35 +01:00
distDir,
2023-02-21 13:36:06 +01:00
watch: {
path: [__dirname + "/" + frontendDir + "/src/**/*"],
},
serve: {
onRequest(args) {
console.log(args)
},
},
browserSync: {
server: {
baseDir: frontendDir,
middleware: [
require("morgan")("dev"),
...bsMiddleware,
require("connect-history-api-fallback")({
index: "/spa.html",
// verbose: true,
}),
],
},
open: false,
// logLevel: "debug",
},
}