@ptdgrp/typedgql 1.0.0-beta.14 → 1.0.0-beta.16
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 +9 -2
- package/README.zh-CN.md +7 -0
- package/dist/index.cjs +61 -36
- package/dist/index.d.cts +41 -7
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +41 -7
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +61 -36
- package/dist/index.mjs.map +1 -1
- package/dist/node.cjs +1 -1
- package/dist/node.mjs +1 -1
- package/dist/{schema-loader-DO6j6Gbm.cjs → schema-loader-BIP51DZO.cjs} +2 -2
- package/dist/{schema-loader-dqwha5aR.mjs → schema-loader-CsabdnfI.mjs} +3 -3
- package/dist/{schema-loader-dqwha5aR.mjs.map → schema-loader-CsabdnfI.mjs.map} +1 -1
- package/dist/vite.cjs +26 -36
- package/dist/vite.d.cts +0 -16
- package/dist/vite.d.cts.map +1 -1
- package/dist/vite.d.mts +0 -16
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +26 -36
- package/dist/vite.mjs.map +1 -1
- package/package.json +1 -1
package/dist/vite.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_schema_loader = require('./schema-loader-
|
|
2
|
+
const require_schema_loader = require('./schema-loader-BIP51DZO.cjs');
|
|
3
3
|
let node_fs_promises = require("node:fs/promises");
|
|
4
4
|
let node_crypto = require("node:crypto");
|
|
5
5
|
let node_path = require("node:path");
|
|
@@ -9,30 +9,25 @@ let vite = require("vite");
|
|
|
9
9
|
/**
|
|
10
10
|
* Vite plugin for @ptdgrp/typedgql — runs codegen automatically.
|
|
11
11
|
*/
|
|
12
|
-
|
|
12
|
+
const isRemote = (schema) => {
|
|
13
13
|
return /^https?:\/\//.test(schema);
|
|
14
|
-
}
|
|
15
|
-
|
|
14
|
+
};
|
|
15
|
+
const makeSchemaLoader = (schema, headers) => {
|
|
16
16
|
return isRemote(schema) ? () => require_schema_loader.loadRemoteSchema(schema, headers) : () => require_schema_loader.loadLocalSchema(schema);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
if (!option) return void 0;
|
|
20
|
-
if (option === true) return { strategy: "reload" };
|
|
21
|
-
return { strategy: option.strategy ?? "reload" };
|
|
22
|
-
}
|
|
23
|
-
function buildNegatedWatchPattern(packageName) {
|
|
17
|
+
};
|
|
18
|
+
const buildNegatedWatchPattern = (packageName) => {
|
|
24
19
|
return `!**/node_modules/${packageName}/**`;
|
|
25
|
-
}
|
|
20
|
+
};
|
|
26
21
|
const DEV_DEP_HMR_PACKAGE_NAME = "@ptdgrp/typedgql";
|
|
27
22
|
const DEV_DEP_HMR_WATCH_DIRS = ["dist"];
|
|
28
|
-
async
|
|
23
|
+
const hashFile = async (path) => {
|
|
29
24
|
try {
|
|
30
25
|
const content = await (0, node_fs_promises.readFile)(path);
|
|
31
26
|
return (0, node_crypto.createHash)("sha256").update(content).digest("hex");
|
|
32
27
|
} catch {
|
|
33
28
|
return;
|
|
34
29
|
}
|
|
35
|
-
}
|
|
30
|
+
};
|
|
36
31
|
/**
|
|
37
32
|
* Vite plugin that runs typedgql codegen automatically.
|
|
38
33
|
*
|
|
@@ -55,9 +50,8 @@ async function hashFile(path) {
|
|
|
55
50
|
* ```
|
|
56
51
|
*/
|
|
57
52
|
function typedgql(options) {
|
|
58
|
-
const { schema, schemaHeaders,
|
|
53
|
+
const { schema, schemaHeaders, ...generatorOptions } = options;
|
|
59
54
|
const remote = isRemote(schema);
|
|
60
|
-
const depHmr = resolveDevDependencyHmr(devDependencyHmr);
|
|
61
55
|
const codegenOptions = {
|
|
62
56
|
...generatorOptions,
|
|
63
57
|
schemaLoader: makeSchemaLoader(schema, schemaHeaders)
|
|
@@ -83,10 +77,9 @@ function typedgql(options) {
|
|
|
83
77
|
return {
|
|
84
78
|
name: "vite-plugin-typedgql",
|
|
85
79
|
config(config) {
|
|
86
|
-
if (!depHmr) return;
|
|
87
|
-
const negatedPattern = buildNegatedWatchPattern(DEV_DEP_HMR_PACKAGE_NAME);
|
|
88
80
|
const exclude = new Set(config.optimizeDeps?.exclude ?? []);
|
|
89
81
|
exclude.add(DEV_DEP_HMR_PACKAGE_NAME);
|
|
82
|
+
const negatedPattern = buildNegatedWatchPattern(DEV_DEP_HMR_PACKAGE_NAME);
|
|
90
83
|
const watchIgnored = config.server?.watch?.ignored;
|
|
91
84
|
const mergedIgnored = Array.isArray(watchIgnored) ? [...watchIgnored, negatedPattern] : watchIgnored === void 0 ? [negatedPattern] : watchIgnored;
|
|
92
85
|
return {
|
|
@@ -102,23 +95,20 @@ function typedgql(options) {
|
|
|
102
95
|
await runCodegen(resolvedConfig?.command === "build" ? "build" : "start");
|
|
103
96
|
},
|
|
104
97
|
configureServer(server) {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
}
|
|
98
|
+
const packageRoot = (0, vite.normalizePath)((0, node_path.resolve)(server.config.root, "node_modules", DEV_DEP_HMR_PACKAGE_NAME));
|
|
99
|
+
const watchedDirs = DEV_DEP_HMR_WATCH_DIRS.map((dir) => (0, vite.normalizePath)((0, node_path.resolve)(packageRoot, dir)));
|
|
100
|
+
for (const dir of watchedDirs) server.watcher.add(dir);
|
|
101
|
+
server.watcher.on("change", async (file) => {
|
|
102
|
+
const changedPath = (0, vite.normalizePath)(file);
|
|
103
|
+
if (!watchedDirs.some((dir) => changedPath.startsWith(dir))) return;
|
|
104
|
+
if (isDependencyRefreshRunning) return;
|
|
105
|
+
isDependencyRefreshRunning = true;
|
|
106
|
+
try {
|
|
107
|
+
await server.restart(true);
|
|
108
|
+
} finally {
|
|
109
|
+
isDependencyRefreshRunning = false;
|
|
110
|
+
}
|
|
111
|
+
});
|
|
122
112
|
if (remote) return;
|
|
123
113
|
const root = server.config.root;
|
|
124
114
|
const schemaPath = (0, vite.normalizePath)((0, node_path.resolve)(root, schema));
|
|
@@ -138,7 +128,7 @@ function typedgql(options) {
|
|
|
138
128
|
if (nextHash && nextHash === lastSchemaHash) return;
|
|
139
129
|
await runCodegen("watch");
|
|
140
130
|
if (nextHash) lastSchemaHash = nextHash;
|
|
141
|
-
server.
|
|
131
|
+
await server.restart(true);
|
|
142
132
|
});
|
|
143
133
|
}
|
|
144
134
|
};
|
package/dist/vite.d.cts
CHANGED
|
@@ -2,17 +2,6 @@ import { t as CodegenOptions } from "./options-CgrZ2der.cjs";
|
|
|
2
2
|
import { Plugin } from "vite";
|
|
3
3
|
|
|
4
4
|
//#region src/vite-plugin.d.ts
|
|
5
|
-
interface DevDependencyHmrOptions {
|
|
6
|
-
/**
|
|
7
|
-
* How to refresh dev server when watched dependency files change.
|
|
8
|
-
*
|
|
9
|
-
* - "reload": send full page reload
|
|
10
|
-
* - "restart": restart Vite dev server
|
|
11
|
-
*
|
|
12
|
-
* @default "reload"
|
|
13
|
-
*/
|
|
14
|
-
strategy?: "reload" | "restart";
|
|
15
|
-
}
|
|
16
5
|
interface TypedGqlPluginOptions extends Omit<CodegenOptions, "schemaLoader"> {
|
|
17
6
|
/**
|
|
18
7
|
* GraphQL schema source.
|
|
@@ -28,11 +17,6 @@ interface TypedGqlPluginOptions extends Omit<CodegenOptions, "schemaLoader"> {
|
|
|
28
17
|
* Only used when `schema` is a URL.
|
|
29
18
|
*/
|
|
30
19
|
schemaHeaders?: Record<string, string>;
|
|
31
|
-
/**
|
|
32
|
-
* Optional dev-time watcher for dependency files in node_modules.
|
|
33
|
-
* Useful when developing typedgql as an installed package rather than workspace source.
|
|
34
|
-
*/
|
|
35
|
-
devDependencyHmr?: boolean | DevDependencyHmrOptions;
|
|
36
20
|
}
|
|
37
21
|
/**
|
|
38
22
|
* Vite plugin that runs typedgql codegen automatically.
|
package/dist/vite.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vite.d.cts","names":[],"sources":["../src/vite-plugin.ts"],"mappings":";;;;UAciB,
|
|
1
|
+
{"version":3,"file":"vite.d.cts","names":[],"sources":["../src/vite-plugin.ts"],"mappings":";;;;UAciB,qBAAA,SAA8B,IAAA,CAC7C,cAAA;;;;;;;;;EAWA,MAAA;EAKA;;;;EAAA,aAAA,GAAgB,MAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;iBAqDF,QAAA,CAAS,OAAA,EAAS,qBAAA,GAAwB,MAAA"}
|
package/dist/vite.d.mts
CHANGED
|
@@ -2,17 +2,6 @@ import { t as CodegenOptions } from "./options-BWcHrC-1.mjs";
|
|
|
2
2
|
import { Plugin } from "vite";
|
|
3
3
|
|
|
4
4
|
//#region src/vite-plugin.d.ts
|
|
5
|
-
interface DevDependencyHmrOptions {
|
|
6
|
-
/**
|
|
7
|
-
* How to refresh dev server when watched dependency files change.
|
|
8
|
-
*
|
|
9
|
-
* - "reload": send full page reload
|
|
10
|
-
* - "restart": restart Vite dev server
|
|
11
|
-
*
|
|
12
|
-
* @default "reload"
|
|
13
|
-
*/
|
|
14
|
-
strategy?: "reload" | "restart";
|
|
15
|
-
}
|
|
16
5
|
interface TypedGqlPluginOptions extends Omit<CodegenOptions, "schemaLoader"> {
|
|
17
6
|
/**
|
|
18
7
|
* GraphQL schema source.
|
|
@@ -28,11 +17,6 @@ interface TypedGqlPluginOptions extends Omit<CodegenOptions, "schemaLoader"> {
|
|
|
28
17
|
* Only used when `schema` is a URL.
|
|
29
18
|
*/
|
|
30
19
|
schemaHeaders?: Record<string, string>;
|
|
31
|
-
/**
|
|
32
|
-
* Optional dev-time watcher for dependency files in node_modules.
|
|
33
|
-
* Useful when developing typedgql as an installed package rather than workspace source.
|
|
34
|
-
*/
|
|
35
|
-
devDependencyHmr?: boolean | DevDependencyHmrOptions;
|
|
36
20
|
}
|
|
37
21
|
/**
|
|
38
22
|
* Vite plugin that runs typedgql codegen automatically.
|
package/dist/vite.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vite.d.mts","names":[],"sources":["../src/vite-plugin.ts"],"mappings":";;;;UAciB,
|
|
1
|
+
{"version":3,"file":"vite.d.mts","names":[],"sources":["../src/vite-plugin.ts"],"mappings":";;;;UAciB,qBAAA,SAA8B,IAAA,CAC7C,cAAA;;;;;;;;;EAWA,MAAA;EAKA;;;;EAAA,aAAA,GAAgB,MAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;iBAqDF,QAAA,CAAS,OAAA,EAAS,qBAAA,GAAwB,MAAA"}
|
package/dist/vite.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as loadRemoteSchema, r as Generator, t as loadLocalSchema } from "./schema-loader-
|
|
1
|
+
import { n as loadRemoteSchema, r as Generator, t as loadLocalSchema } from "./schema-loader-CsabdnfI.mjs";
|
|
2
2
|
import { readFile, realpath } from "node:fs/promises";
|
|
3
3
|
import { createHash } from "node:crypto";
|
|
4
4
|
import { resolve } from "node:path";
|
|
@@ -8,30 +8,25 @@ import { normalizePath } from "vite";
|
|
|
8
8
|
/**
|
|
9
9
|
* Vite plugin for @ptdgrp/typedgql — runs codegen automatically.
|
|
10
10
|
*/
|
|
11
|
-
|
|
11
|
+
const isRemote = (schema) => {
|
|
12
12
|
return /^https?:\/\//.test(schema);
|
|
13
|
-
}
|
|
14
|
-
|
|
13
|
+
};
|
|
14
|
+
const makeSchemaLoader = (schema, headers) => {
|
|
15
15
|
return isRemote(schema) ? () => loadRemoteSchema(schema, headers) : () => loadLocalSchema(schema);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
if (!option) return void 0;
|
|
19
|
-
if (option === true) return { strategy: "reload" };
|
|
20
|
-
return { strategy: option.strategy ?? "reload" };
|
|
21
|
-
}
|
|
22
|
-
function buildNegatedWatchPattern(packageName) {
|
|
16
|
+
};
|
|
17
|
+
const buildNegatedWatchPattern = (packageName) => {
|
|
23
18
|
return `!**/node_modules/${packageName}/**`;
|
|
24
|
-
}
|
|
19
|
+
};
|
|
25
20
|
const DEV_DEP_HMR_PACKAGE_NAME = "@ptdgrp/typedgql";
|
|
26
21
|
const DEV_DEP_HMR_WATCH_DIRS = ["dist"];
|
|
27
|
-
async
|
|
22
|
+
const hashFile = async (path) => {
|
|
28
23
|
try {
|
|
29
24
|
const content = await readFile(path);
|
|
30
25
|
return createHash("sha256").update(content).digest("hex");
|
|
31
26
|
} catch {
|
|
32
27
|
return;
|
|
33
28
|
}
|
|
34
|
-
}
|
|
29
|
+
};
|
|
35
30
|
/**
|
|
36
31
|
* Vite plugin that runs typedgql codegen automatically.
|
|
37
32
|
*
|
|
@@ -54,9 +49,8 @@ async function hashFile(path) {
|
|
|
54
49
|
* ```
|
|
55
50
|
*/
|
|
56
51
|
function typedgql(options) {
|
|
57
|
-
const { schema, schemaHeaders,
|
|
52
|
+
const { schema, schemaHeaders, ...generatorOptions } = options;
|
|
58
53
|
const remote = isRemote(schema);
|
|
59
|
-
const depHmr = resolveDevDependencyHmr(devDependencyHmr);
|
|
60
54
|
const codegenOptions = {
|
|
61
55
|
...generatorOptions,
|
|
62
56
|
schemaLoader: makeSchemaLoader(schema, schemaHeaders)
|
|
@@ -82,10 +76,9 @@ function typedgql(options) {
|
|
|
82
76
|
return {
|
|
83
77
|
name: "vite-plugin-typedgql",
|
|
84
78
|
config(config) {
|
|
85
|
-
if (!depHmr) return;
|
|
86
|
-
const negatedPattern = buildNegatedWatchPattern(DEV_DEP_HMR_PACKAGE_NAME);
|
|
87
79
|
const exclude = new Set(config.optimizeDeps?.exclude ?? []);
|
|
88
80
|
exclude.add(DEV_DEP_HMR_PACKAGE_NAME);
|
|
81
|
+
const negatedPattern = buildNegatedWatchPattern(DEV_DEP_HMR_PACKAGE_NAME);
|
|
89
82
|
const watchIgnored = config.server?.watch?.ignored;
|
|
90
83
|
const mergedIgnored = Array.isArray(watchIgnored) ? [...watchIgnored, negatedPattern] : watchIgnored === void 0 ? [negatedPattern] : watchIgnored;
|
|
91
84
|
return {
|
|
@@ -101,23 +94,20 @@ function typedgql(options) {
|
|
|
101
94
|
await runCodegen(resolvedConfig?.command === "build" ? "build" : "start");
|
|
102
95
|
},
|
|
103
96
|
configureServer(server) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
}
|
|
97
|
+
const packageRoot = normalizePath(resolve(server.config.root, "node_modules", DEV_DEP_HMR_PACKAGE_NAME));
|
|
98
|
+
const watchedDirs = DEV_DEP_HMR_WATCH_DIRS.map((dir) => normalizePath(resolve(packageRoot, dir)));
|
|
99
|
+
for (const dir of watchedDirs) server.watcher.add(dir);
|
|
100
|
+
server.watcher.on("change", async (file) => {
|
|
101
|
+
const changedPath = normalizePath(file);
|
|
102
|
+
if (!watchedDirs.some((dir) => changedPath.startsWith(dir))) return;
|
|
103
|
+
if (isDependencyRefreshRunning) return;
|
|
104
|
+
isDependencyRefreshRunning = true;
|
|
105
|
+
try {
|
|
106
|
+
await server.restart(true);
|
|
107
|
+
} finally {
|
|
108
|
+
isDependencyRefreshRunning = false;
|
|
109
|
+
}
|
|
110
|
+
});
|
|
121
111
|
if (remote) return;
|
|
122
112
|
const root = server.config.root;
|
|
123
113
|
const schemaPath = normalizePath(resolve(root, schema));
|
|
@@ -137,7 +127,7 @@ function typedgql(options) {
|
|
|
137
127
|
if (nextHash && nextHash === lastSchemaHash) return;
|
|
138
128
|
await runCodegen("watch");
|
|
139
129
|
if (nextHash) lastSchemaHash = nextHash;
|
|
140
|
-
server.
|
|
130
|
+
await server.restart(true);
|
|
141
131
|
});
|
|
142
132
|
}
|
|
143
133
|
};
|
package/dist/vite.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vite.mjs","names":[],"sources":["../src/vite-plugin.ts"],"sourcesContent":["/**\n * Vite plugin for @ptdgrp/typedgql — runs codegen automatically.\n */\n\nimport { realpath } from \"node:fs/promises\";\nimport { createHash } from \"node:crypto\";\nimport { readFile } from \"node:fs/promises\";\nimport { resolve } from \"node:path\";\nimport type { Plugin, ResolvedConfig, Logger, UserConfig } from \"vite\";\nimport { normalizePath } from \"vite\";\nimport { Generator } from \"./codegen/generator\";\nimport { loadLocalSchema, loadRemoteSchema } from \"./codegen/schema-loader\";\nimport type { CodegenOptions } from \"./codegen/options\";\n\nexport interface DevDependencyHmrOptions {\n /**\n * How to refresh dev server when watched dependency files change.\n *\n * - \"reload\": send full page reload\n * - \"restart\": restart Vite dev server\n *\n * @default \"reload\"\n */\n strategy?: \"reload\" | \"restart\";\n}\n\nexport interface TypedGqlPluginOptions extends Omit<\n CodegenOptions,\n \"schemaLoader\"\n> {\n /**\n * GraphQL schema source.\n * - Local file path: `\"./schema.graphql\"`\n * - Remote endpoint: `\"http://localhost:4000/graphql\"`\n *\n * Local file → codegen runs on startup, then re-runs on every file change.\n * Remote URL → codegen runs on every `vite dev` / `vite build` invocation.\n */\n schema: string;\n /**\n * HTTP headers forwarded when fetching a remote schema.\n * Only used when `schema` is a URL.\n */\n schemaHeaders?: Record<string, string>;\n /**\n * Optional dev-time watcher for dependency files in node_modules.\n * Useful when developing typedgql as an installed package rather than workspace source.\n */\n devDependencyHmr?: boolean | DevDependencyHmrOptions;\n}\n\nfunction isRemote(schema: string): boolean {\n return /^https?:\\/\\//.test(schema);\n}\n\nfunction makeSchemaLoader(\n schema: string,\n headers?: Record<string, string>,\n): () => Promise<import(\"graphql\").GraphQLSchema> {\n return isRemote(schema)\n ? () => loadRemoteSchema(schema, headers)\n : () => loadLocalSchema(schema);\n}\n\nfunction resolveDevDependencyHmr(\n option: boolean | DevDependencyHmrOptions | undefined,\n): { strategy: \"reload\" | \"restart\" } | undefined {\n if (!option) return undefined;\n if (option === true) {\n return { strategy: \"reload\" };\n }\n return { strategy: option.strategy ?? \"reload\" };\n}\n\nfunction buildNegatedWatchPattern(packageName: string): string {\n return `!**/node_modules/${packageName}/**`;\n}\n\nconst DEV_DEP_HMR_PACKAGE_NAME = \"@ptdgrp/typedgql\";\nconst DEV_DEP_HMR_WATCH_DIRS = [\"dist\"] as const;\n\nasync function hashFile(path: string): Promise<string | undefined> {\n try {\n const content = await readFile(path);\n return createHash(\"sha256\").update(content).digest(\"hex\");\n } catch {\n return undefined;\n }\n}\n\n/**\n * Vite plugin that runs typedgql codegen automatically.\n *\n * - **Local schema** — runs once on startup, then watches for file changes.\n * - **Remote schema** — runs on every `vite dev` / `vite build` invocation.\n *\n * @example\n * ```ts\n * // vite.config.ts\n * import { typedgql } from \"@ptdgrp/typedgql/vite\";\n *\n * export default defineConfig({\n * plugins: [\n * // local file\n * typedgql({ schema: \"./schema.graphql\" }),\n * // or remote endpoint\n * typedgql({ schema: \"http://localhost:4000/graphql\" }),\n * ],\n * });\n * ```\n */\nexport function typedgql(options: TypedGqlPluginOptions): Plugin {\n const { schema, schemaHeaders, devDependencyHmr, ...generatorOptions } = options;\n const remote = isRemote(schema);\n const depHmr = resolveDevDependencyHmr(devDependencyHmr);\n\n const codegenOptions: CodegenOptions = {\n ...generatorOptions,\n schemaLoader: makeSchemaLoader(schema, schemaHeaders),\n };\n\n let isRunning = false;\n let isDependencyRefreshRunning = false;\n let lastSchemaHash: string | undefined;\n let initSchemaHashPromise: Promise<void> | undefined;\n let resolvedConfig: ResolvedConfig;\n let logger: Logger;\n\n async function runCodegen(trigger: string) {\n if (isRunning) return;\n isRunning = true;\n const label = `\\x1b[36m[typedgql:${trigger}]\\x1b[0m`;\n try {\n // logger.info(`${label} running codegen (schema: ${schema})`);\n const generator = new Generator(codegenOptions);\n await generator.generate();\n // logger.info(`${label} done`);\n } catch (err) {\n logger.error(`${label} failed: ${err}`);\n } finally {\n isRunning = false;\n }\n }\n\n return {\n name: \"vite-plugin-typedgql\",\n\n config(config): UserConfig | void {\n if (!depHmr) return;\n const negatedPattern = buildNegatedWatchPattern(DEV_DEP_HMR_PACKAGE_NAME);\n const exclude = new Set(config.optimizeDeps?.exclude ?? []);\n exclude.add(DEV_DEP_HMR_PACKAGE_NAME);\n const watchIgnored = config.server?.watch?.ignored;\n const mergedIgnored = Array.isArray(watchIgnored)\n ? [...watchIgnored, negatedPattern]\n : watchIgnored === undefined\n ? [negatedPattern]\n : watchIgnored;\n return {\n optimizeDeps: {\n exclude: Array.from(exclude),\n },\n server: {\n watch: {\n ignored: mergedIgnored,\n },\n },\n };\n },\n\n configResolved(cfg) {\n resolvedConfig = cfg;\n logger = cfg.logger;\n },\n\n /**\n * Runs on every `vite build` and on `vite dev` startup.\n * For remote schemas this is the only trigger.\n * For local schemas this handles the initial run; the watcher handles subsequent ones.\n */\n async buildStart() {\n const trigger = resolvedConfig?.command === \"build\" ? \"build\" : \"start\";\n await runCodegen(trigger);\n },\n\n /**\n * Dev-only: watch the local schema file and re-run on change.\n * Not registered for remote schemas — those re-run on the next dev/build start.\n */\n configureServer(server) {\n if (depHmr) {\n const packageRoot = normalizePath(\n resolve(server.config.root, \"node_modules\", DEV_DEP_HMR_PACKAGE_NAME),\n );\n const watchedDirs = DEV_DEP_HMR_WATCH_DIRS.map((dir) =>\n normalizePath(resolve(packageRoot, dir)),\n );\n for (const dir of watchedDirs) {\n server.watcher.add(dir);\n }\n server.watcher.on(\"change\", async (file) => {\n const changedPath = normalizePath(file);\n if (!watchedDirs.some((dir) => changedPath.startsWith(dir))) return;\n if (isDependencyRefreshRunning) return;\n isDependencyRefreshRunning = true;\n try {\n if (depHmr.strategy === \"restart\") {\n await server.restart();\n } else {\n server.ws.send({ type: \"full-reload\" });\n }\n } finally {\n isDependencyRefreshRunning = false;\n }\n });\n }\n\n if (remote) return;\n\n const root = server.config.root;\n const schemaPath = normalizePath(resolve(root, schema));\n let realSchemaPath = schemaPath;\n void realpath(schemaPath)\n .then((actualPath) => {\n realSchemaPath = normalizePath(actualPath);\n })\n .catch(() => {\n // Ignore when schema path is not resolvable yet.\n });\n\n server.watcher.add(schemaPath);\n initSchemaHashPromise = hashFile(schemaPath).then((hash) => {\n lastSchemaHash = hash;\n });\n server.watcher.on(\"change\", async (file) => {\n const changedPath = normalizePath(file);\n if (changedPath !== schemaPath && changedPath !== realSchemaPath) return;\n await initSchemaHashPromise;\n const nextHash = await hashFile(schemaPath);\n if (nextHash && nextHash === lastSchemaHash) return;\n await runCodegen(\"watch\");\n if (nextHash) {\n lastSchemaHash = nextHash;\n }\n server.ws.send({ type: \"full-reload\" });\n });\n },\n };\n}\n"],"mappings":";;;;;;;;;;AAmDA,SAAS,SAAS,QAAyB;AACzC,QAAO,eAAe,KAAK,OAAO;;AAGpC,SAAS,iBACP,QACA,SACgD;AAChD,QAAO,SAAS,OAAO,SACb,iBAAiB,QAAQ,QAAQ,SACjC,gBAAgB,OAAO;;AAGnC,SAAS,wBACP,QACgD;AAChD,KAAI,CAAC,OAAQ,QAAO;AACpB,KAAI,WAAW,KACb,QAAO,EAAE,UAAU,UAAU;AAE/B,QAAO,EAAE,UAAU,OAAO,YAAY,UAAU;;AAGlD,SAAS,yBAAyB,aAA6B;AAC7D,QAAO,oBAAoB,YAAY;;AAGzC,MAAM,2BAA2B;AACjC,MAAM,yBAAyB,CAAC,OAAO;AAEvC,eAAe,SAAS,MAA2C;AACjE,KAAI;EACF,MAAM,UAAU,MAAM,SAAS,KAAK;AACpC,SAAO,WAAW,SAAS,CAAC,OAAO,QAAQ,CAAC,OAAO,MAAM;SACnD;AACN;;;;;;;;;;;;;;;;;;;;;;;;AAyBJ,SAAgB,SAAS,SAAwC;CAC/D,MAAM,EAAE,QAAQ,eAAe,kBAAkB,GAAG,qBAAqB;CACzE,MAAM,SAAS,SAAS,OAAO;CAC/B,MAA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QAAQ,GAAG,UAAU,OAAO,SAAS;KAC1C,MAAM,cAAc,cAAc,KAAK;AACvC,SAAI,CAAC,YAAY,MAAM,QAAQ,YAAY,WAAW,IAAI,CAAC,CAAE;AAC7D,SAAI,2BAA4B;AAChC,kCAA6B;AAC7B,SAAI;AACF,UAAI,OAAO,aAAa,UACtB,OAAM,OAAO,SAAS;UAEtB,QAAO,GAAG,KAAK,EAAE,MAAM,eAAe,CAAC;eAEjC;AACR,mCAA6B;;MAE/B;;AAGJ,OAAI,OAAQ;GAEZ,MAAM,OAAO,OAAO,OAAO;GAC3B,MAAM,aAAa,cAAc,QAAQ,MAAM,OAAO,CAAC;GACvD,IAAI,iBAAiB;AACrB,GAAK,SAAS,WAAW,CACtB,MAAM,eAAe;AACpB,qBAAiB,cAAc,WAAW;KAC1C,CACD,YAAY,GAEX;AAEJ,UAAO,QAAQ,IAAI,WAAW;AAC9B,2BAAwB,SAAS,WAAW,CAAC,MAAM,SAAS;AAC1D,qBAAiB;KACjB;AACF,UAAO,QAAQ,GAAG,UAAU,OAAO,SAAS;IAC1C,MAAM,cAAc,cAAc,KAAK;AACvC,QAAI,gBAAgB,cAAc,gBAAgB,eAAgB;AAClE,UAAM;IACN,MAAM,WAAW,MAAM,SAAS,WAAW;AAC3C,QAAI,YAAY,aAAa,eAAgB;AAC7C,UAAM,WAAW,QAAQ;AACzB,QAAI,SACF,kBAAiB;AAEnB,WAAO,GAAG,KAAK,EAAE,MAAM,eAAe,CAAC;KACvC;;EAEL"}
|
|
1
|
+
{"version":3,"file":"vite.mjs","names":[],"sources":["../src/vite-plugin.ts"],"sourcesContent":["/**\n * Vite plugin for @ptdgrp/typedgql — runs codegen automatically.\n */\n\nimport { realpath } from \"node:fs/promises\";\nimport { createHash } from \"node:crypto\";\nimport { readFile } from \"node:fs/promises\";\nimport { resolve } from \"node:path\";\nimport type { Plugin, ResolvedConfig, Logger, UserConfig } from \"vite\";\nimport { normalizePath } from \"vite\";\nimport { Generator } from \"./codegen/generator\";\nimport { loadLocalSchema, loadRemoteSchema } from \"./codegen/schema-loader\";\nimport type { CodegenOptions } from \"./codegen/options\";\n\nexport interface TypedGqlPluginOptions extends Omit<\n CodegenOptions,\n \"schemaLoader\"\n> {\n /**\n * GraphQL schema source.\n * - Local file path: `\"./schema.graphql\"`\n * - Remote endpoint: `\"http://localhost:4000/graphql\"`\n *\n * Local file → codegen runs on startup, then re-runs on every file change.\n * Remote URL → codegen runs on every `vite dev` / `vite build` invocation.\n */\n schema: string;\n /**\n * HTTP headers forwarded when fetching a remote schema.\n * Only used when `schema` is a URL.\n */\n schemaHeaders?: Record<string, string>;\n}\n\nconst isRemote = (schema: string): boolean => {\n return /^https?:\\/\\//.test(schema);\n};\n\nconst makeSchemaLoader = (\n schema: string,\n headers?: Record<string, string>,\n): (() => Promise<import(\"graphql\").GraphQLSchema>) => {\n return isRemote(schema)\n ? () => loadRemoteSchema(schema, headers)\n : () => loadLocalSchema(schema);\n};\n\nconst buildNegatedWatchPattern = (packageName: string): string => {\n return `!**/node_modules/${packageName}/**`;\n};\n\nconst DEV_DEP_HMR_PACKAGE_NAME = \"@ptdgrp/typedgql\";\nconst DEV_DEP_HMR_WATCH_DIRS = [\"dist\"] as const;\n\nconst hashFile = async (path: string): Promise<string | undefined> => {\n try {\n const content = await readFile(path);\n return createHash(\"sha256\").update(content).digest(\"hex\");\n } catch {\n return undefined;\n }\n};\n\n/**\n * Vite plugin that runs typedgql codegen automatically.\n *\n * - **Local schema** — runs once on startup, then watches for file changes.\n * - **Remote schema** — runs on every `vite dev` / `vite build` invocation.\n *\n * @example\n * ```ts\n * // vite.config.ts\n * import { typedgql } from \"@ptdgrp/typedgql/vite\";\n *\n * export default defineConfig({\n * plugins: [\n * // local file\n * typedgql({ schema: \"./schema.graphql\" }),\n * // or remote endpoint\n * typedgql({ schema: \"http://localhost:4000/graphql\" }),\n * ],\n * });\n * ```\n */\nexport function typedgql(options: TypedGqlPluginOptions): Plugin {\n const { schema, schemaHeaders, ...generatorOptions } = options;\n const remote = isRemote(schema);\n\n const codegenOptions: CodegenOptions = {\n ...generatorOptions,\n schemaLoader: makeSchemaLoader(schema, schemaHeaders),\n };\n\n let isRunning = false;\n let isDependencyRefreshRunning = false;\n let lastSchemaHash: string | undefined;\n let initSchemaHashPromise: Promise<void> | undefined;\n let resolvedConfig: ResolvedConfig;\n let logger: Logger;\n\n async function runCodegen(trigger: string) {\n if (isRunning) return;\n isRunning = true;\n const label = `\\x1b[36m[typedgql:${trigger}]\\x1b[0m`;\n try {\n // logger.info(`${label} running codegen (schema: ${schema})`);\n const generator = new Generator(codegenOptions);\n await generator.generate();\n // logger.info(`${label} done`);\n } catch (err) {\n logger.error(`${label} failed: ${err}`);\n } finally {\n isRunning = false;\n }\n }\n\n return {\n name: \"vite-plugin-typedgql\",\n\n config(config): UserConfig | void {\n const exclude = new Set(config.optimizeDeps?.exclude ?? []);\n exclude.add(DEV_DEP_HMR_PACKAGE_NAME);\n\n const negatedPattern = buildNegatedWatchPattern(DEV_DEP_HMR_PACKAGE_NAME);\n const watchIgnored = config.server?.watch?.ignored;\n const mergedIgnored = Array.isArray(watchIgnored)\n ? [...watchIgnored, negatedPattern]\n : watchIgnored === undefined\n ? [negatedPattern]\n : watchIgnored;\n return {\n optimizeDeps: {\n exclude: Array.from(exclude),\n },\n server: {\n watch: {\n ignored: mergedIgnored,\n },\n },\n };\n },\n\n configResolved(cfg) {\n resolvedConfig = cfg;\n logger = cfg.logger;\n },\n\n /**\n * Runs on every `vite build` and on `vite dev` startup.\n * For remote schemas this is the only trigger.\n * For local schemas this handles the initial run; the watcher handles subsequent ones.\n */\n async buildStart() {\n const trigger = resolvedConfig?.command === \"build\" ? \"build\" : \"start\";\n await runCodegen(trigger);\n },\n\n /**\n * Dev-only: watch the local schema file and re-run on change.\n * Not registered for remote schemas — those re-run on the next dev/build start.\n */\n configureServer(server) {\n const packageRoot = normalizePath(\n resolve(server.config.root, \"node_modules\", DEV_DEP_HMR_PACKAGE_NAME),\n );\n const watchedDirs = DEV_DEP_HMR_WATCH_DIRS.map((dir) =>\n normalizePath(resolve(packageRoot, dir)),\n );\n for (const dir of watchedDirs) {\n server.watcher.add(dir);\n }\n server.watcher.on(\"change\", async (file) => {\n const changedPath = normalizePath(file);\n if (!watchedDirs.some((dir) => changedPath.startsWith(dir))) return;\n if (isDependencyRefreshRunning) return;\n isDependencyRefreshRunning = true;\n try {\n await server.restart(true);\n } finally {\n isDependencyRefreshRunning = false;\n }\n });\n\n if (remote) return;\n\n const root = server.config.root;\n const schemaPath = normalizePath(resolve(root, schema));\n let realSchemaPath = schemaPath;\n void realpath(schemaPath)\n .then((actualPath) => {\n realSchemaPath = normalizePath(actualPath);\n })\n .catch(() => {\n // Ignore when schema path is not resolvable yet.\n });\n\n server.watcher.add(schemaPath);\n initSchemaHashPromise = hashFile(schemaPath).then((hash) => {\n lastSchemaHash = hash;\n });\n server.watcher.on(\"change\", async (file) => {\n const changedPath = normalizePath(file);\n if (changedPath !== schemaPath && changedPath !== realSchemaPath)\n return;\n await initSchemaHashPromise;\n const nextHash = await hashFile(schemaPath);\n if (nextHash && nextHash === lastSchemaHash) return;\n await runCodegen(\"watch\");\n if (nextHash) {\n lastSchemaHash = nextHash;\n }\n await server.restart(true);\n });\n },\n };\n}\n"],"mappings":";;;;;;;;;;AAkCA,MAAM,YAAY,WAA4B;AAC5C,QAAO,eAAe,KAAK,OAAO;;AAGpC,MAAM,oBACJ,QACA,YACqD;AACrD,QAAO,SAAS,OAAO,SACb,iBAAiB,QAAQ,QAAQ,SACjC,gBAAgB,OAAO;;AAGnC,MAAM,4BAA4B,gBAAgC;AAChE,QAAO,oBAAoB,YAAY;;AAGzC,MAAM,2BAA2B;AACjC,MAAM,yBAAyB,CAAC,OAAO;AAEvC,MAAM,WAAW,OAAO,SAA8C;AACpE,KAAI;EACF,MAAM,UAAU,MAAM,SAAS,KAAK;AACpC,SAAO,WAAW,SAAS,CAAC,OAAO,QAAQ,CAAC,OAAO,MAAM;SACnD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