UNPKG

3.57 kBPlain TextView Raw
1import babel from '@rollup/plugin-babel';
2import { terser } from 'rollup-plugin-terser';
3import { nodeResolve } from '@rollup/plugin-node-resolve';
4import { camelCase, startCase } from 'lodash';
5
6const pkg = require('./package.json');
7const libraryName = pkg.name.replace('@', '').replace('/', '-');
8const globals = {
9 react: 'React',
10 'react-dom': 'ReactDOM',
11 rxjs: 'Rx',
12 immer: 'immer',
13 'prop-types': 'PropTypes',
14 'styled-components': 'styled',
15 lodash: '_',
16 '@wener/utils': 'WenerUtils',
17};
18const external = Object.keys(require('./package.json').peerDependencies || {}).filter((v) => !['immer'].includes(v));
19export default [
20 ...buildRollup({
21 libraryName,
22 input: 'src/index.ts',
23 globals,
24 external,
25 }),
26 ...buildRollup({
27 libraryName: libraryName + '-antds',
28 input: 'src/antds/index.ts',
29 globals,
30 external,
31 }),
32 ...buildRollup({
33 libraryName: libraryName + '-icons',
34 input: 'src/icons/index.ts',
35 globals,
36 external,
37 }),
38 ...buildRollup({
39 libraryName: libraryName + '-wener',
40 input: 'src/wener/index.ts',
41 globals,
42 external,
43 }),
44].map(report);
45function report(v) {
46 console.log(`${v.input} -> ${v.output.map((v) => v.file).join(', ')}`);
47 return v;
48}
49function onwarn(warning) {
50 if (warning.code === 'THIS_IS_UNDEFINED') {
51 return;
52 }
53 console.warn(warning.code, warning.message);
54}
55
56function addMini({ options = {} } = {}) {
57 return (src) => {
58 const mini = {
59 ...src,
60 output: src.output.map((o) => ({ ...o, file: o.file.replace(/([.]\w+)$/, '.min$1') })),
61 plugins: [...src.plugins],
62 };
63
64 const r = [src];
65 const esm = mini.output.filter((v) => v.format === 'esm');
66 const opt = { ...options };
67 if (esm.length) {
68 mini.output = mini.output.filter((v) => !esm.includes(v));
69
70 r.push({
71 ...mini,
72 output: esm,
73 plugins: [...mini.plugins, terser({ ...opt, ecma: 6, module: true })],
74 });
75 }
76 if (mini.output.length) {
77 mini.plugins.push(terser(opt));
78 r.push(mini);
79 }
80 return r;
81 };
82}
83
84// yarn add -D rollup @rollup/plugin-{commonjs,node-resolve,typescript,json} tslib typescript
85function buildRollup({
86 libraryName = require('./package.json').name.replace('@', '').replace('/', '-'),
87 input = `src/index.ts`,
88 dev = (process.env.NODE_ENV || '').startsWith('dev'),
89 external = Object.keys(require('./package.json').peerDependencies || {}),
90 globalName = startCase(camelCase(libraryName)).replace(/\W/g, ''),
91 globals = {},
92}) {
93 return [
94 {
95 input,
96 onwarn,
97 external,
98 plugins: [
99 nodeResolve({
100 browser: true,
101 mainFields: ['module', 'main'],
102 extensions: ['.js', '.ts', '.jsx', '.tsx'],
103 }),
104 babel({
105 babelHelpers: 'bundled',
106 babelrc: false,
107 presets: ['@babel/preset-env', '@babel/preset-typescript', '@babel/preset-react'],
108 plugins: [['@babel/plugin-proposal-class-properties', { loose: true }]],
109 extensions: ['.js', '.ts', '.jsx', '.tsx'],
110 }),
111 ],
112 output: [
113 {
114 file: `dist/${libraryName}.umd.js`,
115 name: globalName,
116 format: 'umd',
117 sourcemap: true,
118 globals,
119 },
120 { file: `dist/${libraryName}.cjs`, format: 'cjs', sourcemap: true },
121 { file: `dist/${libraryName}.system.js`, format: 'system', sourcemap: true },
122 { file: `dist/${libraryName}.esm.js`, format: 'esm', sourcemap: true },
123 ],
124 },
125 ].flatMap(dev ? (v) => v : addMini());
126}