1import commonjs from '@rollup/plugin-commonjs';
2import resolve from '@rollup/plugin-node-resolve';
3import sucrase from '@rollup/plugin-sucrase';
4import buble from '@rollup/plugin-buble';
5import terser from '@rollup/plugin-terser';
6import cjsCheck from 'rollup-plugin-cjs-check';
7import dts from 'rollup-plugin-dts';
8
9import flowTypings from './flow-typings-plugin.mjs';
10import * as types from '../src/types.mjs';
11
12const minify = terser({
13 warnings: true,
14 ecma: 2015,
15 keep_fnames: true,
16 ie8: false,
17 compress: {
18 pure_getters: true,
19 toplevel: true,
20 booleans_as_integers: false,
21 keep_fnames: true,
22 keep_fargs: true,
23 if_return: false,
24 ie8: false,
25 sequences: false,
26 loops: false,
27 conditionals: false,
28 join_vars: false,
29 },
30 mangle: {
31 module: true,
32 keep_fnames: true,
33 },
34 output: {
35 beautify: true,
36 braces: true,
37 indent_level: 2,
38 },
39});
40
41const commonPlugins = [
42 resolve({
43 extensions: ['.mjs', '.js', '.ts'],
44 mainFields: ['module', 'jsnext', 'main'],
45 preferBuiltins: false,
46 browser: true,
47 }),
48
49 commonjs({
50 ignoreGlobal: true,
51 include: /\/node_modules\//,
52 extensions: ['.mjs', '.js', '.ts'],
53 }),
54
55 sucrase({
56 exclude: ['node_modules/**'],
57 transforms: ['typescript']
58 }),
59];
60
61const jsPlugins = [
62 ...commonPlugins,
63 cjsCheck(),
64
65 buble({
66 transforms: {
67 unicodeRegExp: false,
68 defaultParameter: false,
69 dangerousForOf: true,
70 dangerousTaggedTemplateString: true,
71 destructuring: false,
72 asyncAwait: false,
73 arrow: false,
74 classes: false,
75 computedProperty: false,
76 conciseMethodProperty: false,
77 templateString: false,
78 objectRestSpread: false,
79 parameterDestructuring: false,
80 spreadRest: false,
81 },
82 exclude: 'node_modules/**',
83 }),
84];
85
86const dtsPlugins = [
87 ...commonPlugins,
88 dts(),
89 flowTypings(),
90];
91
92const output = format => {
93 const extension = format === 'esm' ? '.mjs' : '.js';
94 return {
95 chunkFileNames: '[hash]' + extension,
96 entryFileNames: '[name]' + extension,
97 dir: './dist',
98 exports: 'named',
99 sourcemap: true,
100 sourcemapExcludeSources: true,
101 indent: false,
102 freeze: false,
103 strict: false,
104 format,
105 // NOTE: All below settings are important for cjs-module-lexer to detect the export
106 // When this changes (and terser mangles the output) this will interfere with Node.js ESM intercompatibility
107 esModule: format !== 'esm',
108 externalLiveBindings: format !== 'esm',
109 generatedCode: {
110 preset: 'es5',
111 reservedNamesAsProps: false,
112 objectShorthand: false,
113 constBindings: false,
114 },
115 plugins: [
116 {
117 renderChunk(code, _chunk) {
118 const kinds = Object.keys(types);
119 const members = Object.values(types)
120 .reduce((acc, item) => [...acc, ...Object.keys(item)], [])
121 const enumRe = new RegExp(`(${kinds.join('|')})[.](${members.join('|')})`, 'g')
122 return code.replace(enumRe, (match, kind, member) => {
123 const value = (types[kind] && types[kind][member]);
124 return value != null ? '' + value : match;
125 });
126 },
127 },
128
129 minify,
130 ]
131 };
132};
133
134const commonConfig = {
135 input: {
136 wonka: './src/index.ts',
137 },
138 onwarn: () => {},
139 external: () => false,
140 treeshake: {
141 unknownGlobalSideEffects: false,
142 tryCatchDeoptimization: false,
143 moduleSideEffects: false,
144 },
145};
146
147const jsConfig = {
148 ...commonConfig,
149 plugins: jsPlugins,
150 output: [
151 output('esm'),
152 output('cjs'),
153 ],
154};
155
156const dtsConfig = {
157 ...commonConfig,
158 input: {
159 wonka: './src/index.ts',
160 },
161 onwarn: () => {},
162 external: () => false,
163 plugins: dtsPlugins,
164 treeshake: {
165 unknownGlobalSideEffects: false,
166 tryCatchDeoptimization: false,
167 moduleSideEffects: false,
168 },
169 output: {
170 dir: './dist',
171 entryFileNames: '[name].d.ts',
172 format: 'es'
173 },
174};
175
176export default [
177 jsConfig,
178 dtsConfig,
179];