forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.ts
160 lines (144 loc) · 4.25 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
import { promises as fs } from 'fs';
import { resolve } from 'path';
import { env } from 'process';
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 typescript from '@rollup/plugin-typescript';
import type { Plugin, RollupOptions, WarningHandlerWithDefault } from 'rollup';
import { string } from 'rollup-plugin-string';
import { terser } from 'rollup-plugin-terser';
import addCliEntry from './build-plugins/add-cli-entry';
import conditionalFsEventsImport from './build-plugins/conditional-fsevents-import';
import emitModulePackageFile from './build-plugins/emit-module-package-file';
import esmDynamicImport from './build-plugins/esm-dynamic-import';
import getLicenseHandler from './build-plugins/generate-license-file';
import replaceBrowserModules from './build-plugins/replace-browser-modules';
import { version } from './package.json';
async function getBanner(): Promise<string> {
let commitHash: string;
try {
commitHash = await fs.readFile('.commithash', 'utf8');
} catch {
commitHash = 'unknown';
}
const date = new Date(
env.SOURCE_DATE_EPOCH ? 1000 * +env.SOURCE_DATE_EPOCH : Date.now()
).toUTCString();
return `/*
@license
Rollup.js v${version}
${date} - commit ${commitHash}
https://github.com/rollup/rollup
Released under the MIT License.
*/`;
}
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!'
);
throw new Error(warning.message);
};
const moduleAliases = {
entries: {
acorn: resolve('node_modules/acorn/dist/acorn.mjs'),
'help.md': resolve('cli/help.md'),
'package.json': resolve('package.json')
},
resolve: ['.js', '.json', '.md']
};
const treeshake = {
moduleSideEffects: false,
propertyReadSideEffects: false,
tryCatchDeoptimization: false
};
const nodePlugins: Plugin[] = [
alias(moduleAliases),
nodeResolve(),
json(),
conditionalFsEventsImport(),
string({ include: '**/*.md' }),
commonjs({
ignoreTryCatch: false,
include: 'node_modules/**'
}),
typescript()
];
export default async function (
command: Record<string, unknown>
): Promise<RollupOptions | RollupOptions[]> {
const banner = await getBanner();
const { collectLicenses, writeLicense } = getLicenseHandler();
const commonJSBuild: RollupOptions = {
// 'fsevents' is a dependency of 'chokidar' that cannot be bundled as it contains binary code
external: ['fsevents'],
input: {
'loadConfigFile.js': 'cli/run/loadConfigFile.ts',
'rollup.js': 'src/node-entry.ts'
},
onwarn,
output: {
banner,
chunkFileNames: 'shared/[name].js',
dir: 'dist',
entryFileNames: '[name]',
// TODO Only loadConfigFile is using default exports mode; this should be changed in Rollup@3
exports: 'auto',
externalLiveBindings: false,
format: 'cjs',
freeze: false,
generatedCode: 'es2015',
interop: 'default',
manualChunks: { rollup: ['src/node-entry.ts'] },
sourcemap: true
},
plugins: [
...nodePlugins,
addCliEntry(),
esmDynamicImport(),
!command.configTest && collectLicenses()
],
strictDeprecations: true,
treeshake
};
if (command.configTest) {
return commonJSBuild;
}
const esmBuild: RollupOptions = {
...commonJSBuild,
input: { 'rollup.js': 'src/node-entry.ts' },
output: {
...commonJSBuild.output,
dir: 'dist/es',
format: 'es',
minifyInternalExports: false,
sourcemap: false
},
plugins: [...nodePlugins, emitModulePackageFile(), collectLicenses()]
};
const browserBuilds: RollupOptions = {
input: 'src/browser-entry.ts',
onwarn,
output: [
{ banner, file: 'dist/rollup.browser.js', format: 'umd', name: 'rollup', sourcemap: true },
{ banner, file: 'dist/es/rollup.browser.js', format: 'es' }
],
plugins: [
replaceBrowserModules(),
alias(moduleAliases),
nodeResolve({ browser: true }),
json(),
commonjs(),
typescript(),
terser({ module: true, output: { comments: 'some' } }),
collectLicenses(),
writeLicense()
],
strictDeprecations: true,
treeshake
};
return [commonJSBuild, esmBuild, browserBuilds];
}