-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
rollup.config.js
154 lines (140 loc) · 3.93 KB
/
rollup.config.js
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
import fs from 'fs';
import path from 'path';
import alias from 'rollup-plugin-alias';
import commonjs from 'rollup-plugin-commonjs';
import json from 'rollup-plugin-json';
import license from 'rollup-plugin-license';
import resolve from 'rollup-plugin-node-resolve';
import { string } from 'rollup-plugin-string';
import { terser } from 'rollup-plugin-terser';
import typescript from 'rollup-plugin-typescript';
import addBinShebang from './build-plugins/add-bin-shebang';
import conditionalFsEventsImport from './build-plugins/conditional-fsevents-import';
import fixAcornEsmImport from './build-plugins/fix-acorn-esm-import';
import generateLicenseFile from './build-plugins/generate-license-file';
import pkg from './package.json';
const commitHash = (function() {
try {
return fs.readFileSync('.commithash', 'utf-8');
} catch (err) {
return 'unknown';
}
})();
const now = new Date(
process.env.SOURCE_DATE_EPOCH ? process.env.SOURCE_DATE_EPOCH * 1000 : new Date().getTime()
).toUTCString();
const banner = `/*
@license
Rollup.js v${pkg.version}
${now} - commit ${commitHash}
https://github.com/rollup/rollup
Released under the MIT License.
*/`;
const onwarn = 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 = {
resolve: ['.js', '.json', '.md'],
entries: [
{ find: 'help.md', replacement: path.resolve('cli/help.md') },
{ find: 'package.json', replacement: path.resolve('package.json') }
]
};
const treeshake = {
moduleSideEffects: false,
propertyReadSideEffects: false,
tryCatchDeoptimization: false
};
const nodePlugins = [
alias(moduleAliases),
resolve(),
json(),
conditionalFsEventsImport(),
string({ include: '**/*.md' }),
commonjs({ include: 'node_modules/**' }),
typescript({ include: '**/*.{ts,js}' })
];
export default command => {
const commonJSBuild = {
input: {
'rollup.js': 'src/node-entry.ts',
'bin/rollup': 'cli/index.ts'
},
onwarn,
plugins: [
...nodePlugins,
addBinShebang(),
!command.configTest && license({ thirdParty: generateLicenseFile })
],
// acorn needs to be external as some plugins rely on a shared acorn instance
// fsevents is a dependency of chokidar that cannot be bundled as it contains binary code
external: [
'acorn',
'assert',
'crypto',
'events',
'fs',
'fsevents',
'module',
'path',
'os',
'stream',
'util'
],
treeshake,
manualChunks: { rollup: ['src/node-entry.ts'] },
output: {
banner,
chunkFileNames: 'shared-cjs/[name].js',
dir: 'dist',
entryFileNames: '[name]',
externalLiveBindings: false,
format: 'cjs',
freeze: false,
interop: false,
sourcemap: true
}
};
if (command.configTest) {
return commonJSBuild;
}
const esmBuild = Object.assign({}, commonJSBuild, {
input: { 'rollup.es.js': 'src/node-entry.ts' },
plugins: [...nodePlugins, fixAcornEsmImport()],
output: Object.assign({}, commonJSBuild.output, {
chunkFileNames: 'shared-es/[name].js',
format: 'esm',
sourcemap: false
})
});
const browserBuilds = {
input: 'src/browser-entry.ts',
onwarn,
plugins: [
alias(moduleAliases),
resolve({ browser: true }),
json(),
{
load: id => {
if (~id.indexOf('crypto.ts')) return fs.readFileSync('browser/crypto.ts', 'utf-8');
if (~id.indexOf('fs.ts')) return fs.readFileSync('browser/fs.ts', 'utf-8');
if (~id.indexOf('path.ts')) return fs.readFileSync('browser/path.ts', 'utf-8');
}
},
commonjs(),
typescript({ include: '**/*.{ts,js}' }),
terser({ module: true, output: { comments: 'some' } })
],
treeshake,
output: [
{ file: 'dist/rollup.browser.js', format: 'umd', name: 'rollup', banner },
{ file: 'dist/rollup.browser.es.js', format: 'esm', banner }
]
};
return [commonJSBuild, esmBuild, browserBuilds];
};