forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sanity-checks.js
296 lines (273 loc) · 8.36 KB
/
sanity-checks.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
const assert = require('node:assert');
const rollup = require('../../dist/rollup');
const { loader, compareError } = require('../utils.js');
describe('sanity checks', () => {
it('exists', () => {
assert.ok(!!rollup);
});
it('has a rollup method', () => {
assert.equal(typeof rollup.rollup, 'function');
});
it('fails without options', async () => {
let error = null;
try {
await rollup.rollup();
} catch (buildError) {
error = buildError;
}
assert.strictEqual(error && error.message, 'You must supply an options object to rollup');
});
it('node API passes warning and default handler to custom onwarn function', async () => {
let parameters;
await rollup.rollup({
input: 'x',
plugins: [loader({ x: `eval(42);` })],
onwarn(warning, onwarn) {
parameters = [warning, onwarn];
}
});
assert.equal(parameters[0].code, 'EVAL');
assert.equal(
parameters[0].message,
'x (1:0): Use of eval in "x" is strongly discouraged as it poses security risks and may cause issues with minification.'
);
assert.equal(typeof parameters[1], 'function');
});
it('fails without options.input', async () => {
let error = null;
try {
await rollup.rollup({});
} catch (buildError) {
error = buildError;
}
assert.strictEqual(error && error.message, 'You must supply options.input to rollup');
});
it('includes a newline at the end of the bundle', async () => {
const bundle = await rollup.rollup({
input: 'x',
plugins: [loader({ x: `console.log( 42 );` })]
});
const {
output: [{ code }]
} = await bundle.generate({ format: 'iife' });
assert.ok(code[code.length - 1] === '\n');
});
it('throws on missing output options when generating a bundle', async () => {
let error = null;
const bundle = await rollup.rollup({
input: 'x',
plugins: [loader({ x: `console.log( 42 );` })]
});
try {
await bundle.generate();
} catch (generateError) {
error = generateError;
}
assert.strictEqual(error && error.message, 'You must supply an options object');
});
it('throws on missing output options when writing a bundle', async () => {
let error = null;
const bundle = await rollup.rollup({
input: 'x',
plugins: [loader({ x: `console.log( 42 );` })]
});
try {
await bundle.write();
} catch (writeError) {
error = writeError;
}
assert.strictEqual(error && error.message, 'You must supply an options object');
try {
await bundle.write({ format: 'es' });
} catch (writeError) {
error = writeError;
}
assert.strictEqual(
error && error.message,
'You must specify "output.file" or "output.dir" for the build.'
);
});
it('throws on incorrect bundle.generate format option', async () => {
let error = null;
const bundle = await rollup.rollup({
input: 'x',
plugins: [loader({ x: `console.log( 42 );` })]
});
try {
await bundle.generate({ file: 'x', format: 'vanilla' });
} catch (generateError) {
error = generateError;
}
assert.strictEqual(
error && error.message,
'Invalid value "vanilla" for option "output.format" - Valid values are "amd", "cjs", "system", "es", "iife" or "umd".'
);
});
it('defaults to output format `es` if not specified', async () => {
const bundle = await rollup.rollup({
input: 'x',
plugins: [loader({ x: `export function foo(x){ console.log(x); }` })]
});
const {
output: [{ code }]
} = await bundle.generate({});
assert.equal(code, `function foo(x){ console.log(x); }\n\nexport { foo };\n`);
});
it('reuses existing error object', async () => {
class CustomError extends Error {
constructor(message, x) {
super(message);
this.prop = x.toUpperCase();
}
}
let error = null;
const customError = new CustomError('foo', 'bar');
try {
await rollup.rollup({
input: 'x',
plugins: [
loader({ x: `console.log( 42 );` }),
{
transform() {
this.error(customError);
}
}
]
});
} catch (buildError) {
error = buildError;
}
assert.strictEqual(error, customError);
});
it('throws when using multiple inputs together with the "file" option', async () => {
let error = null;
const bundle = await rollup.rollup({
input: ['x', 'y'],
plugins: [loader({ x: 'console.log( "x" );', y: 'console.log( "y" );' })]
});
try {
await bundle.generate({ file: 'x', format: 'es' });
} catch (generateError) {
error = generateError;
}
assert.strictEqual(
error && error.message,
'Invalid value for option "output.file" - when building multiple chunks, the "output.dir" option must be used, not "output.file". To inline dynamic imports, set the "inlineDynamicImports" option.'
);
});
it('does not throw when using a single element array of inputs together with the "file" option', async () => {
const bundle = await rollup.rollup({
input: ['x'],
plugins: [loader({ x: 'console.log( "x" );' })]
});
await bundle.generate({ file: 'x', format: 'es' });
});
it('throws when using dynamic imports with the "file" option', async () => {
let error = null;
const bundle = await rollup.rollup({
input: 'x',
plugins: [loader({ x: 'console.log( "x" );import("y");', y: 'console.log( "y" );' })]
});
try {
await bundle.generate({ file: 'x', format: 'es' });
} catch (generateError) {
error = generateError;
}
assert.strictEqual(
error && error.message,
'Invalid value for option "output.file" - when building multiple chunks, the "output.dir" option must be used, not "output.file". To inline dynamic imports, set the "inlineDynamicImports" option.'
);
});
it('does not throw when using dynamic imports with the "file" option and "inlineDynamicImports"', async () => {
const bundle = await rollup.rollup({
input: 'x',
plugins: [loader({ x: 'console.log( "x" );import("y");', y: 'console.log( "y" );' })]
});
await bundle.generate({
file: 'x',
format: 'es',
inlineDynamicImports: true
});
});
it('throws when using the object form of "input" together with the "file" option', async () => {
let error = null;
const bundle = await rollup.rollup({
input: { main: 'x' },
plugins: [loader({ x: 'console.log( "x" );' })]
});
try {
await bundle.generate({ file: 'x', format: 'es' });
} catch (generateError) {
error = generateError;
}
assert.strictEqual(
error && error.message,
'Invalid value for option "output.file" - you must set "output.dir" instead of "output.file" when providing named inputs.'
);
});
it('throws when using preserveModules together with the "file" option', async () => {
let error = null;
const bundle = await rollup.rollup({
input: 'x',
plugins: [loader({ x: 'console.log( "x" );' })]
});
try {
await bundle.generate({ file: 'x', format: 'es', preserveModules: true });
} catch (generateError) {
error = generateError;
}
assert.strictEqual(
error && error.message,
'Invalid value for option "output.file" - you must set "output.dir" instead of "output.file" when using the "output.preserveModules" option.'
);
});
it('prevents generating and writing from a closed bundle', async () => {
let error = null;
const bundle = await rollup.rollup({
input: 'x',
plugins: [loader({ x: 'console.log( "x" );' })]
});
bundle.close();
// Can be safely called multiple times
bundle.close();
try {
await bundle.generate({ file: 'x', format: 'es' });
} catch (generateError) {
error = generateError;
}
compareError(error, {
code: 'ALREADY_CLOSED',
message: 'Bundle is already closed, no more calls to "generate" or "write" are allowed.'
});
error = null;
try {
await bundle.write({ file: 'x', format: 'es' });
} catch (writeError) {
error = writeError;
}
compareError(error, {
code: 'ALREADY_CLOSED',
message: 'Bundle is already closed, no more calls to "generate" or "write" are allowed.'
});
error = null;
});
it('triggers a warning when using output.amd.id together with the "dir" option', async () => {
let warning = null;
const bundle = await rollup.rollup({
input: 'input',
plugins: [loader({ input: `import('dep')`, dep: `console.log('dep')` })],
onwarn: w => (warning = w)
});
await bundle.generate({
dir: 'x',
format: 'amd',
amd: {
id: 'something'
}
});
assert.strictEqual(
warning && warning.message,
'Invalid value for option "output.amd.id" - this option is only properly supported for single-file builds. Use "output.amd.autoId" and "output.amd.basePath" instead.'
);
});
});