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 { ...@@ -57,17 +57,17 @@ const fn v(major: u32, minor: u32) -> Version {
} }
} }
// https://caniuse.com/es6-module static MODULE_POLYFILLS: &[PolyfillDefinition] = &[
static NOMODULE_POLYFILL: PolyfillDefinition = PolyfillDefinition { // A blanket nomodule polyfill for very old browsers
// https://caniuse.com/es6-module
PolyfillDefinition {
filename: "polyfill-nomodule.js", filename: "polyfill-nomodule.js",
edge: Some(v(16, 0)), edge: Some(v(16, 0)),
firefox: Some(v(60, 0)), firefox: Some(v(60, 0)),
chrome: Some(v(61, 0)), chrome: Some(v(61, 0)),
safari: Some(v(10, 1)), safari: Some(v(10, 1)),
opera: Some(v(48, 0)), opera: Some(v(48, 0)),
}; },
static MODULE_POLYFILLS: &[PolyfillDefinition] = &[
// String.prototype.trimStart/trimEnd // String.prototype.trimStart/trimEnd
// https://caniuse.com/mdn-javascript_builtins_string_trimstart // https://caniuse.com/mdn-javascript_builtins_string_trimstart
PolyfillDefinition { PolyfillDefinition {
...@@ -151,16 +151,11 @@ static MODULE_POLYFILLS: &[PolyfillDefinition] = &[ ...@@ -151,16 +151,11 @@ static MODULE_POLYFILLS: &[PolyfillDefinition] = &[
]; ];
pub fn get_required_polyfills(versions: &Versions) -> Vec<&'static str> { 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 MODULE_POLYFILLS
.iter() .iter()
.filter(|polyfill| polyfill.is_needed(versions)) .filter(|polyfill| polyfill.is_needed(versions))
.map(|d| d.filename) .map(|d| d.filename)
.collect() .collect()
}
} }
#[turbo_tasks::value] #[turbo_tasks::value]
......
...@@ -18,7 +18,7 @@ describe('escheck-output', () => { ...@@ -18,7 +18,7 @@ describe('escheck-output', () => {
}, },
}) })
it('should downlevel JS according to manual browserslist with es2020', () => { it('should downlevel JS', () => {
let esCheckOutput = execSync( let esCheckOutput = execSync(
'node_modules/.bin/es-check es2020 ".next/static/**/*.js"', 'node_modules/.bin/es-check es2020 ".next/static/**/*.js"',
{ cwd: next.testDir } { cwd: next.testDir }
...@@ -29,8 +29,8 @@ describe('escheck-output', () => { ...@@ -29,8 +29,8 @@ describe('escheck-output', () => {
it('should not include outdated polyfills', async () => { it('should not include outdated polyfills', async () => {
expect(await detectPolyfills(next)).toEqual([ expect(await detectPolyfills(next)).toEqual([
'"description"in Symbol.prototype', 'Symbol.prototype.description',
'"trimStart"in String.prototype', 'String.prototype.trimStart',
'Array.prototype.at', 'Array.prototype.at',
'Array.prototype.flat', 'Array.prototype.flat',
'Object.fromEntries', 'Object.fromEntries',
...@@ -51,7 +51,7 @@ describe('escheck-output', () => { ...@@ -51,7 +51,7 @@ describe('escheck-output', () => {
}, },
}) })
it('should downlevel JS according to default browserslist', () => { it('should downlevel JS', () => {
let esCheckOutput = execSync( let esCheckOutput = execSync(
`node_modules/.bin/es-check checkBrowser ".next/static/**/*.js" --browserslistQuery="${browserslist.join(', ')}"`, `node_modules/.bin/es-check checkBrowser ".next/static/**/*.js" --browserslistQuery="${browserslist.join(', ')}"`,
{ cwd: next.testDir } { cwd: next.testDir }
...@@ -64,27 +64,65 @@ describe('escheck-output', () => { ...@@ -64,27 +64,65 @@ describe('escheck-output', () => {
expect(await detectPolyfills(next)).toEqual(['URL.canParse']) expect(await detectPolyfills(next)).toEqual(['URL.canParse'])
}) })
}) })
})
async function detectPolyfills(next) { describe('nomodule browsers', () => {
const { polyfillFiles } = await next.readJSON('.next/build-manifest.json') 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()
const polyfills = [ expect(esCheckOutput).toContain('info: ✓ ES-Check passed!')
'"description"in Symbol.prototype', })
'"trimStart"in String.prototype',
it('should not include outdated polyfills', async () => {
expect(await detectPolyfills(next)).toEqual([
'Symbol.prototype.description',
'String.prototype.trimStart',
'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',
'Array.findIndex',
])
})
})
})
async function detectPolyfills(next) {
const { polyfillFiles } = await next.readJSON('.next/build-manifest.json')
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() let foundPolyfills = new Set()
for (let file of polyfillFiles) { for (let file of polyfillFiles) {
let content = await next.readFile(`.next/${file}`) let content = await next.readFile(`.next/${file}`)
for (let name of polyfills) { for (let [name, matcher] of Object.entries(polyfills)) {
if (content.includes(name)) { if (content.includes(matcher)) {
foundPolyfills.add(name) 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