forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.js
263 lines (248 loc) · 7.17 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
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
const assert = require('node: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;`)
}
]),
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 modules ("util"). You might need to include https://github.com/FredKSchott/rollup-plugin-polyfill-node`
);
});
});
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',
id: 'lodash',
message:
'No name was provided for external module "lodash" in "output.globals" – guessing "_".',
names: ['_'],
url: 'https://rollupjs.org/configuration-options/#output-globals'
}
]);
});
});
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-BED4JKkQ.js', 'dyndep-DOckMt73.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', () =>
rollup
.rollup({
input: 'x',
external: ['the-answer'],
plugins: [loader({ x: `import 'the-answer'` })]
})
.then(bundle =>
Promise.all([
bundle
.generate({ format: 'es' })
.then(generated =>
assert.equal(generated.output[0].code, "import 'the-answer';\n", 'no render path 1')
),
bundle
.generate({ format: 'es', 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: 'es' })
.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',
onwarn(warning, handler) {
if (warning.code !== 'INPUT_HOOK_IN_OUTPUT_PLUGIN') {
handler(warning);
}
},
plugins: [
loader({
input: 'export default 42;'
})
],
output: {
format: 'es'
}
};
return rollup
.rollup(options)
.then(bundle => bundle.generate(options))
.then(output =>
assert.strictEqual(
output.output[0].code,
'var input = 42;\n\nexport { input as default };\n'
)
);
});
it('consistently handles comments when using the cache', async () => {
const FILES = {
main: `import value from "other";
console.log(value);
/*#__PURE__*/console.log('removed');`,
other: `var x = "foo";
export default x;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoib3RoZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJvdGhlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxJQUFNLENBQUMsR0FBVyxLQUFLLENBQUM7QUFDeEIsZUFBZSxDQUFDLENBQUMifQ==`
};
const EXPECTED_OUTPUT = `var x = "foo";
console.log(x);
`;
const firstBundle = await rollup.rollup({
input: 'main',
plugins: [loader(FILES)]
});
assert.strictEqual(
(await firstBundle.generate({ format: 'es' })).output[0].code,
EXPECTED_OUTPUT,
'first'
);
const secondBundle = await rollup.rollup({
cache: firstBundle.cache,
input: 'main',
plugins: [loader(FILES)]
});
assert.strictEqual(
(await secondBundle.generate({ format: 'es' })).output[0].code,
EXPECTED_OUTPUT,
'second'
);
});
it('handles imports from chunks with names that correspond to parent directory names of other chunks', async () => {
const bundle = await rollup.rollup({
input: {
'base/main': 'main.js',
'base/main/feature': 'feature.js',
'base/main/feature/sub': 'subfeature.js',
'base/main/feature/sub/sub': 'subsubfeature.js'
},
plugins: [
loader({
'main.js': 'export function fn () { return "main"; } console.log(fn());',
'feature.js': 'import { fn } from "main.js"; console.log(fn() + " feature");',
'subfeature.js': 'import { fn } from "main.js"; console.log(fn() + " subfeature");',
'subsubfeature.js': 'import { fn } from "main.js"; console.log(fn() + " subsubfeature");'
})
]
});
const {
output: [feature, subfeature, subsubfeature, main]
} = await bundle.generate({
entryFileNames: `[name]`,
chunkFileNames: `[name]`,
format: 'es'
});
assert.strictEqual(main.fileName, 'base/main');
assert.strictEqual(feature.fileName, 'base/main/feature');
assert.ok(feature.code.startsWith("import { fn } from '../main'"));
assert.strictEqual(subfeature.fileName, 'base/main/feature/sub');
assert.ok(subfeature.code.startsWith("import { fn } from '../../main'"));
assert.strictEqual(subsubfeature.fileName, 'base/main/feature/sub/sub');
assert.ok(subsubfeature.code.startsWith("import { fn } from '../../../main'"));
});
it('supports rendering es after rendering iife with inlined dynamic imports', async () => {
const bundle = await rollup.rollup({
input: 'main.js',
plugins: [
loader({
'main.js': "import('other.js');",
'other.js': "export const foo = 'bar';"
})
]
});
await bundle.generate({ format: 'iife', inlineDynamicImports: true });
await bundle.generate({ format: 'es', exports: 'auto' });
});
});