forked from kentcdodds/kcd-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollup.js
More file actions
176 lines (149 loc) · 4.93 KB
/
rollup.js
File metadata and controls
176 lines (149 loc) · 4.93 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const path = require('path')
const fs = require('fs')
const spawn = require('cross-spawn')
const glob = require('glob')
const rimraf = require('rimraf')
const yargsParser = require('yargs-parser')
const {
hasFile,
resolveBin,
fromRoot,
toPOSIX,
getConcurrentlyArgs,
writeExtraEntry,
hasTypescript,
generateTypeDefs,
getRollupInputs,
getRollupOutput,
} = require('../../utils')
const crossEnv = resolveBin('cross-env')
const rollup = resolveBin('rollup')
const args = process.argv.slice(2)
const here = p => path.join(__dirname, p)
const hereRelative = p => here(p).replace(process.cwd(), '.')
const parsedArgs = yargsParser(args)
const useBuiltinConfig =
!args.includes('--config') && !hasFile('rollup.config.js')
const config = useBuiltinConfig
? `--config ${hereRelative('../../config/rollup.config.js')}`
: args.includes('--config')
? ''
: '--config' // --config will pick up the rollup.config.js file
const environment = parsedArgs.environment
? `--environment ${parsedArgs.environment}`
: ''
const watch = parsedArgs.watch ? '--watch' : ''
const sizeSnapshot = parsedArgs['size-snapshot']
let formats = ['esm', 'cjs', 'umd', 'umd.min']
if (typeof parsedArgs.bundle === 'string') {
formats = parsedArgs.bundle.split(',')
}
const defaultEnv = 'BUILD_ROLLUP=true'
const getCommand = (env, ...flags) =>
[crossEnv, defaultEnv, env, rollup, config, environment, watch, ...flags]
.filter(Boolean)
.join(' ')
const buildPreact = args.includes('--p-react')
const scripts = getConcurrentlyArgs(
buildPreact ? getPReactCommands() : getCommands(),
)
const cleanBuildDirs = !args.includes('--no-clean')
if (cleanBuildDirs) {
rimraf.sync(fromRoot('dist'))
if (buildPreact) {
rimraf.sync(fromRoot('preact'))
}
}
function go() {
let result = spawn.sync(resolveBin('concurrently'), scripts, {
stdio: 'inherit',
})
if (result.status !== 0) return result.status
if (buildPreact && !args.includes('--no-package-json')) {
writeExtraEntry(
'preact',
{
cjs: glob.sync(toPOSIX(fromRoot('preact/**/*.cjs.cjs')))[0],
esm: glob.sync(toPOSIX(fromRoot('preact/**/*.esm.mjs')))[0],
},
false,
)
}
if (hasTypescript && !args.includes('--no-ts-defs')) {
console.log('Generating TypeScript definitions')
result = generateTypeDefs(fromRoot('dist'))
if (result.status !== 0) return result.status
const rollupInputs = getRollupInputs()
const typeDefFiles = rollupInputs.map(input => {
return input
.replace(path.join(process.cwd(), 'src'), 'dist')
.replace(/\.(t|j)sx?$/, '.d.ts')
})
for (const format of formats) {
const {dirpath, filename} = getRollupOutput(format)
const isCodesplitting = rollupInputs.length > 1
const outputs = isCodesplitting
? glob.sync(toPOSIX(fromRoot(path.posix.join(dirpath, format, '*.js'))))
: [fromRoot(path.join(dirpath, filename))]
for (const output of outputs) {
const {name, dir} = path.parse(output)
const typeDef = isCodesplitting
? typeDefFiles.find(f => path.basename(f) === `${name}.d.ts`)
: 'dist/index.d.ts'
const relativePath = path
.join(path.relative(dir, process.cwd()), typeDef)
.replace(/\.d\.ts$/, '')
// make a .d.ts file for every generated file that re-exports index.d.ts
fs.writeFileSync(
path.join(dir, `${name}.d.ts`),
`export * from "${relativePath}";\n`,
)
}
}
// because typescript generates type defs for ignored files, we need to
// remove the ignored files
const ignoredFiles = [
...glob.sync(toPOSIX(fromRoot('dist', '**/__tests__/**'))),
...glob.sync(toPOSIX(fromRoot('dist', '**/__mocks__/**'))),
]
ignoredFiles.forEach(ignoredFile => {
rimraf.sync(ignoredFile)
})
console.log('TypeScript definitions generated')
}
return result.status
}
function getPReactCommands() {
return {
...prefixKeys('react.', getCommands()),
...prefixKeys('preact.', getCommands({preact: true})),
}
}
function prefixKeys(prefix, object) {
return Object.entries(object).reduce((cmds, [key, value]) => {
cmds[`${prefix}${key}`] = value
return cmds
}, {})
}
function getCommands({preact = false} = {}) {
return formats.reduce((cmds, format) => {
const [formatName, minify = false] = format.split('.')
const nodeEnv = minify ? 'production' : 'development'
const sourceMap = formatName === 'umd' ? '--sourcemap' : ''
const buildMinify = Boolean(minify)
cmds[format] = getCommand(
[
`BUILD_FORMAT=${formatName}`,
`BUILD_MINIFY=${buildMinify}`,
`NODE_ENV=${nodeEnv}`,
`BUILD_PREACT=${preact}`,
`BUILD_SIZE_SNAPSHOT=${sizeSnapshot}`,
`BUILD_NODE=${process.env.BUILD_NODE || false}`,
`BUILD_REACT_NATIVE=${process.env.BUILD_REACT_NATIVE || false}`,
].join(' '),
sourceMap,
)
return cmds
}, {})
}
process.exit(go())