1import { resolve, basename } from 'path';
2import commonjs from '@rollup/plugin-commonjs';
3import nodeResolve from '@rollup/plugin-node-resolve';
4import typescript from 'rollup-plugin-typescript2';
5import buble from '@rollup/plugin-buble';
6import babel from 'rollup-plugin-babel';
7import { terser } from 'rollup-plugin-terser';
8import compiler from '@ampproject/rollup-plugin-closure-compiler';
9import prettier from 'rollup-plugin-prettier';
10
11import minifyBucklescript from './scripts/minify-bucklescript-plugin';
12
13const cwd = process.cwd();
14const pkgInfo = require('./package.json');
15const name = basename(pkgInfo.main, '.js');
16
17const terserPretty = terser({
18 warnings: true,
19 ecma: 5,
20 ie8: false,
21 compress: {
22 hoist_vars: true,
23 hoist_funs: true,
24 pure_getters: true,
25 toplevel: true,
26 booleans_as_integers: false,
27 if_return: false,
28 ie8: false,
29 sequences: false,
30 loops: false,
31 conditionals: false,
32 join_vars: false,
33 passes: 3,
34 },
35 mangle: false,
36 output: {
37 beautify: true,
38 braces: true,
39 indent_level: 2,
40 },
41});
42
43const terserMinified = terser({
44 warnings: true,
45 ecma: 5,
46 ie8: false,
47 toplevel: true,
48 mangle: true,
49 compress: {
50 keep_infinity: true,
51 pure_getters: true,
52 passes: 10,
53 },
54 output: {
55 comments: false,
56 },
57});
58
59const importAllPlugin = ({ types: t }) => ({
60 visitor: {
61 VariableDeclarator(path) {
62 if (
63 t.isIdentifier(path.node.id) &&
64 t.isCallExpression(path.node.init) &&
65 t.isIdentifier(path.node.init.callee) &&
66 path.node.init.callee.name === 'require' &&
67 path.node.init.arguments.length === 1
68 ) {
69 path.parentPath.replaceWith(
70 t.importDeclaration(
71 [t.importNamespaceSpecifier(path.node.id)],
72 path.node.init.arguments[0]
73 )
74 );
75 }
76 },
77 },
78});
79
80const makePlugins = (isProduction) =>
81 [
82 babel({
83 babelrc: false,
84 extensions: ['ts', 'tsx', 'js'],
85 exclude: 'node_modules/**',
86 presets: [],
87 plugins: ['@babel/plugin-syntax-typescript', importAllPlugin],
88 }),
89 typescript({
90 typescript: require('typescript'),
91 cacheRoot: './node_modules/.cache/.rts2_cache',
92 useTsconfigDeclarationDir: true,
93 tsconfigOverride: {
94 compilerOptions: {
95 strict: false,
96 noUnusedParameters: false,
97 declaration: !isProduction,
98 declarationDir: resolve(cwd, './dist/types/'),
99 target: 'esnext',
100 module: 'es2015',
101 rootDir: cwd,
102 },
103 },
104 }),
105 commonjs({
106 ignoreGlobal: true,
107 include: ['*', '**'],
108 extensions: ['.js', '.ts', '.tsx'],
109 }),
110 nodeResolve({
111 mainFields: ['module', 'jsnext', 'main'],
112 extensions: ['.js', '.ts', '.tsx'],
113 browser: true,
114 }),
115 buble({
116 transforms: {
117 unicodeRegExp: false,
118 dangerousForOf: true,
119 dangerousTaggedTemplateString: true,
120 },
121 objectAssign: 'Object.assign',
122 exclude: 'node_modules/**',
123 }),
124 babel({
125 babelrc: false,
126 extensions: ['ts', 'tsx', 'js'],
127 exclude: 'node_modules/**',
128 presets: [],
129 plugins: ['babel-plugin-closure-elimination'],
130 }),
131 minifyBucklescript(),
132 compiler({
133 formatting: 'PRETTY_PRINT',
134 compilation_level: 'SIMPLE_OPTIMIZATIONS',
135 }),
136 isProduction ? terserMinified : terserPretty,
137 !isProduction &&
138 prettier({
139 parser: 'babel',
140 tabWidth: 2,
141 printWidth: 100,
142 singleQuote: true,
143 }),
144 ].filter(Boolean);
145
146const config = {
147 input: './src/Wonka.ts',
148 onwarn: () => {},
149 external: () => false,
150 treeshake: {
151 propertyReadSideEffects: false,
152 },
153};
154
155export default [
156 {
157 ...config,
158 plugins: makePlugins(false),
159 output: [
160 {
161 legacy: true,
162 freeze: false,
163 esModule: false,
164 file: `./dist/${name}.js`,
165 format: 'cjs',
166 },
167 {
168 compact: true,
169 file: `./dist/${name}.mjs`,
170 format: 'esm',
171 },
172 ],
173 },
174 {
175 ...config,
176 plugins: makePlugins(true),
177 output: [
178 {
179 legacy: true,
180 freeze: false,
181 esModule: false,
182 file: `./dist/${name}.min.js`,
183 format: 'cjs',
184 },
185 ],
186 },
187];