1import * as path from 'path';
2import * as React from 'react';
3
4import commonjs from '@rollup/plugin-commonjs';
5import resolve from '@rollup/plugin-node-resolve';
6import replace from '@rollup/plugin-replace';
7import babel from '@rollup/plugin-babel';
8import visualizer from 'rollup-plugin-visualizer';
9import terser from '@rollup/plugin-terser';
10import cjsCheck from 'rollup-plugin-cjs-check';
11
12import cleanup from './cleanup-plugin.mjs';
13import babelPluginTransformPipe from '../babel/transform-pipe.mjs';
14import babelPluginTransformInvariant from '../babel/transform-invariant-warning.mjs';
15import babelPluginTransformDebugTarget from '../babel/transform-debug-target.mjs';
16
17import * as settings from './settings.mjs';
18
19export const makeBasePlugins = () => [
20 resolve({
21 dedupe: settings.externalModules,
22 extensions: ['.js', '.ts', '.tsx'],
23 mainFields: ['module', 'jsnext', 'main'],
24 preferBuiltins: false,
25 browser: true,
26 }),
27 commonjs({
28 ignoreGlobal: true,
29 include: /\/node_modules\//,
30 }),
31];
32
33export const makePlugins = () => [
34 ...makeBasePlugins(),
35 babel({
36 babelrc: false,
37 babelHelpers: 'bundled',
38 extensions: ['js', 'jsx', 'ts', 'tsx'],
39 exclude: 'node_modules/**',
40 presets: [],
41 plugins: [
42 '@babel/plugin-transform-typescript',
43 '@babel/plugin-transform-block-scoping',
44 babelPluginTransformDebugTarget,
45 babelPluginTransformPipe,
46 babelPluginTransformInvariant,
47 settings.hasReact && [
48 '@babel/plugin-transform-react-jsx',
49 {
50 pragma: 'React.createElement',
51 pragmaFrag: 'React.Fragment',
52 useBuiltIns: true,
53 },
54 ],
55 !settings.hasReact &&
56 settings.hasPreact && [
57 '@babel/plugin-transform-react-jsx',
58 {
59 pragma: 'h',
60 useBuiltIns: true,
61 },
62 ],
63 ].filter(Boolean),
64 }),
65];
66
67export const makeOutputPlugins = ({ isProduction, extension }) => {
68 if (typeof isProduction !== 'boolean')
69 throw new Error(
70 'Missing option `isProduction` on makeOutputPlugins({ ... })'
71 );
72 if (extension !== '.mjs' && extension !== '.js')
73 throw new Error('Missing option `extension` on makeOutputPlugins({ ... })');
74
75 return [
76 isProduction &&
77 replace({
78 'process.env.NODE_ENV': JSON.stringify('production'),
79 }),
80 cjsCheck({ extension }),
81 cleanup(),
82 isProduction ? terserMinified : extension !== '.js' ? terserPretty : null,
83 isProduction &&
84 settings.isAnalyze &&
85 visualizer({
86 filename: path.resolve(
87 settings.cwd,
88 'node_modules/.cache/analyze.html'
89 ),
90 sourcemap: true,
91 }),
92 ].filter(Boolean);
93};
94
95const terserPretty = terser({
96 warnings: true,
97 ecma: 2015,
98 keep_fnames: true,
99 ie8: false,
100 compress: {
101 pure_getters: true,
102 toplevel: true,
103 booleans_as_integers: false,
104 keep_fnames: true,
105 keep_fargs: true,
106 if_return: false,
107 ie8: false,
108 sequences: false,
109 loops: false,
110 conditionals: false,
111 join_vars: false,
112 },
113 mangle: {
114 module: true,
115 keep_fnames: true,
116 },
117 output: {
118 comments: false,
119 beautify: true,
120 braces: true,
121 indent_level: 2,
122 },
123});
124
125const terserMinified = terser({
126 warnings: true,
127 ecma: 2015,
128 ie8: false,
129 toplevel: true,
130 compress: {
131 keep_infinity: true,
132 pure_getters: true,
133 passes: 10,
134 },
135 mangle: {
136 module: true,
137 },
138 output: {
139 comments: false,
140 },
141});