-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.common.js
213 lines (192 loc) · 5.91 KB
/
rollup.config.common.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import babel from "@rollup/plugin-babel"
import commonjs from "@rollup/plugin-commonjs"
import globals from "rollup-plugin-node-globals"
import json from "@rollup/plugin-json"
import replace from "@rollup/plugin-replace"
import resolve from "@rollup/plugin-node-resolve"
import typescript from "rollup-plugin-typescript2"
import { terser } from "rollup-plugin-terser"
import lodash from "lodash"
import Fs from "fs"
import Path from "path"
import { createRequire } from "module"
const { startCase } = lodash
const require = createRequire(import.meta.url)
const __dirname = Path.dirname(new URL(import.meta.url).pathname)
/**
* Return a Rollup configuration for a `pkg` with `env` and `target`.
*/
function configure(pkg, env, target) {
const isProd = env === "production"
const isUmd = target === "umd"
const isModule = target === "module"
const isCommonJs = target === "cjs"
let name = pkg.name.replace("@selkt/", "")
const input = `packages/${name}/src/index.ts`
const deps = []
.concat(pkg.dependencies ? Object.keys(pkg.dependencies) : [])
.concat(pkg.peerDependencies ? Object.keys(pkg.peerDependencies) : [])
// Stop Rollup from warning about circular dependencies.
const onwarn = (warning) => {
if (warning.code !== "CIRCULAR_DEPENDENCY") {
console.warn(`(!) ${warning.message}`) // eslint-disable-line no-console
}
}
const plugins = [
// Allow Rollup to resolve modules from `node_modules`, since it only
// resolves local modules by default.
resolve({
browser: true,
}),
typescript({
abortOnError: true,
tsconfig: `./packages/${name}/tsconfig.json`,
// COMPAT: Without this flag sometimes the declarations are not updated.
clean: true,
check: false,
// Increase to 3 if having issues with types
verbosity: 1,
useTsconfigDeclarationDir: false,
}),
// Allow Rollup to resolve CommonJS modules, since it only resolves ES2015
// modules by default.
commonjs({
exclude: [`packages/${name}/src/**`],
}),
// Convert JSON imports to ES6 modules.
json(),
// Replace `process.env.NODE_ENV` with its value, which enables some modules
// like React and Slate to use their production variant.
replace({
"process.env.NODE_ENV": JSON.stringify(env),
}),
// Register Node.js builtins for browserify compatibility.
// builtins(),
// Use Babel to transpile the result, limiting it to the source code.
babel({
include: [`packages/${name}/src/**`],
extensions: [".js", ".ts", ".tsx"],
babelHelpers: "runtime",
presets: [
"@babel/preset-typescript",
[
"@babel/preset-env",
isUmd
? { modules: false }
: {
exclude: [
"@babel/plugin-transform-regenerator",
"@babel/transform-async-to-generator",
],
modules: false,
targets: {
esmodules: isModule,
},
},
],
"@babel/preset-react",
],
plugins: [
[
"@babel/plugin-transform-runtime",
isUmd
? {}
: {
regenerator: false,
useESModules: isModule,
},
],
"@babel/plugin-proposal-class-properties",
],
}),
// Register Node.js globals for browserify compatibility.
globals(),
// Only minify the output in production, since it is very slow. And only
// for UMD builds, since modules will be bundled by the consumer.
isUmd && isProd && terser(),
].filter(Boolean)
if (isUmd) {
return {
plugins,
input,
onwarn,
output: {
format: "umd",
file: `packages/${name}/${isProd ? pkg.umdMin : pkg.umd}`,
exports: "named",
name: startCase(name).replace(/ /g, ""),
globals: pkg.umdGlobals,
},
external: (id) => {
return !!deps.find((dep) => dep === id || id.startsWith(`${dep}/`))
},
}
}
if (isCommonJs) {
return {
plugins,
input,
onwarn,
output: [
{
file: `packages/${name}/${pkg.main}`,
format: "cjs",
exports: "named",
sourcemap: true,
},
],
// We need to explicitly state which modules are external, meaning that
// they are present at runtime. In the case of non-UMD configs, this means
// all non-Slate packages.
external: (id) => {
return !!deps.find((dep) => dep === id || id.startsWith(`${dep}/`))
},
}
}
if (isModule) {
return {
plugins,
input,
onwarn,
output: [
{
file: `packages/${name}/${pkg.module}`,
format: "es",
sourcemap: true,
},
],
// We need to explicitly state which modules are external, meaning that
// they are present at runtime. In the case of non-UMD configs, this means
// all non-Slate packages.
external: (id) => {
return !!deps.find((dep) => dep === id || id.startsWith(`${dep}/`))
},
}
}
}
/**
* Return a Rollup configuration for a `pkg`.
*/
function factory(src, options = {}) {
const isProd = options.env === "production"
let pkg = require(`${__dirname}/packages/${src}/package.json`)
return [
configure(pkg, "development", "cjs", options),
configure(pkg, "development", "module", options),
isProd && configure(pkg, "development", "umd", options),
isProd && configure(pkg, "production", "umd", options),
].filter(Boolean)
}
const mods = (env) =>
[]
.concat(
...Fs.readdirSync("./packages").map((folder) => {
if (folder.startsWith(".")) return
let folderName = folder.split(Path.sep).pop()
return factory(folderName, { env })
})
)
.filter(Boolean)
// console.log(mods('production').map((mod) => mod.output))
// process.exit()
export default mods