-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrollup.config.js
109 lines (91 loc) · 2.96 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
import fs from 'fs';
import path from 'path';
import typescript from 'rollup-plugin-typescript2';
import workspacesRun from 'workspaces-run';
import copy from 'rollup-plugin-copy';
import dts from 'rollup-plugin-dts';
async function main() {
const copyTargets = []
const plugins = [
typescript({
useTsconfigDeclarationDir: true,
}),
copy({ targets: copyTargets })
];
const results = [];
let packages = [];
await workspacesRun({ cwd: __dirname, orderByDeps: true }, async (pkg) => {
if (!pkg.config.private) {
packages.push(pkg);
}
});
if (!process.env.TARGET) {
console.log('Found the following packages:')
packages.forEach((pkg) => console.log('- ', pkg.name))
} else {
packages = packages.filter((pkg) => pkg.name === process.env.TARGET)
if (!packages.length) throw new Error(`No package with name "${process.env.TARGET}". `)
}
packages.forEach((pkg) => {
const basePath = path.relative(__dirname, pkg.dir)
const outputPath = basePath.replace('packages/', 'dist/');
let copyPath = path.join(basePath, 'copy');
if(fs.existsSync(copyPath)) {
copyTargets.push({ src: `${copyPath}/*`, dest: outputPath })
}
['package.json', 'README.md'].forEach((fileName) => {
const file = path.join(basePath, fileName)
if(fs.existsSync(file)) {
copyTargets.push({ src: file, dest: outputPath })
} else if (fileName === 'README.md') {
copyTargets.push({ src: 'README.md', dest: outputPath })
}
})
const configExports = pkg.config.exports || {'.': {
types: pkg.config.types,
module: pkg.config.module,
default: pkg.config.main,
}}
const entries = Object.keys(configExports)
entries.forEach((entry) => {
const externalLookup = [
...Object.keys(pkg.config.dependencies || []),
...Object.keys(pkg.config.peerDependencies || []),
...entries.filter(s => s !== '.' || s !== entry)
];
const external = (name) => externalLookup.includes(/^((?:\.\/)?(?:.*?))(?:\/|$)/.exec(name)[1])
const entryOutputs = configExports[entry]
if(entry === '.') entry = 'index'
const input = path.join(basePath, 'src', entry + '.tsx');
const output = []
if(entryOutputs.default) {
output.push({
file: path.join(outputPath, entry + '.js'),
format: 'cjs',
})
}
if(entryOutputs.module) {
output.push({
file: path.join(outputPath, entry + '.esm.js'),
format: 'esm',
})
}
results.push({
input,
output,
external,
plugins,
});
if(entryOutputs.types) {
results.push({
input: path.join('dist/ts-out', basePath, `src/${entry}.d.ts`),
output: { file: path.join(outputPath, `${entry}.d.ts`), format: 'es' },
external,
plugins: [dts({})],
});
}
})
});
return results;
}
export default main();