Skip to content

Commit bf0dfee

Browse files
committed
Fine tune rollup config
1 parent d098d0e commit bf0dfee

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

rollup.config.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,25 @@ import { defineConfig } from 'rollup'
88
import pkg from './package.json'
99

1010
/**
11-
* @param {'commonjs'|'module'} type
12-
* @returns { import('rollup').Plugin }
11+
* @type { () => import('rollup').Plugin }
1312
*/
14-
function emitPkg(type) {
13+
function packageType() {
1514
return {
16-
name: 'emit-pkg',
17-
generateBundle() {
18-
this.emitFile({
19-
type: 'asset',
20-
fileName: 'package.json',
21-
source: JSON.stringify({ type }, undefined, 2)
22-
})
15+
name: 'package-type',
16+
renderStart({ format }) {
17+
const type = { cjs: 'commonjs', es: 'module' }[ format ]
18+
if (type) {
19+
this.emitFile({
20+
type: 'asset',
21+
fileName: 'package.json',
22+
source: JSON.stringify({ type }, undefined, 2)
23+
})
24+
}
2325
}
2426
}
2527
}
2628

27-
const cfg = defineConfig({
29+
export default defineConfig({
2830
input: 'src/index.ts',
2931
output: [
3032
{
@@ -33,16 +35,14 @@ const cfg = defineConfig({
3335
interop: 'default',
3436
sourcemap: true,
3537
generatedCode: 'es2015',
36-
exports: 'named',
37-
plugins: [ emitPkg('commonjs') ]
38+
exports: 'named'
3839
},
3940
{
4041
file: pkg.module,
4142
format: 'module',
4243
interop: 'default',
4344
sourcemap: true,
44-
generatedCode: 'es2015',
45-
plugins: [ emitPkg('module') ]
45+
generatedCode: 'es2015'
4646
}
4747
],
4848
plugins: [
@@ -52,9 +52,8 @@ const cfg = defineConfig({
5252
hook: {
5353
outputPath: (_path, kind) => kind === 'declaration' ? normalize(pkg.types) : undefined
5454
}
55-
})
55+
}),
56+
packageType()
5657
],
5758
external: Object.keys(pkg.dependencies) // nodeResolve will take care of builtins
5859
})
59-
60-
export default cfg

0 commit comments

Comments
 (0)