@vueless/nuxt
Version:
Nuxt Styleless UI Component Library, powered by Tailwind CSS.
98 lines (95 loc) • 3.42 kB
JavaScript
import path from 'node:path';
import { cwd } from 'node:process';
import fs from 'node:fs';
import { defineNuxtModule, createResolver, hasNuxtModule, addPlugin, addComponent, addImportsDir } from '@nuxt/kit';
import { TailwindCSS, Vueless } from 'vueless/plugin-vite.js';
import { cacheMergedConfigs } from 'vueless/utils/node/helper.js';
import { NUXT_MODULE_ENV, VUELESS_PACKAGE_DIR, COMPONENTS, VUELESS_CONFIG_FILE_NAME } from 'vueless/constants.js';
const module = defineNuxtModule({
meta: {
name: "@vueless/nuxt",
configKey: "vueless",
compatibility: {
nuxt: ">=3.13.0"
}
},
defaults: {
include: [],
mirrorCacheDir: "",
debug: false
},
async setup(_options, _nuxt) {
const { include, mirrorCacheDir, debug } = _options;
const { resolve } = createResolver(import.meta.url);
const { vuelessConfig, dependencies } = await getVuelessConfig();
_nuxt.options.runtimeConfig.public.vueless = vuelessConfig;
_nuxt.options.build.transpile.push("vueless");
if (hasNuxtModule("@nuxtjs/i18n")) {
_nuxt.hook("i18n:registerModule", (register) => {
register({
langDir: resolve("../node_modules/vueless/locales"),
locales: [
{ code: "en", name: "English", file: "en.json" }
]
});
});
}
_nuxt.hook("vite:extendConfig", async (config) => {
config.plugins = config.plugins || [];
config.plugins.push(
TailwindCSS(),
Vueless({ env: NUXT_MODULE_ENV, mirrorCacheDir, debug, include })
);
});
if (_nuxt.options.dev) {
const chokidarPath = require.resolve("chokidar");
const chokidar = await import(chokidarPath);
const watcher = chokidar.watch(dependencies, { ignoreInitial: true });
watcher.on("change", async () => {
const { dependencies: newDependencies } = await getVuelessConfig();
watcher.unwatch(dependencies);
watcher.add(newDependencies);
_nuxt.callHook("restart");
});
}
await cacheMergedConfigs(VUELESS_PACKAGE_DIR);
addPlugin(resolve("./runtime/plugin"));
for (const componentName in COMPONENTS) {
addComponent({
name: componentName,
filePath: `vueless/${COMPONENTS[componentName]}/${componentName}.vue`
});
}
addImportsDir("vueless/composables");
addImportsDir("vueless/utils");
}
});
async function getVuelessConfig() {
const esbuildPath = require.resolve("esbuild");
const esbuild = await import(esbuildPath);
const esbuildConfig = {
bundle: true,
platform: "node",
format: "esm",
target: "ESNext",
loader: { ".ts": "ts" },
write: false,
metafile: true
// Generate dependency tree
};
const configPathJs = path.join(cwd(), `${VUELESS_CONFIG_FILE_NAME}.js`);
const configPathTs = path.join(cwd(), `${VUELESS_CONFIG_FILE_NAME}.ts`);
let result = null;
if (fs.existsSync(configPathJs)) {
result = await esbuild.build({ ...esbuildConfig, entryPoints: [configPathJs] });
}
if (fs.existsSync(configPathTs)) {
result = await esbuild.build({ ...esbuildConfig, entryPoints: [configPathTs] });
}
const code = result?.outputFiles?.[0]?.text || "";
return {
vuelessConfig: (await import(`data:text/javascript,${encodeURIComponent(code)}`)).default || {},
dependencies: Object.keys(result?.metafile?.inputs || {})
};
}
export { module as default };