-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.ts
163 lines (152 loc) · 4.69 KB
/
rollup.config.ts
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
import { fileURLToPath } from 'node:url';
import alias from '@rollup/plugin-alias';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';
import terser from '@rollup/plugin-terser';
import typescript from '@rollup/plugin-typescript';
import type { Plugin, RollupOptions, WarningHandlerWithDefault } from 'rollup';
import { string } from 'rollup-plugin-string';
import addCliEntry from './build-plugins/add-cli-entry';
import { moduleAliases } from './build-plugins/aliases';
import cleanBeforeWrite from './build-plugins/clean-before-write';
import { copyBrowserTypes, copyNodeTypes } from './build-plugins/copy-types';
import emitModulePackageFile from './build-plugins/emit-module-package-file';
import { emitNativeEntry } from './build-plugins/emit-native-entry';
import emitWasmFile from './build-plugins/emit-wasm-file';
import esmDynamicImport from './build-plugins/esm-dynamic-import';
import { externalNativeImport } from './build-plugins/external-native-import';
import { fsEventsReplacement } from './build-plugins/fs-events-replacement';
import getLicenseHandler from './build-plugins/generate-license-file';
import getBanner from './build-plugins/get-banner';
import replaceBrowserModules from './build-plugins/replace-browser-modules';
const onwarn: WarningHandlerWithDefault = warning => {
// eslint-disable-next-line no-console
console.error(
'Building Rollup produced warnings that need to be resolved. ' +
'Please keep in mind that the browser build may never have external dependencies!'
);
// eslint-disable-next-line unicorn/error-message
throw Object.assign(new Error(), warning);
};
const treeshake = {
moduleSideEffects: false,
propertyReadSideEffects: false,
tryCatchDeoptimization: false
};
const nodePlugins: readonly Plugin[] = [
replace(fsEventsReplacement),
alias(moduleAliases),
nodeResolve(),
json(),
string({ include: '**/*.md' }),
commonjs({
ignoreTryCatch: false,
include: 'node_modules/**'
}),
typescript(),
cleanBeforeWrite('dist'),
externalNativeImport()
];
export default async function getConfig(
command: Record<string, unknown>
): Promise<RollupOptions | RollupOptions[]> {
const { collectLicenses, writeLicense } = getLicenseHandler(
fileURLToPath(new URL('.', import.meta.url))
);
const commonJSBuild: RollupOptions = {
// 'fsevents' is a dependency of 'chokidar' that cannot be bundled as it contains binary code
external: ['fsevents'],
input: {
'getLogFilter.js': 'src/utils/getLogFilter.ts',
'loadConfigFile.js': 'cli/run/loadConfigFile.ts',
'parseAst.js': 'src/utils/parseAst.ts',
'rollup.js': 'src/node-entry.ts'
},
onwarn,
output: {
banner: getBanner,
chunkFileNames: 'shared/[name].js',
dir: 'dist',
entryFileNames: '[name]',
exports: 'named',
externalLiveBindings: false,
format: 'cjs',
freeze: false,
generatedCode: 'es2015',
interop: 'default',
sourcemap: true
},
plugins: [
...nodePlugins,
emitNativeEntry(),
addCliEntry(),
esmDynamicImport(),
!command.configTest && collectLicenses(),
copyNodeTypes()
],
strictDeprecations: true,
treeshake
};
if (command.configTest) {
return commonJSBuild;
}
const esmBuild: RollupOptions = {
...commonJSBuild,
input: {
'getLogFilter.js': 'src/utils/getLogFilter.ts',
'parseAst.js': 'src/utils/parseAst.ts',
'rollup.js': 'src/node-entry.ts'
},
output: {
...commonJSBuild.output,
dir: 'dist/es',
format: 'es',
minifyInternalExports: false,
sourcemap: false
},
plugins: [...nodePlugins, emitModulePackageFile(), collectLicenses(), writeLicense()]
};
if (command.configIsBuildNode) {
return [commonJSBuild, esmBuild];
}
const { collectLicenses: collectLicensesBrowser, writeLicense: writeLicenseBrowser } =
getLicenseHandler(fileURLToPath(new URL('browser', import.meta.url)));
const browserBuilds: RollupOptions = {
input: 'src/browser-entry.ts',
onwarn,
output: [
{
banner: getBanner,
file: 'browser/dist/rollup.browser.js',
format: 'umd',
name: 'rollup',
plugins: [copyBrowserTypes()],
sourcemap: true
},
{
banner: getBanner,
file: 'browser/dist/es/rollup.browser.js',
format: 'es',
plugins: [emitModulePackageFile()]
}
],
plugins: [
replaceBrowserModules(),
alias(moduleAliases),
nodeResolve({ browser: true }),
json(),
commonjs(),
typescript(),
terser({ module: true, output: { comments: 'some' } }),
collectLicensesBrowser(),
writeLicenseBrowser(),
cleanBeforeWrite('browser/dist'),
emitWasmFile()
],
strictDeprecations: true,
treeshake
};
return [commonJSBuild, esmBuild, browserBuilds];
}