This repository was archived by the owner on Mar 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrollup.config.mjs
More file actions
120 lines (105 loc) · 2.78 KB
/
rollup.config.mjs
File metadata and controls
120 lines (105 loc) · 2.78 KB
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
import path from 'node:path';
import commonjs from '@rollup/plugin-commonjs';
import nodeResolve from '@rollup/plugin-node-resolve';
import terser from '@rollup/plugin-terser';
import typescript from '@rollup/plugin-typescript';
import { camelCase, isEmpty } from 'lodash';
/**
* @typedef {import("rollup").RollupOptionsFunction} RollupOptionsFunction
* @typedef {import("rollup").ModuleFormat} ModuleFormat
* @typedef {import("rollup").OutputOptions} OutputOptions
* @typedef {import("rollup").GlobalsOption} GlobalsOption
*/
const pkgName = 'statofu-react';
const inputDir = 'src';
const inputs = ['src/index.ts'];
const outputDir = 'dist';
/**
* @type {GlobalsOption}
*/
const globals = {
react: 'React',
statofu: 'statofu',
};
/**
* @type {RollupOptionsFunction}
*/
export default (cliArgs) => {
/** @type {ModuleFormat} */
const format = cliArgs.format;
return inputs.map((input) => {
const inputName = evaluateInputName(input);
const outputFilePrefix = evaluateOutputFilePrefix(input, format);
/** @type {OutputOptions} */
const outputCommon = {
globals,
format,
name: inputName,
sourcemap: true,
};
/** @type {OutputOptions} */
const outputNormal = {
...outputCommon,
file: `${outputFilePrefix}.js`,
plugins: [
terser({
format: { comments: false, beautify: true },
compress: false,
mangle: false,
}),
],
};
/** @type {OutputOptions} */
const outputCompressed = {
...outputCommon,
file: `${outputFilePrefix}.min.js`,
plugins: [
terser({
format: { comments: false },
mangle: {
properties: { regex: /^_/ },
},
}),
],
};
return {
input,
output: [outputNormal, outputCompressed],
external: Object.keys(globals),
plugins: [
nodeResolve(),
commonjs(),
typescript({
compilerOptions: {
...(format === 'umd' ? { target: 'es5' } : {}),
},
}),
],
};
});
};
/**
* @param {string} input
* @returns {string}
*/
function evaluateInputName(input) {
const { dir, name } = path.parse(path.relative(inputDir, input));
return camelCase(
[
pkgName,
...(isEmpty(dir) ? [] : dir.split(/[\\/]/)),
...(name === 'index' ? [] : [name]),
].join('-')
);
}
/**
* @param {string} input
* @param {ModuleFormat} format
* @returns {string}
*/
function evaluateOutputFilePrefix(input, format) {
const { dir, name } = path.parse(path.relative(inputDir, input));
const parentInfo = isEmpty(dir) ? pkgName : [pkgName, dir].join('-');
const namePrefix = `${name === 'index' ? parentInfo : [parentInfo, name].join('-')}.${format}`;
return path.join(outputDir, namePrefix);
}