Skip to content

Commit f6419fc

Browse files
authored
✨ feat: Support config file using a virtual module (#170)
1 parent a84a80e commit f6419fc

File tree

4 files changed

+55
-78
lines changed

4 files changed

+55
-78
lines changed

packages/drupal-vite/config.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
declare module "drupal-vite/user" {
2+
import type { Exchange } from "@urql/core";
3+
4+
const config: {
5+
exchanges?: Exchange[];
6+
}
7+
8+
export default config;
9+
}

packages/drupal-vite/package.json

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "drupal-vite",
33
"author": "Octahedroid <[email protected]>",
4-
"version": "0.0.3",
4+
"version": "0.4.0",
55
"license": "MIT",
66
"description": "Vite plugin for Drupal integration",
77
"module": "build/index.js",
@@ -15,12 +15,18 @@
1515
},
1616
"./client": {
1717
"types": "./build/client.d.ts"
18+
},
19+
"./config": {
20+
"types": "./build/config.d.ts"
1821
}
1922
},
2023
"typesVersions": {
2124
"*": {
2225
"client": [
2326
"./build/client.d.ts"
27+
],
28+
"config": [
29+
"./build/config.d.ts"
2430
]
2531
}
2632
},
@@ -34,13 +40,13 @@
3440
"scripts": {
3541
"build": "bun build ./src/index.ts --outdir ./build --format esm --target node --external wrangler",
3642
"build:types": "tsc --emitDeclarationOnly --declaration --outDir build",
37-
"copy:dts": "cp client.d.ts build/client.d.ts",
43+
"copy:dts": "cp client.d.ts build/client.d.ts && cp config.d.ts build/config.d.ts",
3844
"build:all": "bun run build && bun run build:types && bun run copy:dts",
3945
"prepublishOnly": "bun run build:all",
4046
"yalc:publish": "bun run build:all && yalc publish"
4147
},
4248
"devDependencies": {
43-
"vite": "^6.3.5"
49+
"vite": "^7.1.3"
4450
},
4551
"peerDependencies": {
4652
"typescript": "^5",
@@ -50,7 +56,8 @@
5056
"dependencies": {
5157
"@types/fs-extra": "^11.0.4",
5258
"drupal-auth-client": "^1.0.2",
53-
"fs-extra": "^11.3.0"
59+
"fs-extra": "^11.3.0",
60+
"jiti": "^2.5.1"
5461
},
5562
"keywords": [
5663
"vite-plugin",

packages/drupal-vite/src/index.ts

Lines changed: 34 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,16 @@
11
import { type Plugin } from "vite";
22
import { resolveValue } from "./utils";
33
import type { DrupalPluginOptions } from "./types";
4+
import { resolve } from "path";
5+
import { existsSync } from "fs";
46

57
export type { DrupalDecoupledConfig } from "./types";
68

79
const VIRTUAL_MODULE_ID = "drupal-vite/client";
10+
const VIRTUAL_CONFIG_MODULE_ID = "drupal-vite/config";
811

912
/**
1013
* Creates a Vite plugin for Drupal integration
11-
*
12-
* @description
13-
* This plugin provides seamless integration between Vite and Drupal by:
14-
* - Setting up authentication with Drupal using OAuth
15-
* - Configuring a GraphQL client for Drupal queries
16-
* - Managing environment variables and configuration
17-
* - Exposing helper functions for auth and GraphQL operations
18-
*
19-
* @example
20-
* ```ts
21-
* // vite.config.ts
22-
* import { defineConfig } from 'vite';
23-
* import { drupal } from 'drupal-vite';
24-
*
25-
* export default defineConfig({
26-
* plugins: [
27-
* drupal()
28-
* ]
29-
* });
30-
* ```
31-
*
32-
* @example
33-
* ```ts
34-
* // vite.config.ts with direct configuration
35-
* import { defineConfig } from 'vite';
36-
* import { drupal } from 'drupal-vite';
37-
*
38-
* export default defineConfig({
39-
* plugins: [
40-
* drupal({
41-
* drupalUrl: 'https://your-drupal-site.com',
42-
* simple_oauth: {
43-
* clientID: 'your-client-id',
44-
* clientSecret: 'your-client-secret'
45-
* },
46-
* graphql: {
47-
* endpoint: '/graphql'
48-
* }
49-
* })
50-
* ]
51-
* });
52-
* ```
53-
*
54-
* @example
55-
* ```ts
56-
* // vite.config.ts with environment variable references
57-
* import { defineConfig } from 'vite';
58-
* import { drupal } from 'drupal-vite';
59-
*
60-
* export default defineConfig({
61-
* plugins: [
62-
* drupal({
63-
* drupalUrl: 'DRUPAL_URL', // Will use process.env.DRUPAL_URL
64-
* simple_oauth: {
65-
* clientID: 'DRUPAL_CLIENT_ID', // Will use process.env.DRUPAL_CLIENT_ID
66-
* clientSecret: 'DRUPAL_CLIENT_SECRET' // Will use process.env.DRUPAL_CLIENT_SECRET
67-
* }
68-
* })
69-
* ]
70-
* });
71-
* ```
72-
*
73-
* @param {DrupalPluginOptions} options - Configuration options for the plugin
74-
* @returns {Plugin} A Vite plugin instance
7514
*/
7615
export function drupal(options?: DrupalPluginOptions): Plugin {
7716
const { simple_oauth, graphql, drupalUrl } = options || {};
@@ -80,13 +19,16 @@ export function drupal(options?: DrupalPluginOptions): Plugin {
8019
const { endpoint = "/graphql" } = graphql || {};
8120

8221
const resolvedVirtualModuleId = `\0${VIRTUAL_MODULE_ID}`;
22+
const resolvedVirtualConfigModuleId = `\0${VIRTUAL_CONFIG_MODULE_ID}`;
23+
8324
let resolvedClientID: string | undefined;
8425
let resolvedClientSecret: string | undefined;
8526
let resolvedDrupalUrl: string | undefined;
8627

8728
return {
8829
name: "vite-plugin-drupal-init",
89-
async configResolved(resolvedConfig) {
30+
31+
async configResolved() {
9032
const defaultClientId = "DRUPAL_CLIENT_ID";
9133
const defaultClientSecret = "DRUPAL_CLIENT_SECRET";
9234
const defaultDrupalUrl = "DRUPAL_URL";
@@ -102,20 +44,30 @@ export function drupal(options?: DrupalPluginOptions): Plugin {
10244
console.error(`[drupal-vite] Client ID is not configured.`);
10345
}
10446
if (!resolvedClientSecret) {
105-
console.error(`[drupal-vite] Client Secret is not configured. `);
47+
console.error(`[drupal-vite] Client Secret is not configured.`);
10648
}
10749
if (!resolvedDrupalUrl) {
108-
console.error(`[drupal-vite] Drupal URL is not configured. '.`);
50+
console.error(`[drupal-vite] Drupal URL is not configured.`);
10951
}
11052
},
53+
11154
resolveId(id) {
55+
if (
56+
id === VIRTUAL_CONFIG_MODULE_ID ||
57+
id === resolvedVirtualConfigModuleId ||
58+
id.endsWith(VIRTUAL_CONFIG_MODULE_ID)
59+
) {
60+
return resolvedVirtualConfigModuleId;
61+
}
62+
11263
if (
11364
id === VIRTUAL_MODULE_ID ||
11465
id === resolvedVirtualModuleId ||
11566
id.endsWith(VIRTUAL_MODULE_ID)
11667
) {
11768
return resolvedVirtualModuleId;
11869
}
70+
11971
return null;
12072
},
12173

@@ -129,11 +81,22 @@ export function drupal(options?: DrupalPluginOptions): Plugin {
12981
const sanitizedDrupalUrl = resolvedDrupalUrl.replace(/\/$/, "");
13082
const fullGraphqlEndpoint = `${sanitizedDrupalUrl}${endpoint}`;
13183

132-
if (id === resolvedVirtualModuleId || id.includes(VIRTUAL_MODULE_ID)) {
133-
const module = `
84+
// ---- user config virtual module
85+
if (id === resolvedVirtualConfigModuleId) {
86+
const configPath = resolve(process.cwd(), "drupal-decoupled.config.ts");
87+
if (existsSync(configPath)) {
88+
return `import userConfig from ${JSON.stringify(configPath)};
89+
export default userConfig;`;
90+
}
91+
return `export default {};`;
92+
}
93+
94+
// ---- client virtual module
95+
if (id === resolvedVirtualModuleId) {
96+
return `
13497
import { drupalAuthClient } from "drupal-auth-client";
13598
import { Client, fetchExchange } from "@urql/core";
136-
import customConfig from "./drupal-decoupled.config.ts"
99+
import { default as config } from "${resolvedVirtualConfigModuleId}";
137100
138101
export async function getDrupalAuth() {
139102
return await drupalAuthClient("${sanitizedDrupalUrl}", {
@@ -146,16 +109,14 @@ export async function getDrupalClient() {
146109
const auth = await getDrupalAuth();
147110
return new Client({
148111
url: "${fullGraphqlEndpoint}",
149-
exchanges: customConfig.exchanges ?? [fetchExchange],
112+
exchanges: config.exchanges ? config.exchanges : [fetchExchange],
150113
fetchOptions: {
151114
headers: {
152115
Authorization: \`\${auth.token_type} \${auth.access_token}\`,
153116
},
154117
},
155118
});
156119
}`;
157-
158-
return module;
159120
}
160121
return null;
161122
},

packages/drupal-vite/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@
3030
"noUnusedParameters": false,
3131
"noPropertyAccessFromIndexSignature": false
3232
},
33-
"include": ["src/**/*", "client.d.ts"],
33+
"include": ["src/**/*", "client.d.ts", "config.d.ts"],
3434
"exclude": ["node_modules", "build"]
3535
}

0 commit comments

Comments
 (0)