drupal-vite
Version:
Vite plugin for Drupal integration
135 lines (130 loc) • 4.67 kB
JavaScript
import { createRequire } from "node:module";
var __create = Object.create;
var __getProtoOf = Object.getPrototypeOf;
var __defProp = Object.defineProperty;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __toESM = (mod, isNodeMode, target) => {
target = mod != null ? __create(__getProtoOf(mod)) : {};
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
for (let key of __getOwnPropNames(mod))
if (!__hasOwnProp.call(to, key))
__defProp(to, key, {
get: () => mod[key],
enumerable: true
});
return to;
};
var __require = /* @__PURE__ */ createRequire(import.meta.url);
// src/utils.ts
async function importWrangler() {
try {
const module = await import("wrangler");
return module;
} catch (e) {
return false;
}
}
var resolveValue = async (configValue, defaultEnvKey) => {
let envAccess = process.env;
const wrangler = await importWrangler();
if (wrangler) {
const { getPlatformProxy } = wrangler;
const platformProxy = await getPlatformProxy();
envAccess = platformProxy.env;
}
if (configValue && envAccess[configValue]) {
return envAccess[configValue];
}
if (configValue) {
return configValue;
}
return envAccess[defaultEnvKey];
};
// src/index.ts
import { resolve } from "path";
import { existsSync } from "fs";
var VIRTUAL_MODULE_ID = "drupal-vite/client";
var VIRTUAL_CONFIG_MODULE_ID = "drupal-vite/config";
function drupal(options) {
const { simple_oauth, graphql, drupalUrl } = options || {};
const { clientID, clientSecret } = simple_oauth || {};
const { endpoint = "/graphql" } = graphql || {};
const resolvedVirtualModuleId = `\x00${VIRTUAL_MODULE_ID}`;
const resolvedVirtualConfigModuleId = `\x00${VIRTUAL_CONFIG_MODULE_ID}`;
let resolvedClientID;
let resolvedClientSecret;
let resolvedDrupalUrl;
return {
name: "vite-plugin-drupal-init",
async configResolved() {
const defaultClientId = "DRUPAL_CLIENT_ID";
const defaultClientSecret = "DRUPAL_CLIENT_SECRET";
const defaultDrupalUrl = "DRUPAL_URL";
resolvedClientID = await resolveValue(clientID, defaultClientId);
resolvedClientSecret = await resolveValue(clientSecret, defaultClientSecret);
resolvedDrupalUrl = await resolveValue(drupalUrl, defaultDrupalUrl);
if (!resolvedClientID) {
console.error(`[drupal-vite] Client ID is not configured.`);
}
if (!resolvedClientSecret) {
console.error(`[drupal-vite] Client Secret is not configured.`);
}
if (!resolvedDrupalUrl) {
console.error(`[drupal-vite] Drupal URL is not configured.`);
}
},
resolveId(id) {
if (id === VIRTUAL_CONFIG_MODULE_ID || id === resolvedVirtualConfigModuleId || id.endsWith(VIRTUAL_CONFIG_MODULE_ID)) {
return resolvedVirtualConfigModuleId;
}
if (id === VIRTUAL_MODULE_ID || id === resolvedVirtualModuleId || id.endsWith(VIRTUAL_MODULE_ID)) {
return resolvedVirtualModuleId;
}
return null;
},
async load(id) {
if (!resolvedClientID || !resolvedClientSecret || !resolvedDrupalUrl) {
this.error("Missing required Drupal configuration. Please check your options or environment variables.");
}
const sanitizedDrupalUrl = resolvedDrupalUrl.replace(/\/$/, "");
const fullGraphqlEndpoint = `${sanitizedDrupalUrl}${endpoint}`;
if (id === resolvedVirtualConfigModuleId) {
const configPath = resolve(process.cwd(), "drupal-decoupled.config.ts");
if (existsSync(configPath)) {
return `import userConfig from ${JSON.stringify(configPath)};
export default userConfig;`;
}
return `export default {};`;
}
if (id === resolvedVirtualModuleId) {
return `
import { drupalAuthClient } from "drupal-auth-client";
import { Client, fetchExchange } from "@urql/core";
import { default as config } from "${resolvedVirtualConfigModuleId}";
export async function getDrupalAuth() {
return await drupalAuthClient("${sanitizedDrupalUrl}", {
clientId: "${resolvedClientID}",
clientSecret: "${resolvedClientSecret}",
});
}
export async function getDrupalClient() {
const auth = await getDrupalAuth();
return new Client({
url: "${fullGraphqlEndpoint}",
exchanges: config.exchanges ? config.exchanges : [fetchExchange],
fetchOptions: {
headers: {
Authorization: \`\${auth.token_type} \${auth.access_token}\`,
},
},
});
}`;
}
return null;
}
};
}
export {
drupal
};