-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrollup.mjs
47 lines (42 loc) · 1.38 KB
/
rollup.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
import { readFileSync } from 'node:fs';
import { builtinModules } from 'node:module';
import { join, resolve } from 'node:path';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import { defineConfig } from 'rollup';
const __dirname = new URL('.', import.meta.url).pathname;
// a simple plugin that adds a package.json file with {type: module}
const modulePackageJson = {
name: 'package-json-module-type',
generateBundle() {
this.emitFile({
type: 'asset',
fileName: 'package.json',
source: '{"type": "module"}',
});
},
};
const tsconfig = resolve(__dirname, 'tsconfig.json');
export function transpile(pkg, format, bundle = false) {
const pkgRoot = join(__dirname, 'packages', pkg);
const pkgJson = JSON.parse(readFileSync(join(pkgRoot, 'package.json')));
const external = [...builtinModules, ...Object.keys(pkgJson.dependencies || {})];
const input = join(pkgRoot, 'src', 'index.ts');
const outDir = join(pkgRoot, 'dist', format);
return defineConfig({
input,
output: {
sourcemap: true,
format,
dir: outDir,
preserveModules: !bundle,
},
treeshake: { moduleSideEffects: builtinModules },
plugins: [
nodeResolve(),
typescript({ include: [input], outDir, tsconfig }),
format === 'esm' ? modulePackageJson : {},
],
external,
});
}