-
Notifications
You must be signed in to change notification settings - Fork 6
/
rollup.config.mjs
105 lines (93 loc) · 2.84 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
103
104
105
import { nodeResolve } from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import commonjs from '@rollup/plugin-commonjs';
import { babel } from '@rollup/plugin-babel';
import terser from '@rollup/plugin-terser';
import json from '@rollup/plugin-json';
import dts from 'rollup-plugin-dts';
import fs from 'fs-extra';
import path from 'path';
const tsConfig = JSON.parse(await fs.readFile(new URL('./tsConfig.base.json', import.meta.url)));
const srcIndex = 'src/index.ts';
const basePath = fs.realpathSync(process.cwd());
const distPath = path.resolve(basePath, 'dist');
const buildOutput = ({ packageName = 'tods-competition-factory', minified, folder, format }) => {
const development = format === 'cjs' && !minified ? 'development.' : '';
const subFolder = folder ? `/${folder}` : '';
const file = `${distPath}${subFolder}/${packageName}.${development}${format}.js`;
const base = {
name: packageName,
exports: 'named',
sourcemap: true,
esModule: true,
freeze: false,
globals: {},
format,
file,
};
if (format === 'cjs' && minified) writeCjsIndex({ subFolder, packageName });
return minified
? [
{
...base,
file: `${distPath}${subFolder}/${packageName}.production.${format}.min.js`,
plugins: [terser()],
},
]
: [base];
};
function writeCjsIndex({ subFolder, packageName }) {
const fileImportRoot = `module.exports = require('./${packageName}`;
const body = `'use strict';
if (process.env.NODE_ENV === 'production') {
${fileImportRoot}.production.cjs.min.js')
} else {
${fileImportRoot}.development.cjs.js')
}
`;
return fs.outputFile(`${distPath}${subFolder}/index.js`, body);
}
function createExport({ input, folder, packageName, cjs, esm }) {
const output = [];
if (esm) {
const esmBuilds = [...buildOutput({ format: 'esm', minified: true, folder, packageName })];
output.push(...esmBuilds);
}
if (cjs) {
const cjsBuilds = [
...buildOutput({ format: 'cjs', minified: false, folder, packageName }),
...buildOutput({ format: 'cjs', minified: true, folder, packageName }),
];
output.push(...cjsBuilds);
}
return {
plugins: [
typescript({ tsconfig: './tsconfig.json', declaration: false, sourceMap: true }),
babel({ babelHelpers: 'bundled' }),
nodeResolve(),
commonjs(),
json(),
],
input,
output,
};
}
const cjsExports = [{ input: srcIndex, cjs: true }].map(createExport);
const engineTypes = [
{
input: srcIndex,
plugins: [
dts({
compilerOptions: {
baseUrl: tsConfig.compilerOptions.baseUrl,
paths: tsConfig.compilerOptions.paths,
},
}),
],
output: {
file: `${distPath}/tods-competition-factory.d.ts`,
format: 'es',
},
},
];
export default [...cjsExports, ...engineTypes];