Unverified Commit bbf705f2 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Simplify Build Output (#9281)

* Simplify Build Output

* Remove noop tests

* Drop ⚡

* Remove bolding
parent 21f0db81
...@@ -15,10 +15,22 @@ module.exports = withCSS( ...@@ -15,10 +15,22 @@ module.exports = withCSS(
// if they live under a 'fonts' or 'pficon' directory // if they live under a 'fonts' or 'pficon' directory
include: [ include: [
path.resolve(__dirname, 'node_modules/patternfly/dist/fonts'), path.resolve(__dirname, 'node_modules/patternfly/dist/fonts'),
path.resolve(__dirname, 'node_modules/@patternfly/react-core/dist/styles/assets/fonts'), path.resolve(
path.resolve(__dirname, 'node_modules/@patternfly/react-core/dist/styles/assets/pficon'), __dirname,
path.resolve(__dirname, 'node_modules/@patternfly/patternfly/assets/fonts'), 'node_modules/@patternfly/react-core/dist/styles/assets/fonts'
path.resolve(__dirname, 'node_modules/@patternfly/patternfly/assets/pficon') ),
path.resolve(
__dirname,
'node_modules/@patternfly/react-core/dist/styles/assets/pficon'
),
path.resolve(
__dirname,
'node_modules/@patternfly/patternfly/assets/fonts'
),
path.resolve(
__dirname,
'node_modules/@patternfly/patternfly/assets/pficon'
)
], ],
use: { use: {
loader: 'file-loader', loader: 'file-loader',
......
...@@ -45,7 +45,6 @@ import { ...@@ -45,7 +45,6 @@ import {
printTreeView, printTreeView,
} from './utils' } from './utils'
import getBaseWebpackConfig from './webpack-config' import getBaseWebpackConfig from './webpack-config'
import { getPageChunks } from './webpack/plugins/chunk-graph-plugin'
import { writeBuildId } from './write-build-id' import { writeBuildId } from './write-build-id'
const fsAccess = promisify(fs.access) const fsAccess = promisify(fs.access)
...@@ -347,8 +346,6 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -347,8 +346,6 @@ export default async function build(dir: string, conf = null): Promise<void> {
const analysisBegin = process.hrtime() const analysisBegin = process.hrtime()
await Promise.all( await Promise.all(
pageKeys.map(async page => { pageKeys.map(async page => {
const chunks = getPageChunks(page)
const actualPage = page === '/' ? '/index' : page const actualPage = page === '/' ? '/index' : page
const size = await getPageSizeInKb( const size = await getPageSizeInKb(
actualPage, actualPage,
...@@ -434,7 +431,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -434,7 +431,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
} }
} }
pageInfos.set(page, { size, chunks, serverBundle, static: isStatic }) pageInfos.set(page, { size, serverBundle, static: isStatic })
}) })
) )
staticCheckWorkers.end() staticCheckWorkers.end()
......
...@@ -2,16 +2,15 @@ import chalk from 'chalk' ...@@ -2,16 +2,15 @@ import chalk from 'chalk'
import fs from 'fs' import fs from 'fs'
import textTable from 'next/dist/compiled/text-table' import textTable from 'next/dist/compiled/text-table'
import path from 'path' import path from 'path'
import { isValidElementType } from 'react-is'
import stripAnsi from 'strip-ansi' import stripAnsi from 'strip-ansi'
import { promisify } from 'util' import { promisify } from 'util'
import { isValidElementType } from 'react-is' import { SPR_GET_INITIAL_PROPS_CONFLICT } from '../lib/constants'
import prettyBytes from '../lib/pretty-bytes' import prettyBytes from '../lib/pretty-bytes'
import { recursiveReadDir } from '../lib/recursive-readdir' import { recursiveReadDir } from '../lib/recursive-readdir'
import { getPageChunks } from './webpack/plugins/chunk-graph-plugin'
import { isDynamicRoute } from '../next-server/lib/router/utils/is-dynamic'
import { getRouteMatcher, getRouteRegex } from '../next-server/lib/router/utils' import { getRouteMatcher, getRouteRegex } from '../next-server/lib/router/utils'
import { SPR_GET_INITIAL_PROPS_CONFLICT } from '../lib/constants' import { isDynamicRoute } from '../next-server/lib/router/utils/is-dynamic'
const fsStatPromise = promisify(fs.stat) const fsStatPromise = promisify(fs.stat)
const fileStats: { [k: string]: Promise<fs.Stats> } = {} const fileStats: { [k: string]: Promise<fs.Stats> } = {}
...@@ -36,7 +35,6 @@ export function collectPages( ...@@ -36,7 +35,6 @@ export function collectPages(
export interface PageInfo { export interface PageInfo {
isAmp?: boolean isAmp?: boolean
size: number size: number
chunks?: ReturnType<typeof getPageChunks>
static?: boolean static?: boolean
serverBundle: string serverBundle: string
} }
...@@ -57,7 +55,7 @@ export function printTreeView( ...@@ -57,7 +55,7 @@ export function printTreeView(
} }
const messages: string[][] = [ const messages: string[][] = [
['Page', 'Size', 'Files', 'Packages'].map(entry => chalk.underline(entry)), ['Page', 'Size'].map(entry => chalk.underline(entry)),
] ]
list list
...@@ -79,22 +77,18 @@ export function printTreeView( ...@@ -79,22 +77,18 @@ export function printTreeView(
item.startsWith('/_') item.startsWith('/_')
? ' ' ? ' '
: pageInfo && pageInfo.static : pageInfo && pageInfo.static
? chalk.bold('⚡') ? '*'
: serverless : serverless
? 'λ' ? 'λ'
: 'σ' : 'σ'
} ${item}`, } ${item}`,
...(pageInfo pageInfo
? [ ? pageInfo.isAmp
pageInfo.isAmp ? chalk.cyan('AMP')
? chalk.cyan('AMP') : pageInfo.size >= 0
: pageInfo.size >= 0 ? getPrettySize(pageInfo.size)
? getPrettySize(pageInfo.size) : ''
: '', : '',
pageInfo.chunks ? pageInfo.chunks.internal.size.toString() : '',
pageInfo.chunks ? pageInfo.chunks.external.size.toString() : '',
]
: ['', '', '']),
]) ])
}) })
...@@ -124,11 +118,7 @@ export function printTreeView( ...@@ -124,11 +118,7 @@ export function printTreeView(
'getInitialProps' 'getInitialProps'
)})`, )})`,
], ],
[ ['*', '(Static File)', 'page was prerendered as static HTML'],
chalk.bold('⚡'),
'(Static File)',
'page was prerendered as static HTML',
],
], ],
{ {
align: ['l', 'l', 'l'], align: ['l', 'l', 'l'],
......
...@@ -25,8 +25,14 @@ import { ...@@ -25,8 +25,14 @@ import {
} from '../next-server/lib/constants' } from '../next-server/lib/constants'
import { findPageFile } from '../server/lib/find-page-file' import { findPageFile } from '../server/lib/find-page-file'
import { WebpackEntrypoints } from './entries' import { WebpackEntrypoints } from './entries'
import {
collectPlugins,
PluginMetaData,
VALID_MIDDLEWARE,
} from './plugins/collect-plugins'
// @ts-ignore: JS file
import { pluginLoaderOptions } from './webpack/loaders/next-plugin-loader'
import BuildManifestPlugin from './webpack/plugins/build-manifest-plugin' import BuildManifestPlugin from './webpack/plugins/build-manifest-plugin'
import { ChunkGraphPlugin } from './webpack/plugins/chunk-graph-plugin'
import ChunkNamesPlugin from './webpack/plugins/chunk-names-plugin' import ChunkNamesPlugin from './webpack/plugins/chunk-names-plugin'
import { CssMinimizerPlugin } from './webpack/plugins/css-minimizer-plugin' import { CssMinimizerPlugin } from './webpack/plugins/css-minimizer-plugin'
import { importAutoDllPlugin } from './webpack/plugins/dll-import' import { importAutoDllPlugin } from './webpack/plugins/dll-import'
...@@ -35,17 +41,10 @@ import NextEsmPlugin from './webpack/plugins/next-esm-plugin' ...@@ -35,17 +41,10 @@ import NextEsmPlugin from './webpack/plugins/next-esm-plugin'
import NextJsSsrImportPlugin from './webpack/plugins/nextjs-ssr-import' import NextJsSsrImportPlugin from './webpack/plugins/nextjs-ssr-import'
import NextJsSSRModuleCachePlugin from './webpack/plugins/nextjs-ssr-module-cache' import NextJsSSRModuleCachePlugin from './webpack/plugins/nextjs-ssr-module-cache'
import PagesManifestPlugin from './webpack/plugins/pages-manifest-plugin' import PagesManifestPlugin from './webpack/plugins/pages-manifest-plugin'
// @ts-ignore: JS file
import { pluginLoaderOptions } from './webpack/loaders/next-plugin-loader'
import { ProfilingPlugin } from './webpack/plugins/profiling-plugin' import { ProfilingPlugin } from './webpack/plugins/profiling-plugin'
import { ReactLoadablePlugin } from './webpack/plugins/react-loadable-plugin' import { ReactLoadablePlugin } from './webpack/plugins/react-loadable-plugin'
import { ServerlessPlugin } from './webpack/plugins/serverless-plugin' import { ServerlessPlugin } from './webpack/plugins/serverless-plugin'
import { TerserPlugin } from './webpack/plugins/terser-webpack-plugin/src/index' import { TerserPlugin } from './webpack/plugins/terser-webpack-plugin/src/index'
import {
collectPlugins,
VALID_MIDDLEWARE,
PluginMetaData,
} from './plugins/collect-plugins'
type ExcludesFalse = <T>(x: T | false) => x is T type ExcludesFalse = <T>(x: T | false) => x is T
...@@ -829,11 +828,6 @@ export default async function getBaseWebpackConfig( ...@@ -829,11 +828,6 @@ export default async function getBaseWebpackConfig(
filename: REACT_LOADABLE_MANIFEST, filename: REACT_LOADABLE_MANIFEST,
}), }),
!isServer && new DropClientPage(), !isServer && new DropClientPage(),
new ChunkGraphPlugin(buildId, {
dir,
distDir,
isServer,
}),
// Moment.js is an extremely popular library that bundles large locale files // Moment.js is an extremely popular library that bundles large locale files
// by default due to how Webpack interprets its code. This is a practical // by default due to how Webpack interprets its code. This is a practical
// solution that requires the user to opt into importing specific locales. // solution that requires the user to opt into importing specific locales.
......
import { CLIENT_STATIC_FILES_RUNTIME_MAIN } from '../../../next-server/lib/constants'
import path from 'path'
import { parse } from 'querystring'
import { Compiler, Plugin } from 'webpack'
type StringDictionary = { [pageName: string]: string[] }
const manifest: {
sharedFiles: string[]
pages: StringDictionary
pageChunks: StringDictionary
chunks: StringDictionary
} = {
sharedFiles: [],
pages: {},
pageChunks: {},
chunks: {},
}
const pageModules: StringDictionary = {}
export function getPageChunks(
page: string
):
| {
external: Set<String>
internal: Set<String>
}
| undefined {
if (!manifest.pages[page] && !pageModules[page]) {
return
}
const external = new Set<string>() // from node_modules
const internal = new Set<string>() // from project
;[...(manifest.pages[page] || []), ...(pageModules[page] || [])].map(mod => {
mod = mod.replace(/\\/g, '/')
if (mod.match(/(next-server|next)\//)) {
return null
}
if (mod.includes('node_modules/')) {
if (
mod.match(
/node_modules\/(@babel|core-js|styled-jsx|string-hash|object-assign|process|react|react-dom|scheduler|regenerator-runtime|webpack|node-libs-browser)\//
)
) {
return null
}
mod = mod.split('node_modules/')[1].split('/')[0]
if (external.has(mod)) {
return null
}
external.add(mod)
return mod
}
// don't include the page itself
if (mod.includes(`pages${page === '/' ? '/index' : page}`)) {
return null
}
// is local e.g. ../components/Layout
if (internal.has(mod)) {
return null
}
internal.add(mod)
return mod
})
return {
external,
internal,
}
}
function getFiles(dir: string, modules: any[]): string[] {
if (!(modules && modules.length)) {
return []
}
function getFileByIdentifier(id: string) {
if (id.startsWith('external ') || id.startsWith('multi ')) {
return null
}
let n
if ((n = id.lastIndexOf('!')) !== -1) {
id = id.substring(n + 1)
}
if (id && !path.isAbsolute(id)) {
id = path.resolve(dir, id)
}
return id
}
return modules
.reduce(
(acc: any[], val: any) =>
val.modules
? acc.concat(getFiles(dir, val.modules))
: (acc.push(
getFileByIdentifier(
typeof val.identifier === 'function'
? val.identifier()
: val.identifier
)
),
acc),
[]
)
.filter(Boolean)
}
export class ChunkGraphPlugin implements Plugin {
private buildId: string
private dir: string
private distDir: string
private isServer: boolean
constructor(
buildId: string,
{
dir,
distDir,
isServer,
}: {
dir: string
distDir: string
isServer: boolean
}
) {
this.buildId = buildId
this.dir = dir
this.distDir = distDir
this.isServer = isServer
}
apply(compiler: Compiler) {
const { dir } = this
compiler.hooks.emit.tap('ChunkGraphPlugin', compilation => {
const sharedFiles = [] as string[]
const sharedChunks = [] as string[]
const pages: StringDictionary = {}
const pageChunks: StringDictionary = {}
compilation.chunks.forEach(chunk => {
if (!chunk.hasEntryModule()) {
return
}
const chunkModules = new Map<any, any>()
const queue = new Set<any>(chunk.groupsIterable)
const chunksProcessed = new Set<any>()
const involvedChunks = new Set<string>()
for (const chunkGroup of queue) {
for (const chunk of chunkGroup.chunks) {
chunk.files.forEach((file: string) => involvedChunks.add(file))
if (!chunksProcessed.has(chunk)) {
chunksProcessed.add(chunk)
for (const m of chunk.modulesIterable) {
chunkModules.set(m.id, m)
}
}
}
for (const child of chunkGroup.childrenIterable) {
queue.add(child)
}
}
const modules = [...chunkModules.values()]
const nodeModules: string[] = []
const files = getFiles(dir, modules)
// we don't care about node_modules (yet) because we invalidate the
// entirety of flying shuttle on package changes
.filter(val => {
// store them for build tree stats though
const isModule = val.includes('node_modules')
if (isModule) nodeModules.push(val)
return !isModule
})
// build artifacts shouldn't be considered, so we ensure all paths
// are outside of this directory
.filter(val => path.relative(this.distDir, val).startsWith('..'))
// convert from absolute path to be portable across operating systems
// and directories
.map(f => path.relative(dir, f))
let pageName: string | undefined
if (chunk.entryModule && chunk.entryModule.loaders) {
const entryLoader = chunk.entryModule.loaders.find(
({
loader,
options,
}: {
loader?: string | null
options?: string | null
}) => loader && loader.match(/next-(\w+-)+loader/) && options
)
if (entryLoader) {
const { page } = parse(entryLoader.options)
if (typeof page === 'string' && page) {
pageName = page
}
}
}
if (pageName) {
if (
pageName === '/_app' ||
pageName === '/_error' ||
pageName === '/_document'
) {
sharedFiles.push(...files)
sharedChunks.push(...involvedChunks)
} else {
pages[pageName] = files
pageChunks[pageName] = [...involvedChunks]
}
pageModules[pageName] = nodeModules
} else {
if (chunk.name === CLIENT_STATIC_FILES_RUNTIME_MAIN) {
sharedFiles.push(...files)
sharedChunks.push(...involvedChunks)
} else {
manifest.chunks[chunk.name] = [
...new Set([...(manifest.chunks[chunk.name] || []), ...files]),
].sort()
}
}
})
const getLambdaChunk = (name: string) =>
name.includes(this.buildId)
? name
.replace(new RegExp(`${this.buildId}[\\/\\\\]`), 'client/')
.replace(/[.]js$/, `.${this.buildId}.js`)
: name
manifest.sharedFiles = [
...new Set([...(manifest.sharedFiles || []), ...sharedFiles]),
].sort()
for (const page in pages) {
manifest.pages[page] = [
...new Set([...(manifest.pages[page] || []), ...pages[page]]),
].sort()
// There's no chunks to save from serverless bundles
if (!this.isServer) {
manifest.pageChunks[page] = [
...new Set([
...(manifest.pageChunks[page] || []),
...pageChunks[page],
...pageChunks[page].map(getLambdaChunk),
...sharedChunks,
...sharedChunks.map(getLambdaChunk),
]),
].sort()
}
}
})
}
}
import fetch from 'isomorphic-unfetch'
console.log(fetch)
export default () => <p>Hello world</p>
/* eslint-env jest */
/* global jasmine */
import { join } from 'path'
import { nextBuild } from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 1
const appDir = join(__dirname, '../react-site')
describe('Build Stats Output', () => {
it('Shows correct package count in output', async () => {
const { stdout } = await nextBuild(appDir, undefined, { stdout: true })
expect(stdout).toMatch(/\/something .*?2/)
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment