Skip to content

Commit

Permalink
Add failing integration test for lazy compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
marcins committed Jun 14, 2023
1 parent 9fb3010 commit 2c22a39
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="./index.js" type="module"></script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
async function main() {
const m = await import('./lazy-1');
await import('./parallel-lazy-1');
return 'sup'; //m.default();
}

main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default async () => {
const { world } = await import('./lazy-2');
return `Hello ${world}`;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const world = 'world';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"private": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default async () => {
const m = await import('./parallel-lazy-2');
return m.default;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'parallel lazy 2';
84 changes: 84 additions & 0 deletions packages/core/integration-tests/test/lazy-compile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import assert from 'assert';
import path from 'path';
import {
bundler,
outputFS,
distDir,
getNextBuild,
assertBundles,
removeDistDirectory,
} from '@parcel/test-utils';

const findBundle = (bundleGraph, nameRegex) => {
return bundleGraph.getBundles().find(b => nameRegex.test(b.name));
};

const distDirIncludes = async matches => {
const files = await outputFS.readdir(distDir);
for (const match of matches) {
if (typeof match === 'string') {
if (!files.some(file => file === match)) {
throw new Error(
`No file matching ${match} was found in ${files.join(', ')}`,
);
}
} else {
if (!files.some(file => match.test(file))) {
throw new Error(
`No file matching ${match} was found in ${files.join(', ')}`,
);
}
}
}
return true;
};

describe('lazy compile', function () {
it('should lazy compile', async function () {
const b = await bundler(
path.join(__dirname, '/integration/lazy-compile/index.js'),
{
shouldBuildLazily: true,
mode: 'development',
shouldContentHash: false,
},
);

await removeDistDirectory();

const subscription = await b.watch();
let result = await getNextBuild(b);

result = await result.requestBundle(
findBundle(result.bundleGraph, /index.js/),
);
result = await result.requestBundle(
findBundle(result.bundleGraph, /^lazy-1/),
);
result = await result.requestBundle(
findBundle(result.bundleGraph, /^lazy-2/),
);

// Expect the bundle graph to contain the whole nest of lazy from `lazy-1`, but not
// `parallel-lazy-1` which wasn't requested.
assertBundles(result.bundleGraph, [
{
assets: ['index.js', 'bundle-url.js', 'cacheLoader.js', 'js-loader.js'],
},
{
assets: ['lazy-1.js', 'esmodule-helpers.js'],
},
{
assets: ['lazy-2.js'],
},
{
assets: ['parallel-lazy-1.js'],
},
]);

subscription.unsubscribe();

// Ensure the files match the bundle graph - lazy-2 should've been produced as it was requested
assert(await distDirIncludes(['index.js', /^lazy-1\./, /^lazy-2\./]));
});
});

0 comments on commit 2c22a39

Please sign in to comment.