-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrollup.config.ts
255 lines (234 loc) · 6.8 KB
/
rollup.config.ts
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* @file Rollup configurations for generating AGLint builds.
*
* ! Please ALWAYS use the "yarn build" command for building!
*/
import typescript from '@rollup/plugin-typescript';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import externals from 'rollup-plugin-node-externals';
import dtsPlugin from 'rollup-plugin-dts';
import nodePolyfills from 'rollup-plugin-polyfill-node';
import { getBabelOutputPlugin } from '@rollup/plugin-babel';
import json from '@rollup/plugin-json';
import terser from '@rollup/plugin-terser';
import path from 'path';
import { fileURLToPath } from 'url';
import { readFileSync } from 'fs';
// Common constants
const ROOT_DIR = './';
const BASE_NAME = 'AGLint';
const BASE_FILE_NAME = 'aglint';
const PKG_FILE_NAME = 'package.json';
const distDirLocation = path.join(ROOT_DIR, 'dist');
const pkgFileLocation = path.join(ROOT_DIR, PKG_FILE_NAME);
// Read package.json
const pkg = JSON.parse(readFileSync(pkgFileLocation, 'utf-8'));
// Check if the package.json file has all required fields
// (we need them for the banner)
const REQUIRED_PKG_FIELDS = [
'author',
'homepage',
'license',
'version',
];
for (const field of REQUIRED_PKG_FIELDS) {
if (!(field in pkg)) {
throw new Error(`Missing required field "${field}" in ${PKG_FILE_NAME}`);
}
}
/**
* Shebang for Node.js CLI build
*
* @see {@link https://en.wikipedia.org/wiki/Shebang_(Unix)}
*/
const SHEBANG = '#!/usr/bin/env node';
// Generate a banner with the current package & build info
const BANNER = `/*
* ${BASE_NAME} v${pkg.version} (build date: ${new Date().toUTCString()})
* (c) ${new Date().getFullYear()} ${pkg.author}
* Released under the ${pkg.license} license
* ${pkg.homepage}
*/`;
// Pre-configured TypeScript plugin
const typeScriptPlugin = typescript({
compilerOptions: {
// Don't emit declarations, we will do it in a separate command "yarn build-types"
declaration: false,
},
});
// Common plugins for all types of builds
const commonPlugins = [
json({ preferConst: true }),
commonjs({ sourceMap: false }),
typeScriptPlugin,
];
// Plugins for Node.js builds
const nodePlugins = [
...commonPlugins,
resolve({ preferBuiltins: false }),
externals(),
];
// Plugins for browser builds
const browserPlugins = [
...commonPlugins,
resolve({
browser: true,
preferBuiltins: false,
}),
nodePolyfills(),
// Provide better browser compatibility with Babel
getBabelOutputPlugin({
presets: [
[
'@babel/preset-env',
{
targets: {
// https://github.com/browserslist/browserslist#best-practices
browsers: [
'last 1 version',
'> 1%',
'not dead',
// Specific versions
'chrome >= 88',
'firefox >= 84',
'edge >= 88',
'opera >= 80',
'safari >= 14',
],
},
},
],
],
allowAllFormats: true,
compact: false,
}),
// Minify the output with Terser
terser({
output: {
// Keep the banner in the minified output
preamble: BANNER,
},
}),
];
// CommonJS build configuration
const cjs = {
input: path.join(ROOT_DIR, 'src', 'index.node.ts'),
output: [
{
file: path.join(distDirLocation, `${BASE_FILE_NAME}.js`),
format: 'cjs',
exports: 'auto',
sourcemap: false,
banner: BANNER,
},
],
plugins: [
...nodePlugins,
// Provide better browser compatibility with Babel
getBabelOutputPlugin({
presets: [
[
'@babel/preset-env',
{
// at least Node.js 17
targets: {
node: '17',
},
},
],
],
allowAllFormats: true,
compact: false,
}),
],
};
// ECMAScript build configuration
const esm = {
input: path.join(ROOT_DIR, 'src', 'index.node.ts'),
output: [
{
file: path.join(distDirLocation, `${BASE_FILE_NAME}.esm.mjs`),
format: 'esm',
sourcemap: false,
banner: BANNER,
},
],
plugins: nodePlugins,
};
// Path to the index file of the library
const linterIndex = fileURLToPath(
new URL(
path.join(ROOT_DIR, 'src', 'index.node.ts'),
import.meta.url,
),
);
// CLI tool build
const cli = {
input: path.join(ROOT_DIR, 'src', 'index.cli.ts'),
output: [
{
file: path.join(distDirLocation, `${BASE_FILE_NAME}.cli.js`),
format: 'cjs',
sourcemap: false,
// Replace import './index.node' with './aglint.esm.js'
paths: {
[linterIndex]: path.join('./', `${BASE_FILE_NAME}.js`),
},
banner: `${SHEBANG}\n${BANNER}`,
},
],
external: [
// It is no makes sense to bundle the core library into the CLI tool,
// so we exclude it from the build and instead we import it from the
// same directory where the CLI tool is located
linterIndex,
],
plugins: nodePlugins,
};
// Browser-friendly UMD build configuration
const umd = {
input: path.join(ROOT_DIR, 'src', 'index.browser.ts'),
output: [
{
file: path.join(distDirLocation, `${BASE_FILE_NAME}.umd.min.js`),
name: BASE_NAME,
format: 'umd',
sourcemap: false,
banner: BANNER,
},
],
plugins: browserPlugins,
};
// Browser-friendly IIFE build configuration
const iife = {
input: path.join(ROOT_DIR, 'src', 'index.browser.ts'),
output: [
{
file: path.join(distDirLocation, `${BASE_FILE_NAME}.iife.min.js`),
name: BASE_NAME,
format: 'iife',
sourcemap: false,
banner: BANNER,
},
],
plugins: browserPlugins,
};
// Merge .d.ts files (requires `tsc` to be run first,
// because it merges .d.ts files from `dist/types` directory)
const dts = {
input: path.join(ROOT_DIR, 'dist', 'types', 'src', 'index.node.d.ts'),
output: [
{
file: path.join(distDirLocation, `${BASE_FILE_NAME}.d.ts`),
format: 'es',
banner: BANNER,
},
],
plugins: [
externals(),
dtsPlugin(),
],
};
// Export build configs for Rollup
export default [dts, cjs, esm, cli, umd, iife];