-
Notifications
You must be signed in to change notification settings - Fork 26.1k
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
feat: Webpack 5 Experimental Support #10885
Closed
ScriptedAlchemy
wants to merge
10
commits into
vercel:master
from
module-federation:canary-backport-wp5-to-zeit
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3cdf71e
feat: Upgrade Next.js to Webpack 5
ScriptedAlchemy 614e3b4
patching code over from other branch
ScriptedAlchemy aae4e38
multi-step build working
ScriptedAlchemy f57ad37
make router external on server
ScriptedAlchemy be2c65b
multi-step build working
ScriptedAlchemy 6ec9246
feat: Upgrade Next.js to Webpack 5
ScriptedAlchemy bf8818a
fix: disable hmr in dev move (#11)
ScriptedAlchemy 886eb96
fix: Ssr router dev dirty (#10)
ScriptedAlchemy d6ca9e4
Feat: Use live reload (#12)
ScriptedAlchemy f8bd32e
fix: Use live reload (#13)
ScriptedAlchemy 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,4 @@ | |
} | ||
] | ||
] | ||
} | ||
} |
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 |
---|---|---|
|
@@ -5,4 +5,4 @@ | |
"plugins": [ | ||
"transform-flow-strip-types" | ||
] | ||
} | ||
} |
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 |
---|---|---|
|
@@ -5,4 +5,4 @@ | |
"plugins": [ | ||
"macros" | ||
] | ||
} | ||
} |
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 |
---|---|---|
|
@@ -10,4 +10,4 @@ | |
"presets": [["next/babel", { "preset-env": { "modules": "commonjs" } }]] | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"presets": ["next/babel"] | ||
} | ||
} |
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 |
---|---|---|
|
@@ -4,4 +4,4 @@ type BlogPost implements Node { | |
id: ID! @isUnique | ||
title: String! | ||
updatedAt: DateTime! | ||
} | ||
} |
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 |
---|---|---|
|
@@ -5,4 +5,4 @@ | |
"plugins": [ | ||
["styled-components", { "ssr": true, "displayName": true, "preprocess": false } ] | ||
] | ||
} | ||
} |
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 |
---|---|---|
|
@@ -5,4 +5,4 @@ | |
"plugins": [ | ||
"relay" | ||
] | ||
} | ||
} |
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 |
---|---|---|
|
@@ -4,4 +4,4 @@ type BlogPost implements Node { | |
id: ID! @isUnique | ||
title: String! | ||
updatedAt: DateTime! | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,6 @@ import ChunkNamesPlugin from './webpack/plugins/chunk-names-plugin' | |
import { CssMinimizerPlugin } from './webpack/plugins/css-minimizer-plugin' | ||
import { importAutoDllPlugin } from './webpack/plugins/dll-import' | ||
import { DropClientPage } from './webpack/plugins/next-drop-client-page-plugin' | ||
import NextEsmPlugin from './webpack/plugins/next-esm-plugin' | ||
import NextJsSsrImportPlugin from './webpack/plugins/nextjs-ssr-import' | ||
import NextJsSSRModuleCachePlugin from './webpack/plugins/nextjs-ssr-module-cache' | ||
import PagesManifestPlugin from './webpack/plugins/pages-manifest-plugin' | ||
|
@@ -51,6 +50,15 @@ import WebpackConformancePlugin, { | |
} from './webpack/plugins/webpack-conformance-plugin' | ||
|
||
type ExcludesFalse = <T>(x: T | false) => x is T | ||
var LiveReloadPlugin = require('./webpack/plugins/livereload.js').default | ||
|
||
let NextEsmPlugin: any | ||
|
||
const webpack5Experiential = parseInt(require('webpack').version) === 5 | ||
|
||
if (!webpack5Experiential) { | ||
NextEsmPlugin = require('./webpack/plugins/next-esm-plugin') | ||
} | ||
|
||
const escapePathVariables = (value: any) => { | ||
return typeof value === 'string' | ||
|
@@ -270,7 +278,7 @@ export default async function getBaseWebpackConfig( | |
...getOptimizedAliases(isServer), | ||
}, | ||
mainFields: isServer ? ['main', 'module'] : ['browser', 'module', 'main'], | ||
plugins: [PnpWebpackPlugin], | ||
plugins: !webpack5Experiential ? [PnpWebpackPlugin] : [], | ||
} | ||
|
||
const webpackMode = dev ? 'development' : 'production' | ||
|
@@ -565,7 +573,7 @@ export default async function getBaseWebpackConfig( | |
// When the 'serverless' target is used all node_modules will be compiled into the output bundles | ||
// So that the 'serverless' bundles have 0 runtime dependencies | ||
'@ampproject/toolbox-optimizer', // except this one | ||
], | ||
].concat(webpack5Experiential ? ['enhanced-resolve'] : []), | ||
optimization: { | ||
checkWasmTypes: false, | ||
nodeEnv: false, | ||
|
@@ -629,8 +637,12 @@ export default async function getBaseWebpackConfig( | |
return '[name]' | ||
}, | ||
libraryTarget: isServer ? 'commonjs2' : 'var', | ||
hotUpdateChunkFilename: 'static/webpack/[id].[hash].hot-update.js', | ||
hotUpdateMainFilename: 'static/webpack/[hash].hot-update.json', | ||
hotUpdateChunkFilename: webpack5Experiential | ||
? 'static/webpack/[id].[fullhash].hot-update.js' | ||
: 'static/webpack/[id].[hash].hot-update.js', | ||
hotUpdateMainFilename: webpack5Experiential | ||
? 'static/webpack/[fullhash].hot-update.json' | ||
: 'static/webpack/[hash].hot-update.json', | ||
// This saves chunks with the name given via `import()` | ||
chunkFilename: isServer | ||
? `${dev ? '[name]' : '[name].[contenthash]'}.js` | ||
|
@@ -663,7 +675,7 @@ export default async function getBaseWebpackConfig( | |
'node_modules', | ||
...nodePathList, // Support for NODE_PATH environment variable | ||
], | ||
plugins: [PnpWebpackPlugin], | ||
plugins: webpack5Experiential ? [] : [PnpWebpackPlugin], | ||
}, | ||
module: { | ||
rules: [ | ||
|
@@ -806,41 +818,59 @@ export default async function getBaseWebpackConfig( | |
] | ||
|
||
if (!isServer) { | ||
const AutoDllPlugin = importAutoDllPlugin({ distDir }) | ||
devPlugins.push( | ||
new AutoDllPlugin({ | ||
filename: '[name]_[hash].js', | ||
path: './static/development/dll', | ||
context: dir, | ||
entry: { | ||
dll: ['react', 'react-dom'], | ||
}, | ||
config: { | ||
devtool, | ||
mode: webpackMode, | ||
resolve: resolveConfig, | ||
}, | ||
}) | ||
) | ||
devPlugins.push(new webpack.HotModuleReplacementPlugin()) | ||
if (!webpack5Experiential) { | ||
// Not needed with webpack 5 | ||
const AutoDllPlugin = importAutoDllPlugin({ distDir }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WP5 has its own cache, and its faast |
||
devPlugins.push( | ||
new AutoDllPlugin({ | ||
filename: '[name]_[hash].js', | ||
path: './static/development/dll', | ||
context: dir, | ||
entry: { | ||
dll: ['react', 'react-dom'], | ||
}, | ||
config: { | ||
devtool, | ||
mode: webpackMode, | ||
resolve: resolveConfig, | ||
}, | ||
}) | ||
) | ||
} | ||
!webpack5Experiential && | ||
devPlugins.push(new webpack.HotModuleReplacementPlugin()) | ||
webpack5Experiential && | ||
devPlugins.push(new LiveReloadPlugin({ appendScriptTag: true })) | ||
} | ||
|
||
return devPlugins | ||
})() | ||
: []), | ||
!dev && new webpack.HashedModuleIdsPlugin(), | ||
!dev && | ||
new webpack.IgnorePlugin({ | ||
checkResource: (resource: string) => { | ||
return /react-is/.test(resource) | ||
}, | ||
checkContext: (context: string) => { | ||
return ( | ||
/next-server[\\/]dist[\\/]/.test(context) || | ||
/next[\\/]dist[\\/]/.test(context) | ||
) | ||
}, | ||
}), | ||
(webpack5Experiential | ||
? // @ts-ignore | ||
new webpack.ids.HashedModuleIdsPlugin() | ||
: new webpack.HashedModuleIdsPlugin()), | ||
!dev && | ||
new webpack.IgnorePlugin( | ||
webpack5Experiential | ||
? { | ||
// webpack 5 only accepts New RegEx | ||
resourceRegExp: /react-is/, | ||
contextRegExp: /(next-server|next)[\\/]dist[\\/]/, | ||
} | ||
: { | ||
checkResource: (resource: string) => { | ||
return /react-is/.test(resource) | ||
}, | ||
checkContext: (context: string) => { | ||
return ( | ||
/next-server[\\/]dist[\\/]/.test(context) || | ||
/next[\\/]dist[\\/]/.test(context) | ||
) | ||
}, | ||
} | ||
), | ||
isServerless && isServer && new ServerlessPlugin(), | ||
isServer && new PagesManifestPlugin(isLikeServerless), | ||
target === 'server' && | ||
|
@@ -874,8 +904,10 @@ export default async function getBaseWebpackConfig( | |
}) | ||
), | ||
config.experimental.modern && | ||
!webpack5Experiential && | ||
!isServer && | ||
!dev && | ||
// does not work with WP5, needs to be refactored. | ||
new NextEsmPlugin({ | ||
filename: (getFileName: Function | string) => (...args: any[]) => { | ||
const name = | ||
|
@@ -932,6 +964,13 @@ export default async function getBaseWebpackConfig( | |
) | ||
} | ||
|
||
if (webpack5Experiential) { | ||
// @ts-ignore | ||
delete webpackConfig.output.futureEmitAssets | ||
// @ts-ignore | ||
delete webpackConfig.node.setImmediate | ||
} | ||
|
||
webpackConfig = await buildConfiguration(webpackConfig, { | ||
rootDirectory: dir, | ||
customAppFile, | ||
|
@@ -943,6 +982,14 @@ export default async function getBaseWebpackConfig( | |
sassOptions: config.experimental.sassOptions, | ||
}) | ||
|
||
// in Webpack 5- var larbraryType output requires a name, will default to package.json name soon | ||
if (!isServer && webpack5Experiential) { | ||
// @ts-ignore | ||
webpackConfig.output.library = webpackConfig.output.library | ||
? webpackConfig.output.library | ||
: 'nextapp' | ||
} | ||
|
||
if (typeof config.webpack === 'function') { | ||
webpackConfig = config.webpack(webpackConfig, { | ||
dir, | ||
|
Oops, something went wrong.
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.
broken in WP5, will address later