|
| 1 | +/** |
| 2 | + * vue-cli-service build --target lib --name module-test --dest dist/module-test module-test/index.js |
| 3 | + */ |
| 4 | + |
| 5 | +const fs = require('fs'); |
| 6 | +const path = require('path'); |
| 7 | +const execa = require('execa'); |
| 8 | + |
| 9 | +async function exec(name, execFile) { |
| 10 | + const { stdout } = await execa(require.resolve('@vue/cli-service/bin/vue-cli-service'), [ |
| 11 | + 'build', |
| 12 | + '--target', |
| 13 | + 'lib', |
| 14 | + '--name', |
| 15 | + name, |
| 16 | + '--formats', |
| 17 | + 'umd,commonjs', |
| 18 | + '--dest', |
| 19 | + `dist/${name}`, |
| 20 | + execFile, |
| 21 | + ]); |
| 22 | + return stdout; |
| 23 | +} |
| 24 | + |
| 25 | +// *.{js,ts,!d.ts} |
| 26 | +function isJsOrTsFile(dirent) { |
| 27 | + return !dirent.name.endsWith('.d.ts') && ['.js', '.ts'].includes(path.extname(dirent.name)); |
| 28 | +} |
| 29 | + |
| 30 | +// *.vue |
| 31 | +function isVueFile(dirent) { |
| 32 | + return ['.vue'].includes(path.extname(dirent.name)); |
| 33 | +} |
| 34 | + |
| 35 | +async function findFile(rootDir, filename = 'index') { |
| 36 | + const dir = await fs.promises.opendir(rootDir); |
| 37 | + for await (const dirent of dir) { |
| 38 | + const extname = path.extname(dirent.name); |
| 39 | + const basename = path.basename(dirent.name, extname); |
| 40 | + |
| 41 | + if ( |
| 42 | + dirent.isFile() && |
| 43 | + (isJsOrTsFile(dirent) || isVueFile(dirent)) && |
| 44 | + path.basename(dirent.name, extname) === filename |
| 45 | + ) { |
| 46 | + return path.resolve(rootDir, dirent.name); |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +async function run(files) { |
| 52 | + // 指定文件 |
| 53 | + if (files && files.length) { |
| 54 | + for (file of files) { |
| 55 | + const fullpath = path.resolve(__dirname, '../', file); |
| 56 | + const lstat = await fs.promises.lstat(fullpath); |
| 57 | + if (lstat.isFile()) { |
| 58 | + const result = await exec(path.basename(fullpath, path.extname(fullpath)), fullpath); |
| 59 | + console.log(result); |
| 60 | + } else { |
| 61 | + const indexFile = await findFile(fullpath); |
| 62 | + if (indexFile) { |
| 63 | + const result = await exec(path.basename(fullpath), indexFile); |
| 64 | + console.log(result); |
| 65 | + } else { |
| 66 | + console.error('can not find index file!'); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } else { |
| 71 | + // 所有 src/*.{!d.ts} or src/*/index.{!d.ts} |
| 72 | + const rootDir = path.resolve(__dirname, '../src'); |
| 73 | + |
| 74 | + const dir = await fs.promises.opendir(rootDir); |
| 75 | + for await (const dirent of dir) { |
| 76 | + if (dirent.isFile()) { |
| 77 | + // 文件 |
| 78 | + if (!isJsOrTsFile(dirent) && !isVueFile(dirent)) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + const rootFile = path.resolve(rootDir, dirent.name); |
| 83 | + const result = await exec(path.basename(dirent.name, path.extname(dirent.name)), rootFile); |
| 84 | + console.log(result); |
| 85 | + } else { |
| 86 | + // 目录=》 index.js/index.ts |
| 87 | + const indexFile = await findFile(path.resolve(rootDir, dirent.name)); |
| 88 | + if (indexFile) { |
| 89 | + const result = await exec(dirent.name, indexFile); |
| 90 | + console.log(result); |
| 91 | + } else { |
| 92 | + console.error('can not find index file!'); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +async function genJsonFile(filename) { |
| 100 | + const distDir = path.resolve(__dirname, '../dist'); |
| 101 | + const filepath = path.resolve(distDir, `${filename}.json`); |
| 102 | + const dir = await fs.promises.opendir(distDir); |
| 103 | + const content = {}; |
| 104 | + for await (const dirent of dir) { |
| 105 | + if (dirent.isDirectory()) { |
| 106 | + const umdFile = await findFile(path.resolve(distDir, dirent.name), `${dirent.name}.umd.min`); |
| 107 | + content[dirent.name] = dirent.name + '/' + path.basename(umdFile); |
| 108 | + } |
| 109 | + } |
| 110 | + fs.writeFile(filepath, JSON.stringify(content), 'utf8', () => { |
| 111 | + console.log(`create json file ${filepath} successfully!`); |
| 112 | + }); |
| 113 | +} |
| 114 | + |
| 115 | +if (require.main === module) { |
| 116 | + const argv = process.argv.splice(2); |
| 117 | + const files = argv.filter((arg) => !arg.startsWith('--')); |
| 118 | + const options = argv |
| 119 | + .filter((arg) => !files.includes(arg)) |
| 120 | + .reduce((prev, curr) => { |
| 121 | + const keyValue = curr |
| 122 | + .substr(2) |
| 123 | + .split('=') |
| 124 | + .filter((arg) => !!arg); |
| 125 | + keyValue.length && (prev[keyValue[0]] = keyValue.length === 1 ? true : keyValue[1]); |
| 126 | + return prev; |
| 127 | + }, {}); |
| 128 | + const genJson = run(files) |
| 129 | + .then(() => { |
| 130 | + // 生成 json 文件 |
| 131 | + options.json && options.json !== 'false' && genJsonFile(options.json === true ? 'modules' : options.json); |
| 132 | + }) |
| 133 | + .catch((err) => { |
| 134 | + // eslint-disable-next-line no-console |
| 135 | + console.error(err); |
| 136 | + process.exit(1); |
| 137 | + }); |
| 138 | +} |
0 commit comments