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

Do not polyfill node built-in modules on edge functions #36190

Merged
merged 21 commits into from Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
c296a68
do not polyfill node built-in modules
nkzawa Apr 15, 2022
5ae8270
Merge branch 'canary' into middleware-no-node-buildin-module
nkzawa Apr 15, 2022
5ffd627
Merge branch 'canary' into middleware-no-node-buildin-module
nkzawa Apr 15, 2022
b5246bb
Merge branch 'canary' into middleware-no-node-buildin-module
nkzawa Apr 18, 2022
d113681
Merge branch 'canary' into middleware-no-node-buildin-module
nkzawa Apr 18, 2022
817463f
Merge branch 'canary' into middleware-no-node-buildin-module
nkzawa Apr 19, 2022
94db083
polyfill built-in modules for edge runtime for now
nkzawa Apr 19, 2022
cd08116
Merge branch 'canary' into middleware-no-node-buildin-module
nkzawa Apr 20, 2022
dd23be0
Merge branch 'canary' into middleware-no-node-buildin-module
nkzawa Apr 21, 2022
bb20bfb
Merge branch 'canary' into middleware-no-node-buildin-module
nkzawa Apr 22, 2022
4bd888b
Merge branch 'canary' into middleware-no-node-buildin-module
nkzawa Apr 22, 2022
92146aa
Revert "polyfill built-in modules for edge runtime for now"
nkzawa Apr 22, 2022
8ea6672
Merge branch 'canary' into middleware-no-node-buildin-module
nkzawa Apr 22, 2022
0ffa323
update internal path import
huozhi Apr 22, 2022
1c91e94
remove use of process polyfill
nkzawa Apr 22, 2022
8127b1b
fix for lint
nkzawa Apr 22, 2022
7cd01cf
fix for lint
nkzawa Apr 22, 2022
0fdb159
fix a test
nkzawa Apr 22, 2022
39e149d
Merge branch 'canary' into middleware-no-node-buildin-module
nkzawa Apr 25, 2022
526a795
disable fallback check for process on edge runtime
nkzawa Apr 25, 2022
5dfd5a4
set existing polyfill instead
nkzawa Apr 25, 2022
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
9 changes: 8 additions & 1 deletion packages/next/build/index.ts
Expand Up @@ -723,7 +723,7 @@ export default async function build(
if (result.errors.length > 5) {
result.errors.length = 5
}
const error = result.errors.join('\n\n')
let error = result.errors.join('\n\n')

console.error(chalk.red('Failed to compile.\n'))

Expand All @@ -739,6 +739,13 @@ export default async function build(
)
}

const breakingChangeIndex = error.indexOf(
'\n\nBREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sokra is there a better way to exclude the compilation from showing these, an option maybe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks there is no other way as far as I checked https://github.com/webpack/webpack/blob/main/lib/ModuleNotFoundError.js#L63

)
if (breakingChangeIndex >= 0) {
error = error.slice(0, breakingChangeIndex)
}

console.error(error)
console.error()

