-
-
Notifications
You must be signed in to change notification settings - Fork 529
/
esbuild.config.prod.mjs
99 lines (94 loc) · 2.14 KB
/
esbuild.config.prod.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
import * as esbuild from 'esbuild'
import cssModulesPlugin from 'esbuild-css-modules-plugin'
import fs from 'fs'
import pkg from './package.json' assert { type: 'json' }
const buildsConfig = [
{
format: 'esm',
outfile: 'dist/react-tooltip.esm.js',
minify: false,
},
{
format: 'cjs',
outfile: 'dist/react-tooltip.cjs.js',
minify: false,
},
{
format: 'iife',
outfile: 'dist/react-tooltip.iife.js',
minify: false,
},
{
format: 'esm',
outfile: 'dist/react-tooltip.js', // for styles be exported as `react-tooltip.css`
minify: false,
},
{
format: 'esm',
outfile: 'dist/react-tooltip.esm.min.js',
minify: true,
},
{
format: 'cjs',
outfile: 'dist/react-tooltip.cjs.min.js',
minify: true,
},
{
format: 'iife',
outfile: 'dist/react-tooltip.iife.min.js',
minify: true,
},
{
format: 'esm',
outfile: 'dist/react-tooltip.min.js',
minify: true,
},
]
const externals = Object.keys({ ...(pkg.peerDependencies ?? {}), ...(pkg.dependencies ?? {}) })
const builds = await Promise.all(
buildsConfig.map(({ format, outfile, minify }) =>
esbuild.build({
entryPoints: ['./src/index.tsx'],
bundle: true,
outfile,
format,
treeShaking: true,
minify,
sourcemap: true,
external: externals,
plugins: [
cssModulesPlugin({
// inject: true,
v2: true,
v2CssModulesOption: {
pattern: `react-tooltip__[local]_[hash]`,
},
}),
],
}),
),
)
const toDelete = new Set()
builds.forEach((build) => {
const outputs = Object.keys(build.metafile.outputs)
outputs.forEach((output) => {
/**
* delete all redundant `.css` and `.css.map` files
* except the ones we actually want to keep
*/
if (/react-tooltip\.css(\.map)?$/.test(output)) {
return
}
if (/\.css(\.map)?$/.test(output)) {
toDelete.add(output)
}
})
})
/**
* delete the extra build files from the CSS build
*/
toDelete.add('dist/react-tooltip.js')
toDelete.add('dist/react-tooltip.js.map')
toDelete.forEach((file) => {
fs.unlink(file, () => null)
})