2023-06-12 11:49:37 +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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-11-13 17:52:40 +05:00
|
|
|
import { Plugin, PluginOption, ResolvedConfig, defineConfig } from "vite";
|
2023-06-12 11:49:37 +05:00
|
|
|
import react from "@vitejs/plugin-react-swc";
|
|
|
|
|
import svgrPlugin from "vite-plugin-svgr";
|
|
|
|
|
import envCompatible from "vite-plugin-env-compatible";
|
2023-07-12 06:51:14 +05:00
|
|
|
import { VitePWA } from "vite-plugin-pwa";
|
2023-06-22 13:25:56 +05:00
|
|
|
import autoprefixer from "autoprefixer";
|
2023-07-12 06:51:14 +05:00
|
|
|
import { WEB_MANIFEST } from "./web-manifest";
|
|
|
|
|
import { execSync } from "child_process";
|
|
|
|
|
import { version } from "./package.json";
|
2023-07-28 09:13:50 +05:00
|
|
|
import { visualizer } from "rollup-plugin-visualizer";
|
2023-08-01 18:32:11 +05:00
|
|
|
import { OutputPlugin } from "rollup";
|
2023-08-08 13:10:50 +05:00
|
|
|
import path from "path";
|
2023-06-12 11:49:37 +05:00
|
|
|
|
2023-07-12 06:51:14 +05:00
|
|
|
const gitHash = (() => {
|
|
|
|
|
try {
|
|
|
|
|
return execSync("git rev-parse --short HEAD").toString().trim();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return process.env.GIT_HASH || "gitless";
|
|
|
|
|
}
|
|
|
|
|
})();
|
2025-02-28 12:00:31 +05:00
|
|
|
// const appVersion = version.replaceAll(".", "").replace("-beta", "");
|
|
|
|
|
const isBeta = version.includes("-beta");
|
2023-06-16 19:10:02 +05:00
|
|
|
const isTesting =
|
2023-07-12 06:51:14 +05:00
|
|
|
process.env.TEST === "true" || process.env.NODE_ENV === "development";
|
|
|
|
|
const isDesktop = process.env.PLATFORM === "desktop";
|
2023-08-08 13:10:50 +05:00
|
|
|
const isThemeBuilder = process.env.THEME_BUILDER === "true";
|
2023-07-28 09:13:50 +05:00
|
|
|
const isAnalyzing = process.env.ANALYZING === "true";
|
2023-06-20 17:49:23 +05:00
|
|
|
|
2023-06-12 11:49:37 +05:00
|
|
|
export default defineConfig({
|
2023-08-01 18:32:11 +05:00
|
|
|
envPrefix: "NN_",
|
2023-08-07 09:17:04 +05:00
|
|
|
root: "src/",
|
2023-08-08 13:10:50 +05:00
|
|
|
publicDir: isThemeBuilder ? path.join(__dirname, "public") : "../public",
|
2023-06-12 11:49:37 +05:00
|
|
|
build: {
|
2023-07-03 06:47:28 +05:00
|
|
|
target: isDesktop ? "esnext" : "modules",
|
2023-08-07 09:17:04 +05:00
|
|
|
outDir: "../build",
|
2023-06-12 11:49:37 +05:00
|
|
|
minify: "esbuild",
|
|
|
|
|
cssMinify: true,
|
|
|
|
|
emptyOutDir: true,
|
2024-05-11 13:09:59 +05:00
|
|
|
sourcemap: !isDesktop,
|
2023-07-08 11:13:48 +05:00
|
|
|
rollupOptions: {
|
|
|
|
|
output: {
|
2023-08-01 18:32:11 +05:00
|
|
|
plugins: [emitEditorStyles()],
|
2023-07-08 11:13:48 +05:00
|
|
|
assetFileNames: "assets/[name]-[hash:12][extname]",
|
2024-06-03 11:07:44 +05:00
|
|
|
chunkFileNames: "assets/[name]-[hash:12].js",
|
|
|
|
|
manualChunks: (id: string) => {
|
|
|
|
|
if (
|
|
|
|
|
(id.includes("/editor/languages/") ||
|
2025-03-01 13:13:25 +05:00
|
|
|
id.includes("/html/languages/") ||
|
|
|
|
|
id.includes("/refractor/lang/")) &&
|
2024-06-03 11:07:44 +05:00
|
|
|
path.basename(id) !== "index.js"
|
|
|
|
|
)
|
|
|
|
|
return `code-lang-${path.basename(id, "js")}`;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-07-08 11:13:48 +05:00
|
|
|
}
|
|
|
|
|
}
|
2023-06-12 11:49:37 +05:00
|
|
|
},
|
2023-07-12 06:51:14 +05:00
|
|
|
define: {
|
2023-08-08 13:18:27 +05:00
|
|
|
APP_TITLE: `"${isThemeBuilder ? "Notesnook Theme Builder" : "Notesnook"}"`,
|
2023-07-12 06:51:14 +05:00
|
|
|
GIT_HASH: `"${gitHash}"`,
|
2025-02-28 12:00:31 +05:00
|
|
|
APP_VERSION: `"${version}"`,
|
2023-07-12 06:51:14 +05:00
|
|
|
PUBLIC_URL: `"${process.env.PUBLIC_URL || ""}"`,
|
|
|
|
|
IS_DESKTOP_APP: isDesktop,
|
|
|
|
|
PLATFORM: `"${process.env.PLATFORM}"`,
|
|
|
|
|
IS_TESTING: process.env.TEST === "true",
|
2024-01-31 14:31:42 +05:00
|
|
|
IS_BETA: isBeta,
|
2023-08-10 07:18:49 +05:00
|
|
|
IS_THEME_BUILDER: isThemeBuilder
|
2023-07-12 06:51:14 +05:00
|
|
|
},
|
2023-06-12 11:49:37 +05:00
|
|
|
logLevel: process.env.NODE_ENV === "production" ? "warn" : "info",
|
|
|
|
|
resolve: {
|
2023-07-28 09:13:50 +05:00
|
|
|
dedupe: [
|
|
|
|
|
"react",
|
|
|
|
|
"react-dom",
|
|
|
|
|
"@mdi/js",
|
|
|
|
|
"@mdi/react",
|
|
|
|
|
"@emotion/react",
|
2024-11-12 14:09:04 +05:00
|
|
|
"katex",
|
|
|
|
|
"react-modal",
|
|
|
|
|
"dayjs",
|
|
|
|
|
"@streetwriters/kysely"
|
2023-07-28 09:13:50 +05:00
|
|
|
],
|
2023-07-03 06:47:28 +05:00
|
|
|
|
|
|
|
|
alias: [
|
|
|
|
|
{
|
2024-02-07 13:32:27 +05:00
|
|
|
find: /\/desktop-bridge$/gm,
|
2023-07-03 06:47:28 +05:00
|
|
|
replacement: isDesktop
|
2024-02-07 13:32:27 +05:00
|
|
|
? "/desktop-bridge/index.desktop"
|
|
|
|
|
: "/desktop-bridge/index"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
find: /\/sqlite$/gm,
|
|
|
|
|
replacement: isDesktop ? "/sqlite/index.desktop" : "/sqlite/index"
|
2023-07-03 06:47:28 +05:00
|
|
|
}
|
|
|
|
|
]
|
2023-06-12 11:49:37 +05:00
|
|
|
},
|
|
|
|
|
server: {
|
|
|
|
|
port: 3000
|
|
|
|
|
},
|
|
|
|
|
worker: {
|
2023-07-26 12:42:10 +05:00
|
|
|
format: "es",
|
|
|
|
|
rollupOptions: {
|
|
|
|
|
output: {
|
2024-06-03 11:07:44 +05:00
|
|
|
assetFileNames: "assets/[name]-[hash:12][extname]",
|
|
|
|
|
chunkFileNames: "assets/[name]-[hash:12].js",
|
2023-07-26 12:42:10 +05:00
|
|
|
inlineDynamicImports: true
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-12 11:49:37 +05:00
|
|
|
},
|
2023-06-22 13:25:56 +05:00
|
|
|
css: {
|
|
|
|
|
postcss: {
|
|
|
|
|
plugins: [autoprefixer()]
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-06-12 11:49:37 +05:00
|
|
|
plugins: [
|
2023-07-28 09:13:50 +05:00
|
|
|
...(isAnalyzing
|
|
|
|
|
? [
|
|
|
|
|
visualizer({
|
|
|
|
|
gzipSize: true,
|
|
|
|
|
brotliSize: true,
|
|
|
|
|
open: true
|
|
|
|
|
}) as PluginOption
|
|
|
|
|
]
|
|
|
|
|
: []),
|
2023-08-08 13:10:50 +05:00
|
|
|
...((isThemeBuilder || isDesktop) && process.env.NODE_ENV === "production"
|
2023-07-03 06:47:28 +05:00
|
|
|
? []
|
2023-06-22 15:28:29 +05:00
|
|
|
: [
|
|
|
|
|
VitePWA({
|
|
|
|
|
strategies: "injectManifest",
|
|
|
|
|
minify: true,
|
|
|
|
|
manifest: WEB_MANIFEST,
|
2025-03-28 20:52:18 +05:00
|
|
|
injectRegister: null,
|
2023-08-07 09:17:04 +05:00
|
|
|
srcDir: "",
|
2025-03-28 20:52:18 +05:00
|
|
|
filename: "service-worker.ts",
|
|
|
|
|
mode: "production",
|
|
|
|
|
workbox: { mode: "production" },
|
2024-06-03 11:07:44 +05:00
|
|
|
injectManifest: {
|
2025-02-13 16:01:26 +05:00
|
|
|
globPatterns: ["**/*.{js,css,html,wasm}", "**/Inter-*.woff2"],
|
2024-06-03 11:07:44 +05:00
|
|
|
globIgnores: [
|
|
|
|
|
"**/node_modules/**/*",
|
|
|
|
|
"**/code-lang-*.js",
|
|
|
|
|
"pdf.worker.min.js"
|
|
|
|
|
]
|
|
|
|
|
}
|
2023-06-22 15:28:29 +05:00
|
|
|
})
|
|
|
|
|
]),
|
2023-06-12 11:49:37 +05:00
|
|
|
react({
|
|
|
|
|
plugins: isTesting
|
|
|
|
|
? undefined
|
2024-06-03 11:07:44 +05:00
|
|
|
: [
|
|
|
|
|
[
|
|
|
|
|
"@swc/plugin-react-remove-properties",
|
|
|
|
|
{
|
|
|
|
|
properties: ["^data-test-id$"]
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
]
|
2023-06-12 11:49:37 +05:00
|
|
|
}),
|
2023-08-01 18:32:11 +05:00
|
|
|
envCompatible({
|
|
|
|
|
prefix: "NN_",
|
|
|
|
|
mountedPath: "process.env"
|
|
|
|
|
}),
|
2023-06-12 11:49:37 +05:00
|
|
|
svgrPlugin({
|
|
|
|
|
svgrOptions: {
|
2024-06-03 11:07:44 +05:00
|
|
|
icon: true,
|
|
|
|
|
namedExport: "ReactComponent"
|
2023-06-12 11:49:37 +05:00
|
|
|
// ...svgr options (https://react-svgr.com/docs/options/)
|
|
|
|
|
}
|
2024-11-13 17:52:40 +05:00
|
|
|
}),
|
|
|
|
|
...(isDesktop
|
|
|
|
|
? []
|
|
|
|
|
: [
|
|
|
|
|
prefetchPlugin({
|
|
|
|
|
excludeFn: (assetName) =>
|
|
|
|
|
assetName.includes("wa-sqlite-async") ||
|
|
|
|
|
!assetName.includes("wa-sqlite")
|
|
|
|
|
})
|
|
|
|
|
])
|
2023-06-12 11:49:37 +05:00
|
|
|
]
|
|
|
|
|
});
|
2023-08-01 18:32:11 +05:00
|
|
|
|
|
|
|
|
function emitEditorStyles(): OutputPlugin {
|
|
|
|
|
return {
|
|
|
|
|
name: "rollup-plugin-emit-editor-styles",
|
|
|
|
|
generateBundle(options, bundle) {
|
|
|
|
|
for (const file in bundle) {
|
|
|
|
|
const chunk = bundle[file];
|
|
|
|
|
if (
|
|
|
|
|
chunk.type === "asset" &&
|
|
|
|
|
chunk.fileName.endsWith(".css") &&
|
|
|
|
|
typeof chunk.source === "string" &&
|
|
|
|
|
(chunk.source.includes("KaTeX_Fraktur-Bold-") ||
|
|
|
|
|
chunk.source.includes("Hack typeface"))
|
|
|
|
|
) {
|
|
|
|
|
this.emitFile({
|
|
|
|
|
type: "asset",
|
|
|
|
|
fileName: "assets/editor-styles.css",
|
|
|
|
|
name: "editor-styles.css",
|
|
|
|
|
source: chunk.source
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-11-13 17:52:40 +05:00
|
|
|
|
|
|
|
|
function prefetchPlugin(options?: {
|
|
|
|
|
excludeFn?: (assetName: string) => boolean;
|
|
|
|
|
}): Plugin {
|
|
|
|
|
let config: ResolvedConfig;
|
|
|
|
|
return {
|
|
|
|
|
name: "vite-plugin-bundle-prefetch",
|
|
|
|
|
apply: "build",
|
|
|
|
|
configResolved(resolvedConfig: ResolvedConfig) {
|
|
|
|
|
// store the resolved config
|
|
|
|
|
config = resolvedConfig;
|
|
|
|
|
},
|
|
|
|
|
transformIndexHtml(
|
|
|
|
|
html: string,
|
|
|
|
|
ctx: {
|
|
|
|
|
path: string;
|
|
|
|
|
filename: string;
|
|
|
|
|
bundle?: import("rollup").OutputBundle;
|
|
|
|
|
chunk?: import("rollup").OutputChunk;
|
|
|
|
|
}
|
|
|
|
|
) {
|
|
|
|
|
const bundles = Object.keys(ctx.bundle ?? {});
|
|
|
|
|
const isLegacy = bundles.some((bundle) => bundle.includes("legacy"));
|
|
|
|
|
if (isLegacy) {
|
|
|
|
|
//legacy build won't add prefetch
|
|
|
|
|
return html;
|
|
|
|
|
}
|
|
|
|
|
// remove map files
|
|
|
|
|
let modernBundles = bundles.filter(
|
|
|
|
|
(bundle) => bundle.endsWith(".map") === false
|
|
|
|
|
);
|
|
|
|
|
const excludeFn = options?.excludeFn;
|
|
|
|
|
if (excludeFn) {
|
|
|
|
|
modernBundles = modernBundles.filter((bundle) => !excludeFn(bundle));
|
|
|
|
|
}
|
|
|
|
|
// Remove existing files and concatenate them into link tags
|
|
|
|
|
const prefechBundlesString = modernBundles
|
|
|
|
|
.filter((bundle) => html.includes(bundle) === false)
|
|
|
|
|
.map((bundle) => `<link rel="prefetch" href="${config.base}${bundle}">`)
|
|
|
|
|
.join("\n");
|
|
|
|
|
|
|
|
|
|
// Use regular expression to get the content within <head> </head>
|
|
|
|
|
const headContent = html.match(/<head>([\s\S]*)<\/head>/)?.[1] ?? "";
|
|
|
|
|
// Insert the content of prefetch into the head
|
|
|
|
|
const newHeadContent = `${headContent}${prefechBundlesString}`;
|
|
|
|
|
// Replace the original head
|
|
|
|
|
html = html.replace(
|
|
|
|
|
/<head>([\s\S]*)<\/head>/,
|
|
|
|
|
`<head>${newHeadContent}</head>`
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return html;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|