-
Notifications
You must be signed in to change notification settings - Fork 11
/
rollup.config.mjs
174 lines (166 loc) · 4.45 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
167
168
169
170
171
172
173
174
import babel from '@rollup/plugin-babel';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import terser from '@rollup/plugin-terser';
import { readFileSync } from 'node:fs';
import * as path from 'path';
import del from 'rollup-plugin-delete';
import dts from 'rollup-plugin-dts';
const pkg = JSON.parse(
readFileSync(new URL('./package.json', import.meta.url))
);
const ROOT_DIR = '.';
const SOURCE_DIR = path.join(ROOT_DIR, 'src');
const OUTPUT_DIR = path.join(ROOT_DIR, 'dist');
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const createBanner = (packageName, version) => `/**
* ${packageName} v${version}
*
* Copyright (c) Olaf Ennen
*
* This source code is licensed under the MIT license found in the
* LICENSE.md file in the root directory of this source tree.
*
* @license MIT
*/`;
// JS modules for bundlers
const modules = [
{
input: path.join(SOURCE_DIR, 'index.ts'),
output: {
file: path.join(OUTPUT_DIR, 'index.js'),
format: 'esm',
banner: createBanner('natural-orderby', pkg.version),
},
plugins: [
nodeResolve({ extensions: ['.ts'] }),
babel({
babelHelpers: 'bundled',
exclude: /node_modules/,
presets: [
['@babel/preset-env', { loose: true }],
'@babel/preset-typescript',
],
extensions: ['.ts'],
}),
],
},
];
// JS modules for <script type=module>
const webModules = [
{
input: path.join(SOURCE_DIR, 'index.ts'),
output: {
file: path.join(OUTPUT_DIR, 'natural-orderby.development.js'),
format: 'esm',
banner: createBanner('natural-orderby', pkg.version),
},
plugins: [
nodeResolve({ extensions: ['.ts'] }),
babel({
babelHelpers: 'bundled',
exclude: /node_modules/,
presets: ['@babel/preset-modules', '@babel/preset-typescript'],
extensions: ['.ts'],
}),
],
},
{
input: path.join(SOURCE_DIR, 'index.ts'),
output: {
file: path.join(OUTPUT_DIR, 'natural-orderby.production.min.js'),
format: 'esm',
banner: createBanner('natural-orderby', pkg.version),
},
plugins: [
nodeResolve({ extensions: ['.ts'] }),
babel({
babelHelpers: 'bundled',
exclude: /node_modules/,
presets: [
[
'@babel/preset-modules',
{
// Don't spoof `.name` for Arrow Functions, which breaks when minified anyway.
loose: true,
},
],
'@babel/preset-typescript',
],
extensions: ['.ts'],
}),
terser({ ecma: 2017, safari10: true }),
],
},
];
// UMD modules for <script> tags and CommonJS (node)
const globals = [
{
input: path.join(SOURCE_DIR, 'index.ts'),
output: {
file: path.join(OUTPUT_DIR, 'umd', 'natural-orderby.development.js'),
format: 'umd',
banner: createBanner('natural-orderby', pkg.version),
name: 'naturalOrderBy',
},
plugins: [
nodeResolve({ extensions: ['.ts'] }),
babel({
babelHelpers: 'bundled',
exclude: /node_modules/,
presets: [
['@babel/preset-env', { loose: true }],
'@babel/preset-typescript',
],
extensions: ['.ts'],
}),
],
},
{
input: path.join(SOURCE_DIR, 'index.ts'),
output: {
file: path.join(OUTPUT_DIR, 'umd', 'natural-orderby.production.min.js'),
format: 'umd',
banner: createBanner('natural-orderby', pkg.version),
name: 'naturalOrderBy',
},
plugins: [
nodeResolve({ extensions: ['.ts'] }),
babel({
babelHelpers: 'bundled',
exclude: /node_modules/,
presets: [
['@babel/preset-env', { loose: true }],
'@babel/preset-typescript',
],
extensions: ['.ts'],
}),
terser(),
],
},
];
// Node entry points
const node = [
{
input: path.join(SOURCE_DIR, 'node-main.js'),
output: {
file: path.join(OUTPUT_DIR, 'main.js'),
format: 'cjs',
banner: createBanner('natural-orderby', pkg.version),
},
},
];
// TypeScript declaration files
const tsdf = [
{
input: path.join(OUTPUT_DIR, 'types', 'index.d.ts'),
output: [{ file: path.join(OUTPUT_DIR, 'index.d.ts'), format: 'es' }],
plugins: [
dts(),
del({
targets: path.join(OUTPUT_DIR, 'types'),
hook: 'buildEnd',
}),
],
},
];
export default [...modules, ...webModules, ...globals, ...node, ...tsdf];