12 | 12 |
|
13 | 13 |
-import * as acorn from 'acorn';
|
14 | 14 |
-import test from 'ava';
|
15 | |
+const acorn = require('acorn');
|
|
15 |
+const * as acorn = require('acorn');
|
16 | 16 |
+const test = require('tape');
|
17 | 17 |
|
18 | 18 |
-import { commonjs } from './helpers/util';
|
|
46 | 46 |
@@ -1,16 +1,16 @@
|
47 | 47 |
/* eslint-disable line-comment-position, no-new-func, no-undefined */
|
48 | 48 |
-import * as path from 'path';
|
49 | |
+const path = require('path');
|
|
49 |
+const * as path = require('path');
|
50 | 50 |
|
51 | 51 |
-import test from 'ava';
|
52 | 52 |
-import { SourceMapConsumer } from 'source-map';
|
|
59 | 59 |
+const { install } = require('source-map-support');
|
60 | 60 |
+const { getLocator } = require('locate-character');
|
61 | 61 |
+const { rollup } = require('rollup');
|
62 | |
+const resolve = require('rollup-plugin-node-resolve');
|
|
62 |
+const resolve = require('@rollup/plugin-node-resolve');
|
63 | 63 |
|
64 | 64 |
-import { testBundle } from '../../../util/test';
|
65 | 65 |
+const { testBundle } = require('../../../util/test');
|
|
94 | 94 |
});
|
95 | 95 |
|
96 | 96 |
test('supports an object of multiple entry points', async (t) => {
|
97 | |
@@ -90,15 +92,17 @@
|
|
97 |
@@ -90,13 +92,14 @@
|
98 | 98 |
|
99 | 99 |
if (Array.isArray(output)) {
|
100 | 100 |
t.is(output.length, 3);
|
|
110 | 110 |
+ t.end();
|
111 | 111 |
});
|
112 | 112 |
|
113 | |
+/*
|
114 | 113 |
test('handles references to `global`', async (t) => {
|
115 | |
const bundle = await rollup({
|
116 | |
input: 'fixtures/samples/global/main.js',
|
117 | |
@@ -124,7 +128,9 @@
|
|
114 |
@@ -124,6 +127,7 @@
|
118 | 115 |
|
119 | 116 |
fn({}, undefined, undefined, undefined, mockSelf);
|
120 | 117 |
t.is(mockSelf.foo, 'bar', code);
|
121 | 118 |
+ t.end();
|
122 | 119 |
});
|
123 | |
+*/
|
124 | 120 |
|
125 | 121 |
test('handles multiple references to `global`', async (t) => {
|
126 | |
const bundle = await rollup({
|
127 | |
@@ -142,8 +148,10 @@
|
|
122 |
@@ -142,8 +146,10 @@
|
128 | 123 |
|
129 | 124 |
fn(module, module.exports, globalThis);
|
130 | 125 |
t.is(globalThis.count, 2);
|
|
135 | 130 |
test('handles transpiled CommonJS modules', async (t) => {
|
136 | 131 |
const bundle = await rollup({
|
137 | 132 |
input: 'fixtures/samples/corejs/literal-with-default.js',
|
138 | |
@@ -157,6 +165,7 @@
|
139 | |
fn(module, module.exports);
|
140 | |
|
141 | |
t.is(module.exports, 'foobar', code);
|
142 | |
+ t.end();
|
143 | |
});
|
144 | |
|
145 | |
test('handles successive builds', async (t) => {
|
146 | |
@@ -182,9 +191,10 @@
|
147 | |
fn(module, module.exports);
|
148 | |
|
149 | |
t.is(module.exports, 'foobar', code);
|
150 | |
+ t.end();
|
151 | |
});
|
152 | |
|
153 | |
-test.serial('handles symlinked node_modules with preserveSymlinks: false', (t) => {
|
154 | |
+test('handles symlinked node_modules with preserveSymlinks: false', (t) => {
|
155 | |
const cwd = process.cwd();
|
156 | |
|
157 | |
// ensure we resolve starting from a directory with
|
158 | |
@@ -216,6 +226,7 @@
|
159 | |
throw err;
|
160 | |
})
|
161 | |
);
|
162 | |
+ t.end();
|
163 | |
});
|
164 | |
|
165 | |
test('converts a CommonJS module with custom file extension', async (t) => {
|
166 | |
@@ -225,6 +236,7 @@
|
167 | |
});
|
168 | |
|
169 | |
t.is((await executeBundle(bundle, t)).exports, 42);
|
170 | |
+ t.end();
|
171 | |
});
|
172 | |
|
173 | |
test('identifies named exports from object literals', async (t) => {
|
174 | |
@@ -236,6 +248,7 @@
|
|
133 |
@@ -236,6 +242,7 @@
|
175 | 134 |
t.plan(3);
|
176 | 135 |
await testBundle(t, bundle);
|
177 | 136 |
});
|
|
179 | 138 |
|
180 | 139 |
test('can ignore references to `global`', async (t) => {
|
181 | 140 |
const bundle = await rollup({
|
182 | |
@@ -254,8 +267,10 @@
|
|
141 |
@@ -254,6 +261,7 @@
|
183 | 142 |
t.is(exports.immediate1, global.setImmediate, code);
|
184 | 143 |
t.is(exports.immediate2, global.setImmediate, code);
|
185 | 144 |
t.is(exports.immediate3, null, code);
|
186 | 145 |
+ t.end();
|
187 | 146 |
});
|
188 | 147 |
|
189 | |
+/*
|
190 | 148 |
test('can handle parens around right have node while producing default export', async (t) => {
|
191 | |
const bundle = await rollup({
|
192 | |
input: 'fixtures/samples/paren-expression/index.js',
|
193 | |
@@ -263,6 +278,7 @@
|
|
149 |
@@ -263,6 +271,7 @@
|
194 | 150 |
});
|
195 | 151 |
|
196 | 152 |
t.is((await executeBundle(bundle, t)).exports, 42);
|
|
198 | 154 |
});
|
199 | 155 |
|
200 | 156 |
test('typeof transforms: correct-scoping', async (t) => {
|
201 | |
@@ -272,6 +288,7 @@
|
|
157 |
@@ -272,6 +281,7 @@
|
202 | 158 |
});
|
203 | 159 |
|
204 | 160 |
t.is((await executeBundle(bundle, t)).exports, 'object');
|
|
206 | 162 |
});
|
207 | 163 |
|
208 | 164 |
test('typeof transforms: protobuf', async (t) => {
|
209 | |
@@ -282,7 +299,9 @@
|
|
165 |
@@ -282,6 +292,7 @@
|
210 | 166 |
});
|
211 | 167 |
|
212 | 168 |
t.is((await executeBundle(bundle, t)).exports, true);
|
213 | 169 |
+ t.end();
|
214 | 170 |
});
|
215 | |
+*/
|
216 | 171 |
|
217 | 172 |
test('typeof transforms: sinon', async (t) => {
|
218 | |
const bundle = await rollup({
|
219 | |
@@ -297,6 +316,7 @@
|
|
173 |
@@ -297,6 +308,7 @@
|
220 | 174 |
t.is(code.indexOf('typeof require'), -1, code);
|
221 | 175 |
// t.not( code.indexOf( 'typeof module' ), -1, code ); // #151 breaks this test
|
222 | 176 |
// t.not( code.indexOf( 'typeof define' ), -1, code ); // #144 breaks this test
|
|
224 | 178 |
});
|
225 | 179 |
|
226 | 180 |
test('deconflicts helper name', async (t) => {
|
227 | |
@@ -307,6 +327,7 @@
|
|
181 |
@@ -307,6 +319,7 @@
|
228 | 182 |
|
229 | 183 |
const { exports } = await executeBundle(bundle, t);
|
230 | 184 |
t.not(exports, 'nope');
|
|
232 | 186 |
});
|
233 | 187 |
|
234 | 188 |
test('deconflicts reserved keywords', async (t) => {
|
235 | |
@@ -317,8 +338,10 @@
|
|
189 |
@@ -317,8 +330,10 @@
|
236 | 190 |
|
237 | 191 |
const reservedProp = (await executeBundle(bundle, t)).exports.delete;
|
238 | 192 |
t.is(reservedProp, 'foo');
|
|
243 | 197 |
test('does not process the entry file when it has a leading "." (issue #63)', async (t) => {
|
244 | 198 |
const bundle = await rollup({
|
245 | 199 |
input: './fixtures/function/basic/main.js',
|
246 | |
@@ -344,6 +367,7 @@
|
|
200 |
@@ -344,6 +359,7 @@
|
247 | 201 |
|
248 | 202 |
await t.notThrowsAsync(executeBundle(bundle, t));
|
249 | 203 |
});
|
|
251 | 205 |
|
252 | 206 |
test('rewrites top-level defines', async (t) => {
|
253 | 207 |
const bundle = await rollup({
|
254 | |
@@ -359,6 +383,7 @@
|
|
208 |
@@ -359,6 +375,7 @@
|
255 | 209 |
|
256 | 210 |
const { exports } = await executeBundle(bundle, t, { context: { define } });
|
257 | 211 |
t.is(exports, 42);
|
|
259 | 213 |
});
|
260 | 214 |
|
261 | 215 |
test('respects options.external', async (t) => {
|
262 | |
@@ -373,6 +398,7 @@
|
|
216 |
@@ -373,6 +390,7 @@
|
263 | 217 |
|
264 | 218 |
const { exports } = await executeBundle(bundle, t);
|
265 | 219 |
t.is(exports, 'HELLO');
|
|
267 | 221 |
});
|
268 | 222 |
|
269 | 223 |
test('prefers to set name using directory for index files', async (t) => {
|
270 | |
@@ -386,6 +412,7 @@
|
|
224 |
@@ -386,6 +404,7 @@
|
271 | 225 |
t.not(code.indexOf('var invalidVar'), -1);
|
272 | 226 |
t.not(code.indexOf('var validVar'), -1);
|
273 | 227 |
t.not(code.indexOf('var nonIndex'), -1);
|
|
275 | 229 |
});
|
276 | 230 |
|
277 | 231 |
test('does not warn even if the ES module does not export "default"', async (t) => {
|
278 | |
@@ -410,8 +437,10 @@
|
|
232 |
@@ -410,8 +429,10 @@
|
279 | 233 |
onwarn: (warn) => warns.push(warn)
|
280 | 234 |
});
|
281 | 235 |
t.is(warns.length, 0);
|
|
286 | 240 |
test('compiles with cache', async (t) => {
|
287 | 241 |
// specific commonjs require() to ensure same instance is used
|
288 | 242 |
// eslint-disable-next-line global-require
|
289 | |
@@ -430,6 +459,7 @@
|
|
243 |
@@ -430,6 +451,7 @@
|
290 | 244 |
})
|
291 | 245 |
);
|
292 | 246 |
});
|
|
294 | 248 |
|
295 | 249 |
test('creates an error with a code frame when parsing fails', async (t) => {
|
296 | 250 |
try {
|
297 | |
@@ -443,9 +473,11 @@
|
|
251 |
@@ -443,225 +465,5 @@
|
298 | 252 |
'1: /* eslint-disable */\n2: export const foo = 2,\n ^'
|
299 | 253 |
);
|
300 | 254 |
}
|
301 | |
+ t.end();
|
302 | |
});
|
303 | |
|
304 | |
// Virtual modules are treated as "requireReturnsDefault: 'always'" to avoid interop
|
305 | |
+/*
|
306 | |
test('ignores virtual modules', async (t) => {
|
307 | |
const bundle = await rollup({
|
308 | |
input: 'fixtures/samples/ignore-virtual-modules/main.js',
|
309 | |
@@ -469,6 +501,7 @@
|
310 | |
});
|
311 | |
t.is((await executeBundle(bundle, t)).exports, 'Virtual export');
|
312 | |
});
|
313 | |
+*/
|
314 | |
|
315 | |
test('does not produce warnings when importing .mjs without default export', async (t) => {
|
316 | |
const bundle = await rollup({
|
317 | |
@@ -504,8 +537,10 @@
|
318 | |
]
|
319 | |
});
|
320 | |
t.deepEqual((await executeBundle(bundle, t)).exports, { result: 'from esm' });
|
321 | |
+ t.end();
|
322 | |
});
|
323 | |
|
324 | |
+/*
|
325 | |
test('produces optimized code when importing esm with a known default export', async (t) => {
|
326 | |
const bundle = await rollup({
|
327 | |
input: 'main.js',
|
328 | |
@@ -539,79 +574,7 @@
|
329 | |
module.exports = main;
|
330 | |
`
|
331 | |
);
|
|
255 |
-});
|
|
256 |
-
|
|
257 |
-// Virtual modules are treated as "requireReturnsDefault: 'always'" to avoid interop
|
|
258 |
-test('ignores virtual modules', async (t) => {
|
|
259 |
- const bundle = await rollup({
|
|
260 |
- input: 'fixtures/samples/ignore-virtual-modules/main.js',
|
|
261 |
- plugins: [
|
|
262 |
- commonjs(),
|
|
263 |
- {
|
|
264 |
- resolveId(id) {
|
|
265 |
- if (id === '\0virtual' || id === '\0resolved-virtual') {
|
|
266 |
- return '\0resolved-virtual';
|
|
267 |
- }
|
|
268 |
- return null;
|
|
269 |
- },
|
|
270 |
- load(id) {
|
|
271 |
- if (id === '\0resolved-virtual') {
|
|
272 |
- return 'export default "Virtual export"';
|
|
273 |
- }
|
|
274 |
- return null;
|
|
275 |
- }
|
|
276 |
- }
|
|
277 |
- ]
|
|
278 |
- });
|
|
279 |
- t.is((await executeBundle(bundle, t)).exports, 'Virtual export');
|
|
280 |
-});
|
|
281 |
-
|
|
282 |
-test('does not produce warnings when importing .mjs without default export', async (t) => {
|
|
283 |
- const bundle = await rollup({
|
|
284 |
- input: 'main.mjs',
|
|
285 |
- onwarn(warning) {
|
|
286 |
- // The interop should not trigger a "default is not exported" warning
|
|
287 |
- throw new Error(`Unexpected warning: ${warning.message}`);
|
|
288 |
- },
|
|
289 |
- plugins: [
|
|
290 |
- commonjs(),
|
|
291 |
- {
|
|
292 |
- load(id) {
|
|
293 |
- if (id === 'main.mjs') {
|
|
294 |
- return 'import cjs from "cjs.js"; export default cjs;';
|
|
295 |
- }
|
|
296 |
- if (id === 'cjs.js') {
|
|
297 |
- // CJS libraries expect to receive a CJS file here
|
|
298 |
- return 'module.exports = require("fromNodeModules");';
|
|
299 |
- }
|
|
300 |
- if (id === 'fromNodeModules.mjs') {
|
|
301 |
- return 'export const result = "from esm";';
|
|
302 |
- }
|
|
303 |
- return null;
|
|
304 |
- },
|
|
305 |
- resolveId(id) {
|
|
306 |
- // rollup-plugin-node-resolve usually prefers ESM versions
|
|
307 |
- if (id === 'fromNodeModules') {
|
|
308 |
- return 'fromNodeModules.mjs';
|
|
309 |
- }
|
|
310 |
- return id;
|
|
311 |
- }
|
|
312 |
- }
|
|
313 |
- ]
|
|
314 |
- });
|
|
315 |
- t.deepEqual((await executeBundle(bundle, t)).exports, { result: 'from esm' });
|
|
316 |
-});
|
|
317 |
-
|
|
318 |
-test('produces optimized code when importing esm with a known default export', async (t) => {
|
|
319 |
- const bundle = await rollup({
|
|
320 |
- input: 'main.js',
|
|
321 |
- plugins: [
|
|
322 |
- commonjs({ requireReturnsDefault: true }),
|
|
323 |
- {
|
|
324 |
- load(id) {
|
|
325 |
- if (id === 'main.js') {
|
|
326 |
- return 'module.exports = require("esm.js")';
|
|
327 |
- }
|
|
328 |
- if (id === 'esm.js') {
|
|
329 |
- return 'export const ignored = "ignored"; export default "default"';
|
|
330 |
- }
|
|
331 |
- return null;
|
|
332 |
- },
|
|
333 |
- resolveId(id) {
|
|
334 |
- return id;
|
|
335 |
- }
|
|
336 |
- }
|
|
337 |
- ]
|
|
338 |
- });
|
|
339 |
- const code = await getCodeFromBundle(bundle);
|
|
340 |
- t.is(
|
|
341 |
- code,
|
|
342 |
- `'use strict';
|
|
343 |
-
|
|
344 |
-var require$$0 = "default";
|
|
345 |
-
|
|
346 |
-var main = require$$0;
|
|
347 |
-
|
|
348 |
-module.exports = main;
|
|
349 |
-`
|
|
350 |
- );
|
332 | 351 |
-});
|
333 | 352 |
-
|
334 | 353 |
-test('produces optimized code when importing esm without a default export', async (t) => {
|
|
364 | 383 |
- value: value
|
365 | 384 |
-});
|
366 | 385 |
-
|
367 | |
-var main = esm;
|
|
386 |
-function getAugmentedNamespace(n) {
|
|
387 |
- if (n.__esModule) return n;
|
|
388 |
- var a = Object.defineProperty({}, '__esModule', {value: true});
|
|
389 |
- Object.keys(n).forEach(function (k) {
|
|
390 |
- var d = Object.getOwnPropertyDescriptor(n, k);
|
|
391 |
- Object.defineProperty(a, k, d.get ? d : {
|
|
392 |
- enumerable: true,
|
|
393 |
- get: function () {
|
|
394 |
- return n[k];
|
|
395 |
- }
|
|
396 |
- });
|
|
397 |
- });
|
|
398 |
- return a;
|
|
399 |
-}
|
|
400 |
-
|
|
401 |
-var require$$0 = /*@__PURE__*/getAugmentedNamespace(esm);
|
|
402 |
-
|
|
403 |
-var main = require$$0;
|
368 | 404 |
-
|
369 | 405 |
-module.exports = main;
|
370 | 406 |
-`
|
|
402 | 438 |
-exports.shuffleArray = shuffleArray_1;
|
403 | 439 |
-`
|
404 | 440 |
- );
|
405 | |
+ t.end();
|
406 | |
});
|
407 | |
|
408 | |
test('can spread an object into module.exports', async (t) => {
|
409 | |
@@ -638,6 +601,7 @@
|
410 | |
message,
|
411 | |
'The namedExports option from "@rollup/plugin-commonjs" is deprecated. Named exports are now handled automatically.'
|
412 | |
);
|
413 | |
+ t.end();
|
414 | |
});
|
415 | |
|
416 | |
test('imports .cjs file extension by default', async (t) => {
|
417 | |
@@ -648,3 +612,4 @@
|
418 | |
const code = await getCodeFromBundle(bundle);
|
419 | |
t.snapshot(code);
|
420 | |
});
|
421 | |
+*/
|
|
441 |
-});
|
|
442 |
-
|
|
443 |
-test('can spread an object into module.exports', async (t) => {
|
|
444 |
- const bundle = await rollup({
|
|
445 |
- input: 'fixtures/samples/module-exports-spread/main.js',
|
|
446 |
- plugins: [commonjs()]
|
|
447 |
- });
|
|
448 |
- const code = await getCodeFromBundle(bundle);
|
|
449 |
- t.snapshot(code);
|
|
450 |
-});
|
|
451 |
-
|
|
452 |
-test('logs a warning when the deprecated namedExports option is used', async (t) => {
|
|
453 |
- let message;
|
|
454 |
- const bundle = await rollup({
|
|
455 |
- onwarn(warning) {
|
|
456 |
- ({ message } = warning);
|
|
457 |
- },
|
|
458 |
- input: 'fixtures/samples/sourcemap/main.js',
|
|
459 |
- plugins: [commonjs({ namedExports: { foo: ['bar'] } })]
|
|
460 |
- });
|
|
461 |
-
|
|
462 |
- await getCodeFromBundle(bundle);
|
|
463 |
- t.is(
|
|
464 |
- message,
|
|
465 |
- 'The namedExports option from "@rollup/plugin-commonjs" is deprecated. Named exports are now handled automatically.'
|
|
466 |
- );
|
|
467 |
-});
|
|
468 |
-
|
|
469 |
-test('imports .cjs file extension by default', async (t) => {
|
|
470 |
- const bundle = await rollup({
|
|
471 |
- input: 'fixtures/samples/cjs-extension/main.js',
|
|
472 |
- plugins: [commonjs()]
|
|
473 |
- });
|
|
474 |
- const code = await getCodeFromBundle(bundle);
|
|
475 |
- t.snapshot(code);
|
|
476 |
+ t.end();
|
|
477 |
});
|