-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrollup.config.js
209 lines (191 loc) · 5.46 KB
/
rollup.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2020-2022 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: Apache-2.0
/* eslint-disable @typescript-eslint/no-var-requires */
// use require for storybook compatible, some library in yarn.lock only have cjs version of low version.
// if upgrade them may cause storybook crush.
// TODO: use other lightweight document replace storybook
const path = require('node:path');
const typescript = require('@rollup/plugin-typescript');
const svg = require('rollup-plugin-svg');
const { terser } = require('rollup-plugin-terser');
const postcss = require('rollup-plugin-postcss');
const copy = require('rollup-plugin-copy');
const fs = require('fs');
function getExtension(filename) {
const match = filename.match(/(\.d\.ts)$/);
return match ? match[1] : path.extname(filename);
}
function getSubfolders(directoryPath) {
let result = [];
const subfolders = fs
.readdirSync(directoryPath, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => path.join(directoryPath, dirent.name));
result = result.concat(subfolders);
subfolders.forEach((subfolder) => {
result = result.concat(getSubfolders(subfolder));
});
return result;
}
function renameDeclarationFiles() {
return {
name: 'rename-declaration-files',
writeBundle() {
const directories = [path.join(__dirname, 'dist'), ...getSubfolders(path.join(__dirname, 'dist'))];
directories.forEach((directoryPath) => {
fs.readdir(directoryPath, (err, files) => {
if (err) {
return console.log('Unable to scan directory: ' + err);
}
files.forEach((file) => {
if (getExtension(file) === '.d.ts') {
const oldPath = path.join(directoryPath, file);
let newPath = path.join(directoryPath, file.replace('.d.ts', '.cjs.d.ts'));
if (newPath.includes('dist/index.cjs.d.ts')) {
newPath = newPath.replace('dist/index.cjs.d.ts', 'dist/subquery-components.cjs.d.ts');
}
fs.copyFile(oldPath, newPath, (err) => {
if (err) {
console.log('Error renaming file: ' + err);
}
});
}
});
});
});
},
};
}
const outputOptions = {
// TODO arrange them.
globals: {
react: 'react',
antd: 'antd',
'react-dom': 'react-dom',
'react-router-dom': 'react-router-dom',
'react/jsx-runtime': 'react/jsx-runtime',
'@ant-design/icons': '@ant-design/icons',
clsx: 'clsx',
graphiql: 'graphiql',
'@graphiql/toolkit': '@graphiql/toolkit',
'use-screen': 'use-screen',
'react-icons/ai': 'react-icons/ai',
'react-icons/io5': 'react-icons/io5',
'react-jazzicon': 'react-jazzicon',
'react-icons/bs': 'react-icons/bs',
'react-icons/pi': 'react-icons/pi',
'react-icons/fa6': 'react-icons/fa6',
'react-icons/fi': 'react-icons/fi',
'react-icons/ri': 'react-icons/ri',
'react-icons/md': 'react-icons/md',
'antd/es/card/Meta': 'antd/es/card/Meta',
'antd/dist/reset.css': 'antd/dist/reset.css',
'graphiql/graphiql.min.css': 'graphiql/graphiql.min.css',
},
};
const resolvePath = (str) => path.resolve(__dirname, str);
const plugins = [
postcss({
minimize: true,
modules: false,
use: {
less: { javascriptEnabled: true },
},
extract: 'subquery-components.css',
}),
svg(),
terser(),
copy({
targets: [
{
src: 'components/assets',
dest: 'dist',
},
],
}),
];
const esConfig = {
input: {
'subquery-components.es': path.resolve('components/index.ts'),
'common/GraphiQL/index': path.resolve('components/common/GraphiQL/index.ts'),
},
external: [
'react',
'react-dom',
'antd',
'node_modules/*',
'react-router-dom',
'react/jsx-runtime',
'clsx',
'graphiql',
/^@graphiql\/.*/,
'use-screen',
'react-icons/ai',
'react-icons/io5',
'react-jazzicon',
'antd/es/card/Meta',
'react-icons/bs',
'react-icons/pi',
'react-icons/fa6',
'react-icons/fi',
'react-icons/ri',
'react-icons/sl',
'react-icons/md',
'react-icons/go',
'uuid',
'localforage',
'antd/dist/reset.css',
'graphiql/graphiql.min.css',
/^@ant-design\/icons\/.*/,
'react-markdown',
'jdenticon',
'lodash-es',
'ahooks',
],
output: [
{
...outputOptions,
format: 'es',
dir: 'dist',
},
],
plugins: [
...plugins,
typescript({
tsconfig: path.resolve(__dirname, 'tsconfig.json'),
rootDir: resolvePath('components/'),
declaration: true,
declarationDir: resolvePath('dist'),
exclude: [resolvePath('assets/**'), resolvePath('node_modules/**')],
allowSyntheticDefaultImports: true,
}),
],
};
const cjsConfig = {
...esConfig,
input: {
'subquery-components.cjs': path.resolve('components/index.ts'),
'common/GraphiQL/index.cjs': path.resolve('components/common/GraphiQL/index.ts'),
},
output: [
{
...outputOptions,
format: 'cjs',
dir: 'dist',
},
],
plugins: [
...plugins,
typescript({
tsconfig: path.resolve(__dirname, 'tsconfig.json'),
rootDir: resolvePath('components/'),
declaration: true,
declarationDir: resolvePath('dist'),
exclude: [resolvePath('assets/**'), resolvePath('node_modules/**')],
allowSyntheticDefaultImports: true,
}),
renameDeclarationFiles(),
],
};
// cjsConfig
export default [esConfig];