Skip to content

Commit

Permalink
chore: added definition files
Browse files Browse the repository at this point in the history
  • Loading branch information
tikazyq committed Jul 2, 2024
1 parent 57a0d73 commit d0e7fab
Show file tree
Hide file tree
Showing 245 changed files with 4,764 additions and 247 deletions.
6 changes: 0 additions & 6 deletions babel.config.js

This file was deleted.

13 changes: 2 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"public",
"src",
"typings",
"babel.config.js",
"vite.config.js"
],
"typings": "typings/index.d.ts",
Expand All @@ -40,22 +39,19 @@
"serve:dist": "serve dist",
"gen:index": "node ./scripts/gen-index.js",
"gen:svg": "node ./scripts/gen-svg.js",
"gen:dts": "pnpm run fix-memory-limit && vue-tsc --declaration --emitDeclarationOnly --declarationDir ./typings --rootDir ./src",
"gen:dts": "node ./scripts/gen-dts.js",
"gen:dts:check": "vue-tsc --noEmit --rootDir ./src",
"gen:interfaces": "node ./scripts/gen-interfaces.js",
"prebuild": "pnpm run prebuild:gen",
"prebuild:gen": "pnpm run gen:index && pnpm run gen:svg",
"postbuild": "pnpm run postbuild:gen",
"postbuild:gen": "pnpm run gen:interfaces",
"postbuild:dist:local": "node ./scripts/copy-dist-local.js",
"build": "pnpm run prebuild && pnpm run build:lib && pnpm run postbuild",
"build:dist:local": "pnpm run build && pnpm run postbuild:dist:local",
"build:docker": "vite build --mode docker",
"build:local": "vite build --mode local",
"build:lib": "vite build",
"build:lib:watch": "vite build --target lib --mode production --watch src/index.ts",
"build:lib:local": "vite build --target lib --mode local src/index.ts",
"build:lib:dist": "pnpm run build:lib && pnpm run postbuild:dist:local",
"build:lib:analyze": "vite build --target lib --mode analyze src/index.ts",
"fix-memory-limit": "cross-env LIMIT=4096 increase-memory-limit",
"lint": "vite lint",
Expand Down Expand Up @@ -110,11 +106,6 @@
"@types/node": "^20.14.8",
"@types/node-os-utils": "^1.3.4",
"@vitejs/plugin-vue": "5.0.5",
"@vue/cli-plugin-babel": "^5.0.8",
"@vue/cli-plugin-router": "^5.0.8",
"@vue/cli-plugin-typescript": "^5.0.8",
"@vue/cli-plugin-vuex": "^5.0.8",
"@vue/cli-service": "^5.0.8",
"@vue/compiler-sfc": "^3.4.29",
"autoprefixer": "^10.4.19",
"base64-img": "^1.0.4",
Expand All @@ -137,7 +128,7 @@
"serve": "^14.2.3",
"tailwindcss": "^3.4.4",
"ts-morph": "^22.0.0",
"typescript": "^5.4.5",
"typescript": "^5",
"vite": "^5",
"vite-aliases": "^0.11.7",
"vite-plugin-dynamic-import": "^1.5.0",
Expand Down
39 changes: 0 additions & 39 deletions scripts/copy-dist-local.js

This file was deleted.

9 changes: 0 additions & 9 deletions scripts/copy-dist-local.sh

This file was deleted.

161 changes: 23 additions & 138 deletions scripts/gen-dts.js
Original file line number Diff line number Diff line change
@@ -1,148 +1,33 @@
import path from 'path';
import fs from 'fs';
import { Project } from 'ts-morph';
import vueCompiler from '@vue/compiler-sfc';
import klawSync from 'klaw-sync';
import { dirname } from 'path';
import { spawn } from 'child_process';
import { resolve, join, dirname } from 'path';
import { fileURLToPath } from 'url';
import { log } from './utils.js';

const __dirname = dirname(fileURLToPath(import.meta.url));

const TSCONFIG_PATH = path.resolve(__dirname, '../tsconfig.dts.json');
const DEMO_RE = /\/demo\/\w+\.vue$/;
const TEST_RE = /__test__|__tests__/;
const excludedFiles = [
'mock',
'package.json',
'spec',
'test',
'tests',
'css',
'.DS_Store',
];
const includedFiles = ['.vue', '.ts'];
const exclude = path => !excludedFiles.some(f => path.includes(f));
const include = path => includedFiles.some(f => path.includes(f));

