-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathbuild.js
More file actions
131 lines (114 loc) · 4 KB
/
Copy pathbuild.js
File metadata and controls
131 lines (114 loc) · 4 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'use strict'
const { resolve } = require('path')
const { readFileSync, existsSync } = require('fs')
const { parseConfigFileTextToJson } = require('typescript')
const rimraf = require('rimraf')
const gzipSize = require('gzip-size')
const prettyBytes = require('pretty-bytes')
const { rollup } = require('rollup')
const typescript2 = require('rollup-plugin-typescript2')
const replace = require('rollup-plugin-replace')
const nodeResolve = require('rollup-plugin-node-resolve')
const stripBanner = require('rollup-plugin-strip-banner')
const { log } = console
const packagePath = process.cwd()
function getPlugins({ isProduction, tsConfigPath }) {
return [
stripBanner(),
nodeResolve(),
replace({ __DEV__: !isProduction }),
typescript2({
clean: true,
tsconfig: tsConfigPath,
useTsconfigDeclarationDir: true,
}),
]
}
function getExternalDependencies(packagePath) {
const packageJsonPath = resolve(packagePath, 'package.json')
const content = readFileSync(packageJsonPath, 'utf-8')
const { dependencies, peerDependencies } = JSON.parse(content)
return [...Object.keys(Object(dependencies)), ...Object.keys(Object(peerDependencies))]
}
function getTypescriptConfig(packagePath) {
const tsConfigPath = resolve(packagePath, 'tsconfig.json')
const content = readFileSync(tsConfigPath, 'utf-8')
const { config } = parseConfigFileTextToJson(tsConfigPath, content)
return { tsConfigPath, tsConfig: config }
}
function getInputFilePath(packagePath, packageName) {
let filePath = resolve(packagePath, `${packageName}.ts`)
// Try check file with `ts` extension.
if (existsSync(filePath)) {
return filePath
}
filePath = filePath.replace('.ts', '.tsx')
// Try check file with `tsx` extension.
if (existsSync(filePath)) {
return filePath
}
throw new Error(`Cannot found main file for package "${packageName}".`)
}
function getPackageData(packagePath) {
const externalDependencies = getExternalDependencies(packagePath)
const { tsConfigPath, tsConfig } = getTypescriptConfig(packagePath)
const packageName = packagePath.split('/').pop()
const buildPath = resolve(packagePath, tsConfig.compilerOptions.outDir)
const inputFile = getInputFilePath(packagePath, packageName)
return {
externalDependencies,
packageName,
tsConfigPath,
inputFile,
outputs: [
{
outputFile: resolve(buildPath, `${packageName}.cjs`),
isProduction: true,
isESM: false,
},
{
outputFile: resolve(buildPath, `${packageName}.mjs`),
isProduction: true,
isESM: true,
},
],
}
}
function build({ packageName, tsConfigPath, externalDependencies, inputFile, outputs }) {
outputs.forEach(async ({ outputFile, isProduction, isESM }) => {
const inputConfig = {
input: inputFile,
plugins: getPlugins({ isProduction, tsConfigPath }),
external: externalDependencies,
}
const outputConfig = {
file: outputFile,
format: isESM ? 'es' : 'cjs',
interop: false,
}
try {
log(`❯ Building(📦): ${packageName} (${isProduction ? 'production' : 'development'})`)
const hrstart = process.hrtime()
const result = await rollup(inputConfig)
const writer = await result.write(outputConfig)
const hrend = process.hrtime(hrstart)
const executionTime = parseInt((hrend[0] * 1e9 + hrend[1]) / 1e6, 10)
const bundleGzipSize = prettyBytes(gzipSize.sync(writer.output[0].code))
log(`❯ Complete(🤘): ${outputFile} (${executionTime}ms) [gzip: ${bundleGzipSize}]`)
} catch (error) {
log(`❯ Building(💥): ${error}`)
}
})
}
function cleanup(packagePath) {
log(`❯ Cleanup: ${packagePath}`)
const { tsConfig } = getTypescriptConfig(packagePath)
try {
rimraf.sync(resolve(packagePath, tsConfig.compilerOptions.outDir))
rimraf.sync(resolve(packagePath, tsConfig.compilerOptions.declarationDir, '*.d.ts'))
} catch (error) {
log(`❯ Cleanup(💥): ${error}`)
}
}
cleanup(packagePath)
build(getPackageData(packagePath))