-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrollup.config.js
123 lines (107 loc) · 3.28 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
113
114
115
116
117
118
119
120
121
122
123
import path from 'path'
import ts from 'rollup-plugin-typescript2'
import json from '@rollup/plugin-json'
import replace from '@rollup/plugin-replace'
import nodeResolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
const { resolveRoot } = require('./scripts/utils')
const pkgJSONPath = resolveRoot('package.json')
const pkg = require(pkgJSONPath)
const name = path.basename(__dirname)
// ensure TS checks only once for each build
let hasTSChecked = false
const outputConfigs = {
cjs: {
file: resolveRoot(`dist/${name}.cjs.js`),
format: `cjs`,
},
esm: {
file: resolveRoot(`dist/${name}.esm.js`),
format: `es`,
},
}
const defaultFormats = ['esm', 'cjs']
const inlineFormats = process.env.FORMATS && process.env.FORMATS.split(',')
const packageFormats = inlineFormats || defaultFormats
const packageConfigs = process.env.PROD_ONLY
? []
: packageFormats.map(format => createConfig(format, outputConfigs[format]))
export default packageConfigs
function createConfig(format, output, plugins = []) {
if (!output) {
throw new Error(`Invalid format: "${format}"`)
}
const isProductionBuild = process.env.__DEV__ === 'false'
const isESMBuild = format === 'esm'
const isNodeBuild = format === 'cjs'
output.exports = 'named'
output.sourcemap = !!process.env.SOURCE_MAP
output.externalLiveBindings = false
const shouldEmitDeclarations = pkg.types && process.env.TYPES != null && !hasTSChecked
const tsPlugin = ts({
check: process.env.NODE_ENV === 'production' && !hasTSChecked,
tsconfig: resolveRoot('tsconfig.json'),
cacheRoot: resolveRoot('node_modules/.rts2_cache'),
tsconfigOverride: {
compilerOptions: {
sourceMap: output.sourcemap,
declaration: shouldEmitDeclarations,
declarationMap: shouldEmitDeclarations,
},
exclude: ['__tests__'],
},
})
// we only need to check TS and generate declarations once for each build.
// it also seems to run into weird issues when checking multiple times
// during a single build.
hasTSChecked = true
const entryFile = `src/index.ts`
return {
input: resolveRoot(entryFile),
external: [...Object.keys(pkg.dependencies || {}), ...Object.keys(pkg.peerDependencies || {})],
plugins: [
tsPlugin,
json({
namedExports: false,
}),
createReplacePlugin(isProductionBuild, isESMBuild, isNodeBuild),
nodeResolve(),
commonjs(),
...plugins,
],
output,
onwarn: (msg, warn) => {
if (!/Circular/.test(msg)) {
warn(msg)
}
},
treeshake: {
moduleSideEffects: false,
},
watch: {
exclude: ['node_modules/**', 'dist/**'],
},
}
}
function createReplacePlugin(isProduction, isESMBuild, isNodeBuild) {
const replacements = {
__VERSION__: `"${pkg.version}"`,
__DEV__: isESMBuild
? // preserve to be handled by bundlers
`(process.env.NODE_ENV !== 'production')`
: // hard coded dev/prod builds
!isProduction,
__ESM__: isESMBuild,
__NODE_JS__: isNodeBuild,
}
// allow inline overrides like
Object.keys(replacements).forEach(key => {
if (key in process.env) {
replacements[key] = process.env[key]
}
})
return replace({
values: replacements,
preventAssignment: true,
})
}