-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.js
77 lines (65 loc) · 2.53 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
import babel from '@rollup/plugin-babel';
import commonjs from '@rollup/plugin-commonjs';
import nodeResolve from '@rollup/plugin-node-resolve';
import { terser } from 'rollup-plugin-terser';
import postcss from 'rollup-plugin-postcss';
import fs from 'fs';
import { readPackageUpSync } from 'read-package-up';
import PeerDepsExternal from 'rollup-plugin-peer-deps-external';
import path from 'path';
const input = 'index.ts';
const { packageJson: pkg } = readPackageUpSync({
cwd: fs.realpathSync(process.cwd()), // node 명령을 호출한 작업 디렉토리의 절대경로
});
function buildJS(input, output, format) {
const defaultOutputConfig = {
format,
sourcemap: true,
};
const esOutputConfig = {
...defaultOutputConfig,
preserveModulesRoot: path.dirname(input),
dir: output,
};
const cjsOutputConfig = {
...defaultOutputConfig,
file: output,
};
const externals = () => {
const defaultExternal = [];
const dependencies = Object.keys(pkg?.dependencies || {});
const peerDependencies = Object.keys(pkg?.peerDependencies || {});
return { ...defaultExternal, ...dependencies, ...peerDependencies };
};
const extensions = ['.js', '.jsx', '.ts', '.tsx'];
const plugins = [
// 바벨 설정
babel({
babelHelpers: 'bundled', // runtime: 바벨 헬퍼 함수를 번들 결과에 포함하지 않음 / bundled: 번들 결과에 포함함
extensions,
presets: [
'@babel/preset-env', // es6 compile
'@babel/preset-react', // react compile
'@babel/preset-typescript', // typescript compile
],
}),
commonjs({ extensions }), // commonjs 코드를 es6로 변환
nodeResolve({ extensions }), // 라이브러리에서 외부 의존성 사용하도록, 트리 셰이킹도 가능하게
PeerDepsExternal(), // 피어 디펜던시를 빌드 결과에 포함하지 않음
terser(), // 번들 결과를 minify,
postcss({
modules: true,
}),
];
return {
input, // 진입점
output: [format === 'es' ? esOutputConfig : cjsOutputConfig], // 결과물 저장 장소
preserveModules: format === 'es', // 모듈 결과물을 단일 파일로 생성할 것인가
external: externals(), // 외부 종속성 처리
plugins, // 플러그인
};
}
export default [
buildJS(input, pkg?.main, 'cjs'),
buildJS(input, 'dist/esm', 'es'),
];