Open History to see the last commit
1// @ts-check
2β
3import { resolve } from 'node:path'
4import { babel } from '@rollup/plugin-babel'
5import { visualizer } from 'rollup-plugin-visualizer'
6import replace from '@rollup/plugin-replace'
7import { nodeResolve } from '@rollup/plugin-node-resolve'
8import commonJS from '@rollup/plugin-commonjs'
9import externals from 'rollup-plugin-node-externals'
10import preserveDirectives from 'rollup-plugin-preserve-directives'
11import { rootDir } from './config.js'
12β
13/** @param {'development' | 'production'} type */
14const forceEnvPlugin = (type) =>
15 replace({
16 'process.env.NODE_ENV': `"${type}"`,
17 delimiters: ['', ''],
18 preventAssignment: true,
19 })
20β
21/** @param {'legacy' | 'modern'} type */
22const babelPlugin = (type) =>
23 babel({
24 configFile: resolve(rootDir, 'babel.config.cjs'),
25 browserslistConfigFile: type === 'modern' ? true : false,
26 targets:
27 type === 'modern'
28 ? ''
29 : {
30 chrome: '73',
31 firefox: '78',
32 edge: '79',
33 safari: '12',
34 ios: '12',
35 opera: '53',
36 },
37 babelHelpers: 'bundled',
38 exclude: /node_modules/,
39 extensions: ['.ts', '.tsx'],
40 })
41β
42/**
43 * @param {Object} opts - Options for building configurations.
44 * @param {string} opts.name - The name.
45 * @param {string} opts.outputFile - The output file.
46 * @param {string} opts.entryFile - The entry file.
47 * @param {boolean} [opts.bundleDeps] - Flag indicating whether to make all deps external.
48 * @param {boolean} [opts.forceDevEnv] - Flag indicating whether to force development environment.
49 * @param {boolean} [opts.forceBundle] - Flag indicating whether to force bundling.
50 * @returns {import('rollup').RollupOptions[]}
51 */
52export function buildConfigs(opts) {
53 return [modernConfig(opts), legacyConfig(opts)]
54}
55β
56/**
57 * @param {Object} opts - Options for building configurations.
58 * @param {string} opts.name - The name.
59 * @param {string} opts.outputFile - The output file.
60 * @param {string} opts.entryFile - The entry file.
61 * @param {boolean} [opts.bundleDeps] - Flag indicating whether to make all deps external.
62 * @param {boolean} [opts.forceDevEnv] - Flag indicating whether to force development environment.
63 * @param {boolean} [opts.forceBundle] - Flag indicating whether to force bundling.
64 * @returns {import('rollup').RollupOptions}
65 */
66function modernConfig(opts) {
67 const forceDevEnv = opts.forceDevEnv || false
68 const forceBundle = opts.forceBundle || false
69 const bundleDeps = opts.bundleDeps || false
70β
71 /** @type {import('rollup').OutputOptions[]} */
72 const bundleOutput = [
73 {
74 format: 'esm',
75 file: `./build/lib/${opts.outputFile}.js`,
76 sourcemap: true,
77 },
78 {
79 format: 'cjs',
80 file: `./build/lib/${opts.outputFile}.cjs`,
81 sourcemap: true,
82 exports: 'named',
83 },
84 ]
85β
86 /** @type {import('rollup').OutputOptions[]} */
87 const normalOutput = [
88 {
89 format: 'esm',
90 dir: `./build/lib`,
91 sourcemap: true,
92 preserveModules: true,
93 entryFileNames: '[name].js',
94 },
95 {
96 format: 'cjs',
97 dir: `./build/lib`,
98 sourcemap: true,
99 exports: 'named',
100 preserveModules: true,
101 entryFileNames: '[name].cjs',
102 },
103 ]
104β
105 return {
106 input: [opts.entryFile],
107 output: forceBundle ? bundleOutput : normalOutput,
108 plugins: [
109 commonJS(),
110 babelPlugin('modern'),
111 nodeResolve({ extensions: ['.ts', '.tsx'] }),
112 forceDevEnv ? forceEnvPlugin('development') : undefined,
113 bundleDeps
114 ? undefined
115 : externals({
116 packagePath: './package.json',
117 deps: true,
118 devDeps: true,
119 peerDeps: true,
120 }),
121 preserveDirectives(),
122 visualizer({
123 filename: `./build/stats.html`,
124 template: 'treemap',
125 gzipSize: true,
126 }),
127 ],
128 }
129}
130β
131/**
132 * @param {Object} opts - Options for building configurations.
133 * @param {string} opts.name - The name.
134 * @param {string} opts.outputFile - The output file.
135 * @param {string} opts.entryFile - The entry file.
136 * @param {boolean} [opts.bundleDeps] - Flag indicating whether to make all deps external.
137 * @param {boolean} [opts.forceDevEnv] - Flag indicating whether to force development environment.
138 * @param {boolean} [opts.forceBundle] - Flag indicating whether to force bundling.
139 * @returns {import('rollup').RollupOptions}
140 */
141function legacyConfig(opts) {
142 const forceDevEnv = opts.forceDevEnv || false
143 const forceBundle = opts.forceBundle || false
144 const bundleDeps = opts.bundleDeps || false
145β
146 /** @type {import('rollup').OutputOptions[]} */
147 const bundleOutput = [
148 {
149 format: 'esm',
150 file: `./build/lib/${opts.outputFile}.legacy.js`,
151 sourcemap: true,
152 },
153 {
154 format: 'cjs',
155 file: `./build/lib/${opts.outputFile}.legacy.cjs`,
156 sourcemap: true,
157 exports: 'named',
158 },
159 ]
160β
161 /** @type {import('rollup').OutputOptions[]} */
162 const normalOutput = [
163 {
164 format: 'esm',
165 dir: `./build/lib`,
166 sourcemap: true,
167 preserveModules: true,
168 entryFileNames: '[name].legacy.js',
169 },
170 {
171 format: 'cjs',
172 dir: `./build/lib`,
173 sourcemap: true,
174 exports: 'named',
175 preserveModules: true,
176 entryFileNames: '[name].legacy.cjs',
177 },
178 ]
179β
180 return {
181 input: [opts.entryFile],
182 output: forceBundle ? bundleOutput : normalOutput,
183 plugins: [
184 commonJS(),
185 babelPlugin('legacy'),
186 nodeResolve({ extensions: ['.ts', '.tsx'] }),
187 forceDevEnv ? forceEnvPlugin('development') : undefined,
188 bundleDeps
189 ? undefined
190 : externals({
191 packagePath: './package.json',
192 deps: true,
193 devDeps: true,
194 peerDeps: true,
195 }),
196 preserveDirectives(),
197 ],
198 }
199}