Skip to content

Commit

Permalink
Merge branch 'canary' into refactor/server-api-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
tarunama committed Jan 18, 2021
2 parents 6b27d59 + 699a7ae commit a6eb637
Show file tree
Hide file tree
Showing 24 changed files with 845 additions and 426 deletions.
2 changes: 1 addition & 1 deletion contributing.md
Expand Up @@ -18,7 +18,7 @@ To contribute to [our examples](examples), take a look at the [“Adding example

Make sure you have `chromedriver` installed for your Chrome version. You can install it with

- `brew cask install chromedriver` on Mac OS X
- `brew install --cask chromedriver` on Mac OS X
- `chocolatey install chromedriver` on Windows
- Or manually download the version that matches your installed chrome version (if there's no match, download a version under it, but not above) from the [chromedriver repo](https://chromedriver.storage.googleapis.com/index.html) and add the binary to `<next-repo>/node_modules/.bin`

Expand Down
4 changes: 2 additions & 2 deletions errors/invalid-webpack-5-version.md
Expand Up @@ -2,11 +2,11 @@

#### Why This Error Occurred

While leveraging webpack 5 support in Next.js the minimum required version of `v5.11.1` was not met. This version is needed while leveraging webpack 5 support with Next.js as early versions are missing patches that cause unexpected behavior.
While leveraging webpack 5 support in Next.js the minimum required version of `v5.15.0` was not met. This version is needed while leveraging webpack 5 support with Next.js as early versions are missing patches that cause unexpected behavior.

#### Possible Ways to Fix It

Upgrade the version of webpack 5 being used with Next.js to at least `v5.11.1` by updating your resolutions field for `webpack` in your `package.json`.
Upgrade the version of webpack 5 being used with Next.js to at least `v5.15.0` by updating your resolutions field for `webpack` in your `package.json`.

### Useful Links

Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Expand Up @@ -17,5 +17,5 @@
"registry": "https://registry.npmjs.org/"
}
},
"version": "10.0.6-canary.3"
"version": "10.0.6-canary.4"
}
2 changes: 1 addition & 1 deletion packages/create-next-app/package.json
@@ -1,6 +1,6 @@
{
"name": "create-next-app",
"version": "10.0.6-canary.3",
"version": "10.0.6-canary.4",
"keywords": [
"react",
"next",
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-next/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/eslint-plugin-next",
"version": "10.0.6-canary.3",
"version": "10.0.6-canary.4",
"description": "ESLint plugin for NextJS.",
"main": "lib/index.js",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-bundle-analyzer/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/bundle-analyzer",
"version": "10.0.6-canary.3",
"version": "10.0.6-canary.4",
"main": "index.js",
"license": "MIT",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/next-codemod/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/codemod",
"version": "10.0.6-canary.3",
"version": "10.0.6-canary.4",
"license": "MIT",
"dependencies": {
"chalk": "4.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-env/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/env",
"version": "10.0.6-canary.3",
"version": "10.0.6-canary.4",
"keywords": [
"react",
"next",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-mdx/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/mdx",
"version": "10.0.6-canary.3",
"version": "10.0.6-canary.4",
"main": "index.js",
"license": "MIT",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/next-plugin-google-analytics/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/plugin-google-analytics",
"version": "10.0.6-canary.3",
"version": "10.0.6-canary.4",
"repository": {
"url": "vercel/next.js",
"directory": "packages/next-plugin-google-analytics"
Expand Down
2 changes: 1 addition & 1 deletion packages/next-plugin-sentry/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/plugin-sentry",
"version": "10.0.6-canary.3",
"version": "10.0.6-canary.4",
"repository": {
"url": "vercel/next.js",
"directory": "packages/next-plugin-sentry"
Expand Down
2 changes: 1 addition & 1 deletion packages/next-plugin-storybook/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/plugin-storybook",
"version": "10.0.6-canary.3",
"version": "10.0.6-canary.4",
"repository": {
"url": "vercel/next.js",
"directory": "packages/next-plugin-storybook"
Expand Down
2 changes: 1 addition & 1 deletion packages/next-polyfill-module/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/polyfill-module",
"version": "10.0.6-canary.3",
"version": "10.0.6-canary.4",
"description": "A standard library polyfill for ES Modules supporting browsers (Edge 16+, Firefox 60+, Chrome 61+, Safari 10.1+)",
"main": "dist/polyfill-module.js",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-polyfill-nomodule/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/polyfill-nomodule",
"version": "10.0.6-canary.3",
"version": "10.0.6-canary.4",
"description": "A polyfill for non-dead, nomodule browsers.",
"main": "dist/polyfill-nomodule.js",
"license": "MIT",
Expand Down
123 changes: 78 additions & 45 deletions packages/next/build/webpack-config.ts
Expand Up @@ -434,17 +434,11 @@ export default async function getBaseWebpackConfig(
cacheGroups: {
default: false,
vendors: false,
// In webpack 5 vendors was renamed to defaultVendors
defaultVendors: false,
},
},
prodGranular: {
chunks: 'all',
cacheGroups: {
default: false,
vendors: false,
// In webpack 5 vendors was renamed to defaultVendors
defaultVendors: false,
framework: {
chunks: 'all',
name: 'framework',
Expand All @@ -458,10 +452,13 @@ export default async function getBaseWebpackConfig(
enforce: true,
},
lib: {
test(module: { size: Function; identifier: Function }): boolean {
test(module: {
size: Function
nameForCondition: Function
}): boolean {
return (
module.size() > 160000 &&
/node_modules[/\\]/.test(module.identifier())
/node_modules[/\\]/.test(module.nameForCondition() || '')
)
},
name(module: {
Expand Down Expand Up @@ -493,26 +490,32 @@ export default async function getBaseWebpackConfig(
minChunks: totalPages,
priority: 20,
},
shared: {
name(module, chunks) {
return (
crypto
.createHash('sha1')
.update(
chunks.reduce(
(acc: string, chunk: webpack.compilation.Chunk) => {
return acc + chunk.name
},
''
...(isWebpack5
? undefined
: {
default: false,
vendors: false,
shared: {
name(module, chunks) {
return (
crypto
.createHash('sha1')
.update(
chunks.reduce(
(acc: string, chunk: webpack.compilation.Chunk) => {
return acc + chunk.name
},
''
)
)
.digest('hex') + (isModuleCSS(module) ? '_CSS' : '')
)
)
.digest('hex') + (isModuleCSS(module) ? '_CSS' : '')
)
},
priority: 10,
minChunks: 2,
reuseExistingChunk: true,
},
},
priority: 10,
minChunks: 2,
reuseExistingChunk: true,
},
}),
},
maxInitialRequests: 25,
minSize: 20000,
Expand Down Expand Up @@ -562,9 +565,13 @@ export default async function getBaseWebpackConfig(
config.conformance
)

function handleExternals(context: any, request: any, callback: any) {
async function handleExternals(
context: string,
request: string,
getResolve: () => (context: string, request: string) => Promise<string>
) {
if (request === 'next') {
return callback(undefined, `commonjs ${request}`)
return `commonjs ${request}`
}

const notExternalModules = [
Expand All @@ -578,7 +585,7 @@ export default async function getBaseWebpackConfig(
]

if (notExternalModules.indexOf(request) !== -1) {
return callback()
return
}

// We need to externalize internal requests for files intended to
Expand All @@ -600,26 +607,28 @@ export default async function getBaseWebpackConfig(
// Absolute requires (require('/foo')) are extremely uncommon, but
// also have no need for customization as they're already resolved.
if (isLocal && !isLikelyNextExternal) {
return callback()
return
}

const resolve = getResolve()

// Resolve the import with the webpack provided context, this
// ensures we're resolving the correct version when multiple
// exist.
let res: string
try {
res = require.resolve(request, { paths: [context] })
res = await resolve(context, request)
} catch (err) {
// If the request cannot be resolved, we need to tell webpack to
// "bundle" it so that webpack shows an error (that it cannot be
// resolved).
return callback()
return
}

// Same as above, if the request cannot be resolved we need to have
// webpack "bundle" it so it surfaces the not found error.
if (!res) {
return callback()
return
}

let isNextExternal: boolean = false
Expand All @@ -631,7 +640,7 @@ export default async function getBaseWebpackConfig(
)

if (!isNextExternal) {
return callback()
return
}
}

Expand All @@ -646,7 +655,7 @@ export default async function getBaseWebpackConfig(
// we need to bundle the code (even if it _should_ be external).
let baseRes: string | null
try {
baseRes = require.resolve(request, { paths: [dir] })
baseRes = await resolve(dir, request)
} catch (err) {
baseRes = null
}
Expand All @@ -660,7 +669,7 @@ export default async function getBaseWebpackConfig(
// if res and baseRes are symlinks they could point to the the same file
realpathSync(baseRes) !== realpathSync(res))
) {
return callback()
return
}
}

Expand All @@ -671,15 +680,15 @@ export default async function getBaseWebpackConfig(
// This is the @babel/plugin-transform-runtime "helpers: true" option
res.match(/node_modules[/\\]@babel[/\\]runtime[/\\]/))
) {
return callback()
return
}

// Webpack itself has to be compiled because it doesn't always use module relative paths
if (
res.match(/node_modules[/\\]webpack/) ||
res.match(/node_modules[/\\]css-loader/)
) {
return callback()
return
}

// Anything else that is standard JavaScript within `node_modules`
Expand All @@ -701,11 +710,10 @@ export default async function getBaseWebpackConfig(
)
: request

return callback(undefined, `commonjs ${externalRequest}`)
return `commonjs ${externalRequest}`
}

// Default behavior: bundle the code!
callback()
}

const emacsLockfilePattern = '**/.#*'
Expand All @@ -719,10 +727,35 @@ export default async function getBaseWebpackConfig(
: !isServerless
? [
isWebpack5
? ({ context, request }, callback) =>
handleExternals(context, request, callback)
: (context, request, callback) =>
handleExternals(context, request, callback),
? ({
context,
request,
getResolve,
}: {
context: string
request: string
getResolve: () => (
context: string,
request: string
) => Promise<string>
}) => handleExternals(context, request, getResolve)
: (
context: string,
request: string,
callback: (err?: Error, result?: string | undefined) => void
) =>
handleExternals(
context,
request,
() => (resolveContext: string, requestToResolve: string) =>
new Promise((resolve) =>
resolve(
require.resolve(requestToResolve, {
paths: [resolveContext],
})
)
)
).then((result) => callback(undefined, result), callback),
]
: [
// When the 'serverless' target is used all node_modules will be compiled into the output bundles
Expand Down

0 comments on commit a6eb637

Please sign in to comment.