Skip to content

Commit 52cf7fe

Browse files
authored
Ensure setImmediate and punycode are polyfilled (#32768)
* Ensure setimmediate and punycode are polyfilled * update compiled
1 parent 82adaee commit 52cf7fe

File tree

12 files changed

+83
-46
lines changed

12 files changed

+83
-46
lines changed

packages/next/build/webpack-config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,8 @@ export default async function getBaseWebpackConfig(
615615
: 'react-dom/cjs/react-dom-server.browser.production.min',
616616
}
617617
: {}),
618+
619+
setimmediate: 'next/dist/compiled/setimmediate',
618620
},
619621
...(targetWeb
620622
? {
@@ -632,7 +634,7 @@ export default async function getBaseWebpackConfig(
632634
https: require.resolve('next/dist/compiled/https-browserify'),
633635
os: require.resolve('next/dist/compiled/os-browserify'),
634636
path: require.resolve('next/dist/compiled/path-browserify'),
635-
punycode: require.resolve('punycode'),
637+
punycode: require.resolve('next/dist/compiled/punycode'),
636638
process: require.resolve('next/dist/compiled/process'),
637639
// Handled in separate alias
638640
querystring: require.resolve('next/dist/compiled/querystring-es3'),
@@ -650,6 +652,7 @@ export default async function getBaseWebpackConfig(
650652
vm: require.resolve('next/dist/compiled/vm-browserify'),
651653
zlib: require.resolve('next/dist/compiled/browserify-zlib'),
652654
events: require.resolve('next/dist/compiled/events/'),
655+
setImmediate: require.resolve('next/dist/compiled/setimmediate'),
653656
},
654657
}
655658
: undefined),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"punycode","main":"punycode.js","author":{"name":"Mathias Bynens","url":"https://mathiasbynens.be/"},"license":"MIT"}

packages/next/compiled/punycode/punycode.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"setimmediate","main":"setImmediate.js","author":"YuzuJS","license":"MIT"}

packages/next/compiled/setimmediate/setImmediate.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/next/compiled/timers-browserify/main.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/next/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@
236236
"postcss-safe-parser": "6.0.0",
237237
"postcss-scss": "3.0.5",
238238
"postcss-value-parser": "4.1.0",
239+
"punycode": "2.1.1",
239240
"process": "0.11.10",
240241
"querystring-es3": "0.2.1",
241242
"raw-body": "2.4.1",
@@ -244,6 +245,7 @@
244245
"sass-loader": "10.2.0",
245246
"schema-utils2": "npm:[email protected]",
246247
"schema-utils3": "npm:[email protected]",
248+
"setimmediate": "1.0.5",
247249
"semver": "7.3.2",
248250
"send": "0.17.1",
249251
"source-map": "0.6.1",

packages/next/taskfile.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,32 @@ export async function ncc_util(task, opts) {
510510
.target('compiled/util')
511511
}
512512

