forked from mongodb/leafygreen-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
178 lines (153 loc) · 4.41 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
import fs from 'fs';
import path from 'path';
import babel from '@rollup/plugin-babel';
import resolve from '@rollup/plugin-node-resolve';
import svgr from '@svgr/rollup';
import { terser } from 'rollup-plugin-terser';
import urlPlugin from '@rollup/plugin-url';
const extensions = ['.ts', '.tsx'];
const { name } = require(path.resolve(process.cwd(), 'package.json'));
function getAllPackages(dir) {
const dirList = fs.readdirSync(dir);
return dirList
.map(subDir => `${path.resolve(dir, subDir)}/package.json`)
.filter(packageJsonPath => fs.existsSync(packageJsonPath))
.map(packageJsonPath => require(packageJsonPath).name);
}
function getLodashExternals() {
return fs
.readdirSync(path.resolve(__dirname, 'node_modules', 'lodash'))
.filter(fileName => {
const splitName = fileName.split('.');
const extension = splitName[splitName.length - 1];
return extension === 'js';
})
.map(fileName => 'lodash/' + fileName.split('.')[0]);
}
function getDirectGlyphImports() {
const glyphsDir = path.resolve(__dirname, './packages/icon/src/glyphs');
return fs
.readdirSync(glyphsDir)
.filter(path => /.svg/.test(path))
.map(
fileName => `@leafygreen-ui/icon/dist/${path.basename(fileName, '.svg')}`,
);
}
function getGeneratedFiles() {
const directory = path.resolve(process.cwd(), 'src/generated');
if (!fs.existsSync(directory)) {
return [];
}
return fs
.readdirSync(directory)
.filter(file => /\.tsx?$/.test(file))
.map(file => path.resolve(directory, file));
}
const allPackages = getAllPackages(path.resolve(__dirname, 'packages'));
const directGlyphImports = getDirectGlyphImports();
// Mapping of packages to the `window` property they'd be
// bound to if used in the browser without a module loader.
// This is defined on a best effort basis since not all
// modules are compatible with being loaded directly.
const globals = {
clipboard: 'ClipboardJS',
'highlightjs-graphql': 'hljsDefineGraphQL',
polished: 'polished',
react: 'React',
'react-dom': 'ReactDOM',
'prop-types': 'PropTypes',
lodash: '_',
/**
* External dependencies that must be loaded by a module loader
* - lodash/*
* - highlight.js
* - create-emotion
* - create-emotion-server
* - react-transition-group
**/
};
allPackages.forEach(packageName => {
globals[packageName] = packageName;
});
directGlyphImports.forEach(glyphImport => {
// e.g. "@leafygreen-ui/icon/dist/Checkmark" -> "Checkmark"
globals[glyphImport] = /[^/]+$/.exec(glyphImport)[0];
});
const moduleFormatToDirectory = {
esm: 'dist/esm/',
umd: 'dist',
};
const config = ['esm', 'umd'].flatMap(format => {
const baseConfig = {
input: 'src/index.ts',
output: {
dir: moduleFormatToDirectory[format],
name,
format,
sourcemap: true,
globals,
},
plugins: [
resolve({ extensions }),
babel({
babelrc: false,
babelHelpers: 'bundled',
extensions,
configFile: path.resolve(__dirname, 'babel.config.js'),
sourceMaps: 'inline',
envName: 'production',
}),
urlPlugin({
limit: 50000,
include: ['**/*.png'],
}),
urlPlugin({
limit: 0,
include: ['**/*.less'],
fileName: '[name][extname]',
}),
svgr(),
terser(),
],
external: id =>
[
'clipboard',
'highlight.js',
'highlightjs-graphql',
'react',
'react-dom',
'@emotion/react',
'@emotion/server',
'@emotion/css',
'@emotion/css/create-instance',
'@emotion/server/create-instance',
'polished',
'prop-types',
'react-is',
'react-transition-group',
'@testing-library/react',
'lodash',
'use-ssr',
'focus-trap-react',
...getLodashExternals(),
...allPackages,
...directGlyphImports,
].includes(id) ||
// We test if an import includes lodash to avoid having
// to whitelist every nested lodash module individually
/^lodash\//.test(id) ||
/^highlight\.js\//.test(id),
};
return [
baseConfig,
...getGeneratedFiles().map(input => ({
...baseConfig,
input: `src/generated/${path.basename(input)}`,
output: {
...baseConfig.output,
name: `${path.basename(input, path.extname(input))}.js`,
},
})),
];
});
export default config;