-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathrollup.config.ts
More file actions
147 lines (131 loc) · 4.17 KB
/
rollup.config.ts
File metadata and controls
147 lines (131 loc) · 4.17 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import { glob, GlobOptions } from 'glob';
import { readFileSync } from 'node:fs';
import { chmod, cp } from 'node:fs/promises';
import { join } from 'node:path';
import { inspect } from 'node:util';
import { defineConfig, ExternalOption, Plugin, RollupOptions } from 'rollup';
import del from 'rollup-plugin-delete';
import dts from 'rollup-plugin-dts';
import esbuild from 'rollup-plugin-esbuild';
const projectRoot = process.cwd();
const tsconfigPath = join(projectRoot, 'tsconfig.json');
const packageJsonPath = join(projectRoot, 'package.json');
const preserveModulesRoot = join(projectRoot, 'src');
const pkg = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
const input: string[] = [];
const external: ExternalOption = [];
if (pkg.bin) {
input.push(getSourceFilePath(pkg.bin));
}
if (pkg.main) {
input.push(getSourceFilePath(pkg.main));
}
// TODO: Remove this once we have a better way to extend this config
if (pkg.name === '@grafana/plugin-e2e') {
input.push(join(preserveModulesRoot, 'auth', 'auth.setup.ts'));
}
// TODO: Remove this once we have a better way to extend this config
if (pkg.name === '@grafana/create-plugin') {
const codeModsGlobOptions: GlobOptions = {
cwd: join(preserveModulesRoot, 'codemods'),
ignore: ['**/*.test.ts'],
absolute: true,
};
const codeMods = glob.sync('{migrations,additions}/scripts/*.ts', codeModsGlobOptions).map((m) => m.toString());
input.push(...codeMods);
external.push('prettier');
external.push(/^recast\/parsers\//);
}
if (pkg.dependencies) {
external.push(...Object.keys(pkg.dependencies));
}
if (pkg.peerDependencies) {
external.push(...Object.keys(pkg.peerDependencies));
}
const defaultOptions: Array<Partial<RollupOptions>> = [
{
input,
output: {
dir: 'dist',
format: pkg.type === 'module' ? 'esm' : 'cjs',
entryFileNames: '[name].js',
preserveModules: true,
preserveModulesRoot,
},
external,
plugins: [
del({ targets: join(projectRoot, 'dist/*') }),
pkg.type !== 'module' && commonjs(),
nodeResolve({
preferBuiltins: true,
}),
json(),
esbuild({
target: 'es2020',
tsconfig: tsconfigPath,
}),
shebang(),
copyAssets(),
],
},
];
if (pkg.types) {
defaultOptions.push({
input: getSourceFilePath(pkg.types),
output: {
file: pkg.types,
format: pkg.type === 'module' ? 'esm' : 'cjs',
},
plugins: [
dts({
tsconfig: tsconfigPath,
}),
],
});
}
if (process.env.DEBUG_ROLLUP_CONFIG) {
console.log(inspect(defaultOptions, { depth: null, colors: true }));
}
export default defineConfig(defaultOptions);
function getSourceFilePath(filePath: string) {
let relativePath = filePath.replace('dist\/', '');
if (relativePath.endsWith('.d.ts')) {
relativePath = relativePath.replace(/\.d\.ts$/, '.ts');
} else if (relativePath.endsWith('.js')) {
relativePath = relativePath.replace(/\.js$/, '.ts');
}
return join(preserveModulesRoot, relativePath);
}
// Make files with a shebang executable
function shebang(): Plugin {
return {
name: 'shebang',
async writeBundle(options, bundle) {
for (const [fileName, chunk] of Object.entries(bundle)) {
if (chunk.type === 'chunk' && /\.(cjs|js|mjs)$/.test(fileName) && chunk.code.startsWith('#!')) {
const filePath = options.dir ? `${options.dir}/${fileName}` : fileName;
await chmod(filePath, 0o755);
}
}
},
};
}
// Copy static assets to dist
function copyAssets(): Plugin {
return {
name: 'copy-assets',
async writeBundle() {
if (pkg.name === '@grafana/plugin-docs-cli') {
const srcViews = join(projectRoot, 'src', 'server', 'views');
const distViews = join(projectRoot, 'dist', 'server', 'views');
await cp(srcViews, distViews, { recursive: true });
const srcStyles = join(projectRoot, 'src', 'server', 'styles');
const distStyles = join(projectRoot, 'dist', 'server', 'styles');
await cp(srcStyles, distStyles, { recursive: true });
}
},
};
}