vite-plugin-postgres-import 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -160,10 +160,6 @@ Make sure you include this one in your `tsconfig.json` as `"${typesFolder}/**/*.
160
160
 
161
161
  _default:_ `'node_modules/@types/vite-plugin-postgres-import/'`
162
162
 
163
- ### `modulePrefix`
164
-
165
- **CURRENTLY DISABLED**. This one changes the type declaration generation strategy to generate everything as `declare module` statements.
166
-
167
163
  ### `rootFolder`
168
164
 
169
165
  Root folder relative to which path calculation will be happening. May be useful for some I guess.
@@ -183,6 +179,8 @@ And JavaScript projects still get the benefits of completions.
183
179
 
184
180
  I primarily use this in a SvelteKit project. The only thing I modify is setting `typesFolder` to `'.svelte-kit/types'` directory, and adding a `".svelte-kit/types/**/*.sql.d.ts"` record to my `include` array in `tsconfig.json`.
185
181
 
182
+ Also, when using SvelteKit aliases you can absolutely shove all your sql modules in a `src/sql` directory with a `$sql` alias and have module declarations generated for all of the files! Works automagically out of the box.
183
+
186
184
  ## License
187
185
 
188
186
  MIT.
package/index.d.ts CHANGED
@@ -16,10 +16,6 @@ export default function postgres(opts?: {
16
16
  * @default "node_modules/@types/vite-plugin-postgres-import/"
17
17
  */
18
18
  typesFolder?: string;
19
- /**
20
- * **CURRENTLY DISABLED**. This one changes the type declaration generation strategy to generate everything as `declare module` statements.
21
- */
22
- modulePrefix?: string;
23
19
  /**
24
20
  * Root folder relative to which path calculation will be happening. May be useful for some I guess.
25
21
  *
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vite-plugin-postgres-import",
3
3
  "description": "Vite plugin for importing SQL files as JS modules with sqlc-style annotations",
4
- "version": "0.6.0",
4
+ "version": "0.7.0",
5
5
  "main": "plugin.js",
6
6
  "type": "module",
7
7
  "author": "SirKostya009",
@@ -19,14 +19,20 @@
19
19
  "sqlc"
20
20
  ],
21
21
  "devDependencies": {
22
+ "@sveltejs/kit": "^2",
23
+ "@types/pg": "*",
22
24
  "vite": "^6"
23
25
  },
24
26
  "peerDependencies": {
27
+ "@sveltejs/kit": "^2",
25
28
  "pg": "*",
26
29
  "pg-cursor": "*",
27
30
  "@types/pg": "*"
28
31
  },
29
32
  "peerDependenciesMeta": {
33
+ "@sveltejs/kit": {
34
+ "optional": true
35
+ },
30
36
  "pg-cursor": {
31
37
  "optional": true
32
38
  },
package/parse.js CHANGED
@@ -1,5 +1,7 @@
1
1
  const annotationRegex = /--\s*name:\s*(?<name>[a-zA-Z][\w_]*)\s*(?<tags>(:\w+\s*)*)?\r?\n/g;
2
2
 
3
+ const partial = Symbol();
4
+
3
5
  export function* parseModule(/** @type {string} */ sql) {
4
6
  annotationRegex.lastIndex = 0;
5
7
 
@@ -88,7 +90,7 @@ function metadata(/** @type {string} */ s, /** @type {ReturnType<typeof metadata
88
90
  export function codegen(
89
91
  /** @type {Iterable<ReturnType<typeof metadata> | ReturnType<typeof metadata>[]>} */ modules,
90
92
  /** @type {string} */ filename,
91
- /** @type {string} */ modulePrefix
93
+ /** @type {ReturnType<typeof parseLocalSvelteConfigAliases>} */ aliases
92
94
  ) {
93
95
  let dts = [
94
96
  // `declare module '${modulePrefix}${filename}.sql' {`
@@ -235,5 +237,38 @@ export function ${module.name}<${module.returnSymbols
235
237
  );
236
238
  }
237
239
 
238
- return { js: js.join("\n"), dts: dts.join("\n\n") + "\n" };
240
+ const match = aliases[filename] ?? aliases[partial].find(({ path }) => filename.startsWith(path));
241
+ let moduleDeclaration = [];
242
+ if (match) {
243
+ const mod = "path" in match ? match.alias + filename.replace(match.path, "") : match.alias;
244
+ moduleDeclaration.push(`declare module "${mod}" {`, dts[0], ...dts.slice(1), `}`);
245
+ }
246
+
247
+ return { js: js.join("\n"), dts: dts.join("\n\n") + "\n", moduleDeclaration };
248
+ }
249
+
250
+ /** @returns {{ [partial]: { path: string; alias: string; }[]; [k: string]: { alias: string } }} */
251
+ export async function parseLocalSvelteConfigAliases() {
252
+ /** @type {{ default: import('@sveltejs/kit').Config }} */
253
+ const { default: svelteConfig } = await import(process.cwd() + "/svelte.config.js").catch(() => ({
254
+ default: {},
255
+ }));
256
+
257
+ const aliases = svelteConfig.kit?.alias ?? {};
258
+
259
+ return Object.entries(aliases).reduce(
260
+ (acc, [alias, path]) => {
261
+ if (path.endsWith(".sql")) {
262
+ acc[path] = { alias };
263
+ } else {
264
+ if (path.endsWith("/*")) {
265
+ path = path.substring(0, path.length - 1);
266
+ alias = alias.substring(0, alias.length - 1);
267
+ }
268
+ acc[partial].push({ path, alias });
269
+ }
270
+ return acc;
271
+ },
272
+ { [partial]: [] }
273
+ );
239
274
  }
package/plugin.js CHANGED
@@ -1,13 +1,16 @@
1
1
  import fs from "node:fs/promises";
2
2
  import path from "node:path";
3
- import { codegen, parseModule } from "./parse.js";
3
+ import { codegen, parseLocalSvelteConfigAliases, parseModule } from "./parse.js";
4
4
 
5
5
  /** @returns {import('vite').Plugin} */
6
- export default function postgres({
6
+ export default async function postgres({
7
7
  typesFolder = "node_modules/@types/vite-plugin-postgres-import/",
8
- modulePrefix = "",
9
8
  rootFolder = process.cwd(),
10
9
  } = {}) {
10
+ const aliases = await parseLocalSvelteConfigAliases();
11
+
12
+ const moduleDeclarations = new Map();
13
+
11
14
  return {
12
15
  name: "vite-plugin-sql-postgres",
13
16
  transform: {
@@ -42,13 +45,17 @@ export default function postgres({
42
45
  if (!/\.sql$/.test(id)) {
43
46
  return;
44
47
  }
45
- const filename = path.basename(id, ".sql");
48
+ const filename = path.relative(rootFolder, id);
46
49
 
47
- const { js, dts } = codegen(parseModule(sql), filename, modulePrefix);
50
+ const { js, dts, moduleDeclaration } = codegen(parseModule(sql), filename, aliases);
48
51
 
49
52
  const dtsFolder = path.join(typesFolder, path.relative(rootFolder, path.dirname(id)));
50
53
  await fs.mkdir(dtsFolder, { recursive: true });
51
- await fs.writeFile(path.join(dtsFolder, filename + ".sql.d.ts"), dts);
54
+ await fs.writeFile(path.join(dtsFolder, path.basename(filename, ".sql") + ".sql.d.ts"), dts);
55
+ if (moduleDeclaration.length) {
56
+ moduleDeclarations.set(filename, moduleDeclaration.join("\n"));
57
+ await fs.writeFile(path.join(typesFolder, "modules.sql.d.ts"), [...moduleDeclarations.values()].join("\n"));
58
+ }
52
59
 
53
60
  return {
54
61
  code: js,