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

Skip build-time dynamic code checks for specific polyfills in the Edge runtime #52009

Merged
merged 4 commits into from Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
14 changes: 14 additions & 0 deletions packages/next/src/build/webpack/plugins/middleware-plugin.ts
Expand Up @@ -9,6 +9,7 @@ import { getModuleBuildInfo } from '../loaders/get-module-build-info'
import { getSortedRoutes } from '../../../shared/lib/router/utils'
import { webpack, sources } from 'next/dist/compiled/webpack/webpack'
import { isMatch } from 'next/dist/compiled/micromatch'
import path from 'path'
import {
EDGE_RUNTIME_WEBPACK,
EDGE_UNSUPPORTED_NODE_APIS,
Expand All @@ -29,6 +30,9 @@ import { normalizeAppPath } from '../../../shared/lib/router/utils/app-paths'
import { INSTRUMENTATION_HOOK_FILENAME } from '../../../lib/constants'
import { NextBuildContext } from '../../build-context'

const KNOWN_SAFE_DYNAMIC_PACKAGES =
require('../../../lib/known-edge-safe-packages.json') as string[]

export interface EdgeFunctionDefinition {
files: string[]
name: string
Expand Down Expand Up @@ -249,6 +253,16 @@ function isDynamicCodeEvaluationAllowed(
middlewareConfig?: MiddlewareConfig,
rootDir?: string
) {
// Some packages are known to use `eval` but are safe to use in the Edge
// Runtime because the dynamic code will never be executed.
if (
KNOWN_SAFE_DYNAMIC_PACKAGES.some((pkg) =>
fileName.includes(`/node_modules/${pkg}/`.replace(/\//g, path.sep))
shuding marked this conversation as resolved.
Show resolved Hide resolved
)
) {
return true
}

const name = fileName.replace(rootDir ?? '', '')
return isMatch(name, middlewareConfig?.unstable_allowDynamicGlobs ?? [])
}
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/lib/known-edge-safe-packages.json
@@ -0,0 +1 @@
["function-bind"]
41 changes: 41 additions & 0 deletions test/production/edge-safe-dynamic-code/index.test.ts
@@ -0,0 +1,41 @@
import { createNext } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'

// This test is basically for https://github.com/vercel/next.js/discussions/51910
// to make sure that some libs that we know are using `eval` but don't break
// because it will never run into that condition, but still can't to be DCE'd.

describe('Edge safe dynamic code', () => {
let next: NextInstance

afterAll(() => next.destroy())

it('should not fail when "function-bind" package is used', async () => {
next = await createNext({
skipStart: true,
dependencies: {
'function-bind': 'latest',
},
files: {
'pages/index.js': `
export default function Page() {
return <p>hello world</p>
}
`,
'middleware.js': `
import { NextResponse } from 'next/server'
import * as bind from 'function-bind'
console.log(bind)
export default async function middleware(request) {
return NextResponse.next()
}
`,
},
})
await next.start()

expect(next.cliOutput).not.toContain(
`Dynamic Code Evaluation (e. g. 'eval', 'new Function', 'WebAssembly.compile') not allowed in Edge Runtime`
)
})
})