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β
21const babelPlugin = () =>
22 babel({
23 configFile: resolve(rootDir, 'babel.config.cjs'),
24 browserslistConfigFile: true,
25 babelHelpers: 'bundled',
26 exclude: /node_modules/,
27 extensions: ['.ts', '.tsx', '.native.ts'],
28 })
29β
30/**
31 * @param {Object} opts - Options for building configurations.
32 * @param {string} opts.name - The name.
33 * @param {string} opts.outputFile - The output file.
34 * @param {string} opts.entryFile - The entry file.
35 * @param {boolean} [opts.bundleDeps] - Flag indicating whether to make all deps external.
36 * @param {boolean} [opts.forceDevEnv] - Flag indicating whether to force development environment.
37 * @param {boolean} [opts.forceBundle] - Flag indicating whether to force bundling.
38 * @returns {import('rollup').RollupOptions}
39 */
40export function buildConfigs(opts) {
41 const input = [opts.entryFile]
42 const forceDevEnv = opts.forceDevEnv || false
43 const forceBundle = opts.forceBundle || false
44 const bundleDeps = opts.bundleDeps || false
45β
46 /** @type {import('rollup').OutputOptions[]} */
47 const bundleOutput = [
48 {
49 format: 'esm',
50 file: `./build/lib/${opts.outputFile}.js`,
51 sourcemap: true,
52 },
53 {
54 format: 'cjs',
55 file: `./build/lib/${opts.outputFile}.cjs`,
56 sourcemap: true,
57 exports: 'named',
58 },
59 ]
60β
61 /** @type {import('rollup').OutputOptions[]} */
62 const normalOutput = [
63 {
64 format: 'esm',
65 dir: `./build/lib`,
66 sourcemap: true,
67 preserveModules: true,
68 entryFileNames: '[name].js',
69 },
70 {
71 format: 'cjs',
72 dir: `./build/lib`,
73 sourcemap: true,
74 exports: 'named',
75 preserveModules: true,
76 entryFileNames: '[name].cjs',
77 },
78 ]
79β
80 return {
81 input,
82 output: forceBundle ? bundleOutput : normalOutput,
83 plugins: [
84 commonJS(),
85 babelPlugin(),
86 nodeResolve({ extensions: ['.ts', '.tsx', '.native.ts'] }),
87 forceDevEnv ? forceEnvPlugin('development') : undefined,
88 bundleDeps
89 ? undefined
90 : externals({
91 packagePath: './package.json',
92 deps: true,
93 devDeps: true,
94 peerDeps: true,
95 }),
96 preserveDirectives(),
97 visualizer({
98 filename: `./build/stats.html`,
99 template: 'treemap',
100 gzipSize: true,
101 }),
102 ],
103 }
104}