Files
notesnook/apps/monograph/vite.config.ts

126 lines
3.4 KiB
TypeScript
Raw Permalink Normal View History

2024-10-01 16:19:34 +05:00
/*
This file is part of the Notesnook project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { vitePlugin as remix } from "@remix-run/dev";
2024-10-01 16:19:34 +05:00
import { defineConfig } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";
import arraybuffer from "vite-plugin-arraybuffer";
2024-11-24 15:18:38 +05:00
import ThemeDark from "@notesnook/theme/theme-engine/themes/default-dark.json" with {type:"json"};
import type { Plugin, ResolvedConfig } from "vite";
import { writeFile } from "fs/promises";
import path from "path";
2024-11-24 15:18:38 +05:00
import { viteStaticCopy } from "vite-plugin-static-copy";
import * as pkg from "./package.json";
2024-10-01 16:19:34 +05:00
const DEDUPE = [
"react",
"react-dom",
"@mdi/js",
"@mdi/react",
"@emotion/react",
"zustand",
"@theme-ui/core",
"@theme-ui/components"
];
const DEFAULT_THEME_KEY =
process.env.NODE_ENV === "development"
? "globalThis.DEFAULT_THEME"
: "globalThis.DEFAULT_THEME";
export default defineConfig(({ isSsrBuild }) => ({
2024-10-01 16:19:34 +05:00
plugins: [
writePlugin({
2024-11-24 15:18:38 +05:00
"../package.json": JSON.stringify({
name: pkg.name,
version: pkg.version,
type: "module",
scripts: { start: pkg.scripts.start },
dependencies: {
"@napi-rs/canvas": pkg.dependencies["@napi-rs/canvas"],
"@remix-run/server-runtime": pkg.devDependencies["@remix-run/server-runtime"],
2024-11-24 15:18:38 +05:00
},
})
}),
2024-10-01 16:19:34 +05:00
remix({
2024-11-24 15:18:38 +05:00
buildDirectory: "output/build",
2024-10-01 16:19:34 +05:00
future: {
v3_fetcherPersist: true,
v3_relativeSplatPath: true,
v3_throwAbortReason: true
}
}),
tsconfigPaths(),
arraybuffer(),
2024-11-24 15:18:38 +05:00
isSsrBuild ? viteStaticCopy({
targets: [
{ src: "./server.ts", dest: "../../" },
{ src: "./app/assets", dest: "../" },
{ src: "./fonts", dest: "../" }
2024-11-24 15:18:38 +05:00
]
}) : undefined
2024-10-01 16:19:34 +05:00
],
worker: {
format: "es",
rollupOptions: {
output: {
inlineDynamicImports: true
}
}
},
ssr: {
2024-11-24 15:18:38 +05:00
...(process.env.NODE_ENV === "development"
? {}
: { noExternal: true, external: ["@napi-rs/canvas"] }),
target: "node"
},
2024-10-01 16:19:34 +05:00
build: {
target: isSsrBuild ? "node20" : undefined,
rollupOptions: {
2024-11-24 15:18:38 +05:00
external: ["@napi-rs/canvas"]
}
2024-10-01 16:19:34 +05:00
},
2024-10-01 16:50:45 +05:00
define: {
[DEFAULT_THEME_KEY]: JSON.stringify(ThemeDark)
2024-10-01 16:50:45 +05:00
},
2024-10-01 16:19:34 +05:00
resolve: {
dedupe: DEDUPE
2024-10-01 16:19:34 +05:00
}
}));
function writePlugin(files: Record<string, string>): Plugin {
let config: ResolvedConfig;
let output = false;
return {
name: "vite-plugin-static-copy:build",
apply: "build",
configResolved(_config) {
config = _config;
},
buildEnd() {
output = false;
},
async writeBundle() {
if (output) return;
for (const file in files) {
const content = files[file];
await writeFile(path.join(config.build.outDir, "..", file), content);
}
}
};
}