-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
148 lines (137 loc) · 4.53 KB
/
Copy pathplugin.js
File metadata and controls
148 lines (137 loc) · 4.53 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import fs from "node:fs/promises";
import path from "node:path";
import { codegen, encodeMappings, exportMappings, parseModule } from "./parse.js";
/** @returns {import('vite').Plugin} */
export default async function postgres({
typesFolder = "node_modules/@types/vite-plugin-postgres-import/",
rootFolder = process.cwd(),
runtime: { module: runtimeModule = "#db-runtime", export: runtimeExport = "db", type: runtimeType = "object" } = {},
} = {}) {
const runtime = { module: runtimeModule, export: runtimeExport, type: runtimeType };
const imports = await fs
.readFile(path.join(rootFolder, "package.json"), "utf8")
.then((s) => JSON.parse(s).imports)
.catch(() => null);
const moduleDeclarations = new Map();
function* targets(/** @type {unknown} */ value) {
if (typeof value === "string") yield value;
else if (Array.isArray(value)) for (const v of value) yield* targets(v);
else if (value) for (const v of Object.values(value)) yield* targets(v);
}
function moduleName(/** @type {string} */ filename) {
const rel = filename.replaceAll(path.sep, "/");
for (const [key, value] of Object.entries(imports ?? {})) {
for (let target of targets(value)) {
target = target.replace(/^\.\//, "");
const star = target.indexOf("*");
if (star < 0) {
if (target === rel) return key;
continue;
}
const prefix = target.substring(0, star);
const suffix = target.substring(star + 1);
if (rel.length >= prefix.length + suffix.length && rel.startsWith(prefix) && rel.endsWith(suffix)) {
return key.replace("*", rel.substring(prefix.length, rel.length - suffix.length));
}
}
}
}
return {
name: "vite-plugin-sql-postgres",
transform: {
filter: {
id: /\.sql$/,
},
handler: transform,
},
async configureServer(server) {
server.watcher.on("add", async (path) => transform(await fs.readFile(path, "utf8"), path));
await walkdir(rootFolder);
async function walkdir(/** @type {string} */ p) {
for await (const entry of await fs.opendir(p)) {
const entryPath = path.join(entry.parentPath, entry.name);
if (entry.isFile()) {
await transform(await fs.readFile(entryPath, "utf8"), entryPath);
} else if (entry.isDirectory()) {
await walkdir(entryPath);
}
}
}
},
handleHotUpdate({ modules }) {
return modules;
},
};
/** @returns {Promise<import("vite").TransformResult>} */
async function transform(/** @type {string} */ sql, /** @type {string} */ id) {
if (!/\.sql$/.test(id)) {
return;
}
const filename = path.relative(rootFolder, id);
const { js, dts, moduleDeclaration, mappings } = codegen(
parseModule(sql),
filename,
moduleName(filename),
runtime,
);
const dtsFolder = path.join(typesFolder, path.relative(rootFolder, path.dirname(id)));
const dtsName = path.basename(filename, ".sql") + ".sql.d.ts";
await fs.mkdir(dtsFolder, { recursive: true });
await fs.writeFile(path.join(dtsFolder, dtsName), dts + `//# sourceMappingURL=${dtsName}.map\n`);
await fs.writeFile(
path.join(dtsFolder, dtsName + ".map"),
JSON.stringify({
version: 3,
file: dtsName,
sources: [path.relative(dtsFolder, id).replaceAll(path.sep, "/")],
names: [],
mappings: encodeMappings([
{ genLine: 0, genCol: 0, srcLine: 0, srcCol: 0 },
...exportMappings(dts, mappings),
]),
}),
);
if (moduleDeclaration.length) {
moduleDeclarations.set(filename, { text: moduleDeclaration.join("\n"), mappings, id });
let line = 0;
const sources = [];
const combined = [];
const texts = [];
for (const { text, mappings, id } of moduleDeclarations.values()) {
const srcIdx = sources.push(path.relative(typesFolder, id).replaceAll(path.sep, "/")) - 1;
combined.push({ genLine: line, genCol: 0, srcIdx, srcLine: 0, srcCol: 0 });
combined.push(...exportMappings(text, mappings, srcIdx, line));
texts.push(text);
line += text.split("\n").length;
}
await fs.writeFile(
path.join(typesFolder, "modules.sql.d.ts"),
texts.join("\n") + "\n//# sourceMappingURL=modules.sql.d.ts.map\n",
);
await fs.writeFile(
path.join(typesFolder, "modules.sql.d.ts.map"),
JSON.stringify({
version: 3,
file: "modules.sql.d.ts",
sources,
names: [],
mappings: encodeMappings(combined),
}),
);
}
return {
code: js,
map: {
version: 3,
sources: [id],
sourcesContent: [sql],
names: [],
mappings: encodeMappings([
{ genLine: 0, genCol: 0, srcLine: 0, srcCol: 0 },
...exportMappings(js, mappings),
]),
},
moduleType: "js",
};
}
}