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

the way towards webpack 5 typings #29105

Merged
merged 35 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
b8e933b
name all dependencies with multiple version by their version
sokra Sep 15, 2021
e7bd7f2
Merge branch 'canary' into refactor/types
sokra Sep 16, 2021
46394a0
fix async modules test case which was never executed before
sokra Sep 16, 2021
7857e86
add missing require hooks which are used by test cases
sokra Sep 16, 2021
b5d3cab
fix require path for webpack 4
sokra Sep 16, 2021
f95ca32
remove left-over debugger
sokra Sep 16, 2021
ea0b3aa
fix react-refresh-utils types
sokra Sep 16, 2021
90e0baf
Merge branch 'canary' into refactor/types
sokra Sep 16, 2021
d2167c7
remove outdated comment
sokra Sep 16, 2021
2d426ff
fix amp usage
sokra Sep 16, 2021
f93a76a
migrate test case to nextStart
sokra Sep 16, 2021
b76ab2e
Merge branch 'canary' into refactor/types
sokra Sep 17, 2021
44994cc
improve test runner to avoid throwing when stopping
sokra Sep 17, 2021
69b12c2
skip test in webpack 4
sokra Sep 17, 2021
b7a8469
update ncced files
sokra Sep 17, 2021
be32d98
Merge branch 'canary' into refactor/types
sokra Sep 20, 2021
7e40d0d
improve check-precompiled script
sokra Sep 20, 2021
1c16ef9
fix test case
sokra Sep 20, 2021
c36a64e
fix precompiled files
sokra Sep 21, 2021
f68fd5f
Merge branch 'canary' into refactor/types
sokra Sep 21, 2021
1d75cd2
fix types for new code
sokra Sep 21, 2021
e64d057
try to get consistent pre-compiled files
sokra Sep 21, 2021
4b6fa65
automatically update pre compiled files
sokra Sep 21, 2021
f127628
remove workarounds
sokra Sep 21, 2021
7fae5a1
need to checkout before commit
sokra Sep 21, 2021
a765fb0
fix pre-committed commit action
sokra Sep 21, 2021
84669da
fix pre-committed commit action
sokra Sep 21, 2021
e79b96e
⚙ Update compiled files
sokra Sep 21, 2021
3384ddd
workaround yarn hoisting problem
sokra Sep 21, 2021
5fcc177
keep line endings
sokra Sep 21, 2021
b198d3a
restore dependency
sokra Sep 21, 2021
93303bd
install chromium
sokra Sep 21, 2021
f4683bb
Merge branch 'canary' into refactor/types
sokra Sep 21, 2021
8e4e059
Revert "install chromium"
sokra Sep 21, 2021
147f46d
Merge branch 'canary' into refactor/types
sokra Sep 21, 2021
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
19 changes: 5 additions & 14 deletions packages/next/build/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ClientPagesLoaderOptions } from './webpack/loaders/next-client-pages-lo
import { ServerlessLoaderQuery } from './webpack/loaders/next-serverless-loader'
import { LoadedEnvFiles } from '@next/env'
import { NextConfigComplete } from '../server/config-shared'
import type webpack5 from 'webpack5'

