-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.mjs
166 lines (152 loc) · 4.47 KB
/
rollup.config.mjs
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
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import babel from '@rollup/plugin-babel'
import terser from '@rollup/plugin-terser'
import command from 'rollup-plugin-command'
import generateDts from 'rollup-plugin-dts'
import generatePackageJson from 'rollup-plugin-generate-package-json'
// this is weird, but if I dynamically import package.json, node complains
// > TypeError: Module ... needs an import assertion of type "json"
// if I add assertion to import, node complains
// > ExperimentalWarning: Import assertions are not a stable feature of the JavaScript language.
// > Avoid relying on their current behavior and syntax as those might change in a future version of Node.js.
// so I figured I'll just use node's old good require for that:
import { createRequire } from 'node:module'
const require = createRequire(import.meta.url)
const ROOT = dirname(fileURLToPath(import.meta.url))
const ROOT_PACKAGE = resolve(ROOT, 'package.json')
const ROOT_PACKAGE_JSON = require(ROOT_PACKAGE)
const WORKSPACE = process.cwd()
const WORKSPACE_NAME = WORKSPACE.split('/').pop()
const WORKSPACE_PACKAGE = resolve(WORKSPACE, 'package.json')
const WORKSPACE_PACKAGE_JSON = require(WORKSPACE_PACKAGE)
const SRC = resolve(WORKSPACE, 'src')
const BUILD = resolve(ROOT, 'build', 'packages', WORKSPACE_NAME)
const external = [
'effector',
'effector-storage',
...Object.keys(WORKSPACE_PACKAGE_JSON.dependencies || {}),
]
const src = () => ({
input: `${SRC}/index.ts`,
output: [
{
file: `${BUILD}/index.cjs`,
format: 'cjs',
sourcemap: process.env.NODE_ENV === 'production',
externalLiveBindings: false,
esModule: false,
exports: 'named',
},
{
file: `${BUILD}/index.js`,
format: 'es',
sourcemap: process.env.NODE_ENV === 'production',
exports: 'named',
},
],
external,
plugins: [
// resolve typescript files
nodeResolve({
extensions: ['.ts'],
}),
// apply babel transformations
babel({
extensions: ['.ts'],
babelHelpers: 'bundled',
presets: ['@babel/preset-typescript'],
plugins: ['@babel/plugin-transform-block-scoping'],
}),
// minify for production
process.env.NODE_ENV === 'production' &&
terser({
compress: {
ecma: 2017,
keep_fargs: false,
passes: 2,
},
format: {
comments: false,
},
}),
// generate package.json files
generatePackageJson({
baseContents: (pkg) => ({
name: pkg.name,
description: pkg.description,
version: pkg.version,
author: pkg.author || ROOT_PACKAGE_JSON.author,
license: ROOT_PACKAGE_JSON.license,
repository: ROOT_PACKAGE_JSON.repository,
bugs: ROOT_PACKAGE_JSON.bugs,
homepage:
ROOT_PACKAGE_JSON.homepage + '/tree/main/packages/' + WORKSPACE_NAME,
keywords: [...ROOT_PACKAGE_JSON.keywords, ...pkg.keywords],
peerDependencies: {
effector: '^22.4.0 || ^23.0.0',
},
// cjs + esm magic
type: 'module',
sideEffects: false,
types: 'index.d.ts',
module: 'index.js',
main: 'index.cjs',
'react-native': 'index.js',
exports: {
'./package.json': './package.json',
'.': {
import: {
types: './index.d.ts',
default: './index.js',
},
require: {
types: './index.d.cts',
default: './index.cjs',
},
},
},
}),
additionalDependencies: {
'effector-storage': '^6.0.0 || ^7.0.0',
},
}),
// copy license and readme
command([
`cp ${ROOT}/LICENSE ${BUILD}/`,
`cp ${WORKSPACE}/README.md ${BUILD}/`,
]),
],
})
const dts = () => ({
input: `${SRC}/index.ts`,
output: [
{
file: `${BUILD}/index.d.ts`,
format: 'es',
},
],
external,
plugins: [
generateDts({ respectExternal: true }),
command([`pnpm exec prettier --write ${BUILD}/index.d.ts`], { wait: true }),
],
})
const cjsdts = () => ({
input: `${SRC}/index.ts`,
output: [
{
file: `${BUILD}/index.d.cts`,
format: 'es',
},
],
external,
plugins: [
generateDts({ respectExternal: true }),
command([`pnpm exec prettier --write ${BUILD}/index.d.cts`], {
wait: true,
}),
],
})
export default [src(), dts(), cjsdts()]