Skip to content

Commit

Permalink
move tests and add new ones
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed Nov 26, 2021
1 parent 55f3cb3 commit ca49498
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const fn = 'add';

module.exports = function (a, b) {
const add = require(`lodash/${fn}`);

return add(a, b);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "commonjs-require",
"private": true,
"main": "dist/index.js",
"targets": {
"main": {
"context": "node"
}
},
"dependencies": {
"lodash": "*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const _ = require(`lodash`);

module.exports = function (a, b) {
return _.add(a, b);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "commonjs-require",
"private": true,
"main": "dist/index.js",
"targets": {
"main": {
"context": "node"
}
},
"dependencies": {
"lodash": "*"
}
}
Empty file.
39 changes: 39 additions & 0 deletions packages/core/integration-tests/test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -4422,6 +4422,45 @@ describe('javascript', function () {
assert.equal(await run(b), 2);
});

it('should detect requires in commonjs with plain template literals', async function () {
let b = await bundle(
path.join(
__dirname,
'/integration/commonjs-template-literal-plain/index.js',
),
);
let dist = await outputFS.readFile(
b.getBundles().find(b => b.type === 'js').filePath,
'utf8',
);
assert(dist.includes('$cPUKg$lodash = require("lodash");'));

let add = await run(b);
assert.equal(add(2, 3), 5);
});

it(`should detect requires in commonjs with plain template literals`, async function () {
let b = await bundle(
path.join(
__dirname,
'/integration/commonjs-template-literal-interpolation/index.js',
),
);
let dist = await outputFS.readFile(
b.getBundles().find(b => b.type === 'js').filePath,
'utf8',
);

assert(
dist.includes(
'const add = require(`lodash/${$8cad8166811e0063$var$fn}`);',
),
);

let add = await run(b);
assert.equal(add(2, 3), 5);
});

it('only updates bundle names of changed bundles for browsers', async () => {
let fixtureDir = path.join(__dirname, '/integration/name-invalidation');
let _bundle = () =>
Expand Down
44 changes: 43 additions & 1 deletion packages/core/integration-tests/test/output-formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,47 @@ describe('output formats', function () {
assert.equal((await run(b, {require})).bar, 2);
});

it.only('should support commonjs output when required with plain template literals', async function () {
let b = await bundle(
path.join(
__dirname,
'/integration/formats/commonjs-require-template-literal-plain/index.js',
),
);
let dist = await outputFS.readFile(
b.getBundles().find(b => b.type === 'js').filePath,
'utf8',
);

assert(dist.includes('import "8cad8166811e0063:lodash"'));
assert(dist.includes('$8cad8166811e0063$var$_.add'));

let add = await run(b);
assert.equal(add(2, 3), 5);
});

it(`shouldn't support commonjs output when required with template literals and interpolation`, async function () {
let b = await bundle(
path.join(
__dirname,
'/integration/formats/commonjs-require-template-literal-interpolation/index.js',
),
);
let dist = await outputFS.readFile(
b.getBundles().find(b => b.type === 'js').filePath,
'utf8',
);

assert(
dist.includes(
'const add = require(`lodash/${$8cad8166811e0063$var$fn}`);',
),
);

let add = await run(b);
assert.equal(add(2, 3), 5);
});

it('should support importing sibling bundles in library mode', async function () {
let b = await bundle(
path.join(__dirname, '/integration/formats/commonjs-siblings/a.js'),
Expand Down Expand Up @@ -416,7 +457,7 @@ describe('output formats', function () {
assert.equal(typeof ns.test, 'function');
});

it('should support commonjs requires without interop', async function () {
it.only('should support commonjs requires without interop', async function () {
let b = await bundle(
path.join(__dirname, '/integration/formats/commonjs-require/index.js'),
);
Expand All @@ -425,6 +466,7 @@ describe('output formats', function () {
b.getBundles().find(b => b.type === 'js').filePath,
'utf8',
);

assert(dist.includes('= require("lodash")'));

let add = await run(b);
Expand Down

0 comments on commit ca49498

Please sign in to comment.