/
rollup.config.js
150 lines (139 loc) · 4 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
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 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 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 expectedAcornImport = "import acorn__default, { tokTypes, Parser as Parser$1 } from 'acorn';";
const newAcornImport =
"import * as acorn__default from 'acorn';\nimport { tokTypes, Parser as Parser$1 } from 'acorn';";
// by default, rollup-plugin-commonjs will translate require statements as default imports
// which can cause issues for secondary tools that use the ESM version of acorn
function fixAcornEsmImport() {
return {
name: 'fix-acorn-esm-import',
renderChunk(code, chunk, options) {
if (/esm?/.test(options.format)) {
let found = false;
const fixedCode = code.replace(expectedAcornImport, () => {
found = true;
return newAcornImport;
});
if (!found) {
this.error(
'Could not find expected acorn import, please deactive this plugin and examine generated code.'
);
}
return fixedCode;
}
}
};
}
const moduleAliases = {
resolve: ['.js', '.json', '.md'],
'acorn-dynamic-import': path.resolve('node_modules/acorn-dynamic-import/src/index.js'),
'help.md': path.resolve('bin/src/help.md'),
'package.json': path.resolve('package.json')
};
export default command => {
const nodeBuilds = [
/* Rollup core node builds */
{
input: 'src/node-entry.ts',
onwarn,
plugins: [
alias(moduleAliases),
resolve(),
json(),
typescript(),
commonjs({ include: 'node_modules/**', namedExports: { micromatch: ['matcher'] } }),
fixAcornEsmImport()
],
// acorn needs to be external as some plugins rely on a shared acorn instance
external: ['fs', 'path', 'events', 'module', 'util', 'crypto', 'acorn', 'tty', 'net', 'url'],
output: [
{ file: 'dist/rollup.js', format: 'cjs', sourcemap: true, banner },
{ file: 'dist/rollup.es.js', format: 'esm', banner }
]
},
/* Rollup CLI */
{
input: 'bin/src/index.ts',
onwarn,
plugins: [
alias(moduleAliases),
resolve(),
json(),
string({ include: '**/*.md' }),
typescript(),
commonjs({ include: 'node_modules/**' })
],
external: ['fs', 'path', 'module', 'events', 'rollup', 'assert', 'os', 'util'],
output: {
file: 'bin/rollup',
format: 'cjs',
banner: '#!/usr/bin/env node',
paths: {
rollup: '../dist/rollup.js'
}
}
}
];
if (command.configNoBrowser) {
return nodeBuilds;
}
return nodeBuilds.concat([
/* Rollup core browser builds */
{
input: 'src/browser-entry.ts',
onwarn,
plugins: [
alias(moduleAliases),
resolve({ browser: true }),
json(),
{
load: id => {
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');
}
},
typescript(),
commonjs(),
terser({ module: true, output: { comments: 'some' } })
],
output: [
{ file: 'dist/rollup.browser.js', format: 'umd', name: 'rollup', banner },
{ file: 'dist/rollup.browser.es.js', format: 'esm', banner }
]
}
]);
};