-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrollup.config.ts
More file actions
105 lines (91 loc) · 2.55 KB
/
Copy pathrollup.config.ts
File metadata and controls
105 lines (91 loc) · 2.55 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
/**
* @file Rollup configurations for generating AGLint builds.
*
* ! Please ALWAYS use the "pnpm build" command for building!
*/
import { readFileSync } from 'node:fs';
import path from 'node:path';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import resolve from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import externals from 'rollup-plugin-node-externals';
// Common constants
const ROOT_DIR = './';
const BASE_NAME = 'AGLint';
const PKG_FILE_NAME = 'package.json';
const distDirLocation = path.join(ROOT_DIR, 'dist');
const pkgFileLocation = path.join(ROOT_DIR, PKG_FILE_NAME);
// Read package.json
const pkg = JSON.parse(readFileSync(pkgFileLocation, 'utf-8'));
// Check if the package.json file has all required fields
// (we need them for the banner)
const REQUIRED_PKG_FIELDS = [
'author',
'homepage',
'license',
'version',
];
for (const field of REQUIRED_PKG_FIELDS) {
if (!(field in pkg)) {
throw new Error(`Missing required field "${field}" in ${PKG_FILE_NAME}`);
}
}
// Generate a banner with the current package & build info
const BANNER = `/*
* ${BASE_NAME} v${pkg.version} (build date: ${new Date().toUTCString()})
* (c) ${new Date().getFullYear()} ${pkg.author}
* Released under the ${pkg.license} license
* ${pkg.homepage}
*/`;
// Pre-configured TypeScript plugin
const typeScriptPlugin = typescript({
tsconfig: path.resolve(ROOT_DIR, 'tsconfig.build.json'),
});
// Common plugins for all types of builds
const commonPlugins = [
json({ preferConst: true }),
commonjs({ sourceMap: false }),
typeScriptPlugin,
];
// Plugins for Node.js builds
const nodePlugins = [
...commonPlugins,
resolve({ preferBuiltins: false }),
externals(),
];
// ECMAScript build configuration
const esm = {
input: path.join(ROOT_DIR, 'src', 'index.node.ts'),
output: [
{
dir: distDirLocation,
format: 'esm',
sourcemap: false,
banner: BANNER,
preserveModules: true,
preserveModulesRoot: 'src',
},
],
plugins: nodePlugins,
};
// CLI tool build
const cli = {
input: path.join(ROOT_DIR, 'src', 'index.cli.ts'),
output: [
{
dir: distDirLocation,
format: 'esm',
sourcemap: false,
banner: BANNER,
preserveModules: true,
preserveModulesRoot: 'src',
},
],
plugins: nodePlugins,
};
// Export build configs for Rollup
export default [
esm,
cli,
];