type PagesMapping = {
[page: string]: string
Expand Down Expand Up @@ -62,19 +63,9 @@ export function createPagesMapping(
return pages
}

export type WebpackEntrypoints = {
[bundle: string]:
| string
| string[]
| {
import: string | string[]
dependOn?: string | string[]
}
}

type Entrypoints = {
client: WebpackEntrypoints
server: WebpackEntrypoints
client: webpack5.EntryObject
server: webpack5.EntryObject
}

export function createEntrypoints(
Expand All @@ -85,8 +76,8 @@ export function createEntrypoints(
config: NextConfigComplete,
loadedEnvFiles: LoadedEnvFiles
): Entrypoints {
const client: WebpackEntrypoints = {}
const server: WebpackEntrypoints = {}
const client: webpack5.EntryObject = {}
const server: webpack5.EntryObject = {}

const hasRuntimeConfig =
Object.keys(config.publicRuntimeConfig).length > 0 ||
Expand Down
56 changes: 26 additions & 30 deletions packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { readFileSync } from 'fs'
import { codeFrameColumns } from 'next/dist/compiled/babel/code-frame'
import semver from 'next/dist/compiled/semver'
import { isWebpack5, webpack } from 'next/dist/compiled/webpack/webpack'
import type webpack5 from 'webpack5'
import path, { join as pathJoin, relative as relativePath } from 'path'
import {
DOT_NEXT_ALIAS,
Expand All @@ -28,7 +29,6 @@ import {
} from '../shared/lib/constants'
import { execOnce } from '../shared/lib/utils'
import { NextConfigComplete } from '../server/config-shared'
import { WebpackEntrypoints } from './entries'
import * as Log from './output/log'
import { build as buildConfiguration } from './webpack/config'
import { __overrideCssConfiguration } from './webpack/config/blocks/css/overrideCssConfiguration'
Expand Down Expand Up @@ -217,7 +217,7 @@ export default async function getBaseWebpackConfig(
pagesDir: string
target?: string
reactProductionProfiling?: boolean
entrypoints: WebpackEntrypoints
entrypoints: webpack5.EntryObject
rewrites: CustomRoutes['rewrites']
isDevFallback?: boolean
runWebpackSpan: Span
Expand Down Expand Up @@ -1337,26 +1337,26 @@ export default async function getBaseWebpackConfig(
// futureEmitAssets is on by default in webpack 5
delete webpackConfig.output?.futureEmitAssets

// webpack 5 no longer polyfills Node.js modules:
if (webpackConfig.node) delete webpackConfig.node.setImmediate

const webpack5Config = webpackConfig as webpack5.Configuration

if (isServer && dev) {
// Enable building of client compilation before server compilation in development
// @ts-ignore dependencies exists
webpackConfig.dependencies = ['client']
webpack5Config.dependencies = ['client']
}
// webpack 5 no longer polyfills Node.js modules:
if (webpackConfig.node) delete webpackConfig.node.setImmediate

// Due to bundling of webpack the default values can't be correctly detected
// This restores the webpack defaults
// @ts-ignore webpack 5
webpackConfig.snapshot = {}
webpack5Config.snapshot = {}
if (process.versions.pnp === '3') {
const match =
/^(.+?)[\\/]cache[\\/]jest-worker-npm-[^\\/]+\.zip[\\/]node_modules[\\/]/.exec(
require.resolve('jest-worker')
)
if (match) {
// @ts-ignore webpack 5
webpackConfig.snapshot.managedPaths = [
webpack5Config.snapshot.managedPaths = [
path.resolve(match[1], 'unplugged'),
]
}
Expand All @@ -1365,8 +1365,7 @@ export default async function getBaseWebpackConfig(
require.resolve('jest-worker')
)
if (match) {
// @ts-ignore webpack 5
webpackConfig.snapshot.managedPaths = [match[1]]
webpack5Config.snapshot.managedPaths = [match[1]]
}
}
if (process.versions.pnp === '1') {
Expand All @@ -1375,26 +1374,24 @@ export default async function getBaseWebpackConfig(
require.resolve('jest-worker')
)
if (match) {
// @ts-ignore webpack 5
webpackConfig.snapshot.immutablePaths = [match[1]]
webpack5Config.snapshot.immutablePaths = [match[1]]
}
} else if (process.versions.pnp === '3') {
const match =
/^(.+?)[\\/]jest-worker-npm-[^\\/]+\.zip[\\/]node_modules[\\/]/.exec(
require.resolve('jest-worker')
)
if (match) {
// @ts-ignore webpack 5
webpackConfig.snapshot.immutablePaths = [match[1]]
webpack5Config.snapshot.immutablePaths = [match[1]]
}
}

if (dev) {
if (!webpackConfig.optimization) {
webpackConfig.optimization = {}
if (!webpack5Config.optimization) {
webpack5Config.optimization = {}
}
webpackConfig.optimization.providedExports = false
webpackConfig.optimization.usedExports = false
webpack5Config.optimization.providedExports = false
webpack5Config.optimization.usedExports = false
}

const configVars = JSON.stringify({
Expand Down Expand Up @@ -1439,7 +1436,7 @@ export default async function getBaseWebpackConfig(
}
}

webpackConfig.cache = cache
webpack5Config.cache = cache

if (process.env.NEXT_WEBPACK_LOGGING) {
const logInfra =
Expand All @@ -1451,8 +1448,7 @@ export default async function getBaseWebpackConfig(
const logDefault = !logInfra && !logProfileClient && !logProfileServer

if (logDefault || logInfra) {
// @ts-ignore TODO: remove ignore when webpack 5 is stable
webpackConfig.infrastructureLogging = {
webpack5Config.infrastructureLogging = {
level: 'verbose',
debug: /FileSystemInfo/,
}
Expand All @@ -1463,12 +1459,11 @@ export default async function getBaseWebpackConfig(
(logProfileClient && !isServer) ||
(logProfileServer && isServer)
) {
webpackConfig.plugins!.push((compiler: webpack.Compiler) => {
webpack5Config.plugins!.push((compiler: webpack5.Compiler) => {
compiler.hooks.done.tap('next-webpack-logging', (stats) => {
console.log(
stats.toString({
colors: true,
// @ts-ignore TODO: remove ignore when webpack 5 is stable
logging: logDefault ? 'log' : 'verbose',
})
)
Expand All @@ -1477,13 +1472,14 @@ export default async function getBaseWebpackConfig(
}

if ((logProfileClient && !isServer) || (logProfileServer && isServer)) {
webpackConfig.plugins!.push(
new webpack.ProgressPlugin({
// @ts-ignore TODO: remove ignore when webpack 5 is stable
const ProgressPlugin =
webpack.ProgressPlugin as unknown as typeof webpack5.ProgressPlugin
webpack5Config.plugins!.push(
new ProgressPlugin({
profile: true,
})
)
webpackConfig.profile = true
webpack5Config.profile = true
}
}
}
Expand Down Expand Up @@ -1844,7 +1840,7 @@ export default async function getBaseWebpackConfig(
const originalEntry: any = webpackConfig.entry
if (typeof originalEntry !== 'undefined') {
const updatedEntry = async () => {
const entry: WebpackEntrypoints =
const entry: webpack5.EntryObject =
typeof originalEntry === 'function'
? await originalEntry()
: originalEntry
Expand Down
41 changes: 26 additions & 15 deletions packages/next/build/webpack/plugins/build-stats-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { Transform, TransformCallback } from 'stream'
// @ts-ignore no types package
import bfj from 'next/dist/compiled/bfj'
import { spans } from './profiling-plugin'
import { webpack } from 'next/dist/compiled/webpack/webpack'
import { isWebpack5 } from 'next/dist/compiled/webpack/webpack'
import type webpack from 'webpack'
import type webpack4 from 'webpack4'
import type webpack5 from 'webpack5'

const STATS_VERSION = 0

Expand Down Expand Up @@ -120,21 +123,29 @@ export default class BuildStatsPlugin {
const writeStatsSpan = compilerSpan!.traceChild('NextJsBuildStats')
await writeStatsSpan.traceAsyncFn(() => {
return new Promise((resolve, reject) => {
const baseOptions = {
all: false,
cached: true,
reasons: true,
entrypoints: true,
chunks: true,
errors: false,
warnings: false,
maxModules: Infinity,
chunkModules: true,
modules: true,
}
const statsJson = reduceSize(
stats.toJson({
all: false,
cached: true,
reasons: true,
entrypoints: true,
chunks: true,
errors: false,
warnings: false,
maxModules: Infinity,
chunkModules: true,
modules: true,
// @ts-ignore this option exists
ids: true,
})
isWebpack5
? (stats as webpack5.Stats).toJson({
...baseOptions,
modulesSpace: Infinity,
ids: true,
})
: (stats as webpack4.Stats).toJson({
...baseOptions,
maxModules: Infinity,
})
)
const fileStream = fs.createWriteStream(
path.join(this.distDir, 'next-stats.json'),
Expand Down
24 changes: 24 additions & 0 deletions packages/next/build/webpack/require-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ const hookPropertyMap = new Map(
'webpack/lib/node/NodeTargetPlugin.js',
'next/dist/compiled/webpack/NodeTargetPlugin',
],
[
'webpack/lib/node/NodeTemplatePlugin',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nobody noticed that these were missing because when running next.js test cases there was a webpack package installed. That's no longer the case (only webpack4 and webpack5) so that was discovered.

Used by worker-loader and @zeit/next-css

'next/dist/compiled/webpack/NodeTemplatePlugin',
],
[
'webpack/lib/node/NodeTemplatePlugin.js',
'next/dist/compiled/webpack/NodeTemplatePlugin',
],
[
'webpack/lib/LibraryTemplatePlugin',
'next/dist/compiled/webpack/LibraryTemplatePlugin',
],
[
'webpack/lib/LibraryTemplatePlugin.js',
'next/dist/compiled/webpack/LibraryTemplatePlugin',
],
[
'webpack/lib/SingleEntryPlugin',
'next/dist/compiled/webpack/SingleEntryPlugin',
],
[
'webpack/lib/SingleEntryPlugin.js',
'next/dist/compiled/webpack/SingleEntryPlugin',
],
[
'webpack/lib/ModuleFilenameHelpers',
'next/dist/compiled/webpack/ModuleFilenameHelpers',
Expand Down
11 changes: 0 additions & 11 deletions packages/next/bundles/package.json

This file was deleted.

31 changes: 16 additions & 15 deletions packages/next/bundles/webpack/bundle4.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@

module.exports = function () {
return {
BasicEvaluatedExpression: require('webpack/lib/BasicEvaluatedExpression'),
NodeEnvironmentPlugin: require('webpack/lib/node/NodeEnvironmentPlugin'),
NodeTargetPlugin: require('webpack/lib/node/NodeTargetPlugin'),
ModuleFilenameHelpers: require('webpack/lib/ModuleFilenameHelpers'),
GraphHelpers: require('webpack/lib/GraphHelpers'),
Module: require('webpack/lib/Module'),
NormalModule: require('webpack/lib/NormalModule'),
Dependency: require('webpack/lib/Dependency'),
LibraryTemplatePlugin: require('webpack/lib/LibraryTemplatePlugin'),
SingleEntryPlugin: require('webpack/lib/SingleEntryPlugin'),
node: require('webpack').node,
util: require('webpack').util,
optimize: require('webpack').optimize,
BasicEvaluatedExpression: require('webpack4/lib/BasicEvaluatedExpression'),
NodeEnvironmentPlugin: require('webpack4/lib/node/NodeEnvironmentPlugin'),
NodeTargetPlugin: require('webpack4/lib/node/NodeTargetPlugin'),
NodeTemplatePlugin: require('webpack4/lib/node/NodeTemplatePlugin'),
ModuleFilenameHelpers: require('webpack4/lib/ModuleFilenameHelpers'),
GraphHelpers: require('webpack4/lib/GraphHelpers'),
Module: require('webpack4/lib/Module'),
NormalModule: require('webpack4/lib/NormalModule'),
Dependency: require('webpack4/lib/Dependency'),
LibraryTemplatePlugin: require('webpack4/lib/LibraryTemplatePlugin'),
SingleEntryPlugin: require('webpack4/lib/SingleEntryPlugin'),
node: require('webpack4').node,
util: require('webpack4').util,
optimize: require('webpack4').optimize,
sources: require('webpack-sources'),
webpack: require('webpack'),
webpack: require('webpack4'),
package: {
version: require('webpack/package.json').version,
version: require('webpack4/package.json').version,
},
}
}
5 changes: 4 additions & 1 deletion packages/next/bundles/webpack/bundle5.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ module.exports = function () {
BasicEvaluatedExpression: require('webpack5/lib/javascript/BasicEvaluatedExpression'),
ModuleFilenameHelpers: require('webpack5/lib/ModuleFilenameHelpers'),
NodeTargetPlugin: require('webpack5/lib/node/NodeTargetPlugin'),
NodeTemplatePlugin: require('webpack5/lib/node/NodeTemplatePlugin'),
LibraryTemplatePlugin: require('webpack5/lib/LibraryTemplatePlugin'),
SingleEntryPlugin: require('webpack5/lib/SingleEntryPlugin'),
StringXor: require('webpack5/lib/util/StringXor'),
NormalModule: require('webpack/lib/NormalModule'),
NormalModule: require('webpack5/lib/NormalModule'),
sources: require('webpack5').sources,
webpack: require('webpack5'),
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./webpack.js').LibraryTemplatePlugin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./webpack.js').NodeTemplatePlugin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./webpack.js').SingleEntryPlugin