* 将打包后的 dist 文件进行二次处理 (复制一些公共文件进 dist 包以及文件的移动)
()
| 19 | * 将打包后的 dist 文件进行二次处理 (复制一些公共文件进 dist 包以及文件的移动) |
| 20 | */ |
| 21 | async function buildMetaFiles() { |
| 22 | for (const { name, moduleJs, iife } of packages) { |
| 23 | const packageRoot = path.resolve(__dirname, '..', 'packages', name) |
| 24 | const packageDist = path.resolve(packageRoot, 'dist') |
| 25 | |
| 26 | // ----- 复制 core README ----- |
| 27 | if (name === 'core') { |
| 28 | await fs.copyFile( |
| 29 | path.join(rootDir, 'README.md'), |
| 30 | path.join(packageDist, 'README.md') |
| 31 | ) |
| 32 | } |
| 33 | |
| 34 | // ----- 复制 LICENSE 证书 ----- |
| 35 | for (const file of FILES_COPY_ROOT) { |
| 36 | await fs.copyFile(path.join(rootDir, file), path.join(packageDist, file)) |
| 37 | } |
| 38 | |
| 39 | // ----- 将部分文件放入dist包 ----- |
| 40 | const files = await fg(FILES_COPY_LOCAL, { cwd: packageRoot }) |
| 41 | for (const file of files) { |
| 42 | await fs.copyFile( |
| 43 | path.join(packageRoot, file), |
| 44 | path.join(packageDist, file) |
| 45 | ) |
| 46 | } |
| 47 | |
| 48 | // ----- 更改本地引用版本 ----- |
| 49 | const packageJSON = await fs.readJSON( |
| 50 | path.join(packageRoot, 'package.json') |
| 51 | ) |
| 52 | // 当子类包互相引用时,要手动更改其版本(不改的话则是 workspace) |
| 53 | for (const key of Object.keys(packageJSON.dependencies || {})) { |
| 54 | if (key.startsWith('@web-tracing/')) { |
| 55 | packageJSON.dependencies[key] = version |
| 56 | } |
| 57 | } |
| 58 | // 因为只有在打包的时候才用这些,开发时需要 ./dist/index.mjs 才能正常引用 |
| 59 | // 更改 exports、types、main、module、unpkg、jsdelivr(./dist/index.mjs => ./index.mjs) |
| 60 | packageJSON.types = './index.d.ts' |
| 61 | packageJSON.main = moduleJs ? './index.mjs' : './index.cjs' |
| 62 | packageJSON.module = './index.mjs' |
| 63 | if (iife !== false) { |
| 64 | packageJSON.unpkg = './index.iife.min.js' |
| 65 | packageJSON.jsdelivr = './index.iife.min.js' |
| 66 | } |
| 67 | packageJSON.exports = { |
| 68 | ...packageJSON.exports, |
| 69 | '.': { |
| 70 | import: './index.mjs', |
| 71 | require: './index.cjs', |
| 72 | types: './index.d.ts' |
| 73 | }, |
| 74 | './*': './*' |
| 75 | } |
| 76 | |
| 77 | await fs.writeJSON(path.join(packageDist, 'package.json'), packageJSON, { |
| 78 | spaces: 2 |