-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
58 lines (46 loc) · 1.43 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const getBuiltins = require('builtins');
const path = require('path');
const readPkg = require('read-pkg');
const safeResolve = require('safe-resolve');
const semver = require('semver');
module.exports = ({
builtins = true,
dependencies = true,
packagePath,
peerDependencies = true,
} = {}) => ({
name: 'auto-external',
options(opts) {
const pkg = readPkg.sync(packagePath);
let ids = [];
if (dependencies && pkg.dependencies) {
ids = ids.concat(Object.keys(pkg.dependencies));
}
if (peerDependencies && pkg.peerDependencies) {
ids = ids.concat(Object.keys(pkg.peerDependencies));
}
if (builtins) {
ids = ids.concat(getBuiltins(semver.valid(builtins)));
}
ids = ids.map(safeResolve).filter(Boolean);
const external = id => {
if (typeof opts.external === 'function' && opts.external(id)) {
return true;
}
if (Array.isArray(opts.external) && opts.external.includes(id)) {
return true;
}
/**
* The `id` argument is a resolved path if `rollup-plugin-node-resolve`
* and `rollup-plugin-commonjs` are included.
*/
const resolvedPath = safeResolve(id);
if (resolvedPath === null) {
return false;
}
const resolvedDirname = path.dirname(resolvedPath);
return ids.some(idx => resolvedDirname.startsWith(path.dirname(idx)));
};
return Object.assign({}, opts, { external });
},
});