Skip to content

Commit 0154531

Browse files
committed
deps: use rollup-plugin-node-externals
- 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
1 parent 6ed1a63 commit 0154531

File tree

3 files changed

+145
-20
lines changed

3 files changed

+145
-20
lines changed

package-lock.json

Lines changed: 141 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
"react": "^17.0.2",
102102
"react-dom": "^17.0.2",
103103
"rollup": "^2.70.2",
104+
"rollup-plugin-node-externals": "^4.0.0",
104105
"rollup-plugin-terser": "^7.0.2",
105106
"rollup-plugin-typescript2": "^0.31.2",
106107
"ts-node": "^10.7.0",

rollup.config.ts

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { IPackageJson } from 'package-json-type'
2-
import type { IsExternal, RollupOptions, OutputOptions } from 'rollup'
2+
import type { RollupOptions, OutputOptions } from 'rollup'
3+
import { externals } from 'rollup-plugin-node-externals'
34
import { nodeResolve } from '@rollup/plugin-node-resolve'
45
import commonjs from '@rollup/plugin-commonjs'
56
import { babel } from '@rollup/plugin-babel'
@@ -10,24 +11,6 @@ import packageJson from './package.json'
1011

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

13-
// treat deps and peerDeps as externals -- don't bundle them
14-
const depsList = [
15-
...Object.keys(pkgJson.dependencies ?? []),
16-
...Object.keys(pkgJson.peerDependencies ?? [])
17-
]
18-
19-
// TODO: split this into a rollup plugin? submodule match is often missing
20-
const isExternal: IsExternal = (id) => {
21-
// simple case: exact match (ex: '@babel/runtime')
22-
if (depsList.includes(id)) return true
23-
// submodule match (ex: '@babel/runtime/helpers/get')
24-
for (const dep of depsList) {
25-
if (id.startsWith(dep)) return true
26-
}
27-
// otherwise false
28-
return false
29-
}
30-
3114
const outputDefaults: OutputOptions = {
3215
// always provide a sourcemap for better debugging for consumers
3316
sourcemap: true,
@@ -56,8 +39,8 @@ const configs: RollupOptions[] = [{
5639
})],
5740
...outputDefaults
5841
}],
59-
external: isExternal,
6042
plugins: [
43+
externals(), // https://github.com/Septh/rollup-plugin-node-externals#3-order-matters
6144
nodeResolve(),
6245
commonjs(),
6346
babel({

0 commit comments

Comments
 (0)