-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement webpack require hook (#21802)
This implements the compatibility require hook as per #21789. The hook is applied at the point of webpack initialization. In addition the separate packages are exposed for the various webpack subrequires. The test then ensures instance equality for the basic require hook from the next.js config file. I suspect this might have bad interactions with Yarn Pnp support, but maybe we will be lucky.
- Loading branch information
1 parent
9a930b6
commit 234e1c9
Showing
23 changed files
with
153 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// sync injects a hook for webpack and webpack/... requires to use the internal ncc webpack version | ||
// this is in order for userland plugins to attach to the same webpack instance as next.js | ||
// the individual compiled modules are as defined for the compilation in bundles/webpack/packages/* | ||
|
||
const hookPropertyMap = new Map([ | ||
['webpack', 'next/dist/compiled/webpack/webpack-lib'], | ||
['webpack/package', 'next/dist/compiled/webpack/package'], | ||
['webpack/package.json', 'next/dist/compiled/webpack/package'], | ||
['webpack/lib/webpack', 'next/dist/compiled/webpack/webpack-lib'], | ||
['webpack/lib/webpack.js', 'next/dist/compiled/webpack/webpack-lib'], | ||
[ | ||
'webpack/lib/node/NodeEnvironmentPlugin', | ||
'next/dist/compiled/webpack/NodeEnvironmentPlugin', | ||
], | ||
[ | ||
'webpack/lib/node/NodeEnvironmentPlugin.js', | ||
'next/dist/compiled/webpack/NodeEnvironmentPlugin', | ||
], | ||
[ | ||
'webpack/lib/BasicEvaluatedExpression', | ||
'next/dist/compiled/webpack/BasicEvaluatedExpression', | ||
], | ||
[ | ||
'webpack/lib/BasicEvaluatedExpression.js', | ||
'next/dist/compiled/webpack/BasicEvaluatedExpression', | ||
], | ||
[ | ||
'webpack/lib/node/NodeTargetPlugin', | ||
'next/dist/compiled/webpack/NodeTargetPlugin', | ||
], | ||
[ | ||
'webpack/lib/node/NodeTargetPlugin.js', | ||
'next/dist/compiled/webpack/NodeTargetPlugin', | ||
], | ||
[ | ||
'webpack/lib/ModuleFilenameHelpers', | ||
'next/dist/compiled/webpack/ModuleFilenameHelpers', | ||
], | ||
[ | ||
'webpack/lib/ModuleFilenameHelpers.js', | ||
'next/dist/compiled/webpack/ModuleFilenameHelpers', | ||
], | ||
['webpack/lib/GraphHelpers', 'next/dist/compiled/webpack/GraphHelpers'], | ||
['webpack/lib/GraphHelpers.js', 'next/dist/compiled/webpack/GraphHelpers'], | ||
['webpack-sources', 'next/dist/compiled/webpack/sources'], | ||
['webpack-sources/lib', 'next/dist/compiled/webpack/sources'], | ||
['webpack-sources/lib/index', 'next/dist/compiled/webpack/sources'], | ||
['webpack-sources/lib/index.js', 'next/dist/compiled/webpack/sources'], | ||
]) | ||
|
||
const mod = require('module') | ||
const resolveFilename = mod._resolveFilename | ||
mod._resolveFilename = function ( | ||
request: string, | ||
parent: any, | ||
isMain: boolean, | ||
options: any | ||
) { | ||
const hookResolved = hookPropertyMap.get(request) | ||
if (hookResolved) request = hookResolved | ||
return resolveFilename.call(mod, request, parent, isMain, options) | ||
} |
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
1 change: 1 addition & 0 deletions
1
packages/next/bundles/webpack/packages/BasicEvaluatedExpression.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 @@ | ||
module.exports = require('./webpack.js').BasicEvaluatedExpression |
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 @@ | ||
module.exports = require('./webpack.js').GraphHelpers |
1 change: 1 addition & 0 deletions
1
packages/next/bundles/webpack/packages/ModuleFilenameHelpers.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 @@ | ||
module.exports = require('./webpack.js').ModuleFilenameHelpers |
1 change: 1 addition & 0 deletions
1
packages/next/bundles/webpack/packages/NodeEnvironmentPlugin.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 @@ | ||
module.exports = require('./webpack.js').NodeEnvironmentPlugin |
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 @@ | ||
module.exports = require('./webpack.js').NodeTargetPlugin |
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 @@ | ||
module.exports = require('./webpack.js').package |
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 @@ | ||
module.exports = require('./webpack.js').sources |
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 @@ | ||
module.exports = require('./webpack.js').webpack |
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 @@ | ||
module.exports = require('./webpack.js').BasicEvaluatedExpression |
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 @@ | ||
module.exports = require('./webpack.js').GraphHelpers |
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 @@ | ||
module.exports = require('./webpack.js').ModuleFilenameHelpers |
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 @@ | ||
module.exports = require('./webpack.js').NodeEnvironmentPlugin |
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 @@ | ||
module.exports = require('./webpack.js').NodeTargetPlugin |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./webpack.js').package |
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 @@ | ||
module.exports = require('./webpack.js').sources |
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 @@ | ||
module.exports = require('./webpack.js').webpack |
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,14 @@ | ||
module.exports = { | ||
future: { | ||
webpack5: true, | ||
}, | ||
webpack(config, options) { | ||
console.log('Initialized config') | ||
if ( | ||
require('webpack/lib/node/NodeTargetPlugin') !== | ||
require('next/dist/compiled/webpack/NodeTargetPlugin') | ||
) | ||
throw new Error('Webpack require hook not applying') | ||
return config | ||
}, | ||
} |
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,5 @@ | ||
export default () => ( | ||
<> | ||
<h3>Hi 👋</h3> | ||
</> | ||
) |
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,45 @@ | ||
/* eslint-env jest */ | ||
|
||
import path from 'path' | ||
|
||
import { | ||
nextBuild, | ||
findPort, | ||
launchApp, | ||
renderViaHTTP, | ||
killApp, | ||
} from 'next-test-utils' | ||
|
||
jest.setTimeout(1000 * 60 * 1) | ||
const appDir = path.join(__dirname, '..') | ||
|
||
describe('Handles Webpack Require Hook', () => { | ||
describe('build', () => { | ||
it('Does not error during build', async () => { | ||
const { stdout, stderr } = await nextBuild(appDir, [], { | ||
stdout: true, | ||
stderr: true, | ||
}) | ||
console.log(stderr) | ||
expect(stderr.length).toStrictEqual(0) | ||
expect(stdout).toMatch(/Initialized config/) | ||
}) | ||
}) | ||
|
||
describe('dev mode', () => { | ||
it('Applies and does not error during development', async () => { | ||
let output | ||
const handleOutput = (msg) => { | ||
output += msg | ||
} | ||
const appPort = await findPort() | ||
const app = await launchApp(appDir, appPort, { | ||
onStdout: handleOutput, | ||
onStderr: handleOutput, | ||
}) | ||
await renderViaHTTP(appPort, '/') | ||
await killApp(app) | ||
expect(output).toMatch(/Initialized config/) | ||
}) | ||
}) | ||
}) |