/
rollup.config.mjs
102 lines (89 loc) · 2.4 KB
/
rollup.config.mjs
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
import path from 'node:path';
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
*/
const pkgName = 'statofu';
const inputDir = 'src';
const inputs = ['src/index.ts', 'src/ssr/index.ts'];
const outputDir = 'dist';
/**
* @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 outputNormal = {
file: `${outputFilePrefix}.js`,
format,
name: inputName,
sourcemap: true,
plugins: [
terser({
format: { comments: false, beautify: true },
compress: false,
mangle: false,
}),
],
};
/** @type {OutputOptions} */
const outputCompressed = {
file: `${outputFilePrefix}.min.js`,
format,
name: inputName,
sourcemap: true,
plugins: [
terser({
format: { comments: false },
mangle: {
properties: { regex: /^_/ },
},
}),
],
};
return {
input,
output: [outputNormal, outputCompressed],
plugins: [
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);
}