vue3-yuanma/scripts/build.js

100 lines
2.5 KiB
JavaScript
Raw Normal View History

2018-09-20 00:21:00 +08:00
/*
Produce prodcution builds and stitch toegether d.ts files.
To specific the package to build, simply pass its name and the desired build
formats to output (defaults to `buildOptions.formats` specified in that package,
or "esm,cjs"):
```
# name supports fuzzy match. will build all packages with name containing "dom":
yarn build dom
# specify the format to output
yarn build core --formats cjs
```
*/
2018-09-19 23:35:38 +08:00
const fs = require('fs-extra')
const path = require('path')
const zlib = require('zlib')
const chalk = require('chalk')
const execa = require('execa')
const dts = require('dts-bundle')
const { targets, fuzzyMatchTarget } = require('./utils')
2018-09-20 00:21:00 +08:00
const args = require('minimist')(process.argv.slice(2))
const target = args._[0]
const formats = args.formats || args.f
2018-10-23 23:58:37 +08:00
const buildAllMatching = args.all || args.a
2018-09-19 23:35:38 +08:00
;(async () => {
if (!target) {
await buildAll(targets)
checkAllSizes(targets)
} else {
2018-10-23 23:58:37 +08:00
await buildAll(fuzzyMatchTarget(target, buildAllMatching))
checkAllSizes(fuzzyMatchTarget(target, buildAllMatching))
2018-09-19 23:35:38 +08:00
}
})()
2018-09-20 00:21:00 +08:00
async function buildAll(targets) {
2018-09-19 23:35:38 +08:00
for (const target of targets) {
await build(target)
}
}
2018-09-20 00:21:00 +08:00
async function build(target) {
2018-09-19 23:35:38 +08:00
const pkgDir = path.resolve(`packages/${target}`)
2018-10-17 05:41:59 +08:00
const pkg = require(`${pkgDir}/package.json`)
2018-09-19 23:35:38 +08:00
await fs.remove(`${pkgDir}/dist`)
2018-09-20 00:21:00 +08:00
await execa(
'rollup',
[
'-c',
'--environment',
`NODE_ENV:production,` +
`TARGET:${target}` +
(formats ? `,FORMATS:${formats}` : ``)
2018-09-20 00:21:00 +08:00
],
{ stdio: 'inherit' }
)
2018-09-19 23:35:38 +08:00
2018-10-17 05:41:59 +08:00
if (pkg.types) {
const dtsOptions = {
name: target === 'vue' ? target : `@vue/${target}`,
main: `${pkgDir}/dist/packages/${target}/src/index.d.ts`,
out: `${pkgDir}/${pkg.types}`
}
dts.bundle(dtsOptions)
console.log()
console.log(
chalk.blue(chalk.bold(`generated typings at ${dtsOptions.out}`))
)
2018-09-19 23:35:38 +08:00
2018-10-17 05:41:59 +08:00
await fs.remove(`${pkgDir}/dist/packages`)
}
2018-09-19 23:35:38 +08:00
}
2018-09-20 00:21:00 +08:00
function checkAllSizes(targets) {
2018-09-19 23:35:38 +08:00
console.log()
for (const target of targets) {
checkSize(target)
}
console.log()
}
2018-09-20 00:21:00 +08:00
function checkSize(target) {
2018-09-19 23:35:38 +08:00
const pkgDir = path.resolve(`packages/${target}`)
const esmProdBuild = `${pkgDir}/dist/${target}.esm-browser.prod.js`
if (fs.existsSync(esmProdBuild)) {
const file = fs.readFileSync(esmProdBuild)
const minSize = (file.length / 1024).toFixed(2) + 'kb'
const gzipped = zlib.gzipSync(file)
const gzipSize = (gzipped.length / 1024).toFixed(2) + 'kb'
2018-09-20 00:21:00 +08:00
console.log(
`${chalk.gray(chalk.bold(target))} min:${minSize} / gzip:${gzipSize}`
)
2018-09-19 23:35:38 +08:00
}
}