Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: loosen webpack compilation with fallbackNodePolyfills: false #40612

Merged
merged 3 commits into from Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 24 additions & 1 deletion packages/next/build/webpack-config.ts
Expand Up @@ -1590,7 +1590,30 @@ export default async function getBaseWebpackConfig(
resolve: {
fallback:
config.experimental.fallbackNodePolyfills === false
? {}
? {
assert: false,
buffer: false,
constants: false,
crypto: false,
domain: false,
http: false,
https: false,
os: false,
path: false,
punycode: false,
process: false,
querystring: false,
stream: false,
string_decoder: false,
sys: false,
timers: false,
tty: false,
util: false,
vm: false,
zlib: false,
events: false,
setImmediate: false,
}
: {
assert: require.resolve(
'next/dist/compiled/assert'
Expand Down
4 changes: 2 additions & 2 deletions scripts/next-with-deps.sh
Expand Up @@ -29,11 +29,11 @@ done

if [ ! -z $HAS_CONFLICTING_DEP ] || [ ! -d "$PROJECT_DIR/node_modules" ];then
cd $PROJECT_DIR
yarn install
pnpm install
for dep in ${CONFLICTING_DEPS[@]};do
rm -rf node_modules/$dep
done
fi

cd $START_DIR
yarn next $@
pnpm next $@
41 changes: 33 additions & 8 deletions test/production/disable-fallback-polyfills/index.test.ts
Expand Up @@ -4,6 +4,11 @@ import { NextInstance } from 'test/lib/next-modes/base'
describe('Disable fallback polyfills', () => {
let next: NextInstance

function getFirstLoadSize(output: string) {
const firstLoadRe = /○ \/\s*(\d*) k?B\s*(?<firstLoad>.*) kB/
return Number(output.match(firstLoadRe).groups.firstLoad)
}

beforeAll(async () => {
next = await createNext({
files: {
Expand All @@ -19,21 +24,19 @@ describe('Disable fallback polyfills', () => {
}
`,
},
dependencies: {},
dependencies: {
axios: '0.27.2',
},
})
await next.stop()
})
afterAll(() => next.destroy())

it('Fallback polyfills added by default', async () => {
const firstLoadJSSize = Number(
next.cliOutput.match(/○ \/\s{38}(\d*) kB\s{10}(?<firstLoad>\d*) kB/)
.groups.firstLoad
)
expect(firstLoadJSSize).not.toBeLessThan(200)
expect(getFirstLoadSize(next.cliOutput)).not.toBeLessThan(200)
})

it('Build should fail, when fallback polyfilling is disabled', async () => {
it('Reduced bundle size when polyfills are disabled', async () => {
await next.patchFile(
'next.config.js',
`module.exports = {
Expand All @@ -42,7 +45,29 @@ describe('Disable fallback polyfills', () => {
}
}`
)
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"
}`
)

await expect(next.start()).rejects.toThrow(/next build failed/)
await expect(next.start()).not.toReject()
})
})