-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(function): avoid page template if it's not used (#593)
- Loading branch information
Showing
4 changed files
with
99 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict' | ||
|
||
const walk = require('acorn-walk') | ||
const acorn = require('acorn') | ||
|
||
module.exports = code => { | ||
const ast = acorn.parse(code, { ecmaVersion: 2023, sourceType: 'module' }) | ||
|
||
let isUsingPage = false | ||
|
||
walk.simple(ast, { | ||
ObjectPattern (node) { | ||
node.properties.forEach(prop => { | ||
if (prop.type === 'Property' && prop.key.name === 'page') { | ||
isUsingPage = true | ||
} | ||
if (prop.type === 'RestElement' && prop.argument.name === 'page') { | ||
isUsingPage = true | ||
} | ||
}) | ||
}, | ||
MemberExpression (node) { | ||
if (node.property.name === 'page' || node.property.value === 'page') { | ||
isUsingPage = true | ||
} | ||
} | ||
}) | ||
|
||
if (!isUsingPage) return `async (url, _, opts) => (${code})(opts)` | ||
return ` | ||
async (url, browserWSEndpoint, opts) => { | ||
const puppeteer = require('@cloudflare/puppeteer') | ||
const browser = await puppeteer.connect({ browserWSEndpoint }) | ||
const page = (await browser.pages())[1] | ||
try { | ||
return await (${code})({ page, ...opts }) | ||
} finally { | ||
await browser.disconnect() | ||
} | ||
}` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict' | ||
|
||
const test = require('ava') | ||
|
||
const template = require('../src/template') | ||
|
||
test('use a simplified template if page is not used', t => { | ||
{ | ||
const code = () => { | ||
function isStrict () { | ||
return !this | ||
} | ||
return isStrict() | ||
} | ||
|
||
t.is(template(code.toString()).includes('puppeteer'), false) | ||
} | ||
{ | ||
const code = () => ({ page: 'title' }) | ||
t.is(template(code.toString()).includes('puppeteer'), false) | ||
} | ||
{ | ||
const code = () => 'page' | ||
t.is(template(code.toString()).includes('puppeteer'), false) | ||
} | ||
}) | ||
|
||
test('require puppeteer if page is used', t => { | ||
{ | ||
const code = ({ page }) => page.title() | ||
t.is(template(code.toString()).includes('puppeteer'), true) | ||
} | ||
{ | ||
const code = ({ page: p }) => p.title() | ||
t.is(template(code.toString()).includes('puppeteer'), true) | ||
} | ||
{ | ||
const code = obj => obj.page.title() | ||
t.is(template(code.toString()).includes('puppeteer'), true) | ||
} | ||
{ | ||
const code = obj => (() => obj.page.title())() | ||
t.is(template(code.toString()).includes('puppeteer'), true) | ||
} | ||
{ | ||
const code = ({ ...page }) => page.title | ||
t.is(template(code.toString()).includes('puppeteer'), true) | ||
} | ||
{ | ||
const code = obj => obj.page.title() | ||
t.is(template(code.toString()).includes('puppeteer'), true) | ||
} | ||
}) |