| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- const fs = require('fs');
- const path = require('path');
- /**
- * 扫描目录,找到所有需要导出的文件
- */
- function scanDirectory(dir, basePath = '') {
- const exports = {};
-
- if (!fs.existsSync(dir)) {
- return exports;
- }
- const entries = fs.readdirSync(dir, { withFileTypes: true });
- for (const entry of entries) {
- const fullPath = path.join(dir, entry.name);
- const relativePath = basePath ? `${basePath}/${entry.name}` : entry.name;
- if (entry.isDirectory()) {
- // 检查目录是否有 index.js
- const indexJsPath = path.join(fullPath, 'index.js');
- if (fs.existsSync(indexJsPath)) {
- // 为目录生成导出映射(指向 index.js)
- const exportKey = `./lib/es/${relativePath}`;
- const exportKeyNoLib = `./${relativePath}`;
- const esPath = `lib/es/${relativePath}/index.js`;
- const cjsPath = `lib/cjs/${relativePath}/index.js`;
- const typesPath = `lib/es/${relativePath}/index.d.ts`;
- const exportValue = {
- types: `./${typesPath}`,
- import: `./${esPath}`,
- require: `./${cjsPath}`
- };
- // 添加带 lib/es 前缀的键
- exports[exportKey] = exportValue;
- // 添加不带 lib/es 前缀的键
- exports[exportKeyNoLib] = exportValue;
- }
- // 递归扫描子目录
- const subExports = scanDirectory(fullPath, relativePath);
- Object.assign(exports, subExports);
- } else if (entry.isFile()) {
- if (entry.name.endsWith('.js')) {
- // 找到 .js 文件,生成导出映射
- // relativePath 已经是相对于 lib/es 的路径
- // 生成两个键:带 .js 和不带 .js(因为导入时可能不带扩展名)
- const pathWithoutExt = relativePath.replace(/\.js$/, '');
- const exportKeyWithExt = `./lib/es/${relativePath}`;
- const exportKeyWithoutExt = `./lib/es/${pathWithoutExt}`;
- // 添加不带 lib/es 前缀的路径
- const exportKeyWithExtNoLib = `./${relativePath}`;
- const exportKeyWithoutExtNoLib = `./${pathWithoutExt}`;
- const esPath = `lib/es/${relativePath}`;
- const cjsPath = `lib/cjs/${relativePath}`;
- const typesPath = `lib/es/${relativePath.replace(/\.js$/, '.d.ts')}`;
- const exportValue = {
- types: `./${typesPath}`,
- import: `./${esPath}`,
- require: `./${cjsPath}`
- };
- // 添加带扩展名的键(带 lib/es)
- exports[exportKeyWithExt] = exportValue;
- // 添加不带扩展名的键(带 lib/es,如果不同)
- if (exportKeyWithExt !== exportKeyWithoutExt) {
- exports[exportKeyWithoutExt] = exportValue;
- }
- // 添加不带 lib/es 前缀的键(带扩展名)
- exports[exportKeyWithExtNoLib] = exportValue;
- // 添加不带 lib/es 前缀的键(不带扩展名,如果不同)
- if (exportKeyWithExtNoLib !== exportKeyWithoutExtNoLib) {
- exports[exportKeyWithoutExtNoLib] = exportValue;
- }
- } else if (entry.name.endsWith('.css')) {
- // 找到 .css 文件,生成导出映射
- const exportKey = `./lib/es/${relativePath}`;
- const exportKeyNoLib = `./${relativePath}`;
- const esPath = `lib/es/${relativePath}`;
- const cjsPath = `lib/cjs/${relativePath}`;
- const exportValue = {
- import: `./${esPath}`,
- require: `./${cjsPath}`,
- default: `./${esPath}`
- };
- // 添加带 lib/es 前缀的键
- exports[exportKey] = exportValue;
- // 添加不带 lib/es 前缀的键
- exports[exportKeyNoLib] = exportValue;
- } else if (entry.name.endsWith('.scss')) {
- // 找到 .scss 文件,生成导出映射
- const exportKey = `./lib/es/${relativePath}`;
- const exportKeyNoLib = `./${relativePath}`;
- const esPath = `lib/es/${relativePath}`;
- const cjsPath = `lib/cjs/${relativePath}`;
- const exportValue = {
- import: `./${esPath}`,
- require: `./${cjsPath}`,
- default: `./${esPath}`
- };
- // 添加带 lib/es 前缀的键
- exports[exportKey] = exportValue;
- // 添加不带 lib/es 前缀的键
- exports[exportKeyNoLib] = exportValue;
- }
- }
- }
- return exports;
- }
- /**
- * 生成 exports 字段
- */
- function generateExports() {
- const libEsPath = path.join(__dirname, '../lib/es');
-
- if (!fs.existsSync(libEsPath)) {
- console.error('Error: lib/es directory does not exist. Please run build:lib first.');
- process.exit(1);
- }
- console.log('Scanning lib/es directory...');
- const exports = scanDirectory(libEsPath, '');
- // 添加主入口
- const mainExports = {
- '.': {
- types: './lib/es/base/index.d.ts',
- import: './lib/es/base/index.js',
- require: './lib/cjs/base/index.js'
- }
- };
- // 添加 CSS 文件的通配符规则(这些通常可以正常工作)
- const cssExports = {
- './lib/es/*/*.css': {
- import: './lib/es/*/*.css',
- require: './lib/cjs/*/*.css',
- default: './lib/es/*/*.css'
- },
- './lib/es/*.css': {
- import: './lib/es/*.css',
- require: './lib/cjs/*.css',
- default: './lib/es/*.css'
- },
- './lib/cjs/*/*.css': {
- import: './lib/es/*/*.css',
- require: './lib/cjs/*/*.css',
- default: './lib/cjs/*/*.css'
- },
- './lib/cjs/*.css': {
- import: './lib/es/*.css',
- require: './lib/cjs/*.css',
- default: './lib/cjs/*.css'
- },
- // 添加不带 lib/es 前缀的通配符规则
- './*/*.css': {
- import: './lib/es/*/*.css',
- require: './lib/cjs/*/*.css',
- default: './lib/es/*/*.css'
- },
- './*.css': {
- import: './lib/es/*.css',
- require: './lib/cjs/*.css',
- default: './lib/es/*.css'
- }
- };
- // 添加 SCSS 文件的通配符规则
- const scssExports = {
- './lib/es/*/*.scss': {
- import: './lib/es/*/*.scss',
- require: './lib/cjs/*/*.scss',
- default: './lib/es/*/*.scss'
- },
- './lib/es/*.scss': {
- import: './lib/es/*.scss',
- require: './lib/cjs/*.scss',
- default: './lib/es/*.scss'
- },
- './lib/cjs/*/*.scss': {
- import: './lib/es/*/*.scss',
- require: './lib/cjs/*/*.scss',
- default: './lib/cjs/*/*.scss'
- },
- './lib/cjs/*.scss': {
- import: './lib/es/*.scss',
- require: './lib/cjs/*.scss',
- default: './lib/cjs/*.scss'
- },
- // 添加不带 lib/es 前缀的通配符规则
- './*/*.scss': {
- import: './lib/es/*/*.scss',
- require: './lib/cjs/*/*.scss',
- default: './lib/es/*/*.scss'
- },
- './*.scss': {
- import: './lib/es/*.scss',
- require: './lib/cjs/*.scss',
- default: './lib/es/*.scss'
- }
- };
- // 合并所有 exports
- const allExports = {
- ...mainExports,
- ...exports,
- ...cssExports,
- ...scssExports
- };
- console.log(`Generated ${Object.keys(exports).length} export paths`);
- return allExports;
- }
- /**
- * 更新 package.json
- */
- function updatePackageJson() {
- const packageJsonPath = path.join(__dirname, '../package.json');
- const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
- // 生成新的 exports
- const newExports = generateExports();
- // 更新 package.json
- packageJson.exports = newExports;
- // 写回文件
- fs.writeFileSync(
- packageJsonPath,
- JSON.stringify(packageJson, null, 4) + '\n',
- 'utf-8'
- );
- console.log('Successfully updated package.json exports field');
- console.log(`Total exports: ${Object.keys(newExports).length}`);
- }
- // 运行脚本
- if (require.main === module) {
- try {
- updatePackageJson();
- } catch (error) {
- console.error('Error:', error.message);
- process.exit(1);
- }
- }
- module.exports = { generateExports, updatePackageJson };
|