Mirror: React hooks for accessible, common web interactions. UI super powers without the UI.
1import { readFileSync } from 'fs';
2import * as path from 'path';
3
4import commonjs from '@rollup/plugin-commonjs';
5import resolve from '@rollup/plugin-node-resolve';
6import terser from '@rollup/plugin-terser';
7import babel from '@rollup/plugin-babel';
8import cjsCheck from 'rollup-plugin-cjs-check';
9import dts from 'rollup-plugin-dts';
10
11const cwd = process.cwd();
12const pkg = JSON.parse(readFileSync(path.resolve(cwd, './package.json'), 'utf-8'));
13
14export const externalModules = ['dns', 'fs', 'path', 'url'];
15if (pkg.peerDependencies)
16 externalModules.push(...Object.keys(pkg.peerDependencies));
17if (pkg.dependencies) externalModules.push(...Object.keys(pkg.dependencies));
18
19const externalPredicate = new RegExp(`^(${externalModules.join('|')})($|/)`);
20
21const terserPretty = terser({
22 warnings: true,
23 ecma: 2015,
24 keep_fnames: true,
25 ie8: false,
26 compress: {
27 pure_getters: true,
28 toplevel: true,
29 booleans_as_integers: false,
30 keep_fnames: true,
31 keep_fargs: true,
32 if_return: false,
33 ie8: false,
34 sequences: false,
35 loops: false,
36 conditionals: false,
37 join_vars: false
38 },
39 mangle: {
40 module: true,
41 keep_fnames: true,
42 },
43 output: {
44 comments: false,
45 beautify: true,
46 braces: true,
47 indent_level: 2
48 }
49});
50
51const terserMinified = terser({
52 warnings: true,
53 ecma: 2015,
54 ie8: false,
55 toplevel: true,
56 compress: {
57 keep_infinity: true,
58 pure_getters: true,
59 passes: 10
60 },
61 mangle: {
62 module: true,
63 },
64 output: {
65 comments: false
66 }
67});
68
69const commonPlugins = [
70 resolve({
71 extensions: ['.mjs', '.js', '.ts'],
72 mainFields: ['module', 'jsnext', 'main'],
73 preferBuiltins: false,
74 browser: true,
75 }),
76
77 commonjs({
78 ignoreGlobal: true,
79 include: /\/node_modules\//,
80 extensions: ['.mjs', '.js', '.ts'],
81 }),
82];
83
84const output = ({ format, isProduction }) => {
85 if (typeof isProduction !== 'boolean')
86 throw new Error('Invalid option `isProduction` at output({ ... })');
87 if (format !== 'cjs' && format !== 'esm')
88 throw new Error('Invalid option `format` at output({ ... })');
89
90 let extension = format === 'esm'
91 ? '.es.js'
92 : '.js';
93 if (isProduction) {
94 extension = '.min' + extension;
95 }
96
97 return {
98 entryFileNames: `[name]${extension}`,
99 dir: './dist',
100 exports: 'named',
101 sourcemap: true,
102 sourcemapExcludeSources: false,
103 hoistTransitiveImports: false,
104 indent: false,
105 freeze: false,
106 strict: false,
107 format,
108 plugins: [
109 cjsCheck({ extension }),
110 isProduction ? terserMinified : terserPretty,
111 ],
112 // NOTE: All below settings are important for cjs-module-lexer to detect the export
113 // When this changes (and terser mangles the output) this will interfere with Node.js ESM intercompatibility
114 esModule: format !== 'esm',
115 externalLiveBindings: format !== 'esm',
116 generatedCode: {
117 preset: 'es5',
118 reservedNamesAsProps: false,
119 objectShorthand: false,
120 constBindings: false,
121 },
122 };
123};
124
125const commonConfig = {
126 input: {
127 'use-interactions': './src/index.ts',
128 },
129 external(id) {
130 return externalPredicate.test(id);
131 },
132 onwarn() {},
133 treeshake: {
134 unknownGlobalSideEffects: false,
135 tryCatchDeoptimization: false,
136 moduleSideEffects: false,
137 },
138};
139
140export default [
141 {
142 ...commonConfig,
143 plugins: [
144 ...commonPlugins,
145 babel({
146 babelrc: false,
147 babelHelpers: 'bundled',
148 extensions: ['js', 'jsx', 'ts', 'tsx'],
149 exclude: 'node_modules/**',
150 presets: [],
151 plugins: [
152 '@babel/plugin-transform-typescript',
153 '@babel/plugin-transform-block-scoping',
154 ],
155 }),
156 ],
157 output: [
158 output({ format: 'cjs', isProduction: false }),
159 output({ format: 'esm', isProduction: false }),
160 output({ format: 'cjs', isProduction: true }),
161 output({ format: 'esm', isProduction: true }),
162 ],
163 },
164 {
165 ...commonConfig,
166 input: {
167 'use-interactions': './src/index.ts',
168 },
169 plugins: [
170 ...commonPlugins,
171 dts({
172 compilerOptions: {
173 preserveSymlinks: false,
174 },
175 }),
176 ],
177 output: {
178 minifyInternalExports: false,
179 entryFileNames: '[name].d.ts',
180 dir: './dist',
181 },
182 },
183];