513+
// eslint-disable-next-line camelcase
514+
export async function ncc_punycode(task, opts) {
515+
await task
516+
.source(opts.src || relative(__dirname, require.resolve('punycode/')))
517+
.ncc({
518+
packageName: 'punycode',
519+
externals,
520+
mainFields: ['browser', 'main'],
521+
target: 'es5',
522+
})
523+
.target('compiled/punycode')
524+
}
525+
526+
// eslint-disable-next-line camelcase
527+
export async function ncc_set_immediate(task, opts) {
528+
await task
529+
.source(opts.src || relative(__dirname, require.resolve('setimmediate/')))
530+
.ncc({
531+
packageName: 'setimmediate',
532+
externals,
533+
mainFields: ['browser', 'main'],
534+
target: 'es5',
535+
})
536+
.target('compiled/setimmediate')
537+
}
538+
513539
// eslint-disable-next-line camelcase
514540
export async function ncc_timers_browserify(task, opts) {
515541
await task
@@ -518,7 +544,10 @@ export async function ncc_timers_browserify(task, opts) {
518544
)
519545
.ncc({
520546
packageName: 'timers-browserify',
521-
externals,
547+
externals: {
548+
...externals,
549+
setimmediate: 'next/dist/compiled/setimmediate',
550+
},
522551
mainFields: ['browser', 'main'],
523552
target: 'es5',
524553
})
@@ -1444,6 +1473,8 @@ export async function ncc(task, opts) {
14441473
'ncc_querystring_es3',
14451474
'ncc_string_decoder',
14461475
'ncc_util',
1476+
'ncc_punycode',
1477+
'ncc_set_immediate',
14471478
'ncc_timers_browserify',
14481479
'ncc_tty_browserify',
14491480
'ncc_vm_browserify',

test/integration/polyfill-node-modules/test/index.test.js renamed to test/development/basic/node-browser-polyfills.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
/* eslint-env jest */
2-
31
import { join } from 'path'
42
import webdriver from 'next-webdriver'
5-
import { findPort, launchApp, killApp } from 'next-test-utils'
3+
import { createNext, FileRef } from 'e2e-utils'
4+
import { NextInstance } from 'test/lib/next-modes/base'
65

7-
const appDir = join(__dirname, '../')
8-
let appPort
9-
let app
6+
describe('theme-ui SWC option', () => {
7+
let next: NextInstance
108

11-
describe('Basic Features', () => {
129
beforeAll(async () => {
13-
appPort = await findPort()
14-
app = await launchApp(appDir, appPort)
10+
next = await createNext({
11+
files: {
12+
pages: new FileRef(join(__dirname, 'node-browser-polyfills/pages')),
13+
},
14+
})
1515
})
16-
afterAll(() => killApp(app))
16+
afterAll(() => next.destroy())
1717

18-
it('should polyfill Node.js modules', async () => {
19-
const browser = await webdriver(appPort, '/node-browser-polyfills')
18+
it('should have polyfilled correctly', async () => {
19+
const browser = await webdriver(next.url, '/')
2020

2121
await browser.waitForCondition('window.didRender')
2222

test/integration/polyfill-node-modules/pages/node-browser-polyfills.js renamed to test/development/basic/node-browser-polyfills/pages/index.js

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import timers from 'timers'
1919
import tty from 'tty'
2020
import util from 'util'
2121
import zlib from 'zlib'
22+
import 'setimmediate'
2223

2324
export default function NodeBrowserPolyfillPage() {
2425
const [state, setState] = useState({})
@@ -50,26 +51,28 @@ export default function NodeBrowserPolyfillPage() {
5051
assert.ok(!!util.inspect)
5152
assert.ok(!!zlib.Gzip)
5253

53-
setState({
54-
assert: true,
55-
buffer: Buffer.from('hello world').toString('utf8'),
56-
constants: constants.E2BIG,
57-
hash: crypto.createHash('sha256').update('hello world').digest('hex'),
58-
domain: true,
59-
os: os.EOL,
60-
path: path.join('/hello/world', 'test.txt'),
61-
process: process.title,
62-
querystring: querystring.stringify({ a: 'b' }),
63-
stream: closedStream,
64-
stringDecoder: true,
65-
sys: true,
66-
timers: true,
67-
tty: true,
68-
util: true,
69-
http: true,
70-
https: true,
71-
vm: vm.runInNewContext('a + 5', { a: 100 }),
72-
zlib: true,
54+
setImmediate(() => {
55+
setState({
56+
assert: true,
57+
buffer: Buffer.from('hello world').toString('utf8'),
58+
constants: constants.E2BIG,
59+
hash: crypto.createHash('sha256').update('hello world').digest('hex'),
60+
domain: true,
61+
os: os.EOL,
62+
path: path.join('/hello/world', 'test.txt'),
63+
process: process.title,
64+
querystring: querystring.stringify({ a: 'b' }),
65+
stream: closedStream,
66+
stringDecoder: true,
67+
sys: true,
68+
timers: true,
69+
tty: true,
70+
util: true,
71+
http: true,
72+
https: true,
73+
vm: vm.runInNewContext('a + 5', { a: 100 }),
74+
zlib: true,
75+
})
7376
})
7477
}, [])
7578

0 commit comments

Comments
 (0)