diff --git a/packages/next/build/webpack-config.ts b/packages/next/build/webpack-config.ts index 37c918d622e4d..bbac49486d94a 100644 --- a/packages/next/build/webpack-config.ts +++ b/packages/next/build/webpack-config.ts @@ -615,6 +615,8 @@ export default async function getBaseWebpackConfig( : 'react-dom/cjs/react-dom-server.browser.production.min', } : {}), + + setimmediate: 'next/dist/compiled/setimmediate', }, ...(targetWeb ? { @@ -632,7 +634,7 @@ export default async function getBaseWebpackConfig( https: require.resolve('next/dist/compiled/https-browserify'), os: require.resolve('next/dist/compiled/os-browserify'), path: require.resolve('next/dist/compiled/path-browserify'), - punycode: require.resolve('punycode'), + punycode: require.resolve('next/dist/compiled/punycode'), process: require.resolve('next/dist/compiled/process'), // Handled in separate alias querystring: require.resolve('next/dist/compiled/querystring-es3'), @@ -650,6 +652,7 @@ export default async function getBaseWebpackConfig( vm: require.resolve('next/dist/compiled/vm-browserify'), zlib: require.resolve('next/dist/compiled/browserify-zlib'), events: require.resolve('next/dist/compiled/events/'), + setImmediate: require.resolve('next/dist/compiled/setimmediate'), }, } : undefined), diff --git a/packages/next/compiled/punycode/package.json b/packages/next/compiled/punycode/package.json new file mode 100644 index 0000000000000..cf52cbab85fed --- /dev/null +++ b/packages/next/compiled/punycode/package.json @@ -0,0 +1 @@ +{"name":"punycode","main":"punycode.js","author":{"name":"Mathias Bynens","url":"https://mathiasbynens.be/"},"license":"MIT"} diff --git a/packages/next/compiled/punycode/punycode.js b/packages/next/compiled/punycode/punycode.js new file mode 100644 index 0000000000000..ae4e2f0f0b46a --- /dev/null +++ b/packages/next/compiled/punycode/punycode.js @@ -0,0 +1 @@ +(function(){"use strict";var t={701:function(t){const e=2147483647;const o=36;const n=1;const r=26;const c=38;const s=700;const i=72;const f=128;const u="-";const l=/^xn--/;const a=/[^\0-\x7E]/;const p=/[\x2E\u3002\uFF0E\uFF61]/g;const d={overflow:"Overflow: input needs wider integers to process","not-basic":"Illegal input >= 0x80 (not a basic code point)","invalid-input":"Invalid input"};const _=o-n;const h=Math.floor;const w=String.fromCharCode;function error(t){throw new RangeError(d[t])}function map(t,e){const o=[];let n=t.length;while(n--){o[n]=e(t[n])}return o}function mapDomain(t,e){const o=t.split("@");let n="";if(o.length>1){n=o[0]+"@";t=o[1]}t=t.replace(p,".");const r=t.split(".");const c=map(r,e).join(".");return n+c}function ucs2decode(t){const e=[];let o=0;const n=t.length;while(o=55296&&r<=56319&&oString.fromCodePoint(...t);const basicToDigit=function(t){if(t-48<10){return t-22}if(t-65<26){return t-65}if(t-97<26){return t-97}return o};const digitToBasic=function(t,e){return t+22+75*(t<26)-((e!=0)<<5)};const adapt=function(t,e,n){let i=0;t=n?h(t/s):t>>1;t+=h(t/e);for(;t>_*r>>1;i+=o){t=h(t/_)}return h(i+(_+1)*t/(t+c))};const decode=function(t){const c=[];const s=t.length;let l=0;let a=f;let p=i;let d=t.lastIndexOf(u);if(d<0){d=0}for(let e=0;e=128){error("not-basic")}c.push(t.charCodeAt(e))}for(let i=d>0?d+1:0;i=s){error("invalid-input")}const u=basicToDigit(t.charCodeAt(i++));if(u>=o||u>h((e-l)/c)){error("overflow")}l+=u*c;const a=f<=p?n:f>=p+r?r:f-p;if(uh(e/d)){error("overflow")}c*=d}const u=c.length+1;p=adapt(l-f,u,f==0);if(h(l/u)>e-a){error("overflow")}a+=h(l/u);l%=u;c.splice(l++,0,a)}return String.fromCodePoint(...c)};const encode=function(t){const c=[];t=ucs2decode(t);let s=t.length;let l=f;let a=0;let p=i;for(const e of t){if(e<128){c.push(w(e))}}let d=c.length;let _=d;if(d){c.push(u)}while(_=l&&eh((e-a)/i)){error("overflow")}a+=(s-l)*i;l=s;for(const s of t){if(se){error("overflow")}if(s==l){let t=a;for(let e=o;;e+=o){const s=e<=p?n:e>=p+r?r:e-p;if(t=0){e._idleTimeoutId=setTimeout((function onTimeout(){if(e._onTimeout)e._onTimeout()}),t)}};n(619);t.setImmediate=typeof self!=="undefined"&&self.setImmediate||typeof global!=="undefined"&&global.setImmediate||this&&this.setImmediate;t.clearImmediate=typeof self!=="undefined"&&self.clearImmediate||typeof global!=="undefined"&&global.clearImmediate||this&&this.clearImmediate}};var t={};function __nccwpck_require__(n){var a=t[n];if(a!==undefined){return a.exports}var i=t[n]={exports:{}};var o=true;try{e[n].call(i.exports,i,i.exports,__nccwpck_require__);o=false}finally{if(o)delete t[n]}return i.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var n=__nccwpck_require__(832);module.exports=n})(); \ No newline at end of file +(function(){var e={832:function(e,t,i){var o=typeof global!=="undefined"&&global||typeof self!=="undefined"&&self||window;var n=Function.prototype.apply;t.setTimeout=function(){return new Timeout(n.call(setTimeout,o,arguments),clearTimeout)};t.setInterval=function(){return new Timeout(n.call(setInterval,o,arguments),clearInterval)};t.clearTimeout=t.clearInterval=function(e){if(e){e.close()}};function Timeout(e,t){this._id=e;this._clearFn=t}Timeout.prototype.unref=Timeout.prototype.ref=function(){};Timeout.prototype.close=function(){this._clearFn.call(o,this._id)};t.enroll=function(e,t){clearTimeout(e._idleTimeoutId);e._idleTimeout=t};t.unenroll=function(e){clearTimeout(e._idleTimeoutId);e._idleTimeout=-1};t._unrefActive=t.active=function(e){clearTimeout(e._idleTimeoutId);var t=e._idleTimeout;if(t>=0){e._idleTimeoutId=setTimeout((function onTimeout(){if(e._onTimeout)e._onTimeout()}),t)}};i(505);t.setImmediate=typeof self!=="undefined"&&self.setImmediate||typeof global!=="undefined"&&global.setImmediate||this&&this.setImmediate;t.clearImmediate=typeof self!=="undefined"&&self.clearImmediate||typeof global!=="undefined"&&global.clearImmediate||this&&this.clearImmediate},505:function(e){"use strict";e.exports=require("next/dist/compiled/setimmediate")}};var t={};function __nccwpck_require__(i){var o=t[i];if(o!==undefined){return o.exports}var n=t[i]={exports:{}};var r=true;try{e[i].call(n.exports,n,n.exports,__nccwpck_require__);r=false}finally{if(r)delete t[i]}return n.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var i=__nccwpck_require__(832);module.exports=i})(); \ No newline at end of file diff --git a/packages/next/package.json b/packages/next/package.json index 23686a9cab205..45fd4ac6bc5f9 100644 --- a/packages/next/package.json +++ b/packages/next/package.json @@ -236,6 +236,7 @@ "postcss-safe-parser": "6.0.0", "postcss-scss": "3.0.5", "postcss-value-parser": "4.1.0", + "punycode": "2.1.1", "process": "0.11.10", "querystring-es3": "0.2.1", "raw-body": "2.4.1", @@ -244,6 +245,7 @@ "sass-loader": "10.2.0", "schema-utils2": "npm:schema-utils@2.7.1", "schema-utils3": "npm:schema-utils@3.0.0", + "setimmediate": "1.0.5", "semver": "7.3.2", "send": "0.17.1", "source-map": "0.6.1", diff --git a/packages/next/taskfile.js b/packages/next/taskfile.js index 7b088e7d4e0bc..f0c348e9c57c9 100644 --- a/packages/next/taskfile.js +++ b/packages/next/taskfile.js @@ -510,6 +510,32 @@ export async function ncc_util(task, opts) { .target('compiled/util') } +// eslint-disable-next-line camelcase +export async function ncc_punycode(task, opts) { + await task + .source(opts.src || relative(__dirname, require.resolve('punycode/'))) + .ncc({ + packageName: 'punycode', + externals, + mainFields: ['browser', 'main'], + target: 'es5', + }) + .target('compiled/punycode') +} + +// eslint-disable-next-line camelcase +export async function ncc_set_immediate(task, opts) { + await task + .source(opts.src || relative(__dirname, require.resolve('setimmediate/'))) + .ncc({ + packageName: 'setimmediate', + externals, + mainFields: ['browser', 'main'], + target: 'es5', + }) + .target('compiled/setimmediate') +} + // eslint-disable-next-line camelcase export async function ncc_timers_browserify(task, opts) { await task @@ -518,7 +544,10 @@ export async function ncc_timers_browserify(task, opts) { ) .ncc({ packageName: 'timers-browserify', - externals, + externals: { + ...externals, + setimmediate: 'next/dist/compiled/setimmediate', + }, mainFields: ['browser', 'main'], target: 'es5', }) @@ -1444,6 +1473,8 @@ export async function ncc(task, opts) { 'ncc_querystring_es3', 'ncc_string_decoder', 'ncc_util', + 'ncc_punycode', + 'ncc_set_immediate', 'ncc_timers_browserify', 'ncc_tty_browserify', 'ncc_vm_browserify', diff --git a/test/integration/polyfill-node-modules/test/index.test.js b/test/development/basic/node-browser-polyfills.test.ts similarity index 70% rename from test/integration/polyfill-node-modules/test/index.test.js rename to test/development/basic/node-browser-polyfills.test.ts index 0b10c4d09c546..de6352b400231 100644 --- a/test/integration/polyfill-node-modules/test/index.test.js +++ b/test/development/basic/node-browser-polyfills.test.ts @@ -1,22 +1,22 @@ -/* eslint-env jest */ - import { join } from 'path' import webdriver from 'next-webdriver' -import { findPort, launchApp, killApp } from 'next-test-utils' +import { createNext, FileRef } from 'e2e-utils' +import { NextInstance } from 'test/lib/next-modes/base' -const appDir = join(__dirname, '../') -let appPort -let app +describe('theme-ui SWC option', () => { + let next: NextInstance -describe('Basic Features', () => { beforeAll(async () => { - appPort = await findPort() - app = await launchApp(appDir, appPort) + next = await createNext({ + files: { + pages: new FileRef(join(__dirname, 'node-browser-polyfills/pages')), + }, + }) }) - afterAll(() => killApp(app)) + afterAll(() => next.destroy()) - it('should polyfill Node.js modules', async () => { - const browser = await webdriver(appPort, '/node-browser-polyfills') + it('should have polyfilled correctly', async () => { + const browser = await webdriver(next.url, '/') await browser.waitForCondition('window.didRender') diff --git a/test/integration/polyfill-node-modules/pages/node-browser-polyfills.js b/test/development/basic/node-browser-polyfills/pages/index.js similarity index 69% rename from test/integration/polyfill-node-modules/pages/node-browser-polyfills.js rename to test/development/basic/node-browser-polyfills/pages/index.js index 46fd712929db3..8f3c6e39be1f9 100644 --- a/test/integration/polyfill-node-modules/pages/node-browser-polyfills.js +++ b/test/development/basic/node-browser-polyfills/pages/index.js @@ -19,6 +19,7 @@ import timers from 'timers' import tty from 'tty' import util from 'util' import zlib from 'zlib' +import 'setimmediate' export default function NodeBrowserPolyfillPage() { const [state, setState] = useState({}) @@ -50,26 +51,28 @@ export default function NodeBrowserPolyfillPage() { assert.ok(!!util.inspect) assert.ok(!!zlib.Gzip) - setState({ - assert: true, - buffer: Buffer.from('hello world').toString('utf8'), - constants: constants.E2BIG, - hash: crypto.createHash('sha256').update('hello world').digest('hex'), - domain: true, - os: os.EOL, - path: path.join('/hello/world', 'test.txt'), - process: process.title, - querystring: querystring.stringify({ a: 'b' }), - stream: closedStream, - stringDecoder: true, - sys: true, - timers: true, - tty: true, - util: true, - http: true, - https: true, - vm: vm.runInNewContext('a + 5', { a: 100 }), - zlib: true, + setImmediate(() => { + setState({ + assert: true, + buffer: Buffer.from('hello world').toString('utf8'), + constants: constants.E2BIG, + hash: crypto.createHash('sha256').update('hello world').digest('hex'), + domain: true, + os: os.EOL, + path: path.join('/hello/world', 'test.txt'), + process: process.title, + querystring: querystring.stringify({ a: 'b' }), + stream: closedStream, + stringDecoder: true, + sys: true, + timers: true, + tty: true, + util: true, + http: true, + https: true, + vm: vm.runInNewContext('a + 5', { a: 100 }), + zlib: true, + }) }) }, []) diff --git a/test/integration/polyfill-node-modules/next.config.js b/test/integration/polyfill-node-modules/next.config.js deleted file mode 100644 index cc17cf48c578f..0000000000000 --- a/test/integration/polyfill-node-modules/next.config.js +++ /dev/null @@ -1,6 +0,0 @@ -module.exports = { - onDemandEntries: { - // Make sure entries are not getting disposed. - maxInactiveAge: 1000 * 60 * 60, - }, -} diff --git a/yarn.lock b/yarn.lock index d991d307b0cb1..73d0f74becf85 100644 --- a/yarn.lock +++ b/yarn.lock @@ -16277,14 +16277,14 @@ punycode@1.3.2: resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.3.2.tgz#9653a036fb7c1ee42342f2325cceefea3926c48d" integrity sha1-llOgNvt8HuQjQvIyXM7v6jkmxI0= +punycode@2.1.1, punycode@^2.1.0, punycode@^2.1.1: + version "2.1.1" + resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec" + punycode@^1.2.4: version "1.4.1" resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.4.1.tgz#c0d5a63b2718800ad8e1eb0fa5269c84dd41845e" -punycode@^2.1.0, punycode@^2.1.1: - version "2.1.1" - resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec" - pupa@^2.0.1: version "2.1.1" resolved "https://registry.yarnpkg.com/pupa/-/pupa-2.1.1.tgz#f5e8fd4afc2c5d97828faa523549ed8744a20d62" @@ -17565,7 +17565,7 @@ set-value@^2.0.0, set-value@^2.0.1: is-plain-object "^2.0.3" split-string "^3.0.1" -setimmediate@^1.0.4: +setimmediate@1.0.5, setimmediate@^1.0.4: version "1.0.5" resolved "https://registry.yarnpkg.com/setimmediate/-/setimmediate-1.0.5.tgz#290cbb232e306942d7d7ea9b83732ab7856f8285" integrity sha1-KQy7Iy4waULX1+qbg3Mqt4VvgoU=