|
| 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 | +} |
0 commit comments