-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathrollup.config.mjs
172 lines (146 loc) · 4.08 KB
/
rollup.config.mjs
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
import { createRequire } from 'node:module';
import { fileURLToPath } from 'node:url';
import babel from '@rollup/plugin-babel';
import nodeResolve from '@rollup/plugin-node-resolve';
import terser from '@rollup/plugin-terser';
import urlPlugin from '@rollup/plugin-url';
import svgr from '@svgr/rollup';
import fs from 'fs';
import { glob } from 'glob';
import path from 'path';
import { nodeExternals } from 'rollup-plugin-node-externals';
import nodePolyfills from 'rollup-plugin-polyfill-node';
import { bundleStats } from 'rollup-plugin-bundle-stats';
const require = createRequire(import.meta.url);
const storyGlob = 'src/*.stor{y,ies}.tsx';
const extensions = ['.ts', '.tsx'];
const {
name,
dependencies,
devDependencies,
peerDependencies,
} = require(path.resolve(process.cwd(), 'package.json'));
const babelConfigPath = fileURLToPath(
new URL('babel.config.js', import.meta.url),
);
const allDependencies = {
...dependencies,
...devDependencies,
...peerDependencies,
};
/**
*
* @returns An array of all glyph import paths
*/
function getDirectGlyphImports() {
const pkgHasIconDependency = allDependencies['@leafygreen-ui/icon'];
const glyphsDir = path.resolve(process.cwd(), '../icon/src/glyphs');
if (pkgHasIconDependency && fs.existsSync(glyphsDir)) {
return fs
.readdirSync(glyphsDir)
.filter(path => /.svg/.test(path))
.map(
fileName =>
`@leafygreen-ui/icon/dist/${path.basename(fileName, '.svg')}`,
);
}
return [];
}
const lgGlobals = Object.keys(allDependencies).reduce((acc, pkg) => {
acc[pkg] = pkg;
return acc;
}, {});
const iconGlobals = getDirectGlyphImports().reduce((acc, glyph) => {
acc[glyph] = /[^/]+$/.exec(glyph)[0];
return acc;
}, {});
// 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 globalsMap = {
clipboard: 'ClipboardJS',
'cross-spawn': 'crossSpawn',
'@emotion/server/create-instance': 'createEmotionServer',
'@emotion/css/create-instance': 'createEmotion',
'highlight.js/lib/core': 'hljs',
'highlightjs-graphql': 'hljsDefineGraphQL',
'fs-extra': 'fse',
polished: 'polished',
react: 'React',
'react-dom': 'ReactDOM',
lodash: '_',
...lgGlobals,
...iconGlobals,
};
const globals = id => {
if (globalsMap[id]) return globalsMap[id];
if (/lodash/.test(id)) return id.replace(/lodash/, '');
if (/highlight\.js\/lib\/languages/.test(id)) {
return id.replace(/highlight\.js\/lib\/languages/, '');
}
};
const external = [/node_modules/];
const moduleFormatToDirectory = {
esm: 'dist/esm/',
umd: 'dist',
};
const configForFormat = format => ({
input: 'src/index.ts',
output: {
dir: moduleFormatToDirectory[format],
name,
format,
sourcemap: true,
globals,
interop: 'compat', // https://rollupjs.org/configuration-options/#output-interop
validate: true,
},
plugins: [
nodePolyfills(),
nodeExternals({ deps: true }),
nodeResolve({ extensions }),
babel({
babelrc: false,
babelHelpers: 'bundled',
extensions,
configFile: babelConfigPath,
sourceMaps: 'inline',
envName: 'production',
}),
urlPlugin({
limit: 50000,
include: ['**/*.png'],
}),
urlPlugin({
limit: 0,
include: ['**/*.less'],
fileName: '[name][extname]',
}),
svgr(),
terser(),
],
external,
strictDeprecations: true,
treeshake: {
preset: 'recommended',
moduleSideEffects: false,
},
});
const esmConfig = configForFormat('esm');
const umdConfig = configForFormat('umd');
const storiesExist = glob.sync(storyGlob).length > 0;
const storiesConfig = {
...esmConfig,
input: glob.sync(storyGlob)[0],
output: {
format: 'esm',
file: 'stories.js',
sourcemap: false,
globals: esmConfig.output.globals,
},
};
const defaultConfig = [esmConfig, umdConfig];
storiesExist && defaultConfig.push(storiesConfig);
export { esmConfig, storiesConfig, umdConfig };
export default defaultConfig;