Expand Down
107 changes: 76 additions & 31 deletions packages/next/build/webpack-config.ts
Expand Up @@ -626,40 +626,10 @@ export default async function getBaseWebpackConfig(

setimmediate: 'next/dist/compiled/setimmediate',
},
...(targetWeb
...(isEdgeRuntime
? {
// Full list of old polyfills is accessible here:
// https://github.com/webpack/webpack/blob/2a0536cf510768111a3a6dceeb14cb79b9f59273/lib/ModuleNotFoundError.js#L13-L42
fallback: {
assert: require.resolve('next/dist/compiled/assert'),
buffer: require.resolve('next/dist/compiled/buffer/'),
constants: require.resolve(
'next/dist/compiled/constants-browserify'
),
crypto: require.resolve('next/dist/compiled/crypto-browserify'),
domain: require.resolve('next/dist/compiled/domain-browser'),
http: require.resolve('next/dist/compiled/stream-http'),
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('next/dist/compiled/punycode'),
process: require.resolve('./polyfills/process'),
// Handled in separate alias
querystring: require.resolve('next/dist/compiled/querystring-es3'),
stream: require.resolve('next/dist/compiled/stream-browserify'),
string_decoder: require.resolve(
'next/dist/compiled/string_decoder'
),
sys: require.resolve('next/dist/compiled/util/'),
timers: require.resolve('next/dist/compiled/timers-browserify'),
tty: require.resolve('next/dist/compiled/tty-browserify'),
// Handled in separate alias
// url: require.resolve('url/'),
util: require.resolve('next/dist/compiled/util/'),
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),
Expand Down Expand Up @@ -1280,6 +1250,81 @@ export default async function getBaseWebpackConfig(
},
]
: []),
...(!isServer && !isEdgeRuntime
? [
{
oneOf: [
{
issuerLayer: 'middleware',
resolve: {
fallback: {
process: require.resolve('./polyfills/process'),
},
},
},
{
resolve: {
// Full list of old polyfills is accessible here:
// https://github.com/webpack/webpack/blob/2a0536cf510768111a3a6dceeb14cb79b9f59273/lib/ModuleNotFoundError.js#L13-L42
fallback: {
assert: require.resolve('next/dist/compiled/assert'),
buffer: require.resolve('next/dist/compiled/buffer/'),
constants: require.resolve(
'next/dist/compiled/constants-browserify'
),
crypto: require.resolve(
'next/dist/compiled/crypto-browserify'
),
domain: require.resolve(
'next/dist/compiled/domain-browser'
),
http: require.resolve('next/dist/compiled/stream-http'),
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(
'next/dist/compiled/punycode'
),
process: require.resolve('./polyfills/process'),
// Handled in separate alias
querystring: require.resolve(
'next/dist/compiled/querystring-es3'
),
stream: require.resolve(
'next/dist/compiled/stream-browserify'
),
string_decoder: require.resolve(
'next/dist/compiled/string_decoder'
),
sys: require.resolve('next/dist/compiled/util/'),
timers: require.resolve(
'next/dist/compiled/timers-browserify'
),
tty: require.resolve(
'next/dist/compiled/tty-browserify'
),
// Handled in separate alias
// url: require.resolve('url/'),
util: require.resolve('next/dist/compiled/util/'),
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'
),
},
},
},
],
},
]
: []),
].filter(Boolean),
},
plugins: [
Expand Down
1 change: 0 additions & 1 deletion packages/next/server/web/sandbox/context.ts
Expand Up @@ -210,7 +210,6 @@ function createContext(options: {
File,
FormData,
process: {
...polyfills.process,
env: buildEnvironmentVariablesFrom(options.env),
},
ReadableStream,
Expand Down
3 changes: 1 addition & 2 deletions packages/next/server/web/sandbox/polyfills.ts
@@ -1,7 +1,6 @@
import { Crypto as WebCrypto } from 'next/dist/compiled/@peculiar/webcrypto'
import { CryptoKey } from 'next/dist/compiled/@peculiar/webcrypto'
import { v4 as uuid } from 'next/dist/compiled/uuid'
import processPolyfill from 'next/dist/compiled/process'

import crypto from 'crypto'

Expand All @@ -13,7 +12,7 @@ export function btoa(str: string) {
return Buffer.from(str, 'binary').toString('base64')
}

export { CryptoKey, processPolyfill as process }
export { CryptoKey }

export class Crypto extends WebCrypto {
// @ts-ignore Remove once types are updated and we deprecate node 12
Expand Down
2 changes: 2 additions & 0 deletions test/integration/middleware/core/test/index.test.js
Expand Up @@ -131,6 +131,8 @@ describe('Middleware base tests', () => {
env: {
MIDDLEWARE_TEST: 'asdf',
},
// it's poflyfilled since there is the "process" module
// as a devDepencies of the next package
nextTick: 'function',
},
})
Expand Down
@@ -0,0 +1,7 @@
import { NextResponse } from 'next/server'
import { spawn } from 'child_process'

export async function middleware(request) {
console.log(spawn('ls', ['-lh', '/usr']))
return NextResponse.next()
}
@@ -0,0 +1,7 @@
import { NextResponse } from 'next/server'
import { basename } from 'path'

export async function middleware(request) {
console.log(basename('/foo/bar/baz/asdf/quux.html'))
return NextResponse.next()
}
@@ -0,0 +1,90 @@
/* eslint-env jest */

import stripAnsi from 'next/dist/compiled/strip-ansi'
import { join } from 'path'
import {
fetchViaHTTP,
findPort,
killApp,
launchApp,
nextBuild,
waitFor,
} from 'next-test-utils'

const context = {}
const WEBPACK_BREAKING_CHANGE = 'BREAKING CHANGE:'

jest.setTimeout(1000 * 60 * 2)
context.appDir = join(__dirname, '../')

describe('Middleware importing Node.js built-in module', () => {
function getModuleNotFound(name) {
return `Module not found: Can't resolve '${name}'`
}

function getBuiltinApisNotSupported(name) {
return `Native Node.js APIs are not supported in the Edge Runtime. Found \`${name}\` imported.\n`
}

describe('dev mode', () => {
let output = ''

beforeAll(async () => {
context.appPort = await findPort()
context.app = await launchApp(context.appDir, context.appPort, {
env: { __NEXT_TEST_WITH_DEVTOOL: 1 },
onStdout(msg) {
output += msg
},
onStderr(msg) {
output += msg
},
})
})

beforeEach(() => (output = ''))
afterAll(() => killApp(context.app))

it('shows error when importing path module', async () => {
const res = await fetchViaHTTP(context.appPort, '/using-path')
await waitFor(500)
expect(res.status).toBe(500)
expect(output).toContain(getModuleNotFound('path'))
expect(output).toContain(getBuiltinApisNotSupported('path'))
expect(stripAnsi(output)).toContain("import { basename } from 'path'")
expect(output).not.toContain(WEBPACK_BREAKING_CHANGE)
})

it('shows error when importing child_process module', async () => {
const res = await fetchViaHTTP(context.appPort, '/using-child-process')
await waitFor(500)
expect(res.status).toBe(500)
expect(output).toContain(getModuleNotFound('child_process'))
expect(output).toContain(getBuiltinApisNotSupported('child_process'))
expect(stripAnsi(output)).toContain(
"import { spawn } from 'child_process'"
)
expect(output).not.toContain(WEBPACK_BREAKING_CHANGE)
})
})

describe('production mode', () => {
let buildResult

beforeAll(async () => {
buildResult = await nextBuild(context.appDir, undefined, {
stderr: true,
stdout: true,
})
})

it('should have middleware error during build', () => {
expect(buildResult.stderr).toContain(getModuleNotFound('child_process'))
expect(buildResult.stderr).toContain(getModuleNotFound('path'))
expect(buildResult.stderr).toContain(
getBuiltinApisNotSupported('child_process')
)
expect(buildResult.stderr).not.toContain(WEBPACK_BREAKING_CHANGE)
})
})
})