Skip to content

Commit 8bf082a

Browse files
fix: loosen webpack compilation with fallbackNodePolyfills: false (vercel#40612)
If in `resolve.fallback` we set previously polyfilled modules to `false` instead of an empty object, webpack will pass the compilation _and_ not include any polyfills. Fixes vercel#40522, fixes vercel#40364 ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm lint` - [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
1 parent 1ea65cf commit 8bf082a

File tree

3 files changed

+59
-11
lines changed

3 files changed

+59
-11
lines changed

packages/next/build/webpack-config.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1590,7 +1590,30 @@ export default async function getBaseWebpackConfig(
15901590
resolve: {
15911591
fallback:
15921592
config.experimental.fallbackNodePolyfills === false
1593-
? {}
1593+
? {
1594+
assert: false,
1595+
buffer: false,
1596+
constants: false,
1597+
crypto: false,
1598+
domain: false,
1599+
http: false,
1600+
https: false,
1601+
os: false,
1602+
path: false,
1603+
punycode: false,
1604+
process: false,
1605+
querystring: false,
1606+
stream: false,
1607+
string_decoder: false,
1608+
sys: false,
1609+
timers: false,
1610+
tty: false,
1611+
util: false,
1612+
vm: false,
1613+
zlib: false,
1614+
events: false,
1615+
setImmediate: false,
1616+
}
15941617
: {
15951618
assert: require.resolve(
15961619
'next/dist/compiled/assert'

scripts/next-with-deps.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ done
2929

3030
if [ ! -z $HAS_CONFLICTING_DEP ] || [ ! -d "$PROJECT_DIR/node_modules" ];then
3131
cd $PROJECT_DIR
32-
yarn install
32+
pnpm install
3333
for dep in ${CONFLICTING_DEPS[@]};do
3434
rm -rf node_modules/$dep
3535
done
3636
fi
3737

3838
cd $START_DIR
39-
yarn next $@
39+
pnpm next $@

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

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import { NextInstance } from 'test/lib/next-modes/base'
44
describe('Disable fallback polyfills', () => {
55
let next: NextInstance
66

7+
function getFirstLoadSize(output: string) {
8+
const firstLoadRe = / \/\s*(\d*) k?B\s*(?<firstLoad>.*) kB/
9+
return Number(output.match(firstLoadRe).groups.firstLoad)
10+
}
11+
712
beforeAll(async () => {
813
next = await createNext({
914
files: {
@@ -19,21 +24,19 @@ describe('Disable fallback polyfills', () => {
1924
}
2025
`,
2126
},
22-
dependencies: {},
27+
dependencies: {
28+
axios: '0.27.2',
29+
},
2330
})
2431
await next.stop()
2532
})
2633
afterAll(() => next.destroy())
2734

2835
it('Fallback polyfills added by default', async () => {
29-
const firstLoadJSSize = Number(
30-
next.cliOutput.match(/ \/\s{38}(\d*) kB\s{10}(?<firstLoad>\d*) kB/)
31-
.groups.firstLoad
32-
)
33-
expect(firstLoadJSSize).not.toBeLessThan(200)
36+
expect(getFirstLoadSize(next.cliOutput)).not.toBeLessThan(200)
3437
})
3538

36-
it('Build should fail, when fallback polyfilling is disabled', async () => {
39+
it('Reduced bundle size when polyfills are disabled', async () => {
3740
await next.patchFile(
3841
'next.config.js',
3942
`module.exports = {
@@ -42,7 +45,29 @@ describe('Disable fallback polyfills', () => {
4245
}
4346
}`
4447
)
48+
await next.start()
49+
await next.stop()
50+
51+
expect(getFirstLoadSize(next.cliOutput)).toBeLessThan(200)
52+
})
53+
54+
it('Pass build without error if non-polyfilled module is unreachable', async () => {
55+
// `axios` uses `Buffer`, but it should be unreachable in the browser.
56+
// https://github.com/axios/axios/blob/649d739288c8e2c55829ac60e2345a0f3439c730/lib/helpers/toFormData.js#L138
57+
await next.patchFile(
58+
'pages/index.js',
59+
`import axios from 'axios'
60+
import { useEffect } from 'react'
61+
62+
export default function Home() {
63+
useEffect(() => {
64+
axios.get('/api')
65+
}, [])
66+
67+
return "hello world"
68+
}`
69+
)
4570

46-
await expect(next.start()).rejects.toThrow(/next build failed/)
71+
await expect(next.start()).not.toReject()
4772
})
4873
})

0 commit comments

Comments
 (0)