From cc19942d3acda00e723ccf413ade8a8e59fd4e50 Mon Sep 17 00:00:00 2001 From: Luckas Date: Sat, 3 Aug 2024 20:01:37 +0300 Subject: [PATCH] fix: `tailwind.config.ts` support & cleanup --- rplugin/node/tailwind-tools/src/index.js | 24 ++++++++++++------------ rplugin/node/tailwind-tools/src/utils.js | 8 ++++++++ 2 files changed, 20 insertions(+), 12 deletions(-) create mode 100644 rplugin/node/tailwind-tools/src/utils.js diff --git a/rplugin/node/tailwind-tools/src/index.js b/rplugin/node/tailwind-tools/src/index.js index 93d25d2..69b3ecb 100644 --- a/rplugin/node/tailwind-tools/src/index.js +++ b/rplugin/node/tailwind-tools/src/index.js @@ -1,5 +1,6 @@ -const fs = require("fs"); -const path = require("path"); +const fs = require("node:fs"); +const path = require("node:path"); +const { getNodeModuleResolver } = require("./utils"); class Plugin { /** @@ -19,21 +20,20 @@ class Plugin { async getRawConfig() { const cwd = await this.nvim.call("getcwd"); - const tailwindPath = path.join(cwd, "node_modules/tailwindcss"); + const tailwindPath = path.join(cwd, "node_modules", "tailwindcss"); if (!fs.existsSync(tailwindPath)) return; - const resolveFnPath = path.join(tailwindPath, "resolveConfig"); - const resolveConfig = require(resolveFnPath); - const projectConfigPath = path.join(cwd, "tailwind.config.js"); + const _require = getNodeModuleResolver(cwd); + const resolveConfig = _require("tailwindcss/resolveConfig"); + const loadConfig = _require("tailwindcss/lib/public/load-config").default; - let projectConfig = {}; + const configExtensions = ["js", "ts", "cjs"]; + const configPath = configExtensions + .map((ext) => path.join(cwd, `tailwind.config.${ext}`)) + .find((filePath) => fs.existsSync(filePath)); - if (fs.existsSync(projectConfigPath)) { - projectConfig = await import(projectConfigPath); - } - - return resolveConfig(projectConfig.default ?? projectConfig); + return resolveConfig(configPath ? loadConfig(configPath) : {}); } } diff --git a/rplugin/node/tailwind-tools/src/utils.js b/rplugin/node/tailwind-tools/src/utils.js new file mode 100644 index 0000000..ddceabb --- /dev/null +++ b/rplugin/node/tailwind-tools/src/utils.js @@ -0,0 +1,8 @@ +const path = require("node:path"); + +const getNodeModuleResolver = (rootDir) => (modulePath) => + require(path.join(rootDir, "node_modules", modulePath)); + +module.exports = { + getNodeModuleResolver, +};