Skip to content

Commit 2ef4040

Browse files
author
Stephan Septh Schreiber
committed
Ignore falsy values in include/exclude
1 parent 03fc63d commit 2ef4040

File tree

6 files changed

+67
-57
lines changed

6 files changed

+67
-57
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ externals({
118118
```
119119
Just like `exclude`, the `include` option can be a string, a regex or an array of those.
120120

121-
> Note: this plugin uses an exact match against your imports, so if your are using some path substitution in your code, eg.:
121+
> Note 1 : falsy values in `include` and `exclude` are silently ignored. This allows for conditional constructs like so: `exclude: process.env.NODE_ENV === 'production' && /mydep/`.
122+
123+
> Note2 : this plugin uses an exact match against your imports, so if your are using some path substitution in your code, eg.:
122124
```js
123125
// in your code:
124126
import something from '@/mylib' // Say '@/' is mapped to some directory

package-lock.json

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

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,19 @@
3737
"scripts": {
3838
"build": "rollup -c",
3939
"watch": "rollup -cw",
40-
"dist": "trash dist && rollup -c && tsc src/index.ts --declaration --emitDeclarationOnly --outDir dist",
41-
"clean": "trash dist"
40+
"types": "tsc src/index.ts --declaration --emitDeclarationOnly --outDir dist",
41+
"clean": "trash dist",
42+
"prepublishOnly": "npm run clean && npm run build && npm run types"
4243
},
4344
"peerDependencies": {
4445
"builtin-modules": "^3.1.0"
4546
},
4647
"devDependencies": {
48+
"@rollup/plugin-node-resolve": "^7.1.3",
4749
"@wessberg/rollup-plugin-ts": "^1.2.24",
48-
"rollup": "^2.4.0",
49-
"rollup-plugin-node-resolve": "^5.2.0",
50+
"rollup": "^2.7.6",
5051
"trash": "^6.1.1",
51-
"tslint": "^6.1.1",
52+
"tslint": "^6.1.2",
5253
"typescript": "^3.8.3",
5354
"typescript-tslint-plugin": "^0.5.5"
5455
}

rollup.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const input = 'src/index.ts'
77
const sourcemap = true
88
const external = builtins.concat(Object.keys(pkg.devDependencies), Object.keys(pkg.peerDependencies))
99
const tsOptions = {
10+
tsconfig: cfg => ({ ...cfg, removeComments: true })
1011
}
1112

1213
/** @type {import('rollup').RollupOptions} */

src/index.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,37 @@ import { Plugin } from 'rollup'
1010
import * as builtinModules from 'builtin-modules'
1111

1212
export interface ExternalsOptions {
13-
packagePath: string
14-
builtins: boolean
15-
deps: boolean
16-
devDeps: boolean
17-
peerDeps: boolean
18-
optDeps: boolean
19-
include: string | RegExp | (string | RegExp)[]
20-
exclude: string | RegExp | (string | RegExp)[]
21-
/** @deprecated. Use include/exclude instead. */
22-
except: string | RegExp | (string | RegExp)[]
13+
/** Path/to/your/package.json file. Defaults to the one in `process.cwd()`. */
14+
packagePath?: string
15+
/** Mark node built-in modules like `path`, `fs`... as external. Defaults to `true`. */
16+
builtins?: boolean
17+
/** Mark dependencies as external. Defaults to `false`. */
18+
deps?: boolean
19+
/** Mark devDependencies as external. Defaults to `true`. */
20+
devDeps?: boolean
21+
/** Mark peerDependencies as external. Defaults to `true`. */
22+
peerDeps?: boolean
23+
/** Mark optionalDependencies as external. Defaults to `true`. */
24+
optDeps?: boolean
25+
/** Force these deps in the list of externals, regardless of other settings. Defaults to `[]` */
26+
include?: string | RegExp | (string | RegExp)[]
27+
/** Exclude these deps from the list of externals, regardless of other settings. Defaults to `[]` */
28+
exclude?: string | RegExp | (string | RegExp)[]
29+
/** @deprecated Use `exclude` instead. */
30+
except?: string | RegExp | (string | RegExp)[]
2331
}
2432

2533
/** For backward compatibility. Use `ExternalsOptions` instead. */
2634
export type ExternalOptions = ExternalsOptions
2735

2836
// The plugin implementation
29-
export default function externals(options: Partial<ExternalsOptions> = {}): Plugin {
37+
export default function externals(options: ExternalsOptions = {}): Plugin {
3038

3139
// Store eventual warnings until we can display them
3240
const warnings: string[] = []
3341

3442
// Consolidate options
35-
const opts: ExternalsOptions = {
43+
const opts: Required<ExternalsOptions> = {
3644
packagePath: resolve(process.cwd(), 'package.json'),
3745
builtins: true,
3846
deps: false,
@@ -56,7 +64,9 @@ export default function externals(options: Partial<ExternalsOptions> = {}): Plug
5664
return new RegExp('^' + entry.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + '$')
5765
}
5866
else {
59-
warnings.push(`Ignoring wrong entry type #${index} in '${option}' option: '${entry}'`)
67+
if (!!entry) {
68+
warnings.push(`Ignoring wrong entry type #${index} in '${option}' option: '${entry}'`)
69+
}
6070
return /(?=no)match/
6171
}
6272
})

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"importHelpers": false,
1919
"noEmitHelpers": false,
2020
"alwaysStrict": true,
21-
"removeComments": true,
21+
"removeComments": false,
2222
"declaration": false,
2323
// "module": "esnext", // overriden by rollup-plugin-ts
2424
// "outDir": "out", // overriden by rollup-plugin-ts

0 commit comments

Comments
 (0)