Skip to content

Instantly share code, notes, and snippets.

@swedesjs
Created September 17, 2021 16:19
Show Gist options
  • Save swedesjs/7a7b7f34e91473e9aa09676451a29220 to your computer and use it in GitHub Desktop.
Save swedesjs/7a7b7f34e91473e9aa09676451a29220 to your computer and use it in GitHub Desktop.
import jsonPlugin from '@rollup/plugin-json';
import { builtinModules } from 'module';
import { join as pathJoin } from 'path';
import { tmpdir } from 'os';
import typescriptPlugin from 'rollup-plugin-typescript2';
const coreModules = builtinModules.filter(name => (
!/(^_|\/)/.test(name)
));
const cacheRoot = pathJoin(tmpdir(), '.rpt2_cache');
export default async () => {
const modulePkg = await import(pathJoin(__dirname, "package.json"));
const src = pathJoin(__dirname, "src")
const lib = pathJoin(__dirname, "lib")
return {
input: pathJoin(src, 'index.ts'),
plugins: [
jsonPlugin(),
typescriptPlugin({
cacheRoot,
useTsconfigDeclarationDir: false,
tsconfigOverride: {
outDir: lib,
rootDir: src,
include: [src]
}
}),
// https://rollupjs.org/guide/en/#renderdynamicimport
{
name: 'retain-import-expression',
resolveDynamicImport(specifier) {
if (specifier === 'node-fetch') return false;
return null;
},
renderDynamicImport({ targetModuleId }) {
if (targetModuleId === 'node-fetch') {
return {
left: 'import(',
right: ')'
};
}
return undefined;
}
}
],
external: [
...Object.keys(modulePkg.dependencies || {}),
...Object.keys(modulePkg.peerDependencies || {}),
...coreModules
],
output: [
{
file: pathJoin(lib, 'index.js'),
format: 'cjs',
exports: 'named'
},
{
file: pathJoin(lib, 'index.mjs'),
format: 'esm'
}
]
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment