| 1 |
import { builtinModules } from 'module'; |
| 2 |
|
| 3 |
// eslint-disable-next-line import/no-extraneous-dependencies |
| 4 |
import typescript from '@rollup/plugin-typescript'; |
| 5 |
|
| 6 |
/** |
| 7 |
* Create a base rollup config |
| 8 |
* @param {Record<string,any>} pkg Imported package.json |
| 9 |
* @param {string[]} external Imported package.json |
| 10 |
* @returns {import('rollup').RollupOptions} |
| 11 |
*/ |
| 12 |
export function createConfig({ pkg, external = [] }) { |
| 13 |
return { |
| 14 |
input: 'src/index.ts', |
| 15 |
external: Object.keys(pkg.dependencies || {}) |
| 16 |
.concat(Object.keys(pkg.peerDependencies || {})) |
| 17 |
.concat(builtinModules) |
| 18 |
.concat(external), |
| 19 |
onwarn: (warning) => { |
| 20 |
throw Object.assign(new Error(), warning); |
| 21 |
}, |
| 22 |
strictDeprecations: true, |
| 23 |
output: [ |
| 24 |
{ |
| 25 |
format: 'cjs', |
| 26 |
file: pkg.main, |
| 27 |
exports: 'named', |
| 28 |
footer: 'module.exports = Object.assign(exports.default, exports);', |
| 29 |
sourcemap: true |
| 30 |
}, |
| 31 |
{ |
| 32 |
format: 'es', |
| 33 |
file: pkg.module, |
| 34 |
plugins: [emitModulePackageFile()], |
| 35 |
sourcemap: true |
| 36 |
} |
| 37 |
], |
| 38 |
plugins: [typescript({ sourceMap: true })] |
| 39 |
}; |
| 40 |
} |
| 41 |
|
| 42 |
export function emitModulePackageFile() { |
| 43 |
return { |
| 44 |
name: 'emit-module-package-file', |
| 45 |
generateBundle() { |
| 46 |
this.emitFile({ |
| 47 |
type: 'asset', |
| 48 |
fileName: 'package.json', |
| 49 |
source: `{"type":"module"}` |
| 50 |
}); |
| 51 |
} |
| 52 |
}; |
| 53 |
} |