Commit fc329dc9 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig

polyfill-nomodule

parent 275b64d2
......@@ -57,17 +57,17 @@ const fn v(major: u32, minor: u32) -> Version {
}
}
// https://caniuse.com/es6-module
static NOMODULE_POLYFILL: PolyfillDefinition = PolyfillDefinition {
filename: "polyfill-nomodule.js",
edge: Some(v(16, 0)),
firefox: Some(v(60, 0)),
chrome: Some(v(61, 0)),
safari: Some(v(10, 1)),
opera: Some(v(48, 0)),
};
static MODULE_POLYFILLS: &[PolyfillDefinition] = &[
// A blanket nomodule polyfill for very old browsers
// https://caniuse.com/es6-module
PolyfillDefinition {
filename: "polyfill-nomodule.js",
edge: Some(v(16, 0)),
firefox: Some(v(60, 0)),
chrome: Some(v(61, 0)),
safari: Some(v(10, 1)),
opera: Some(v(48, 0)),
},
// String.prototype.trimStart/trimEnd
// https://caniuse.com/mdn-javascript_builtins_string_trimstart
PolyfillDefinition {
......@@ -151,16 +151,11 @@ static MODULE_POLYFILLS: &[PolyfillDefinition] = &[
];
pub fn get_required_polyfills(versions: &Versions) -> Vec<&'static str> {
if NOMODULE_POLYFILL.is_needed(versions) {
// NOMODULE_POLYFILL includes everything in MODULE_POLYFILLS
vec![NOMODULE_POLYFILL.filename]
} else {
MODULE_POLYFILLS
.iter()
.filter(|polyfill| polyfill.is_needed(versions))
.map(|d| d.filename)
.collect()
}
MODULE_POLYFILLS
.iter()
.filter(|polyfill| polyfill.is_needed(versions))
.map(|d| d.filename)
.collect()
}
#[turbo_tasks::value]
......
......@@ -18,7 +18,7 @@ describe('escheck-output', () => {
},
})
it('should downlevel JS according to manual browserslist with es2020', () => {
it('should downlevel JS', () => {
let esCheckOutput = execSync(
'node_modules/.bin/es-check es2020 ".next/static/**/*.js"',
{ cwd: next.testDir }
......@@ -29,8 +29,8 @@ describe('escheck-output', () => {
it('should not include outdated polyfills', async () => {
expect(await detectPolyfills(next)).toEqual([
'"description"in Symbol.prototype',
'"trimStart"in String.prototype',
'Symbol.prototype.description',
'String.prototype.trimStart',
'Array.prototype.at',
'Array.prototype.flat',
'Object.fromEntries',
......@@ -51,7 +51,7 @@ describe('escheck-output', () => {
},
})
it('should downlevel JS according to default browserslist', () => {
it('should downlevel JS', () => {
let esCheckOutput = execSync(
`node_modules/.bin/es-check checkBrowser ".next/static/**/*.js" --browserslistQuery="${browserslist.join(', ')}"`,
{ cwd: next.testDir }
......@@ -64,27 +64,65 @@ describe('escheck-output', () => {
expect(await detectPolyfills(next)).toEqual(['URL.canParse'])
})
})
describe('nomodule browsers', () => {
let browserslist = ['chrome 60']
const { next } = nextTestSetup({
files: __dirname,
dependencies: { 'es-check': '9.5.3' },
packageJson: {
browserslist,
},
})
// Fails due to https://github.com/yowainwright/es-check/issues/383
it.skip('should downlevel JS', () => {
let esCheckOutput = execSync(
`node_modules/.bin/es-check checkBrowser ".next/static/**/*.js" --browserslistQuery="${browserslist.join(', ')}"`,
{ cwd: next.testDir, stdio: 'inherit' }
).toString()
expect(esCheckOutput).toContain('info: ✓ ES-Check passed!')
})
it('should not include outdated polyfills', async () => {
expect(await detectPolyfills(next)).toEqual([
'Symbol.prototype.description',
'String.prototype.trimStart',
'Array.prototype.at',
'Array.prototype.flat',
'Object.fromEntries',
'Object.hasOwn',
'URL.canParse',
'Object.assign',
'Array.findIndex',
])
})
})
})
async function detectPolyfills(next) {
const { polyfillFiles } = await next.readJSON('.next/build-manifest.json')
const polyfills = [
'"description"in Symbol.prototype',
'"trimStart"in String.prototype',
'Array.prototype.at',
'Array.prototype.flat',
'Object.fromEntries',
'Object.hasOwn',
'URL.canParse',
]
const polyfills = {
'Symbol.prototype.description': '"description"in Symbol.prototype',
'String.prototype.trimStart': '"trimStart"in String.prototype',
'Array.prototype.at': 'Array.prototype.at=',
'Array.prototype.flat': 'Array.prototype.flat=',
'Object.fromEntries': 'Object.fromEntries=',
'Object.hasOwn': 'Object.hasOwn=',
'URL.canParse': 'URL.canParse=',
'Object.assign': 'Object.assign=',
'Array.findIndex': 'Maximum allowed index exceeded',
}
let foundPolyfills = new Set()
for (let file of polyfillFiles) {
let content = await next.readFile(`.next/${file}`)
for (let name of polyfills) {
if (content.includes(name)) {
for (let [name, matcher] of Object.entries(polyfills)) {
if (content.includes(matcher)) {
foundPolyfills.add(name)
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment