-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvite.config.ts
120 lines (110 loc) · 4.37 KB
/
vite.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { defineConfig, Plugin } from "vite";
import react from "@vitejs/plugin-react";
import svgr from "vite-plugin-svgr";
import fs from "fs";
import path from "path";
export default defineConfig(() => {
return {
server: {
open: true,
proxy: {
// get past the CORS issue. getBloomApiUrl() detects we are running locally and uses "/api" so that we come here.
"/api": {
target: "https://api.bloomlibrary.org",
changeOrigin: true,
secure: true,
rewrite: (path) => path.replace(/^\/api/, ""),
},
"/s3": {
target: "https://s3.amazonaws.com",
changeOrigin: true,
rewrite: (path) => {
return path.replace(/^\/s3/, "");
},
},
},
},
build: {
outDir: "build",
// Before we used vite, assets went into "static", so we're keeping it that way to minimize CD changes.
assetsDir: "static",
},
plugins: [
serveTranslationsPlugin(),
copyTranslationsPlugin(),
// if you import an svg file with this ?react at the end, it will be converted to a React component
svgr({
include: "**/*.svg?react",
}),
react(),
],
};
});
// Copies translation files from src to build
function copyTranslationsPlugin(): Plugin {
// BloomLibrary.org is where the source (English) files are. We don't need them in the build.
// I tried hard to make crowdin not download qaa-x-test but to no avail. I tried using the config file
// and even directly putting --exclude-language on the command line call.
const excludedDirectories = ["BloomLibrary.org", "qaa-x-test"];
return {
name: "copy-translations-plugin",
writeBundle({ dir }) {
const buildDir = dir || path.join(__dirname, "build");
const srcTranslationsDir = path.join(__dirname, "src/translations");
const buildTranslationsDir = path.join(buildDir, "translations");
fs.mkdirSync(buildTranslationsDir, { recursive: true });
fs.readdirSync(srcTranslationsDir, { withFileTypes: true })
.filter(
(entry) =>
// We don't want the files (README.md, etc.), only the language directories.
entry.isDirectory() &&
!excludedDirectories.includes(entry.name)
)
.forEach((language) => {
fs.cpSync(
path.join(srcTranslationsDir, language.name),
path.join(buildTranslationsDir, language.name),
{ recursive: true }
);
});
},
};
}
// Used to serve translation files when running `yarn dev`
function serveTranslationsPlugin(): Plugin {
return {
name: "serve-translations",
configureServer(server) {
server.middlewares.use("/translations", (req, res, next) => {
if (!req.url) {
next();
return;
}
// URL format: /translations/lang/BloomLibrary.org/file.json
const parts = req.url.split("/");
if (parts.length >= 4) {
const lang = parts[1];
const filename = parts[parts.length - 1];
const filePath = path.join(
__dirname,
"src/translations",
lang,
"BloomLibrary.org",
decodeURIComponent(filename)
);
if (fs.existsSync(filePath)) {
const content = fs.readFileSync(filePath, "utf8");
res.setHeader("Content-Type", "application/json");
res.end(content);
return;
} else {
console.error(
`Attempted to serve nonexistent translation file ${filePath}`
);
}
}
next();
});
},
};
}