Skip to content

Commit

Permalink
deps: use rollup-plugin-node-externals
Browse files Browse the repository at this point in the history
Basically same as agilgur5/react-signature-canvas@0154531
- c.f. https://github.com/Septh/rollup-plugin-node-externals

- instead of the custom JS code I wrote
  - I was thinking of separating that into a plugin, but someone alreadydid that!
    - and, importantly, also covered submodules, unlike _most_ of the externals plugins (e.g. stevenbenisek/rollup-plugin-auto-external#16)
      - 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 Jun 13, 2022
1 parent be0922a commit ff487e5
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 20 deletions.
144 changes: 144 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 @@ -73,6 +73,7 @@
"jest-config": "^27.5.1",
"package-json-type": "^1.0.3",
"rollup": "^2.70.1",
"rollup-plugin-node-externals": "^4.0.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.32.1",
"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 @@ -61,8 +44,8 @@ const configs: RollupOptions[] = [{
})],
...outputDefaults,
}],
external: isExternal,
plugins: [
externals(),
nodeResolve(),
commonjs(),
babel({
Expand Down

0 comments on commit ff487e5

Please sign in to comment.