Codebase list node-rollup-plugin-commonjs / 40db9fd
Refresh patches Xavier Guimard 3 years ago
2 changed file(s) with 173 addition(s) and 117 deletion(s). Raw diff Collapse all Expand all
1313 , node-rollup-plugin-babel (>= 4.4~) <!pkg.node-rollup-plugin-commonjs.transpilebuble>
1414 , node-rollup-plugin-buble <pkg.node-rollup-plugin-commonjs.transpilebuble>
1515 , node-rollup-plugin-json
16 , node-rollup-plugin-node-resolve <!nocheck>
16 , node-rollup-plugin-node-resolve (>= 9) <!nocheck>
1717 , node-rollup-pluginutils (>= 2.3.3-5)
1818 , node-source-map <!nocheck>
1919 , node-source-map-support <!nocheck>
1212
1313 -import * as acorn from 'acorn';
1414 -import test from 'ava';
15 +const acorn = require('acorn');
15 +const * as acorn = require('acorn');
1616 +const test = require('tape');
1717
1818 -import { commonjs } from './helpers/util';
4646 @@ -1,16 +1,16 @@
4747 /* eslint-disable line-comment-position, no-new-func, no-undefined */
4848 -import * as path from 'path';
49 +const path = require('path');
49 +const * as path = require('path');
5050
5151 -import test from 'ava';
5252 -import { SourceMapConsumer } from 'source-map';
5959 +const { install } = require('source-map-support');
6060 +const { getLocator } = require('locate-character');
6161 +const { rollup } = require('rollup');
62 +const resolve = require('rollup-plugin-node-resolve');
62 +const resolve = require('@rollup/plugin-node-resolve');
6363
6464 -import { testBundle } from '../../../util/test';
6565 +const { testBundle } = require('../../../util/test');
9494 });
9595
9696 test('supports an object of multiple entry points', async (t) => {
97 @@ -90,15 +92,17 @@
97 @@ -90,13 +92,14 @@
9898
9999 if (Array.isArray(output)) {
100100 t.is(output.length, 3);
110110 + t.end();
111111 });
112112
113 +/*
114113 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 @@
118115
119116 fn({}, undefined, undefined, undefined, mockSelf);
120117 t.is(mockSelf.foo, 'bar', code);
121118 + t.end();
122119 });
123 +*/
124120
125121 test('handles multiple references to `global`', async (t) => {
126 const bundle = await rollup({
127 @@ -142,8 +148,10 @@
122 @@ -142,8 +146,10 @@
128123
129124 fn(module, module.exports, globalThis);
130125 t.is(globalThis.count, 2);
135130 test('handles transpiled CommonJS modules', async (t) => {
136131 const bundle = await rollup({
137132 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 @@
175134 t.plan(3);
176135 await testBundle(t, bundle);
177136 });
179138
180139 test('can ignore references to `global`', async (t) => {
181140 const bundle = await rollup({
182 @@ -254,8 +267,10 @@
141 @@ -254,6 +261,7 @@
183142 t.is(exports.immediate1, global.setImmediate, code);
184143 t.is(exports.immediate2, global.setImmediate, code);
185144 t.is(exports.immediate3, null, code);
186145 + t.end();
187146 });
188147
189 +/*
190148 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 @@
194150 });
195151
196152 t.is((await executeBundle(bundle, t)).exports, 42);
198154 });
199155
200156 test('typeof transforms: correct-scoping', async (t) => {
201 @@ -272,6 +288,7 @@
157 @@ -272,6 +281,7 @@
202158 });
203159
204160 t.is((await executeBundle(bundle, t)).exports, 'object');
206162 });
207163
208164 test('typeof transforms: protobuf', async (t) => {
209 @@ -282,7 +299,9 @@
165 @@ -282,6 +292,7 @@
210166 });
211167
212168 t.is((await executeBundle(bundle, t)).exports, true);
213169 + t.end();
214170 });
215 +*/
216171
217172 test('typeof transforms: sinon', async (t) => {
218 const bundle = await rollup({
219 @@ -297,6 +316,7 @@
173 @@ -297,6 +308,7 @@
220174 t.is(code.indexOf('typeof require'), -1, code);
221175 // t.not( code.indexOf( 'typeof module' ), -1, code ); // #151 breaks this test
222176 // t.not( code.indexOf( 'typeof define' ), -1, code ); // #144 breaks this test
224178 });
225179
226180 test('deconflicts helper name', async (t) => {
227 @@ -307,6 +327,7 @@
181 @@ -307,6 +319,7 @@
228182
229183 const { exports } = await executeBundle(bundle, t);
230184 t.not(exports, 'nope');
232186 });
233187
234188 test('deconflicts reserved keywords', async (t) => {
235 @@ -317,8 +338,10 @@
189 @@ -317,8 +330,10 @@
236190
237191 const reservedProp = (await executeBundle(bundle, t)).exports.delete;
238192 t.is(reservedProp, 'foo');
243197 test('does not process the entry file when it has a leading "." (issue #63)', async (t) => {
244198 const bundle = await rollup({
245199 input: './fixtures/function/basic/main.js',
246 @@ -344,6 +367,7 @@
200 @@ -344,6 +359,7 @@
247201
248202 await t.notThrowsAsync(executeBundle(bundle, t));
249203 });
251205
252206 test('rewrites top-level defines', async (t) => {
253207 const bundle = await rollup({
254 @@ -359,6 +383,7 @@
208 @@ -359,6 +375,7 @@
255209
256210 const { exports } = await executeBundle(bundle, t, { context: { define } });
257211 t.is(exports, 42);
259213 });
260214
261215 test('respects options.external', async (t) => {
262 @@ -373,6 +398,7 @@
216 @@ -373,6 +390,7 @@
263217
264218 const { exports } = await executeBundle(bundle, t);
265219 t.is(exports, 'HELLO');
267221 });
268222
269223 test('prefers to set name using directory for index files', async (t) => {
270 @@ -386,6 +412,7 @@
224 @@ -386,6 +404,7 @@
271225 t.not(code.indexOf('var invalidVar'), -1);
272226 t.not(code.indexOf('var validVar'), -1);
273227 t.not(code.indexOf('var nonIndex'), -1);
275229 });
276230
277231 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 @@
279233 onwarn: (warn) => warns.push(warn)
280234 });
281235 t.is(warns.length, 0);
286240 test('compiles with cache', async (t) => {
287241 // specific commonjs require() to ensure same instance is used
288242 // eslint-disable-next-line global-require
289 @@ -430,6 +459,7 @@
243 @@ -430,6 +451,7 @@
290244 })
291245 );
292246 });
294248
295249 test('creates an error with a code frame when parsing fails', async (t) => {
296250 try {
297 @@ -443,9 +473,11 @@
251 @@ -443,225 +465,5 @@
298252 '1: /* eslint-disable */\n2: export const foo = 2,\n ^'
299253 );
300254 }
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 - );
332351 -});
333352 -
334353 -test('produces optimized code when importing esm without a default export', async (t) => {
364383 - value: value
365384 -});
366385 -
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;
368404 -
369405 -module.exports = main;
370406 -`
402438 -exports.shuffleArray = shuffleArray_1;
403439 -`
404440 - );
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 });