Skip to content

Commit

Permalink
feat: include next polyfils (including global fetch support) (#40)
Browse files Browse the repository at this point in the history
Co-authored-by: Vadorequest <[email protected]>
  • Loading branch information
ricokahler and Vadorequest committed Jun 1, 2021
1 parent 3b02c49 commit be216d8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/__example-files__/uses-fetch.preval.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import preval from 'next-plugin-preval';

async function getDataViaFetch() {
const response = await fetch('https://example.com');
const html = await response.text();

return { html };
}

export default preval(getDataViaFetch());
10 changes: 10 additions & 0 deletions src/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ describe('_prevalLoader', () => {

expect(caught).toBe(true);
});

it("polyfills fetch (and others) via require('next')", async () => {
const result = await _prevalLoader(
'',
require.resolve('./__example-files__/uses-fetch.preval.ts'),
{}
);

expect(result.includes('Example Domain')).toBe(true);
});
});

describe('loader', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export async function _prevalLoader(
const data = await (async () => {
try {
const mod = requireFromString(
`module.exports = require(${JSON.stringify(resource)})`,
`require('next');\nmodule.exports = require(${JSON.stringify(resource)})`,
`${resource}.preval.js`
);

Expand Down

0 comments on commit be216d8

Please sign in to comment.