forked from newnivers/web-packages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
84 lines (72 loc) · 2.66 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
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path');
const babel = require('@rollup/plugin-babel').default;
const commonjs = require('@rollup/plugin-commonjs');
const json = require('@rollup/plugin-json');
const resolve = require('@rollup/plugin-node-resolve').default;
const builtins = require('builtin-modules');
const extensions = ['.js', '.ts', '.jsx', '.tsx'];
exports.generateRollupConfig = function generateRollupConfig({ packageDir }) {
const packageJSON = require(path.join(packageDir, 'package.json'));
if (packageJSON.publishConfig.exports == null) {
throw new Error('package.json의 exports 필드를 정의해주세요.');
}
if (packageJSON.publishConfig.main == null) {
throw new Error('package.json의 main 필드를 정의해주세요.');
}
if (packageJSON.publishConfig.module == null) {
throw new Error('package.json의 module 필드를 정의해주세요.');
}
const entrypoints = Object.keys(packageJSON.exports).filter(x => x !== './package.json');
const external = pkg => {
const externals = [...Object.keys({ ...packageJSON.dependencies, ...packageJSON.peerDependencies }), ...builtins];
return externals.some(externalPkg => {
return pkg.startsWith(externalPkg);
});
};
function buildJS(input, output, format) {
const isESMFormat = format === 'es';
return {
input,
external,
output: [
{
format,
...(isESMFormat
? {
dir: path.dirname(output),
entryFileNames: `[name]${path.extname(output)}`,
preserveModulesRoot: isESMFormat ? path.dirname(input) : undefined,
}
: { file: output }),
},
],
plugins: [
resolve({
extensions,
}),
commonjs(),
babel({
extensions,
babelHelpers: 'bundled',
rootMode: 'upward',
}),
json(),
],
preserveModules: isESMFormat,
};
}
function buildCJS(input, output) {
return buildJS(input, output, 'cjs');
}
function buildESM(input, output) {
return buildJS(input, output, 'es');
}
return entrypoints.flatMap(entrypoint => {
const cjsEntrypoint = path.resolve(packageDir, packageJSON?.publishConfig.main);
const cjsOutput = path.resolve(packageDir, packageJSON?.publishConfig.exports?.[entrypoint].require);
const esmEntrypoint = path.resolve(packageDir, packageJSON?.publishConfig.module);
const esmOutput = path.resolve(packageDir, packageJSON?.publishConfig.exports?.[entrypoint].import);
return [buildCJS(cjsEntrypoint, cjsOutput), buildESM(esmEntrypoint, esmOutput)];
});
};