-
-
Notifications
You must be signed in to change notification settings - Fork 562
/
only.js
95 lines (82 loc) 路 2.49 KB
/
only.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const { join, resolve } = require('path');
const test = require('ava');
const { rollup } = require('rollup');
const { getImports, getResolvedModules } = require('../../../util/test');
const { nodeResolve } = require('..');
process.chdir(join(__dirname, 'fixtures'));
test('specify the only packages to resolve', async (t) => {
const warnings = [];
const bundle = await rollup({
input: ['only.js'],
onwarn: (warning) => warnings.push(warning),
plugins: [
nodeResolve({
resolveOnly: ['test']
})
]
});
const imports = await getImports(bundle);
const modules = await getResolvedModules(bundle);
t.is(warnings.length, 0);
t.snapshot(warnings);
t.deepEqual(imports, ['@scoped/foo', '@scoped/bar']);
t.assert(Object.keys(modules).includes(resolve('only-local.js')));
});
test('handles nested entry modules', async (t) => {
const warnings = [];
const bundle = await rollup({
input: ['nested/only.js'],
onwarn: (warning) => warnings.push(warning),
plugins: [
nodeResolve({
resolveOnly: ['test']
})
]
});
const imports = await getImports(bundle);
const modules = await getResolvedModules(bundle);
t.is(warnings.length, 0);
t.snapshot(warnings);
t.deepEqual(imports, ['@scoped/foo', '@scoped/bar']);
t.assert(Object.keys(modules).includes(resolve('only-local.js')));
});
test('regex', async (t) => {
const warnings = [];
const bundle = await rollup({
input: 'only.js',
onwarn: (warning) => warnings.push(warning),
plugins: [
nodeResolve({
resolveOnly: [/^@scoped\/.*$/]
})
]
});
const imports = await getImports(bundle);
const modules = await getResolvedModules(bundle);
t.is(warnings.length, 0);
t.snapshot(warnings);
t.deepEqual(imports, ['test']);
t.assert(Object.keys(modules).includes(resolve('only-local.js')));
});
test('allows a function as the parameter', async (t) => {
function allowed(...modules) {
const set = new Set(modules);
return (id) => set.has(id);
}
const warnings = [];
const bundle = await rollup({
input: ['only.js'],
onwarn: (warning) => warnings.push(warning),
plugins: [
nodeResolve({
resolveOnly: allowed('test')
})
]
});
const imports = await getImports(bundle);
const modules = await getResolvedModules(bundle);
t.is(warnings.length, 0);
t.snapshot(warnings);
t.deepEqual(imports, ['@scoped/foo', '@scoped/bar']);
t.assert(Object.keys(modules).includes(resolve('only-local.js')));
});