/
misc.js
138 lines (130 loc) · 3.43 KB
/
misc.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const assert = require('assert');
const rollup = require('../../dist/rollup');
const { loader } = require('../utils.js');
describe('misc', () => {
it('warns if node builtins are unresolved in a non-CJS, non-ES bundle (#1051)', () => {
const warnings = [];
return rollup
.rollup({
input: 'input',
plugins: [
loader({
input: `import { format } from 'util';\nexport default format( 'this is a %s', 'formatted string' );`
})
],
onwarn: warning => warnings.push(warning)
})
.then(bundle =>
bundle.generate({
format: 'iife',
name: 'myBundle'
})
)
.then(() => {
const relevantWarnings = warnings.filter(
warning => warning.code === 'MISSING_NODE_BUILTINS'
);
assert.equal(relevantWarnings.length, 1);
assert.equal(
relevantWarnings[0].message,
`Creating a browser bundle that depends on Node.js built-in module ('util'). You might need to include https://www.npmjs.com/package/rollup-plugin-node-builtins`
);
});
});
it('warns when a global module name is guessed in a UMD bundle (#2358)', () => {
const warnings = [];
return rollup
.rollup({
input: 'input',
external: ['lodash'],
plugins: [
loader({
input: `import * as _ from 'lodash'; console.log(_);`
})
],
onwarn: warning => warnings.push(warning)
})
.then(bundle =>
bundle.generate({
format: 'umd',
globals: [],
name: 'myBundle'
})
)
.then(() => {
delete warnings[0].toString;
assert.deepEqual(warnings, [
{
code: 'MISSING_GLOBAL_NAME',
guess: '_',
message:
"No name was provided for external module 'lodash' in output.globals – guessing '_'",
source: 'lodash'
}
]);
});
});
it('sorts chunks in the output', () => {
const warnings = [];
return rollup
.rollup({
input: ['main1', 'main2'],
plugins: [
loader({
main1: 'import "dep";console.log("main1");',
main2: 'import "dep";console.log("main2");',
dep: 'console.log("dep");import("dyndep");',
dyndep: 'console.log("dyndep");'
})
],
onwarn: warning => warnings.push(warning)
})
.then(bundle => bundle.generate({ format: 'es' }))
.then(({ output }) => {
assert.equal(warnings.length, 0);
assert.deepEqual(output.map(({ fileName }) => fileName), [
'main1.js',
'main2.js',
'chunk-9d1272f4.js',
'chunk-80285050.js'
]);
});
});
it('ignores falsy plugins', () => {
return rollup.rollup({
input: 'x',
plugins: [loader({ x: `console.log( 42 );` }), null, false, undefined]
});
});
it('handles different import paths for different outputs', () => {
return rollup
.rollup({
input: 'x',
external: ['the-answer'],
plugins: [loader({ x: `import 'the-answer'` })]
})
.then(bundle =>
Promise.all([
bundle
.generate({ format: 'esm' })
.then(generated =>
assert.equal(generated.output[0].code, "import 'the-answer';\n", 'no render path 1')
),
bundle
.generate({ format: 'esm', paths: id => `//unpkg.com/${id}@?module` })
.then(generated =>
assert.equal(
generated.output[0].code,
"import '//unpkg.com/the-answer@?module';\n",
'with render path'
)
),
bundle
.generate({ format: 'esm' })
.then(generated =>
assert.equal(generated.output[0].code, "import 'the-answer';\n", 'no render path 2')
)
])
);
});
});