This repository was archived by the owner on Jun 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrollup.config.js
112 lines (103 loc) · 3.31 KB
/
rollup.config.js
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
/** TODO: bundler tricks
* 🔇 shim the console.log on both `bytebeallcore` and `headless-wallet`
*/
import {dirname, resolve, parse} from "path"
import {sync as glob} from "globby"
import {logger} from "@rollup/log"
import {rm, mv} from "shelljs"
import {sync as rmEmptyDir} from "delete-empty"
import nodeResolve from "rollup-plugin-node-resolve"
import json from "rollup-plugin-json"
import typescript from "rollup-plugin-typescript2"
import babel from "rollup-plugin-babel"
const log = logger({timestamp: true})
const {LERNA_PACKAGE_NAME, LERNA_ROOT_PATH, ROLLUP_WATCH} = process.env
let numOutput, count = 0
const lernaInfo = {
name: "lerna-info",
buildStart: input => {
rm("-rf", "dist", ".rpt2_cache", "types")
log.info(`start building ${LERNA_PACKAGE_NAME} 🚧`)
},
generateBundle: output => log.pass(`finish building ${LERNA_PACKAGE_NAME} as ${output.format.toUpperCase()} module 🏁`),
writeBundle: result => {
if (numOutput <= ++count) {
if (!ROLLUP_WATCH) mv("types/*/src/*", "types")
rmEmptyDir("types")
}
},
renderError: error => log.fail(error.message + ' ❌')
}
const splice = (items, name, ...item) => ({
at: (idx, del=0) => {
items.splice(items.indexOf(name) + idx, del, ...item)
return {chain: obj => obj}
}
})
// #region helper
export const pkg = require(resolve(process.cwd(), "package.json"))
export const pkgRoot = require(resolve(LERNA_ROOT_PATH, "package.json"))
export function mapInput(inputs) {
const result = {}
for (const key in inputs) {
if (!key.includes("*")) Object.assign(result, {[key]: inputs[key]})
else { // support glob pattern
const [prefix, suffix] = key.split("*")
const input = glob(resolve(process.cwd(), inputs[key])).reduce(
(obj, item) => (obj[`${prefix}/${parse(item).name}${suffix}`] = item, obj), {}
)
Object.assign(result, input)
}
}
return result
}
export function mapOutput(outputs) {
numOutput = outputs.length
return outputs.map(output => {
const ext = output.format === "es" ? "mjs" : "js"
const dist = output.format === "es" ? pkg.module : pkg.main
const subdir = outputs.length > 1 ? output.format : ''
return {
dir: resolve(dirname(dist), subdir),
// TODO: try https://github.com/rollup/rollup/issues/2336 when merged
chunkFileNames: `chunks/[name]-[hash].${ext}`,
entryFileNames: `[name].${ext}`,
...output
}
})
}
export const modify = plugins => ({
plug: (...plugin) => ({
before: name => splice(plugins, name, ...plugin).at(0).chain(modify(plugins)),
after: name => splice(plugins, name, ...plugin).at(1).chain(modify(plugins)),
replacing: name => splice(plugins, name, ...plugin).at(0, 1).chain(modify(plugins)),
})
})
// #endregion helper
// Rollup Configuration
export default {
watch: {clearScreen: false},
plugins: [
lernaInfo,
json(),
ROLLUP_WATCH && babel({
root: LERNA_ROOT_PATH,
cwd: process.cwd(),
extensions: [".ts"]
}),
nodeResolve({extensions: [".ts"]}),
!ROLLUP_WATCH && typescript({
exclude: ["test/**"],
tsconfig: resolve(LERNA_ROOT_PATH, "tsconfig.json"),
// cacheRoot: `${require("temp-dir")}/.rpt2_cache`, // enable this if it's difficult to read the packages structure
tsconfigOverride: {
compilerOptions: {
module: "esnext",
declaration: true,
declarationDir: `${process.cwd()}/types`
}
},
useTsconfigDeclarationDir: true
}),
],
}