-
Notifications
You must be signed in to change notification settings - Fork 8
/
rollup.config.ts
78 lines (73 loc) · 2.32 KB
/
rollup.config.ts
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
import nodeResolve from '@rollup/plugin-node-resolve'
import typescript from '@rollup/plugin-typescript'
import MagicString from 'magic-string'
import type { LogLevel, LogOrStringHandler, Plugin, RollupLog } from 'rollup'
import { RollupOptions } from 'rollup'
import pkg from './package.json' with { type: 'json' }
import { multiInput } from './rollup-multi-plugin'
const normaliseEsmOutput = (): Plugin => {
const CJSFilenameRegex = /__filename/g
const CJSDirnameRegex = /__dirname/g
// Replace with dynamically imported ESM equivalents, so that they work in the browser and in Node.js
const filenameReplacement = "await import('url').then((url) => url.fileURLToPath(import.meta.url))"
const dirnameReplacement = `await import('path').then(async (path) => path.dirname(${filenameReplacement}))`
return {
name: 'normaliseEsmOutput',
renderChunk(code, _chunk, opts) {
if (opts.format === 'es') {
if (!CJSDirnameRegex.test(code) && !CJSFilenameRegex.test(code)) {
return null
}
const s = new MagicString(code.replace(CJSDirnameRegex, dirnameReplacement).replace(CJSFilenameRegex, filenameReplacement))
return {
code: s.toString(),
...(opts.sourcemap !== false ? { map: s.generateMap({ hires: true }) } : {}),
}
}
return null
},
}
}
const config: RollupOptions = {
input: ['src/index.ts', 'src/testing/index.ts', 'src/types/*.ts', '!src/types/*.spec.ts'],
output: [
{
dir: 'dist',
format: 'cjs',
entryFileNames: '[name].js',
preserveModules: true,
sourcemap: true,
dynamicImportInCjs: false,
},
{
dir: 'dist',
format: 'es',
entryFileNames: '[name].mjs',
preserveModules: true,
sourcemap: true,
},
],
treeshake: {
moduleSideEffects: false,
propertyReadSideEffects: false,
},
plugins: [
typescript({
tsconfig: 'tsconfig.build.json',
}),
nodeResolve({
preferBuiltins: true,
}),
normaliseEsmOutput(),
multiInput(),
],
external: [...Object.keys(pkg.dependencies), ...Object.keys(pkg.peerDependencies)],
onLog(level: LogLevel, log: RollupLog, handler: LogOrStringHandler) {
if (log.code === 'CIRCULAR_DEPENDENCY') {
handler('error', log)
} else {
handler(level, log)
}
},
}
export default config