-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathrollup.config.js
181 lines (178 loc) · 3.98 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
'use strict';
const path = require('path');
const { promises: fs } = require('fs');
const { default: babel } = require('@rollup/plugin-babel');
const commonjs = require('@rollup/plugin-commonjs');
const execa = require('execa');
const cpy = require('cpy');
const del = require('del');
function getConfig(filename, options = {}) {
const { cjsOutro = '', cjsExports = 'auto' } = options;
return {
input: filename,
output: [
{
file: `cjs/${filename}`,
format: 'cjs',
exports: cjsExports,
sourcemap: true,
outro: cjsOutro
},
{
file: `esm/${filename}`,
format: 'esm',
sourcemap: true
}
],
plugins: [
(() => {
return {
name: 'resolve-id',
async resolveId(source, importer) {
if (source === 'url') {
return require.resolve('url/');
}
if (source === 'process/browser.js') {
return require.resolve('process/browser.js');
}
if (
source === 'path' &&
importer.includes('proxy/url.js')
) {
return require.resolve(
'rollup-plugin-node-builtins/src/es6/path.js'
);
}
return null;
}
};
})(),
(() => {
return {
name: 'types',
async writeBundle(output) {
let prefix;
if (output.file.includes('cjs/')) {
prefix = 'cjs';
} else if (output.file.includes('esm/')) {
prefix = 'esm';
}
if (typeof prefix !== 'undefined') {
const tsconfig = {
extends: './tsconfig',
exclude: [
'test/**/*.js',
'types/**/*-test*',
'helpers/**/*.js'
],
compilerOptions: {
declaration: true,
declarationMap: true,
declarationDir: prefix,
emitDeclarationOnly: true,
noEmit: false,
listEmittedFiles: true
}
};
const file = `.${prefix}.tsconfig.json`;
try {
try {
await cpy('types/**/*.d.ts', 'types', {
rename: (basename) =>
basename.replace('.d.ts', '.ts')
});
} catch (error) {}
await fs.writeFile(
file,
JSON.stringify(tsconfig),
'utf-8'
);
const { stdout } = await execa(
'tsc',
['-p', file],
{
preferLocal: true
}
);
try {
await del([
'types/**/*.ts',
'!types/**/*.d.ts'
]);
} catch (error) {}
console.log(stdout);
} finally {
await fs.unlink(file);
}
}
}
};
})(),
(() => {
return {
name: 'package-type',
async writeBundle(output) {
let prefix, type;
if (output.file.includes('index.js')) {
return;
}
if (output.file.includes('cjs/')) {
prefix = 'cjs';
type = 'commonjs';
} else if (output.file.includes('esm/')) {
prefix = 'esm';
type = 'module';
}
if (typeof prefix !== 'undefined') {
const package_ = path.join(prefix, 'package.json');
try {
await fs.unlink(package_);
} catch (error) {}
await fs.writeFile(
package_,
JSON.stringify({ type }),
'utf8'
);
}
}
};
})(),
commonjs(),
babel({
babelHelpers: 'bundled',
exclude: 'node_modules/**'
})
]
};
}
module.exports = [
'index.js',
'mock/buffer.js',
'mock/console.js',
'mock/dns.js',
'mock/empty.js',
'mock/net.js',
'mock/process.js',
'mock/punycode.js',
'mock/tls.js',
'mock/tty.js',
[
'proxy/url.js',
{ cjsOutro: 'exports = module.exports = api;', cjsExports: 'named' }
],
[
'proxy/querystring.js',
{ cjsOutro: 'exports = module.exports = api;', cjsExports: 'named' }
],
[
'proxy/process.js',
{ cjsOutro: 'exports = module.exports = api;', cjsExports: 'named' }
],
[
'proxy/process/browser.js',
{ cjsOutro: 'exports = module.exports = api;', cjsExports: 'named' }
]
].map((entry) => {
const [filename, options = {}] = [].concat(entry);
return getConfig(filename, options);
});