-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollup.config.mjs
More file actions
139 lines (135 loc) · 4.47 KB
/
rollup.config.mjs
File metadata and controls
139 lines (135 loc) · 4.47 KB
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
// modules
import clean from '@rollup-extras/plugin-clean';
import progress from 'rollup-plugin-progress';
import tsConfigPaths from 'rollup-plugin-tsconfig-paths';
import typescript from 'rollup-plugin-typescript2';
import external from 'rollup-plugin-peer-deps-external';
import dynamicImportVars from '@rollup/plugin-dynamic-import-vars';
import commonjs from '@rollup/plugin-commonjs';
import { babel } from '@rollup/plugin-babel';
import strip from '@rollup/plugin-strip';
import stripCode from 'rollup-plugin-strip-code';
import replace from '@rollup/plugin-replace';
import terser from '@rollup/plugin-terser';
import filesize from 'rollup-plugin-filesize';
import { visualizer } from 'rollup-plugin-visualizer';
// local
import { createRequire } from 'module';
const pkg = createRequire(import.meta.url)('./package.json');
// prod build
const production = process.env.NODE_ENV === 'production';
export function emitModulePackageFile() {
return {
name: 'emit-module-package-file',
generateBundle() {
this.emitFile({
type: 'asset',
fileName: 'package.json',
source: `{"type":"module"}`
});
}
};
}
export default {
input: 'src/index.ts',
external: (id) => {
if (Object.keys(pkg.dependencies).includes(id) || id === 'module') return true;
return false;
},
output: [
{ prop: 'main', format: 'cjs' },
{ prop: 'module', format: 'es' }
].map(({ prop, format }) => ({
file: pkg[prop],
format,
sourcemap: !production && 'inline',
...(format === 'es' ? { plugins: [emitModulePackageFile()] } : {})
})),
plugins: [
progress({
clearLine: true
}),
clean({
targets: ['./dist', './stats'],
deleteOnce: true
}),
...((production && [
stripCode({
start_comment: 'NO_PRODUCTION_START',
end_comment: 'NO_PRODUCTION_END'
})
]) ||
[]),
tsConfigPaths(),
typescript({ useTsconfigDeclarationDir: true }),
external(),
dynamicImportVars(),
commonjs(),
babel({
babelHelpers: 'bundled',
exclude: 'node_modules/**'
}),
...((production && [
strip({
debugger: true,
sourceMap: true,
functions: ['assert.*']
}),
replace({
preventAssignment: true,
...[
{ key: 'NODE_ENV', value: 'production' },
{ key: 'MODULE_NAME', value: pkg.name },
{ key: 'MODULE_VERSION', value: pkg.version }
].reduce((obj, { key, value }) => ({ ...obj, [`process.env.${key}`]: JSON.stringify(value) }), {})
}),
terser({
toplevel: false,
compress: {
passes: 4
},
output: {
ascii_only: true,
preamble: [
'/*',
` Ⓒ __copyright __company\n`,
' package: __projectName',
' version: __buildVersion',
' date: __buildDate',
'*/'
].join('\n')
}
}),
replace({
preventAssignment: true,
__copyright: new Date().getFullYear(),
__company: pkg.author.split(' ')[0],
__projectName: pkg.name,
__buildVersion: pkg.version,
__buildDate: () => new Date().toUTCString()
}),
...['treemap', 'sunburst'].reduce(
(arr, type) =>
arr.push(
visualizer({
filename: `./stats/${type}_${pkg.version}_${new Date().getTime()}.html`,
template: type,
gzipSize: true,
brotliSize: true
})
) && arr,
[]
)
]) || [
replace({
preventAssignment: true,
__buildVersion: `${pkg.version}-dev`
})
]),
filesize({
showMinifiedSize: true,
showGzippedSize: true,
showBrotliSize: true
})
]
};