-
Notifications
You must be signed in to change notification settings - Fork 3
/
nuxt-module.ts
44 lines (37 loc) · 1.35 KB
/
nuxt-module.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { addComponentsDir, addImportsDir, addPlugin } from '@nuxt/kit';
import { Nuxt } from '@nuxt/schema';
import fs from 'fs-extra';
import { resolve } from 'pathe';
export const setupNuxtModule = (name: string, nuxt: Nuxt) => {
const dirname = resolve(process.cwd(), 'src/modules', name);
const COMPOSABLES_DIR = resolve(dirname, 'composables');
const COMPONENTS_DIR = resolve(dirname, 'components');
const PLUGINS_DIR = resolve(dirname, 'plugins');
let composablesImported = false;
let componentsImported = false;
const importedPlugins: string[] = [];
const handleAutoImports = () => {
if (fs.existsSync(COMPOSABLES_DIR) && !componentsImported) {
addImportsDir(COMPOSABLES_DIR);
componentsImported = true;
}
if (fs.existsSync(COMPONENTS_DIR) && !composablesImported) {
addComponentsDir({ path: COMPONENTS_DIR, prefix: name });
composablesImported = true;
}
if (fs.existsSync(PLUGINS_DIR)) {
fs.readdirSync(PLUGINS_DIR)
.map(fileName => resolve(PLUGINS_DIR, fileName))
.forEach(plugin => {
if (importedPlugins.includes(plugin)) return;
addPlugin(plugin);
importedPlugins.push(plugin);
});
}
};
nuxt.hook('builder:watch', (_event, path) => {
if (path.startsWith('generated')) return;
handleAutoImports();
});
handleAutoImports();
};