rsnext/test/production/disable-fallback-polyfills/index.test.ts

75 lines
1.9 KiB
TypeScript
Raw Normal View History

feat: add `experimental.fallbackNodePolyfills` flag (#39248) For historical reasons, Next.js has been falling back to polyfill certain Node.js APIs in the browser. `webpack` itself stopped doing it, and so should Next.js. This might unexpectedly break some packages in the ecosystem though, so it is being introduced as an experimental flag. These imports will now throw a `Module not found` error and the package maintainer should make sure that the library isn't relying on these Node.js APIs when the package is meant for browser usage. Let's take a look at a common example, the `crypto` API, which can be imported as `import crypto from "crypto"` but [should already be available in browsers](https://caniuse.com/cryptography). Until now, Next.js has fallen back to use a polyfilled version for the import, which resulted in a bundle-size increase. ```js import crypto from 'crypto' import { useEffect } from 'react' export default function Page() { useEffect(() => { console.log(crypto) }, []) } ``` it imports `crypto`, which currently resolves to [`crypto-browserify`](https://www.npmjs.com/package/crypto-browserify). So the bundle will include `crypto-browserify` as well: ```sh Page Size First Load JS ┌ ○ / 131 kB 213 kB # <-- └ ○ /404 194 B 82.2 kB + First Load JS shared by all 82 kB ├ chunks/framework-bcc2dc0ea27ab0c6.js 45.1 kB ├ chunks/main-dc2421aef72299b4.js 35.4 kB ├ chunks/pages/_app-a85935458980c5c2.js 708 B └ chunks/webpack-9b312e20a4e32339.js 836 B ``` Here, we can just remove the import, as we are [safely accessing](https://nextjs.org/docs/migrating/from-create-react-app#safely-accessing-web-apis) the [Crypto Web API](https://caniuse.com/cryptography): ```diff - import crypto from 'crypto' import { useEffect } from 'react' export default function Page() { useEffect(() => { console.log(crypto) }, []) } ``` Which will reduce the bundle size: ```sh Page Size First Load JS ┌ ○ / 269 B 82.2 kB # <-- └ ○ /404 194 B 82.1 kB + First Load JS shared by all 81.9 kB ├ chunks/framework-bcc2dc0ea27ab0c6.js 45.1 kB ├ chunks/main-dc2421aef72299b4.js 35.4 kB ├ chunks/pages/_app-a85935458980c5c2.js 708 B └ chunks/webpack-fd82975a6094609f.js 727 B ``` This is harder to detect if the `crypto` import is in a third-party package though. By setting `experimental: { fallbackNodePolyfills: false }`, Next.js will now fail at build-time and should show where the unnecessary import comes from, so the developer can reach out to the package maintainer to fix this issue. Note: There might be differences between the living standard and some of these older polyfills, so you have to make sure your code works well without the polyfilled version. Related feedback: https://twitter.com/lfredolo/status/1539608666026000384
2022-08-27 01:11:57 +02:00
import { createNext } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
describe('Disable fallback polyfills', () => {
let next: NextInstance
function getFirstLoadSize(output: string) {
const firstLoadRe =
/○ \/.*? (?<size>\d.*?) [^\d]{2} (?<firstLoad>\d.*?) [^\d]{2}/
return Number(output.match(firstLoadRe).groups.firstLoad)
}
feat: add `experimental.fallbackNodePolyfills` flag (#39248) For historical reasons, Next.js has been falling back to polyfill certain Node.js APIs in the browser. `webpack` itself stopped doing it, and so should Next.js. This might unexpectedly break some packages in the ecosystem though, so it is being introduced as an experimental flag. These imports will now throw a `Module not found` error and the package maintainer should make sure that the library isn't relying on these Node.js APIs when the package is meant for browser usage. Let's take a look at a common example, the `crypto` API, which can be imported as `import crypto from "crypto"` but [should already be available in browsers](https://caniuse.com/cryptography). Until now, Next.js has fallen back to use a polyfilled version for the import, which resulted in a bundle-size increase. ```js import crypto from 'crypto' import { useEffect } from 'react' export default function Page() { useEffect(() => { console.log(crypto) }, []) } ``` it imports `crypto`, which currently resolves to [`crypto-browserify`](https://www.npmjs.com/package/crypto-browserify). So the bundle will include `crypto-browserify` as well: ```sh Page Size First Load JS ┌ ○ / 131 kB 213 kB # <-- └ ○ /404 194 B 82.2 kB + First Load JS shared by all 82 kB ├ chunks/framework-bcc2dc0ea27ab0c6.js 45.1 kB ├ chunks/main-dc2421aef72299b4.js 35.4 kB ├ chunks/pages/_app-a85935458980c5c2.js 708 B └ chunks/webpack-9b312e20a4e32339.js 836 B ``` Here, we can just remove the import, as we are [safely accessing](https://nextjs.org/docs/migrating/from-create-react-app#safely-accessing-web-apis) the [Crypto Web API](https://caniuse.com/cryptography): ```diff - import crypto from 'crypto' import { useEffect } from 'react' export default function Page() { useEffect(() => { console.log(crypto) }, []) } ``` Which will reduce the bundle size: ```sh Page Size First Load JS ┌ ○ / 269 B 82.2 kB # <-- └ ○ /404 194 B 82.1 kB + First Load JS shared by all 81.9 kB ├ chunks/framework-bcc2dc0ea27ab0c6.js 45.1 kB ├ chunks/main-dc2421aef72299b4.js 35.4 kB ├ chunks/pages/_app-a85935458980c5c2.js 708 B └ chunks/webpack-fd82975a6094609f.js 727 B ``` This is harder to detect if the `crypto` import is in a third-party package though. By setting `experimental: { fallbackNodePolyfills: false }`, Next.js will now fail at build-time and should show where the unnecessary import comes from, so the developer can reach out to the package maintainer to fix this issue. Note: There might be differences between the living standard and some of these older polyfills, so you have to make sure your code works well without the polyfilled version. Related feedback: https://twitter.com/lfredolo/status/1539608666026000384
2022-08-27 01:11:57 +02:00
beforeAll(async () => {
next = await createNext({
files: {
'pages/index.js': `
import { useEffect } from 'react'
import crypto from 'crypto'
export default function Page() {
useEffect(() => {
crypto;
}, [])
return <p>hello world</p>
}
`,
},
dependencies: {
axios: '0.27.2',
},
feat: add `experimental.fallbackNodePolyfills` flag (#39248) For historical reasons, Next.js has been falling back to polyfill certain Node.js APIs in the browser. `webpack` itself stopped doing it, and so should Next.js. This might unexpectedly break some packages in the ecosystem though, so it is being introduced as an experimental flag. These imports will now throw a `Module not found` error and the package maintainer should make sure that the library isn't relying on these Node.js APIs when the package is meant for browser usage. Let's take a look at a common example, the `crypto` API, which can be imported as `import crypto from "crypto"` but [should already be available in browsers](https://caniuse.com/cryptography). Until now, Next.js has fallen back to use a polyfilled version for the import, which resulted in a bundle-size increase. ```js import crypto from 'crypto' import { useEffect } from 'react' export default function Page() { useEffect(() => { console.log(crypto) }, []) } ``` it imports `crypto`, which currently resolves to [`crypto-browserify`](https://www.npmjs.com/package/crypto-browserify). So the bundle will include `crypto-browserify` as well: ```sh Page Size First Load JS ┌ ○ / 131 kB 213 kB # <-- └ ○ /404 194 B 82.2 kB + First Load JS shared by all 82 kB ├ chunks/framework-bcc2dc0ea27ab0c6.js 45.1 kB ├ chunks/main-dc2421aef72299b4.js 35.4 kB ├ chunks/pages/_app-a85935458980c5c2.js 708 B └ chunks/webpack-9b312e20a4e32339.js 836 B ``` Here, we can just remove the import, as we are [safely accessing](https://nextjs.org/docs/migrating/from-create-react-app#safely-accessing-web-apis) the [Crypto Web API](https://caniuse.com/cryptography): ```diff - import crypto from 'crypto' import { useEffect } from 'react' export default function Page() { useEffect(() => { console.log(crypto) }, []) } ``` Which will reduce the bundle size: ```sh Page Size First Load JS ┌ ○ / 269 B 82.2 kB # <-- └ ○ /404 194 B 82.1 kB + First Load JS shared by all 81.9 kB ├ chunks/framework-bcc2dc0ea27ab0c6.js 45.1 kB ├ chunks/main-dc2421aef72299b4.js 35.4 kB ├ chunks/pages/_app-a85935458980c5c2.js 708 B └ chunks/webpack-fd82975a6094609f.js 727 B ``` This is harder to detect if the `crypto` import is in a third-party package though. By setting `experimental: { fallbackNodePolyfills: false }`, Next.js will now fail at build-time and should show where the unnecessary import comes from, so the developer can reach out to the package maintainer to fix this issue. Note: There might be differences between the living standard and some of these older polyfills, so you have to make sure your code works well without the polyfilled version. Related feedback: https://twitter.com/lfredolo/status/1539608666026000384
2022-08-27 01:11:57 +02:00
})
await next.stop()
})
afterAll(() => next.destroy())
it('Fallback polyfills added by default', async () => {
expect(getFirstLoadSize(next.cliOutput)).not.toBeLessThan(200)
feat: add `experimental.fallbackNodePolyfills` flag (#39248) For historical reasons, Next.js has been falling back to polyfill certain Node.js APIs in the browser. `webpack` itself stopped doing it, and so should Next.js. This might unexpectedly break some packages in the ecosystem though, so it is being introduced as an experimental flag. These imports will now throw a `Module not found` error and the package maintainer should make sure that the library isn't relying on these Node.js APIs when the package is meant for browser usage. Let's take a look at a common example, the `crypto` API, which can be imported as `import crypto from "crypto"` but [should already be available in browsers](https://caniuse.com/cryptography). Until now, Next.js has fallen back to use a polyfilled version for the import, which resulted in a bundle-size increase. ```js import crypto from 'crypto' import { useEffect } from 'react' export default function Page() { useEffect(() => { console.log(crypto) }, []) } ``` it imports `crypto`, which currently resolves to [`crypto-browserify`](https://www.npmjs.com/package/crypto-browserify). So the bundle will include `crypto-browserify` as well: ```sh Page Size First Load JS ┌ ○ / 131 kB 213 kB # <-- └ ○ /404 194 B 82.2 kB + First Load JS shared by all 82 kB ├ chunks/framework-bcc2dc0ea27ab0c6.js 45.1 kB ├ chunks/main-dc2421aef72299b4.js 35.4 kB ├ chunks/pages/_app-a85935458980c5c2.js 708 B └ chunks/webpack-9b312e20a4e32339.js 836 B ``` Here, we can just remove the import, as we are [safely accessing](https://nextjs.org/docs/migrating/from-create-react-app#safely-accessing-web-apis) the [Crypto Web API](https://caniuse.com/cryptography): ```diff - import crypto from 'crypto' import { useEffect } from 'react' export default function Page() { useEffect(() => { console.log(crypto) }, []) } ``` Which will reduce the bundle size: ```sh Page Size First Load JS ┌ ○ / 269 B 82.2 kB # <-- └ ○ /404 194 B 82.1 kB + First Load JS shared by all 81.9 kB ├ chunks/framework-bcc2dc0ea27ab0c6.js 45.1 kB ├ chunks/main-dc2421aef72299b4.js 35.4 kB ├ chunks/pages/_app-a85935458980c5c2.js 708 B └ chunks/webpack-fd82975a6094609f.js 727 B ``` This is harder to detect if the `crypto` import is in a third-party package though. By setting `experimental: { fallbackNodePolyfills: false }`, Next.js will now fail at build-time and should show where the unnecessary import comes from, so the developer can reach out to the package maintainer to fix this issue. Note: There might be differences between the living standard and some of these older polyfills, so you have to make sure your code works well without the polyfilled version. Related feedback: https://twitter.com/lfredolo/status/1539608666026000384
2022-08-27 01:11:57 +02:00
})
it('Reduced bundle size when polyfills are disabled', async () => {
feat: add `experimental.fallbackNodePolyfills` flag (#39248) For historical reasons, Next.js has been falling back to polyfill certain Node.js APIs in the browser. `webpack` itself stopped doing it, and so should Next.js. This might unexpectedly break some packages in the ecosystem though, so it is being introduced as an experimental flag. These imports will now throw a `Module not found` error and the package maintainer should make sure that the library isn't relying on these Node.js APIs when the package is meant for browser usage. Let's take a look at a common example, the `crypto` API, which can be imported as `import crypto from "crypto"` but [should already be available in browsers](https://caniuse.com/cryptography). Until now, Next.js has fallen back to use a polyfilled version for the import, which resulted in a bundle-size increase. ```js import crypto from 'crypto' import { useEffect } from 'react' export default function Page() { useEffect(() => { console.log(crypto) }, []) } ``` it imports `crypto`, which currently resolves to [`crypto-browserify`](https://www.npmjs.com/package/crypto-browserify). So the bundle will include `crypto-browserify` as well: ```sh Page Size First Load JS ┌ ○ / 131 kB 213 kB # <-- └ ○ /404 194 B 82.2 kB + First Load JS shared by all 82 kB ├ chunks/framework-bcc2dc0ea27ab0c6.js 45.1 kB ├ chunks/main-dc2421aef72299b4.js 35.4 kB ├ chunks/pages/_app-a85935458980c5c2.js 708 B └ chunks/webpack-9b312e20a4e32339.js 836 B ``` Here, we can just remove the import, as we are [safely accessing](https://nextjs.org/docs/migrating/from-create-react-app#safely-accessing-web-apis) the [Crypto Web API](https://caniuse.com/cryptography): ```diff - import crypto from 'crypto' import { useEffect } from 'react' export default function Page() { useEffect(() => { console.log(crypto) }, []) } ``` Which will reduce the bundle size: ```sh Page Size First Load JS ┌ ○ / 269 B 82.2 kB # <-- └ ○ /404 194 B 82.1 kB + First Load JS shared by all 81.9 kB ├ chunks/framework-bcc2dc0ea27ab0c6.js 45.1 kB ├ chunks/main-dc2421aef72299b4.js 35.4 kB ├ chunks/pages/_app-a85935458980c5c2.js 708 B └ chunks/webpack-fd82975a6094609f.js 727 B ``` This is harder to detect if the `crypto` import is in a third-party package though. By setting `experimental: { fallbackNodePolyfills: false }`, Next.js will now fail at build-time and should show where the unnecessary import comes from, so the developer can reach out to the package maintainer to fix this issue. Note: There might be differences between the living standard and some of these older polyfills, so you have to make sure your code works well without the polyfilled version. Related feedback: https://twitter.com/lfredolo/status/1539608666026000384
2022-08-27 01:11:57 +02:00
await next.patchFile(
'next.config.js',
`module.exports = {
experimental: {
fallbackNodePolyfills: false
}
}`
)
await next.start()
await next.stop()
expect(getFirstLoadSize(next.cliOutput)).toBeLessThan(200)
})
it('Pass build without error if non-polyfilled module is unreachable', async () => {
// `axios` uses `Buffer`, but it should be unreachable in the browser.
// https://github.com/axios/axios/blob/649d739288c8e2c55829ac60e2345a0f3439c730/lib/helpers/toFormData.js#L138
await next.patchFile(
'pages/index.js',
`import axios from 'axios'
import { useEffect } from 'react'
export default function Home() {
useEffect(() => {
axios.get('/api')
}, [])
return "hello world"
}`
)
feat: add `experimental.fallbackNodePolyfills` flag (#39248) For historical reasons, Next.js has been falling back to polyfill certain Node.js APIs in the browser. `webpack` itself stopped doing it, and so should Next.js. This might unexpectedly break some packages in the ecosystem though, so it is being introduced as an experimental flag. These imports will now throw a `Module not found` error and the package maintainer should make sure that the library isn't relying on these Node.js APIs when the package is meant for browser usage. Let's take a look at a common example, the `crypto` API, which can be imported as `import crypto from "crypto"` but [should already be available in browsers](https://caniuse.com/cryptography). Until now, Next.js has fallen back to use a polyfilled version for the import, which resulted in a bundle-size increase. ```js import crypto from 'crypto' import { useEffect } from 'react' export default function Page() { useEffect(() => { console.log(crypto) }, []) } ``` it imports `crypto`, which currently resolves to [`crypto-browserify`](https://www.npmjs.com/package/crypto-browserify). So the bundle will include `crypto-browserify` as well: ```sh Page Size First Load JS ┌ ○ / 131 kB 213 kB # <-- └ ○ /404 194 B 82.2 kB + First Load JS shared by all 82 kB ├ chunks/framework-bcc2dc0ea27ab0c6.js 45.1 kB ├ chunks/main-dc2421aef72299b4.js 35.4 kB ├ chunks/pages/_app-a85935458980c5c2.js 708 B └ chunks/webpack-9b312e20a4e32339.js 836 B ``` Here, we can just remove the import, as we are [safely accessing](https://nextjs.org/docs/migrating/from-create-react-app#safely-accessing-web-apis) the [Crypto Web API](https://caniuse.com/cryptography): ```diff - import crypto from 'crypto' import { useEffect } from 'react' export default function Page() { useEffect(() => { console.log(crypto) }, []) } ``` Which will reduce the bundle size: ```sh Page Size First Load JS ┌ ○ / 269 B 82.2 kB # <-- └ ○ /404 194 B 82.1 kB + First Load JS shared by all 81.9 kB ├ chunks/framework-bcc2dc0ea27ab0c6.js 45.1 kB ├ chunks/main-dc2421aef72299b4.js 35.4 kB ├ chunks/pages/_app-a85935458980c5c2.js 708 B └ chunks/webpack-fd82975a6094609f.js 727 B ``` This is harder to detect if the `crypto` import is in a third-party package though. By setting `experimental: { fallbackNodePolyfills: false }`, Next.js will now fail at build-time and should show where the unnecessary import comes from, so the developer can reach out to the package maintainer to fix this issue. Note: There might be differences between the living standard and some of these older polyfills, so you have to make sure your code works well without the polyfilled version. Related feedback: https://twitter.com/lfredolo/status/1539608666026000384
2022-08-27 01:11:57 +02:00
await expect(next.start()).not.toReject()
feat: add `experimental.fallbackNodePolyfills` flag (#39248) For historical reasons, Next.js has been falling back to polyfill certain Node.js APIs in the browser. `webpack` itself stopped doing it, and so should Next.js. This might unexpectedly break some packages in the ecosystem though, so it is being introduced as an experimental flag. These imports will now throw a `Module not found` error and the package maintainer should make sure that the library isn't relying on these Node.js APIs when the package is meant for browser usage. Let's take a look at a common example, the `crypto` API, which can be imported as `import crypto from "crypto"` but [should already be available in browsers](https://caniuse.com/cryptography). Until now, Next.js has fallen back to use a polyfilled version for the import, which resulted in a bundle-size increase. ```js import crypto from 'crypto' import { useEffect } from 'react' export default function Page() { useEffect(() => { console.log(crypto) }, []) } ``` it imports `crypto`, which currently resolves to [`crypto-browserify`](https://www.npmjs.com/package/crypto-browserify). So the bundle will include `crypto-browserify` as well: ```sh Page Size First Load JS ┌ ○ / 131 kB 213 kB # <-- └ ○ /404 194 B 82.2 kB + First Load JS shared by all 82 kB ├ chunks/framework-bcc2dc0ea27ab0c6.js 45.1 kB ├ chunks/main-dc2421aef72299b4.js 35.4 kB ├ chunks/pages/_app-a85935458980c5c2.js 708 B └ chunks/webpack-9b312e20a4e32339.js 836 B ``` Here, we can just remove the import, as we are [safely accessing](https://nextjs.org/docs/migrating/from-create-react-app#safely-accessing-web-apis) the [Crypto Web API](https://caniuse.com/cryptography): ```diff - import crypto from 'crypto' import { useEffect } from 'react' export default function Page() { useEffect(() => { console.log(crypto) }, []) } ``` Which will reduce the bundle size: ```sh Page Size First Load JS ┌ ○ / 269 B 82.2 kB # <-- └ ○ /404 194 B 82.1 kB + First Load JS shared by all 81.9 kB ├ chunks/framework-bcc2dc0ea27ab0c6.js 45.1 kB ├ chunks/main-dc2421aef72299b4.js 35.4 kB ├ chunks/pages/_app-a85935458980c5c2.js 708 B └ chunks/webpack-fd82975a6094609f.js 727 B ``` This is harder to detect if the `crypto` import is in a third-party package though. By setting `experimental: { fallbackNodePolyfills: false }`, Next.js will now fail at build-time and should show where the unnecessary import comes from, so the developer can reach out to the package maintainer to fix this issue. Note: There might be differences between the living standard and some of these older polyfills, so you have to make sure your code works well without the polyfilled version. Related feedback: https://twitter.com/lfredolo/status/1539608666026000384
2022-08-27 01:11:57 +02:00
})
})