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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
|
/* eslint-disable line-comment-position, no-new-func, no-undefined */
const path = require('path');
const resolve = require('@rollup/plugin-node-resolve').default;
const test = require('tape');
const { getLocator } = require('locate-character');
const { rollup } = require('rollup');
const { SourceMapConsumer } = require('source-map');
const { install } = require('source-map-support');
const { testBundle } = require('../../../util/test');
const { peerDependencies } = require('../package.json');
const { commonjs, executeBundle, getCodeFromBundle } = require('./helpers/util');
install();
process.chdir(__dirname);
/*
test('Rollup peer dependency has correct format', async (t) => {
t.regex(peerDependencies.rollup, /^\^\d+\.\d+\.\d+(\|\|\^\d+\.\d+\.\d+)*$/);
t.end();
});
*/
// most of these should be moved over to function...
test('generates a sourcemap', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/sourcemap/main.js',
plugins: [commonjs({ sourceMap: true })]
});
const {
output: [{ code, map }]
} = await bundle.generate({
exports: 'auto',
format: 'cjs',
sourcemap: true,
sourcemapFile: path.resolve('bundle.js')
});
const smc = await new SourceMapConsumer(map);
const locator = getLocator(code, { offsetLine: 1 });
let generatedLoc = locator('42');
let loc = smc.originalPositionFor(generatedLoc); // 42
t.is(loc.source, 'fixtures/samples/sourcemap/foo.js');
t.is(loc.line, 1);
t.is(loc.column, 15);
generatedLoc = locator('log');
loc = smc.originalPositionFor(generatedLoc); // log
t.is(loc.source, 'fixtures/samples/sourcemap/main.js');
t.is(loc.line, 3);
t.is(loc.column, 8);
t.end();
});
test('supports an array of multiple entry points', async (t) => {
const bundle = await rollup({
input: [
'fixtures/samples/multiple-entry-points/b.js',
'fixtures/samples/multiple-entry-points/c.js'
],
plugins: [commonjs()]
});
const { output } = await bundle.generate({
exports: 'auto',
format: 'cjs',
chunkFileNames: '[name].js'
});
if (Array.isArray(output)) {
t.is(output.length, 3);
t.ok(output.find(({ fileName }) => fileName === 'b.js'));
t.ok(output.find(({ fileName }) => fileName === 'c.js'));
} else {
t.is(Object.keys(output).length, 3);
t.is('b.js' in output, true);
t.is('c.js' in output, true);
}
t.end();
});
test('supports an object of multiple entry points', async (t) => {
const bundle = await rollup({
input: {
b: require.resolve('./fixtures/samples/multiple-entry-points/b.js'),
c: require.resolve('./fixtures/samples/multiple-entry-points/c.js')
},
plugins: [resolve(), commonjs()]
});
const { output } = await bundle.generate({
exports: 'auto',
format: 'cjs',
chunkFileNames: '[name].js'
});
if (Array.isArray(output)) {
t.is(output.length, 3);
t.ok(output.find(({ fileName }) => fileName === 'b.js'));
t.ok(output.find(({ fileName }) => fileName === 'c.js'));
} else {
t.is(Object.keys(output).length, 3);
t.is('b.js' in output, true);
t.is('c.js' in output, true);
}
t.end();
});
test('handles references to `global`', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/global/main.js',
plugins: [commonjs()]
});
const code = await getCodeFromBundle(bundle);
const mockWindow = {};
const mockGlobal = {};
const mockSelf = {};
const fn = new Function('module', 'globalThis', 'window', 'global', 'self', code);
fn({}, undefined, mockWindow, mockGlobal, mockSelf);
t.is(mockWindow.foo, 'bar', code);
t.is(mockGlobal.foo, undefined, code);
t.is(mockSelf.foo, undefined, code);
fn({}, undefined, undefined, mockGlobal, mockSelf);
t.is(mockGlobal.foo, 'bar', code);
t.is(mockSelf.foo, undefined, code);
fn({}, undefined, undefined, undefined, mockSelf);
t.is(mockSelf.foo, 'bar', code);
t.end();
});
test('handles multiple references to `global`', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/global-in-if-block/main.js',
plugins: [commonjs()]
});
const code = await getCodeFromBundle(bundle);
const fn = new Function('module', 'exports', 'globalThis', code);
const module = { exports: {} };
const globalThis = {};
fn(module, module.exports, globalThis);
t.is(globalThis.count, 1);
fn(module, module.exports, globalThis);
t.is(globalThis.count, 2);
t.end();
});
/*
test('handles transpiled CommonJS modules', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/corejs/literal-with-default.js',
plugins: [commonjs()]
});
const code = await getCodeFromBundle(bundle);
const module = { exports: {} };
const fn = new Function('module', 'exports', code);
fn(module, module.exports);
t.is(module.exports, 'foobar', code);
});
test('handles successive builds', async (t) => {
const plugin = commonjs();
let bundle = await rollup({
input: 'fixtures/samples/corejs/literal-with-default.js',
plugins: [plugin]
});
await bundle.generate({
exports: 'auto',
format: 'cjs'
});
bundle = await rollup({
input: 'fixtures/samples/corejs/literal-with-default.js',
plugins: [plugin]
});
const code = await getCodeFromBundle(bundle);
const module = { exports: {} };
const fn = new Function('module', 'exports', code);
fn(module, module.exports);
t.is(module.exports, 'foobar', code);
});
test.serial('handles symlinked node_modules with preserveSymlinks: false', (t) => {
const cwd = process.cwd();
// ensure we resolve starting from a directory with
// symlinks in node_modules.
process.chdir('fixtures/samples/symlinked-node-modules');
return t.notThrowsAsync(
rollup({
input: './index.js',
onwarn(warning) {
// should not get a warning about unknown export 'foo'
throw new Error(`Unexpected warning: ${warning.message}`);
},
plugins: [
resolve({
preserveSymlinks: false,
preferBuiltins: false
}),
commonjs()
]
})
.then((v) => {
process.chdir(cwd);
return v;
})
.catch((err) => {
process.chdir(cwd);
throw err;
})
);
});
test('converts a CommonJS module with custom file extension', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/extension/main.coffee',
plugins: [commonjs({ extensions: ['.coffee'] })]
});
t.is((await executeBundle(bundle, t)).exports, 42);
});
test('identifies named exports from object literals', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/named-exports-from-object-literal/main.js',
plugins: [commonjs()]
});
t.plan(3);
await testBundle(t, bundle);
});
*/
test('can ignore references to `global`', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/ignore-global/main.js',
plugins: [commonjs({ ignoreGlobal: true })],
onwarn: (warning) => {
if (warning.code === 'THIS_IS_UNDEFINED') return;
// eslint-disable-next-line no-console
console.warn(warning.message);
}
});
const code = await getCodeFromBundle(bundle);
const { exports, global } = await executeBundle(bundle, t);
t.is(exports.immediate1, global.setImmediate, code);
t.is(exports.immediate2, global.setImmediate, code);
t.is(exports.immediate3, null, code);
t.end();
});
test('can handle parens around right have node while producing default export', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/paren-expression/index.js',
plugins: [commonjs()]
});
t.is((await executeBundle(bundle, t)).exports, 42);
t.end();
});
test('typeof transforms: correct-scoping', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/umd/correct-scoping.js',
plugins: [commonjs()]
});
t.is((await executeBundle(bundle, t)).exports, 'object');
t.end();
});
test('typeof transforms: protobuf', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/umd/protobuf.js',
external: ['bytebuffer', 'foo'],
plugins: [commonjs()]
});
t.is((await executeBundle(bundle, t)).exports, true);
t.end();
});
test('typeof transforms: sinon', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/umd/sinon.js',
plugins: [commonjs()]
});
const {
output: [{ code }]
} = await bundle.generate({ format: 'es' });
t.is(code.indexOf('typeof require'), -1, code);
// t.not( code.indexOf( 'typeof module' ), -1, code ); // #151 breaks this test
// t.not( code.indexOf( 'typeof define' ), -1, code ); // #144 breaks this test
t.end();
});
test('deconflicts helper name', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/deconflict-helpers/main.js',
plugins: [commonjs()]
});
const { exports } = await executeBundle(bundle, t);
t.not(exports, 'nope');
t.end();
});
test('deconflicts reserved keywords', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/reserved-as-property/main.js',
plugins: [commonjs()]
});
const reservedProp = (await executeBundle(bundle, t, { exports: 'named' })).exports.delete;
t.is(reservedProp, 'foo');
t.end();
});
/*
test('does not process the entry file when it has a leading "." (issue #63)', async (t) => {
const bundle = await rollup({
input: './fixtures/function/basic/main.js',
plugins: [commonjs()]
});
await t.notThrowsAsync(executeBundle(bundle, t));
});
test('respects other plugins', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/other-transforms/main.js',
plugins: [
{
transform(code, id) {
if (id[0] === '\0') return null;
return code.replace('40', '41');
}
},
commonjs()
]
});
await t.notThrowsAsync(executeBundle(bundle, t));
});
*/
test('rewrites top-level defines', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/define-is-undefined/main.js',
plugins: [commonjs()]
});
function define() {
throw new Error('nope');
}
define.amd = true;
const { exports } = await executeBundle(bundle, t, { context: { define } });
t.is(exports, 42);
t.end();
});
test('respects options.external', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/external/main.js',
plugins: [resolve(), commonjs()],
external: ['baz']
});
const code = await getCodeFromBundle(bundle);
t.is(code.indexOf('hello'), -1);
const { exports } = await executeBundle(bundle, t);
t.is(exports, 'HELLO');
t.end();
});
test('prefers to set name using directory for index files', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/rename-index/main.js',
plugins: [commonjs()]
});
const code = await getCodeFromBundle(bundle);
t.is(code.indexOf('var index'), -1);
t.not(code.indexOf('var invalidVar'), -1);
t.not(code.indexOf('var validVar'), -1);
t.not(code.indexOf('var nonIndex'), -1);
t.end();
});
test('does not warn even if the ES module does not export "default"', async (t) => {
const warns = [];
await rollup({
input: 'fixtures/samples/es-modules-without-default-export/main.js',
plugins: [commonjs()],
onwarn: (warn) => warns.push(warn)
});
t.is(warns.length, 0);
await rollup({
input: 'fixtures/function/bare-import/bar.js',
plugins: [commonjs()],
onwarn: (warn) => warns.push(warn)
});
t.is(warns.length, 0);
await rollup({
input: 'fixtures/function/bare-import-comment/main.js',
plugins: [commonjs()],
onwarn: (warn) => warns.push(warn)
});
t.is(warns.length, 0);
t.end();
});
/*
test('compiles with cache', async (t) => {
// specific commonjs require() to ensure same instance is used
// eslint-disable-next-line global-require
const commonjsInstance = require('../dist/index');
const bundle = await rollup({
input: 'fixtures/function/index/main.js',
plugins: [commonjsInstance()]
});
await t.notThrowsAsync(
rollup({
input: 'fixtures/function/index/main.js',
plugins: [commonjsInstance()],
cache: bundle
})
);
});
*/
test('creates an error with a code frame when parsing fails', async (t) => {
try {
await rollup({
input: 'fixtures/samples/invalid-syntax/main.js',
plugins: [commonjs()]
});
} catch (error) {
t.is(
error.frame,
'1: /* eslint-disable */\n2: export const foo = 2,\n ^'
);
}
t.end();
});
/*
test('registers dynamic requires when entry is from a different loader', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/dynamic-require-different-loader/main.js',
plugins: [
{
load(id) {
if (id === path.resolve('fixtures/samples/dynamic-require-different-loader/main.js')) {
return 'import submodule1 from "./submodule1"; export default submodule1();';
}
return null;
}
},
commonjs({
dynamicRequireTargets: ['fixtures/samples/dynamic-require-different-loader/submodule2.js'],
transformMixedEsModules: true
})
]
});
t.is((await executeBundle(bundle, t)).exports, 'Hello there');
t.end();
});
test('transforms the es file with a `commonjsRequire` and no `require`s', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/dynamic-require-es-mixed-helpers/main.js',
plugins: [
commonjs({
dynamicRequireTargets: ['fixtures/samples/dynamic-require-es-mixed-helpers/submodule.js'],
transformMixedEsModules: true
})
]
});
const code = await getCodeFromBundle(bundle);
t.is(/commonjsRequire\(["']\.\/submodule\.js/.test(code), true);
t.end();
});
test('does not wrap commonjsRegister calls in createCommonjsModule', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/dynamic-require-double-wrap/main.js',
plugins: [
commonjs({
sourceMap: true,
dynamicRequireTargets: ['fixtures/samples/dynamic-require-double-wrap/submodule.js']
})
]
});
const code = await getCodeFromBundle(bundle, { exports: 'named' });
t.not(/createCommonjsModule\(function/.test(code), true);
t.end();
});
// This test uses worker threads to simulate an empty internal cache and needs at least Node 12
if (Number(/^v(\d+)/.exec(process.version)[1]) >= 12) {
test('can be cached across instances', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/caching/main.js',
plugins: [commonjs()]
});
const { cache } = bundle;
const code = await getCodeFromBundle(bundle);
// We do a second run in a worker so that all internal state is cleared
const { Worker } = await import('worker_threads');
const getRollupUpCodeWithCache = new Worker(
path.join(__dirname, 'fixtures/samples/caching/rollupWorker.js'),
{
workerData: cache
}
);
t.is(code, await new Promise((done) => getRollupUpCodeWithCache.on('message', done)));
t.end();
});
}
*/
|