/**
* fork = require( https://github.com/egoist/vue-dts-gen/blob/main/src/index.ts
*/
async function genVueTypes(
root,
outDir = path.resolve(__dirname, '../typings')
) {
const options = {
compilerOptions: {
allowJs: false,
declaration: true,
emitDeclarationOnly: true,
noEmitOnError: false,
outDir,
paths: {
'@': [path.resolve(__dirname, '../src')],
'@/*': [path.resolve(__dirname, '../src/*')],
},
skipLibCheck: true,
},
tsConfigFilePath: TSCONFIG_PATH,
skipAddingFilesFromTsConfig: true,
};
const project = new Project(options);

const start = Date.now();

log('Getting all files...', 'info');
const filePaths = klawSync(root, { nodir: true })
.map(item => item.path)
.filter(path => !DEMO_RE.test(path))
.filter(path => !TEST_RE.test(path))
.filter(exclude)
.filter(include);

filePaths.forEach((file, i) => {
let sourceFile;
try {
if (file.endsWith('.vue')) {
// .vue file
const content = fs.readFileSync(file, 'utf-8');
const sfc = vueCompiler.parse(content);
const { script, scriptSetup } = sfc.descriptor;
if (script || scriptSetup) {
let content = '';
let isTS = false;
if (script && script.content) {
content += script.content;
if (script.lang === 'ts') isTS = true;
}
if (scriptSetup) {
const compiled = vueCompiler.compileScript(sfc.descriptor, {
id: 'xxx',
});
content += compiled.content;
if (scriptSetup.lang === 'ts') isTS = true;
}
sourceFile = project.createSourceFile(
path.relative(process.cwd(), file) + (isTS ? '.ts' : '.js'),
content
);
}
} else if (file.endsWith('.ts')) {
// .ts file
const content = fs.readFileSync(file, 'utf-8');
sourceFile = project.createSourceFile(
path.relative(process.cwd(), file),
content
);
}

if (!sourceFile) return;

log(`Processing ${sourceFile.getFilePath()}`, 'info');
const emitOutput = sourceFile.getEmitOutput({ emitOnlyDtsFiles: true });
log(`Emitting ${sourceFile.getFilePath()}`, 'info');
const outputFiles = emitOutput.getOutputFiles();
outputFiles.forEach(outputFile => {
const filepath = outputFile.getFilePath();
fs.mkdirSync(path.dirname(filepath), {
recursive: true,
});
fs.writeFileSync(filepath, outputFile.getText(), 'utf8');
});

if (((i + 1) % 100 === 0 && i > 0) || i + 1 === filePaths.length) {
log(`Processed: ${i + 1}/${filePaths.length}`, 'info');
}
} catch (e) {
console.error(e);
throw e;
}
});
log('All definition files generated', 'success');

// export interfaces in typings/index.d.ts
log('Exporting interfaces...', 'info');
const idxFilePath = path.resolve(__dirname, '../typings/index.d.ts');
if (fs.existsSync(idxFilePath)) {
let fileContent = fs.readFileSync(idxFilePath);
const exportInterfacesLine = "export * from './interfaces';";
if (!fileContent.includes(exportInterfacesLine)) {
fileContent = exportInterfacesLine + '\n' + fileContent;
}
fs.writeFileSync(idxFilePath, fileContent);
function main() {
const typingsPath = resolve(join(__dirname, '..', 'typings'));
if (fs.existsSync(typingsPath)) {
fs.rmSync(typingsPath, { recursive: true });
}
log('Interfaces exported', 'success');

const end = Date.now();

const duration = ((end - start) / 1000).toFixed(1);

log(`Done in ${duration}s`, 'success');
const child = spawn(
'vue-tsc',
'--declaration --emitDeclarationOnly --declarationDir ./typings --rootDir ./src'.split(
' '
),
{ shell: true }
);
child.stdout.on('data', data => {
log(`${data}`, 'info');
});
child.stderr.on('data', data => {
log(`${data}`, 'error');
});
child.on('close', code => {
log(`child process exited with code ${code}`, 'info');
});
}

(async function () {
await genVueTypes(
path.resolve(__dirname, '../src'),
path.resolve(__dirname, '../typings')
);
})();
main();
11 changes: 1 addition & 10 deletions scripts/gen-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,7 @@ import fs from 'fs';
import rd from 'rd';
import os from 'os';

const EXPORT_MODULES = [
'components',
'views',
'directives',
'layouts',
// 'services',
// 'store',
// 'utils',
// 'constants',
];
const EXPORT_MODULES = ['components', 'views', 'directives', 'layouts'];

const COMPONENT_PREFIX = 'Cl';
const INDEX_COMP_NAME = 'index';
Expand Down
Loading

0 comments on commit d0e7fab

Please sign in to comment.