-
-
Notifications
You must be signed in to change notification settings - Fork 304
/
Copy pathbuildAssets.ts
115 lines (104 loc) · 3.5 KB
/
buildAssets.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
Produces production assets files.
To specify the package assets to build, simply pass its name
```
# name supports fuzzy match. will build all packages with name containing "vue-quill":
npm run assets:build -- vue-quill
```
*/
;(async () => {
const fs = require('fs-extra')
const path = require('path')
const chalk = require('chalk')
const execa = require('execa')
const logger = require('./logger')
const {
targets: allTargets,
getPackageDir,
getAssetsConfigJson,
fuzzyMatchTarget,
checkAssetsSize,
runParallel,
} = require('./utils')
const args: any = require('minimist')(process.argv.slice(2))
const targets: string[] = args._
const devOnly: boolean = args.devOnly || args.d
const prodOnly: boolean = !devOnly && (args.prodOnly || args.p)
const sourceMap = args.sourcemap || args.s
const isRelease: boolean = args.release
const buildAllMatching: boolean = args.all || args.a
// const nextVersion: string =
// args.nextVersion ||
// require(path.resolve(__dirname, '../package.json')).version
if (isRelease) {
// remove build cache for release builds to avoid outdated enum values
await fs.remove(path.resolve(__dirname, '../node_modules/.rts2_cache'))
}
if (!targets.length) {
logger.header(allTargets, 'BUILD ASSETS')
await buildAll(allTargets)
checkAllSizes(allTargets)
} else {
const matchedTargets = fuzzyMatchTarget(allTargets, targets, buildAllMatching)
logger.header(matchedTargets, 'BUILD ASSETS')
await buildAll(matchedTargets)
checkAllSizes(matchedTargets)
}
async function buildAll(targets: string[]) {
await runParallel(require('os').cpus().length, targets, buildAssets)
}
async function buildAssets(target: string) {
const pkgDir = getPackageDir(target)
const assets = getAssetsConfigJson(target)
// only build published packages for release
if (isRelease && assets.private) return
if (!assets || !assets.css.length) {
logger.warning(
target,
`Can't find assets configuration or file configuration ${chalk.underline(
target + '/assets.config.json'
)}`
)
return
}
logger.info(target, 'Compiling assets...')
for (const css of assets.css) {
const input: string = path.resolve(pkgDir, css.input)
const inputExt: string = path.extname(input)
const output: string = path.resolve(pkgDir, css.output)
const outputProd: string = path.resolve(
pkgDir,
path.dirname(output),
path.parse(output).name + '.prod.css'
)
if (!fs.existsSync(input)) {
console.log()
logger.error(target, `Asset file input doesn't exist ${input}`)
process.exit(1)
}
if (inputExt === '.styl' || inputExt === '.css') {
if (!prodOnly) {
const commands: string[] = ['stylus', input, '-o', output]
if (sourceMap) commands.push('--sourcemap')
await execa('npx', commands, { stdio: 'inherit' })
}
// create production build
if (!devOnly) {
const commands: string[] = ['stylus', input, '-o', outputProd, '-c']
if (sourceMap) commands.push('--sourcemap')
await execa('npx', commands, { stdio: 'inherit' })
}
} else {
logger.error(target, `File extention not supported: ${input}`)
}
}
}
function checkAllSizes(targets: string[]) {
if (devOnly) return
console.log()
for (const target of targets) {
checkAssetsSize(target)
}
console.log()
}
})()