Skip to content

Commit

Permalink
Added test for preloadModules option (#1399)
Browse files Browse the repository at this point in the history
* added test for preloadmodules

* corrected function name
  • Loading branch information
thescripted authored Oct 26, 2020
1 parent 99f67f7 commit 6473682
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
36 changes: 35 additions & 1 deletion plugins/plugin-optimize/test/__snapshots__/plugin.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,47 @@ exports[`@snowpack/plugin-optimize minimal - no minification: fs.writeFileSync 1
exports[`@snowpack/plugin-optimize minimal - no options: console.log 1`] = ``;

exports[`@snowpack/plugin-optimize minimal - no options: fs.writeFileSync 1`] = `
FILE: stubs/minimal/esm_example.js
export default function e(){return console.log("example"),1}
--------------------------------------------------------------------------------
FILE: stubs/minimal/index.html
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Minimal test</title><link rel="stylesheet" href="style.css"></head><body><script type="module" src="index.js"></script></body></html>
--------------------------------------------------------------------------------
FILE: stubs/minimal/index.js
(()=>{function n(o){console.log(o)}n("test")})();
import"./esm_example.js";(()=>{function e(m){console.log(m)}e("test")})();
--------------------------------------------------------------------------------
FILE: stubs/minimal/style.css
body{border:1px solid red}
`;
exports[`@snowpack/plugin-optimize no HTML minification, with preloadModules: console.log 1`] = ``;
exports[`@snowpack/plugin-optimize no HTML minification, with preloadModules: fs.writeFileSync 1`] = `
FILE: stubs/minimal/esm_example.js
export default function e(){return console.log("example"),1}
--------------------------------------------------------------------------------
FILE: stubs/minimal/index.html
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Minimal test</title><link rel="stylesheet" href="style.css"> <!-- @snowpack/plugin-optimize] Add modulepreload to improve unbundled load performance (More info: https://developers.google.com/web/updates/2017/12/modulepreload) -->
<link rel="modulepreload" href="/esm_example.js" />
</head><body><script type="module" src="index.js"></script> <!-- [@snowpack/plugin-optimize] modulepreload fallback for browsers that do not support it yet -->
<script type="module" src="/esm_example.js"></script>
</body></html>
--------------------------------------------------------------------------------
FILE: stubs/minimal/index.js
import"./esm_example.js";(()=>{function e(m){console.log(m)}e("test")})();
--------------------------------------------------------------------------------
Expand Down
18 changes: 18 additions & 0 deletions plugins/plugin-optimize/test/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,22 @@ describe('@snowpack/plugin-optimize', () => {
expect(fs.writeFileSync).toMatchSnapshot('fs.writeFileSync');
expect(console.log).toMatchSnapshot('console.log');
});

it('no HTML minification, with preloadModules', async () => {
const pluginInstance = plugin(
{
buildOptions: {},
},
{
minifyHTML: false,
preloadModules: true
})

await pluginInstance.optimize({
buildDirectory: path.resolve(__dirname, 'stubs/minimal/'),
});

expect(fs.writeFileSync).toMatchSnapshot('fs.writeFileSync');
expect(console.log).toMatchSnapshot('console.log');
});
});
4 changes: 4 additions & 0 deletions plugins/plugin-optimize/test/stubs/minimal/esm_example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default function esm_example() {
console.log('example')
return 1
}
2 changes: 2 additions & 0 deletions plugins/plugin-optimize/test/stubs/minimal/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import esm_example from './esm_example.js'

(() => {
function n(o) {
console.log(o);
Expand Down

1 comment on commit 6473682

@vercel
Copy link

@vercel vercel bot commented on 6473682 Oct 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.