-
Notifications
You must be signed in to change notification settings - Fork 1
/
rollup.config.js
104 lines (102 loc) · 2.12 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
'use strict';
const path = require('path');
const { promises: fs } = require('fs');
const { default: babel } = require('@rollup/plugin-babel');
const execa = require('execa');
const cpy = require('cpy');
module.exports = {
input: 'index.js',
output: [
{
file: 'cjs/index.js',
format: 'cjs',
exports: 'auto',
sourcemap: true
},
{
file: 'esm/index.js',
format: 'esm',
sourcemap: true
}
],
plugins: [
(() => {
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'],
compilerOptions: {
declaration: true,
declarationMap: true,
declarationDir: prefix,
emitDeclarationOnly: true,
noEmit: false,
listEmittedFiles: true
}
};
const file = `.${prefix}.tsconfig.json`;
try {
await fs.writeFile(
file,
JSON.stringify(tsconfig),
'utf-8'
);
const { stdout } = await execa(
'tsc',
['-p', file],
{
preferLocal: true
}
);
try {
await cpy('types', `${prefix}/types`);
} catch (error) {}
console.log(stdout);
} finally {
await fs.unlink(file);
}
}
}
};
})(),
(() => {
return {
name: 'package-type',
async writeBundle(output) {
let prefix, type;
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'
);
}
}
};
})(),
babel({
babelHelpers: 'bundled',
exclude: 'node_modules/**'
})
]
};