vite-plugin-postgres-import 0.3.3 → 0.4.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/plugin.js +15 -13
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.3.3",
4
+ "version": "0.4.0",
5
5
  "main": "plugin.js",
6
6
  "type": "module",
7
7
  "author": "SirKostya009",
package/plugin.js CHANGED
@@ -10,23 +10,24 @@ export default function postgres({
10
10
  } = {}) {
11
11
  return {
12
12
  name: "vite-plugin-sql-postgres",
13
- resolveId(id) {
14
- if (id.endsWith(".sql")) {
15
- return id;
16
- }
13
+ transform: {
14
+ filter: {
15
+ id: /\.sql$/,
16
+ },
17
+ handler: transform,
17
18
  },
18
- load,
19
19
  async configureServer(server) {
20
- server.watcher.on("add", load);
20
+ server.watcher.on("add", async (path) => transform(await fs.readFile(path, "utf8"), path));
21
21
 
22
22
  await walkdir(rootFolder);
23
23
 
24
24
  async function walkdir(/** @type {string} */ p) {
25
25
  for await (const entry of await fs.opendir(p)) {
26
+ const entryPath = path.join(entry.parentPath, entry.name);
26
27
  if (entry.isFile()) {
27
- await load(path.relative(rootFolder, path.join(entry.parentPath, entry.name)));
28
+ await transform(await fs.readFile(entryPath, "utf8"), entryPath);
28
29
  } else if (entry.isDirectory()) {
29
- await walkdir(path.join(entry.parentPath, entry.name));
30
+ await walkdir(entryPath);
30
31
  }
31
32
  }
32
33
  }
@@ -36,10 +37,8 @@ export default function postgres({
36
37
  },
37
38
  };
38
39
 
39
- async function load(/** @type {string} */ id) {
40
- if (!id.endsWith(".sql")) return null;
41
- const sql = await fs.readFile(id, "utf8");
42
-
40
+ /** @returns {Promise<import("vite").TransformResult>} */
41
+ async function transform(/** @type {string} */ sql, /** @type {string} */ id) {
43
42
  const filename = path.basename(id, ".sql");
44
43
 
45
44
  const { js, dts } = codegen(parseModule(sql), filename, modulePrefix);
@@ -48,6 +47,9 @@ export default function postgres({
48
47
  await fs.mkdir(dtsFolder, { recursive: true });
49
48
  await fs.writeFile(path.join(dtsFolder, filename + ".sql.d.ts"), dts);
50
49
 
51
- return js;
50
+ return {
51
+ code: js,
52
+ moduleType: "js",
53
+ };
52
54
  }
53
55
  }