/
misc.js
179 lines (168 loc) · 4.3 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const assert = require('assert');
const rollup = require('../../dist/rollup');
const { loader } = require('../utils.js');
describe('misc', () => {
it('avoids modification of options or their properties', () => {
const { freeze } = Object;
return rollup.rollup(
freeze({
input: 'input',
external: freeze([]),
plugins: freeze([
{
name: 'loader',
resolveId: freeze(() => 'input'),
load: freeze(() => `export default 0;`)
}
]),
acornInjectPlugins: freeze([]),
acorn: freeze({}),
experimentalTopLevelAwait: true,
treeshake: freeze({})
})
);
});
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',
'dep-f8bec8a7.js',
'dyndep-b0a9ee12.js'
]);
});
});
it('ignores falsy plugins', () =>
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')
)
])
);
});
it('allows passing the same object to `rollup` and `generate`', () => {
const options = {
input: 'input',
plugins: [
loader({
input: 'export default 42;'
})
],
output: {
format: 'esm'
}
};
return rollup
.rollup(options)
.then(bundle => bundle.generate(options))
.then(output =>
assert.strictEqual(output.output[0].code, 'var input = 42;\n\nexport default input;\n')
);
});
});