Skip to content

Commit

Permalink
chore: изменена генерация типов
Browse files Browse the repository at this point in the history
  • Loading branch information
LorexIQ committed Dec 2, 2024
1 parent 1444a4e commit bca126f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
13 changes: 12 additions & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export default defineNuxtConfig({
],

modules: [
'../src/module',
// '../src/module',
'../dist/module',
'@nuxthub/core'
],

Expand All @@ -14,6 +15,16 @@ export default defineNuxtConfig({

compatibilityDate: '2024-11-11',

vite: {
css: {
preprocessorOptions: {
scss: {
api: 'modern'
}
}
}
},

autoImport: {
connectors: [
'./connectors/icons.ts',
Expand Down
4 changes: 3 additions & 1 deletion playground/pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<script setup lang="ts">
import type { AutoImportIcons } from '../../dist/types';
const autoImport = useAutoImport();
const icons = autoImport.defines.icons;
// example use AutoImport type
const item = {} as AutoImportIcons;
const item = 'Cluster' as AutoImportIcons;
console.log(item);
const clicksInside = ref(0);
Expand Down
7 changes: 2 additions & 5 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,8 @@ export default defineNuxtModule<ModuleOptions>({
}
}, 500);
});
nuxt.hook('prepare:types', (options) => {
options.tsConfig.include = [
...(options.tsConfig.include || []),
...autoImport.createConnectorsTypes(true).map(p => createResolver(p).resolve())
];
nuxt.hook('prepare:types', () => {
autoImport.createConnectorsTypes(true);
});
nuxt.hook('app:templatesGenerated', () => {
useLogger('Modules').info('Generation Modules types...');
Expand Down
21 changes: 20 additions & 1 deletion src/runtime/autoImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,31 @@ export class Module {

createConnectorsTypes(tryRead = false) {
const typesDir = this.resolver.resolve('runtime', 'types', 'connectors');
const typesIndexFile = this.resolver.resolve('types.d.ts');

if (!fs.existsSync(typesDir)) fs.mkdirSync(typesDir);

return this.typeGeneratorListFunc
const filesPaths = this.typeGeneratorListFunc
.map(file => file(typesDir, tryRead) as string)
.filter(Boolean);

if (fs.existsSync(typesIndexFile)) {
const connectorsRelativePath = './runtime/types/connectors/';
const indexFile = tsMorphProject.createSourceFile(typesIndexFile, fs.readFileSync(typesIndexFile, 'utf-8'), { overwrite: true });
indexFile.getExportDeclarations().forEach((statement) => {
const typePath = statement.getModuleSpecifierValue();
if (typePath && typePath.startsWith(connectorsRelativePath)) statement.remove();
});
indexFile.addStatements(filesPaths
.map((filePath) => {
const relativePath = filePath.slice(typesDir.length + 1);
return `export * from '${connectorsRelativePath}${relativePath.replace('.ts', '')}'`;
})
.join('\n'));
indexFile.saveSync();
}

return filesPaths;
}

createBuildMeta() {
Expand Down
11 changes: 4 additions & 7 deletions src/runtime/helpers/typeGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@ export default function (typesDir: string, typeName: string, content: string | W
if (typeName.length === 0) return;
if (typeName.length === 1) typeName = typeName.toUpperCase();

const filePath = path.join(typesDir, `${typeName}.d.ts`);
const filePath = path.join(typesDir, `${typeName}.ts`);
const isWriter = typeof content === 'function';

if (tryRead && fs.existsSync(filePath)) return filePath;

const file = tsMorphProject.createSourceFile(filePath, '', { overwrite: true });

file.addStatements((writer) => {
writer.write('declare global').block(() => {
writer.write(`type AutoImport${typeName[0].toUpperCase()}${typeName.slice(1)} =${isWriter ? ' ' : content[0] === '\n' ? '' : ' '}`);
isWriter ? content(writer) : writer.write(content);
writer.write(';');
});
writer.write(`export type AutoImport${typeName[0].toUpperCase()}${typeName.slice(1)} =${isWriter ? ' ' : content[0] === '\n' ? '' : ' '}`);
isWriter ? content(writer) : writer.write(content);
writer.write(';');
});
file.addExportDeclaration({});
file.saveSync();

return filePath;
Expand Down

0 comments on commit bca126f

Please sign in to comment.