11import { type Plugin } from "vite" ;
22import { resolveValue } from "./utils" ;
33import type { DrupalPluginOptions } from "./types" ;
4+ import { resolve } from "path" ;
5+ import { existsSync } from "fs" ;
46
57export type { DrupalDecoupledConfig } from "./types" ;
68
79const 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 */
7615export 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 `
13497import { drupalAuthClient } from "drupal-auth-client";
13598import { Client, fetchExchange } from "@urql/core";
136- import customConfig from "./drupal-decoupled.config.ts"
99+ import { default as config } from "${ resolvedVirtualConfigModuleId } ";
137100
138101export 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 } ,
0 commit comments