Skip to content

Commit

Permalink
deps: use rollup-plugin-node-externals
Browse files Browse the repository at this point in the history
- instead of the custom JS code I wrote
  - I was thinking of separating that into a plugin, but someone already
    did that!
    - and, importantly, also covered submodules, unlike _most_ of the
      externals plugins
      - see regex here: https://github.com/Septh/rollup-plugin-node-externals/blob/11a7b4454f57c76436e71ecead0cc59ab0cc3b80/src/index.ts#L106

- put it _before_ `node-resolve` as the docs state

- reduces my code a bit, which is always nice, and will make it easier
  to split my Rollup config into its own "preset" package later on
  • Loading branch information
agilgur5 committed May 18, 2022
1 parent 3db849f commit 63fb3f7
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 20 deletions.
141 changes: 141 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"react": "^17.0.2",
"react-dom": "^17.0.2",
"rollup": "^2.70.2",
"rollup-plugin-node-externals": "^4.0.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.31.2",
"ts-node": "^10.7.0",
Expand Down
23 changes: 3 additions & 20 deletions rollup.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { IPackageJson } from 'package-json-type'
import type { IsExternal, RollupOptions, OutputOptions } from 'rollup'
import type { RollupOptions, OutputOptions } from 'rollup'
import { externals } from 'rollup-plugin-node-externals'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import { babel } from '@rollup/plugin-babel'
Expand All @@ -10,24 +11,6 @@ import packageJson from './package.json'

const pkgJson = packageJson as IPackageJson // coerce to the right type

// treat deps and peerDeps as externals -- don't bundle them
const depsList = [
...Object.keys(pkgJson.dependencies ?? []),
...Object.keys(pkgJson.peerDependencies ?? [])
]

// TODO: split this into a rollup plugin? submodule match is often missing
const isExternal: IsExternal = (id) => {
// simple case: exact match (ex: '@babel/runtime')
if (depsList.includes(id)) return true
// submodule match (ex: '@babel/runtime/helpers/get')
for (const dep of depsList) {
if (id.startsWith(dep)) return true
}
// otherwise false
return false
}

const outputDefaults: OutputOptions = {
// always provide a sourcemap for better debugging for consumers
sourcemap: true,
Expand Down Expand Up @@ -56,8 +39,8 @@ const configs: RollupOptions[] = [{
})],
...outputDefaults
}],
external: isExternal,
plugins: [
externals(), // https://github.com/Septh/rollup-plugin-node-externals#3-order-matters
nodeResolve(),
commonjs(),
babel({
Expand Down

0 comments on commit 63fb3f7

Please sign in to comment.