Skip to content

Commit

Permalink
remove type imports
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Dec 30, 2020
1 parent ddacd42 commit 32b8989
Show file tree
Hide file tree
Showing 32 changed files with 90 additions and 119 deletions.
14 changes: 9 additions & 5 deletions packages/next/build/compiler.ts
@@ -1,13 +1,14 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import type { Stats, Configuration } from 'webpack'
import webpack from 'next/dist/compiled/webpack/webpack'

export type CompilerResult = {
errors: string[]
warnings: string[]
}

function generateStats(result: CompilerResult, stat: Stats): CompilerResult {
function generateStats(
result: CompilerResult,
stat: webpack.Stats
): CompilerResult {
const { errors, warnings } = stat.toJson('errors-warnings')
if (errors.length > 0) {
result.errors.push(...errors)
Expand All @@ -34,12 +35,15 @@ function closeCompiler(compiler: webpack.Compiler | webpack.MultiCompiler) {
}

export function runCompiler(
config: Configuration | Configuration[]
config: webpack.Configuration | webpack.Configuration[]
): Promise<CompilerResult> {
return new Promise((resolve, reject) => {
const compiler = webpack(config)
compiler.run(
(err: Error, statsOrMultiStats: { stats: Stats[] } | Stats) => {
(
err: Error,
statsOrMultiStats: { stats: webpack.Stats[] } | webpack.Stats
) => {
closeCompiler(compiler).then(() => {
if (err) {
const reason = err?.toString()
Expand Down
19 changes: 10 additions & 9 deletions packages/next/build/webpack-config.ts
Expand Up @@ -12,7 +12,6 @@ import webpack, {
init as initWebpack,
} from 'next/dist/compiled/webpack/webpack'
// eslint-disable-next-line import/no-extraneous-dependencies
import type { Configuration } from 'webpack'
import {
DOT_NEXT_ALIAS,
NEXT_PROJECT_ROOT,
Expand Down Expand Up @@ -66,14 +65,16 @@ import { NextConfig } from '../next-server/server/config'

type ExcludesFalse = <T>(x: T | false) => x is T

const devtoolRevertWarning = execOnce((devtool: Configuration['devtool']) => {
console.warn(
chalk.yellow.bold('Warning: ') +
chalk.bold(`Reverting webpack devtool to '${devtool}'.\n`) +
'Changing the webpack devtool in development mode will cause severe performance regressions.\n' +
'Read more: https://err.sh/next.js/improper-devtool'
)
})
const devtoolRevertWarning = execOnce(
(devtool: webpack.Configuration['devtool']) => {
console.warn(
chalk.yellow.bold('Warning: ') +
chalk.bold(`Reverting webpack devtool to '${devtool}'.\n`) +
'Changing the webpack devtool in development mode will cause severe performance regressions.\n' +
'Read more: https://err.sh/next.js/improper-devtool'
)
}
)

function parseJsonFile(filePath: string) {
const JSON5 = require('next/dist/compiled/json5')
Expand Down
5 changes: 2 additions & 3 deletions packages/next/build/webpack/config/blocks/base.ts
@@ -1,14 +1,13 @@
import isWslBoolean from 'next/dist/compiled/is-wsl'
import curry from 'next/dist/compiled/lodash.curry'
// eslint-disable-next-line import/no-extraneous-dependencies
import type { Configuration } from 'webpack'
import webpack from 'next/dist/compiled/webpack/webpack'
import { ConfigurationContext } from '../utils'

const isWindows = process.platform === 'win32' || isWslBoolean

export const base = curry(function base(
ctx: ConfigurationContext,
config: Configuration
config: webpack.Configuration
) {
config.mode = ctx.isDevelopment ? 'development' : 'production'
config.name = ctx.isServer ? 'server' : 'client'
Expand Down
2 changes: 1 addition & 1 deletion packages/next/build/webpack/config/blocks/css/index.ts
@@ -1,6 +1,6 @@
import curry from 'next/dist/compiled/lodash.curry'
import path from 'path'
import type webpack from 'next/dist/compiled/webpack/webpack'
import webpack from 'next/dist/compiled/webpack/webpack'
import MiniCssExtractPlugin from '../../../plugins/mini-css-extract-plugin'
import { loader, plugin } from '../../helpers'
import { ConfigurationContext, ConfigurationFn, pipe } from '../../utils'
Expand Down
@@ -1,5 +1,4 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import type webpack from 'webpack'
import webpack from 'next/dist/compiled/webpack/webpack'
import MiniCssExtractPlugin from '../../../../plugins/mini-css-extract-plugin'

export function getClientStyleLoader({
Expand Down
@@ -1,7 +1,6 @@
import loaderUtils from 'loader-utils'
import path from 'path'
// eslint-disable-next-line import/no-extraneous-dependencies
import type webpack from 'webpack'
import webpack from 'next/dist/compiled/webpack/webpack'

const regexLikeIndexModule = /(?<!pages[\\/])index\.module\.(scss|sass|css)$/

Expand Down
@@ -1,6 +1,5 @@
import { AcceptedPlugin } from 'postcss'
// eslint-disable-next-line import/no-extraneous-dependencies
import type webpack from 'webpack'
import webpack from 'next/dist/compiled/webpack/webpack'
import { ConfigurationContext } from '../../../utils'
import { getClientStyleLoader } from './client'
import { cssFileResolve } from './file-resolve'
Expand Down
@@ -1,6 +1,5 @@
import { AcceptedPlugin } from 'postcss'
// eslint-disable-next-line import/no-extraneous-dependencies
import type webpack from 'webpack'
import webpack from 'next/dist/compiled/webpack/webpack'
import { ConfigurationContext } from '../../../utils'
import { getClientStyleLoader } from './client'
import { cssFileResolve } from './file-resolve'
Expand Down
@@ -1,15 +1,14 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import type { Configuration, RuleSetRule } from 'webpack'
import webpack from 'next/dist/compiled/webpack/webpack'
import { getPostCssPlugins } from './plugins'

export async function __overrideCssConfiguration(
rootDirectory: string,
isProduction: boolean,
config: Configuration
config: webpack.Configuration
) {
const postCssPlugins = await getPostCssPlugins(rootDirectory, isProduction)

function patch(rule: RuleSetRule) {
function patch(rule: webpack.RuleSetRule) {
if (
rule.options &&
typeof rule.options === 'object' &&
Expand Down
16 changes: 9 additions & 7 deletions packages/next/build/webpack/config/helpers.ts
@@ -1,10 +1,9 @@
import curry from 'next/dist/compiled/lodash.curry'
// eslint-disable-next-line import/no-extraneous-dependencies
import type { Configuration, Plugin, RuleSetRule } from 'webpack'
import webpack from 'next/dist/compiled/webpack/webpack'

export const loader = curry(function loader(
rule: RuleSetRule,
config: Configuration
rule: webpack.RuleSetRule,
config: webpack.Configuration
) {
if (!config.module) {
config.module = { rules: [] }
Expand All @@ -23,8 +22,8 @@ export const loader = curry(function loader(
})

export const unshiftLoader = curry(function unshiftLoader(
rule: RuleSetRule,
config: Configuration
rule: webpack.RuleSetRule,
config: webpack.Configuration
) {
if (!config.module) {
config.module = { rules: [] }
Expand All @@ -42,7 +41,10 @@ export const unshiftLoader = curry(function unshiftLoader(
return config
})

export const plugin = curry(function plugin(p: Plugin, config: Configuration) {
export const plugin = curry(function plugin(
p: webpack.Plugin,
config: webpack.Configuration
) {
if (!config.plugins) {
config.plugins = []
}
Expand Down
3 changes: 1 addition & 2 deletions packages/next/build/webpack/config/utils.ts
@@ -1,5 +1,4 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import type webpack from 'webpack'
import webpack from 'next/dist/compiled/webpack/webpack'

export type ConfigurationContext = {
rootDirectory: string
Expand Down
5 changes: 2 additions & 3 deletions packages/next/build/webpack/loaders/error-loader.ts
@@ -1,10 +1,9 @@
import chalk from 'chalk'
import loaderUtils from 'loader-utils'
import path from 'path'
// eslint-disable-next-line import/no-extraneous-dependencies
import type { loader } from 'webpack'
import webpack from 'next/dist/compiled/webpack/webpack'

const ErrorLoader: loader.Loader = function () {
const ErrorLoader: webpack.loader.Loader = function () {
const options = loaderUtils.getOptions(this) || {}

const { reason = 'An unknown error has occurred' } = options
Expand Down
@@ -1,5 +1,4 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import type { loader } from 'webpack'
import webpack from 'next/dist/compiled/webpack/webpack'
import loaderUtils from 'loader-utils'
import { tracer, traceFn } from '../../tracer'

Expand All @@ -8,7 +7,7 @@ export type ClientPagesLoaderOptions = {
page: string
}

const nextClientPagesLoader: loader.Loader = function () {
const nextClientPagesLoader: webpack.loader.Loader = function () {
const span = tracer.startSpan('next-client-pages-loader')
return traceFn(span, () => {
const { absolutePagePath, page } = loaderUtils.getOptions(
Expand Down
5 changes: 2 additions & 3 deletions packages/next/build/webpack/loaders/next-data-loader.ts
@@ -1,8 +1,7 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import type { loader } from 'webpack'
import webpack from 'next/dist/compiled/webpack/webpack'
import hash from 'next/dist/compiled/string-hash'
import { basename } from 'path'
const nextDataLoader: loader.Loader = function () {
const nextDataLoader: webpack.loader.Loader = function () {
const filename = this.resourcePath
return `
import {createHook} from 'next/data'
Expand Down
5 changes: 2 additions & 3 deletions packages/next/build/webpack/loaders/next-plugin-loader.ts
@@ -1,5 +1,4 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import type { loader } from 'webpack'
import webpack from 'next/dist/compiled/webpack/webpack'
import { parse } from 'querystring'
import { PluginMetaData, getPluginId } from '../../plugins/collect-plugins'

Expand All @@ -13,7 +12,7 @@ export const pluginLoaderOptions: {
plugins: [],
}

const nextPluginLoader: loader.Loader = function () {
const nextPluginLoader: webpack.loader.Loader = function () {
const { middleware }: NextPluginLoaderQuery =
typeof this.query === 'string' ? parse(this.query.substr(1)) : this.query

Expand Down
Expand Up @@ -2,8 +2,7 @@ import devalue from 'next/dist/compiled/devalue'
import escapeRegexp from 'next/dist/compiled/escape-string-regexp'
import { join } from 'path'
import { parse } from 'querystring'
// eslint-disable-next-line import/no-extraneous-dependencies
import type { loader } from 'webpack'
import webpack from 'next/dist/compiled/webpack/webpack'
import { API_ROUTE } from '../../../../lib/constants'
import { isDynamicRoute } from '../../../../next-server/lib/router/utils'
import { __ApiPreviewProps } from '../../../../next-server/server/api-utils'
Expand Down Expand Up @@ -34,7 +33,7 @@ export type ServerlessLoaderQuery = {
i18n: string
}

const nextServerlessLoader: loader.Loader = function () {
const nextServerlessLoader: webpack.loader.Loader = function () {
const span = tracer.startSpan('next-serverless-loader')
return traceFn(span, () => {
const {
Expand Down
5 changes: 2 additions & 3 deletions packages/next/build/webpack/loaders/noop-loader.ts
@@ -1,5 +1,4 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import type { loader } from 'webpack'
import webpack from 'next/dist/compiled/webpack/webpack'

const NoopLoader: loader.Loader = (source) => source
const NoopLoader: webpack.loader.Loader = (source) => source
export default NoopLoader
6 changes: 2 additions & 4 deletions packages/next/build/webpack/plugins/build-manifest-plugin.ts
Expand Up @@ -3,8 +3,6 @@ import webpack, {
isWebpack5,
onWebpackInit,
} from 'next/dist/compiled/webpack/webpack'
// eslint-disable-next-line import/no-extraneous-dependencies
import type { Compiler, compilation as CompilationType } from 'webpack'
import sources from 'webpack-sources'
import {
BUILD_MANIFEST,
Expand Down Expand Up @@ -109,7 +107,7 @@ export default class BuildManifestPlugin {
createAssets(compilation: any, assets: any) {
const span = tracer.startSpan('NextJsBuildManifest-createassets')
return traceFn(span, () => {
const namedChunks: Map<string, CompilationType.Chunk> =
const namedChunks: Map<string, webpack.compilation.Chunk> =
compilation.namedChunks
const assetMap: DeepMutable<BuildManifest> = {
polyfillFiles: [],
Expand Down Expand Up @@ -223,7 +221,7 @@ export default class BuildManifestPlugin {
})
}

apply(compiler: Compiler) {
apply(compiler: webpack.Compiler) {
if (isWebpack5) {
compiler.hooks.make.tap('NextJsBuildManifest', (compilation) => {
// @ts-ignore TODO: Remove ignore when webpack 5 is stable
Expand Down
5 changes: 2 additions & 3 deletions packages/next/build/webpack/plugins/chunk-names-plugin.ts
@@ -1,10 +1,9 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import type { Compiler } from 'webpack'
import webpack from 'next/dist/compiled/webpack/webpack'
// This plugin mirrors webpack 3 `filename` and `chunkfilename` behavior
// This fixes https://github.com/webpack/webpack/issues/6598
// This plugin is based on https://github.com/researchgate/webpack/commit/2f28947fa0c63ccbb18f39c0098bd791a2c37090
export default class ChunkNamesPlugin {
apply(compiler: Compiler) {
apply(compiler: webpack.Compiler) {
compiler.hooks.compilation.tap(
'NextJsChunkNamesPlugin',
(compilation: any) => {
Expand Down
@@ -1,5 +1,3 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import type { compilation as CompilationType, Compiler } from 'webpack'
import webpack, {
BasicEvaluatedExpression,
isWebpack5,
Expand Down Expand Up @@ -37,12 +35,12 @@ async function minifyCss(css: string): Promise<string> {
}

export class FontStylesheetGatheringPlugin {
compiler?: Compiler
compiler?: webpack.Compiler
gatheredStylesheets: Array<string> = []
manifestContent: FontManifest = []

private parserHandler = (
factory: CompilationType.NormalModuleFactory
factory: webpack.compilation.NormalModuleFactory
): void => {
const JS_TYPES = ['auto', 'esm', 'dynamic']
// Do an extra walk per module and add interested visitors to the walk.
Expand Down Expand Up @@ -130,7 +128,7 @@ export class FontStylesheetGatheringPlugin {
}
}

public apply(compiler: Compiler) {
public apply(compiler: webpack.Compiler) {
this.compiler = compiler
compiler.hooks.normalModuleFactory.tap(
this.constructor.name,
Expand Down
5 changes: 2 additions & 3 deletions packages/next/build/webpack/plugins/jsconfig-paths-plugin.ts
Expand Up @@ -4,8 +4,7 @@
* https://github.com/microsoft/TypeScript/blob/214df64e287804577afa1fea0184c18c40f7d1ca/LICENSE.txt
*/
import path from 'path'
// eslint-disable-next-line import/no-extraneous-dependencies
import type { ResolvePlugin } from 'webpack'
import webpack from 'next/dist/compiled/webpack/webpack'
import { debug } from 'next/dist/compiled/debug'

const log = debug('next:jsconfig-paths-plugin')
Expand Down Expand Up @@ -137,7 +136,7 @@ type Paths = { [match: string]: string[] }
* Largely based on how the TypeScript compiler handles it:
* https://github.com/microsoft/TypeScript/blob/1a9c8197fffe3dace5f8dca6633d450a88cba66d/src/compiler/moduleNameResolver.ts#L1362
*/
export class JsConfigPathsPlugin implements ResolvePlugin {
export class JsConfigPathsPlugin implements webpack.ResolvePlugin {
paths: Paths
resolvedBaseUrl: string
constructor(paths: Paths, resolvedBaseUrl: string) {
Expand Down
@@ -1,25 +1,24 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import type { Compiler, compilation as CompilationType, Plugin } from 'webpack'
import webpack from 'next/dist/compiled/webpack/webpack'
import { isWebpack5 } from 'next/dist/compiled/webpack/webpack'
import { STRING_LITERAL_DROP_BUNDLE } from '../../../next-server/lib/constants'

export const ampFirstEntryNamesMap: WeakMap<
CompilationType.Compilation,
webpack.compilation.Compilation,
string[]
> = new WeakMap()

const PLUGIN_NAME = 'DropAmpFirstPagesPlugin'

// Prevents outputting client pages when they are not needed
export class DropClientPage implements Plugin {
export class DropClientPage implements webpack.Plugin {
ampPages = new Set()

apply(compiler: Compiler) {
apply(compiler: webpack.Compiler) {
compiler.hooks.compilation.tap(
PLUGIN_NAME,
(compilation: any, { normalModuleFactory }: any) => {
// Recursively look up the issuer till it ends up at the root
function findEntryModule(mod: any): CompilationType.Module | null {
function findEntryModule(mod: any): webpack.compilation.Module | null {
const queue = new Set([mod])
for (const module of queue) {
if (isWebpack5) {
Expand Down

0 comments on commit 32b8989

Please sign in to comment.