Skip to content

Commit ffe29a2

Browse files
committed
add functions for making npm rollup configs
1 parent 5a97847 commit ffe29a2

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

rollup/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ import * as plugins from './plugins/index.js';
55
export { plugins };
66

77
export * from './bundleHelpers.js';
8+
export * from './npmHelpers.js';
89
export { insertAt } from './utils.js';

rollup/npmHelpers.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Rollup config docs: https://rollupjs.org/guide/en/#big-list-of-options
3+
*/
4+
5+
import { builtinModules } from 'module';
6+
import * as path from 'path';
7+
8+
import deepMerge from 'deepmerge';
9+
10+
import { makeNodeResolvePlugin, makeSucrasePlugin } from './plugins/index.js';
11+
12+
const packageDotJSON = require(path.resolve(process.cwd(), './package.json'));
13+
14+
export function makeBaseNPMConfig(options = {}) {
15+
const { entrypoints = ['src/index.ts'] } = options;
16+
17+
const nodeResolvePlugin = makeNodeResolvePlugin();
18+
const sucrasePlugin = makeSucrasePlugin();
19+
20+
return {
21+
input: entrypoints,
22+
23+
output: {
24+
sourcemap: true,
25+
26+
// output individual files rather than one big bundle
27+
preserveModules: true,
28+
29+
// any wrappers or helper functions generated by rollup can use ES6 features
30+
generatedCode: 'es2015',
31+
32+
// don't add `"use strict"` to the top of cjs files
33+
strict: false,
34+
35+
// do TS-3.8-style exports
36+
// exports.dogs = are.great
37+
// rather than TS-3.9-style exports
38+
// Object.defineProperty(exports, 'dogs', {
39+
// enumerable: true,
40+
// get: () => are.great,
41+
// });
42+
externalLiveBindings: false,
43+
},
44+
45+
plugins: [nodeResolvePlugin, sucrasePlugin],
46+
47+
// don't include imported modules from outside the package in the final output
48+
external: [
49+
...builtinModules,
50+
...Object.keys(packageDotJSON.dependencies || {}),
51+
...Object.keys(packageDotJSON.devDependencies || {}),
52+
...Object.keys(packageDotJSON.peerDependencies || {}),
53+
],
54+
55+
// TODO `'smallest'` will get rid of `isDebugBuild()` by evaluating it and inlining the result and then treeshaking
56+
// from there. The current setting (false) prevents this, in case we want to leave it there for users to use in
57+
// their own bundling. That said, we don't yet know for sure that that works, so come back to this.
58+
// treeshake: 'smallest',
59+
treeshake: false,
60+
};
61+
}
62+
63+
export function makeNPMConfigVariants(baseConfig) {
64+
const variantSpecificConfigs = [
65+
{ output: { format: 'cjs', dir: 'build/cjs' } },
66+
{ output: { format: 'esm', dir: 'build/esm' } },
67+
];
68+
69+
return variantSpecificConfigs.map(variant => deepMerge(baseConfig, variant));
70+
}

rollup/plugins/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './bundlePlugins';
2+
export * from './npmPlugins';

rollup/plugins/npmPlugins.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Sucrase plugin docs: https://github.com/rollup/plugins/tree/master/packages/sucrase
3+
*/
4+
5+
import sucrase from '@rollup/plugin-sucrase';
6+
7+
/**
8+
* Create a plugin to transpile TS syntax using `sucrase`.
9+
*
10+
* @returns An instance of the `@rollup/plugin-sucrase` plugin
11+
*/
12+
export function makeSucrasePlugin() {
13+
return sucrase({
14+
transforms: ['typescript', 'jsx'],
15+
});
16+
}

0 commit comments

Comments
 (0)