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
kodiakhq
merged 21 commits into
vercel:canary
from
nkzawa:middleware-no-node-buildin-module
Apr 25, 2022
Merged
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 5ae8270
Merge branch 'canary' into middleware-no-node-buildin-module
nkzawa 5ffd627
Merge branch 'canary' into middleware-no-node-buildin-module
nkzawa b5246bb
Merge branch 'canary' into middleware-no-node-buildin-module
nkzawa d113681
Merge branch 'canary' into middleware-no-node-buildin-module
nkzawa 817463f
Merge branch 'canary' into middleware-no-node-buildin-module
nkzawa 94db083
polyfill built-in modules for edge runtime for now
nkzawa cd08116
Merge branch 'canary' into middleware-no-node-buildin-module
nkzawa dd23be0
Merge branch 'canary' into middleware-no-node-buildin-module
nkzawa bb20bfb
Merge branch 'canary' into middleware-no-node-buildin-module
nkzawa 4bd888b
Merge branch 'canary' into middleware-no-node-buildin-module
nkzawa 92146aa
Revert "polyfill built-in modules for edge runtime for now"
nkzawa 8ea6672
Merge branch 'canary' into middleware-no-node-buildin-module
nkzawa 0ffa323
update internal path import
huozhi 1c91e94
remove use of process polyfill
nkzawa 8127b1b
fix for lint
nkzawa 7cd01cf
fix for lint
nkzawa 0fdb159
fix a test
nkzawa 39e149d
Merge branch 'canary' into middleware-no-node-buildin-module
nkzawa 526a795
disable fallback check for process on edge runtime
nkzawa 5dfd5a4
set existing polyfill instead
nkzawa File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
test/integration/middleware/with-builtin-module/pages/using-child-process/_middleware.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
7 changes: 7 additions & 0 deletions
7
test/integration/middleware/with-builtin-module/pages/using-path/_middleware.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
90 changes: 90 additions & 0 deletions
90
test/integration/middleware/with-builtin-module/test/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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