Skip to content

Commit 7c7ecaa

Browse files
authored
Do not alias Node modules for webpack 4 (#16452)
Fixes #16259 Caused by #16022 Follow up issue: #16450 This PR skips polyfilling Node modules in webpack 4 which are ignored by the `browsers` key.
1 parent 4507422 commit 7c7ecaa

File tree

5 files changed

+64
-5
lines changed

5 files changed

+64
-5
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
"release": "6.3.0",
113113
"request-promise-core": "1.1.2",
114114
"rimraf": "2.6.3",
115+
"seedrandom": "3.0.5",
115116
"selenium-standalone": "6.18.0",
116117
"selenium-webdriver": "4.0.0-alpha.7",
117118
"shell-quote": "1.7.2",

packages/next/build/webpack-config.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,16 +366,16 @@ export default async function getBaseWebpackConfig(
366366
'next/router': 'next/dist/client/router.js',
367367
'next/config': 'next/dist/next-server/lib/runtime-config.js',
368368
'next/dynamic': 'next/dist/next-server/lib/dynamic.js',
369-
...(isServer
370-
? {}
371-
: {
369+
next: NEXT_PROJECT_ROOT,
370+
...(isWebpack5 && !isServer
371+
? {
372372
stream: require.resolve('stream-browserify'),
373373
path: require.resolve('path-browserify'),
374374
crypto: require.resolve('crypto-browserify'),
375375
buffer: require.resolve('buffer'),
376376
vm: require.resolve('vm-browserify'),
377-
next: NEXT_PROJECT_ROOT,
378-
}),
377+
}
378+
: undefined),
379379
[PAGES_DIR_ALIAS]: pagesDir,
380380
[DOT_NEXT_ALIAS]: distDir,
381381
...getOptimizedAliases(isServer),
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { useEffect, useState } from 'react'
2+
import seedrandom from 'seedrandom'
3+
4+
const rng = seedrandom('hello')
5+
6+
export default function () {
7+
const [value, setValue] = useState(null)
8+
useEffect(() => {
9+
if (value) return
10+
setValue(rng())
11+
}, [value])
12+
return <div>{value == null ? 'loading' : value.toString()}</div>
13+
}

test/integration/build-output/test/index.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,46 @@ describe('Build Output', () => {
134134
})
135135
})
136136

137+
describe('Crypto Application', () => {
138+
let stdout
139+
const appDir = join(fixturesDir, 'with-crypto')
140+
141+
beforeAll(async () => {
142+
await remove(join(appDir, '.next'))
143+
})
144+
145+
it('should not include crypto', async () => {
146+
;({ stdout } = await nextBuild(appDir, [], {
147+
stdout: true,
148+
}))
149+
150+
console.log(stdout)
151+
152+
const parsePageSize = (page) =>
153+
stdout.match(
154+
new RegExp(` ${page} .*?((?:\\d|\\.){1,} (?:\\w{1,})) `)
155+
)[1]
156+
157+
const parsePageFirstLoad = (page) =>
158+
stdout.match(
159+
new RegExp(
160+
` ${page} .*?(?:(?:\\d|\\.){1,}) .*? ((?:\\d|\\.){1,} (?:\\w{1,}))`
161+
)
162+
)[1]
163+
164+
const indexSize = parsePageSize('/')
165+
const indexFirstLoad = parsePageFirstLoad('/')
166+
167+
expect(parseFloat(indexSize)).toBeLessThanOrEqual(3)
168+
expect(parseFloat(indexSize)).toBeGreaterThanOrEqual(2)
169+
expect(indexSize.endsWith('kB')).toBe(true)
170+
171+
expect(parseFloat(indexFirstLoad)).toBeLessThanOrEqual(65)
172+
expect(parseFloat(indexFirstLoad)).toBeGreaterThanOrEqual(60)
173+
expect(indexFirstLoad.endsWith('kB')).toBe(true)
174+
})
175+
})
176+
137177
describe('Custom App Output', () => {
138178
const appDir = join(fixturesDir, 'with-app')
139179

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15333,6 +15333,11 @@ scss-tokenizer@^0.2.3:
1533315333
js-base64 "^2.1.8"
1533415334
source-map "^0.4.2"
1533515335

15336+
15337+
version "3.0.5"
15338+
resolved "https://registry.yarnpkg.com/seedrandom/-/seedrandom-3.0.5.tgz#54edc85c95222525b0c7a6f6b3543d8e0b3aa0a7"
15339+
integrity sha512-8OwmbklUNzwezjGInmZ+2clQmExQPvomqjL7LFqOYqtmuxRgQYqOD3mHaU+MvZn5FLUeVxVfQjwLZW/n/JFuqg==
15340+
1533615341
1533715342
version "6.18.0"
1533815343
resolved "https://registry.yarnpkg.com/selenium-standalone/-/selenium-standalone-6.18.0.tgz#011e0672b1b86893f77244a86ddea1b6baadfb87"

0 commit comments

Comments
 (0)