Skip to content

Commit

Permalink
Merge branch 'canary' into remove-image-all-initial-for-some-css
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] committed Oct 23, 2021
2 parents 3c5c739 + 7054096 commit a19e31b
Show file tree
Hide file tree
Showing 19 changed files with 444 additions and 69 deletions.
217 changes: 166 additions & 51 deletions packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1024,70 +1024,185 @@ export default async function build(
}

if (config.experimental.outputFileTracing) {
const globOrig =
require('next/dist/compiled/glob') as typeof import('next/dist/compiled/glob')
const glob = (pattern: string): Promise<string[]> => {
return new Promise((resolve, reject) => {
globOrig(pattern, { cwd: dir }, (err, files) => {
if (err) {
return reject(err)
}
resolve(files)
const { nodeFileTrace } =
require('next/dist/compiled/@vercel/nft') as typeof import('next/dist/compiled/@vercel/nft')

const includeExcludeSpan = nextBuildSpan.traceChild(
'apply-include-excludes'
)

await includeExcludeSpan.traceAsyncFn(async () => {
const globOrig =
require('next/dist/compiled/glob') as typeof import('next/dist/compiled/glob')
const glob = (pattern: string): Promise<string[]> => {
return new Promise((resolve, reject) => {
globOrig(pattern, { cwd: dir }, (err, files) => {
if (err) {
return reject(err)
}
resolve(files)
})
})
})
}
}

for (let page of pageKeys) {
await includeExcludeSpan
.traceChild('include-exclude', { page })
.traceAsyncFn(async () => {
const includeGlobs = pageTraceIncludes.get(page)
const excludeGlobs = pageTraceExcludes.get(page)
page = normalizePagePath(page)

for (let page of pageKeys) {
const includeGlobs = pageTraceIncludes.get(page)
const excludeGlobs = pageTraceExcludes.get(page)
page = normalizePagePath(page)
if (!includeGlobs?.length && !excludeGlobs?.length) {
return
}

if (!includeGlobs?.length && !excludeGlobs?.length) {
continue
const traceFile = path.join(
distDir,
'server/pages',
`${page}.js.nft.json`
)
const pageDir = path.dirname(traceFile)
const traceContent = JSON.parse(
await promises.readFile(traceFile, 'utf8')
)
let includes: string[] = []

if (includeGlobs?.length) {
for (const includeGlob of includeGlobs) {
const results = await glob(includeGlob)
includes.push(
...results.map((file) => {
return path.relative(pageDir, path.join(dir, file))
})
)
}
}
const combined = new Set([...traceContent.files, ...includes])

if (excludeGlobs?.length) {
const resolvedGlobs = excludeGlobs.map((exclude) =>
path.join(dir, exclude)
)
combined.forEach((file) => {
if (isMatch(path.join(pageDir, file), resolvedGlobs)) {
combined.delete(file)
}
})
}

await promises.writeFile(
traceFile,
JSON.stringify({
version: traceContent.version,
files: [...combined],
})
)
})
}
})

const traceFile = path.join(
distDir,
'server/pages',
`${page}.js.nft.json`
)
const pageDir = path.dirname(traceFile)
const traceContent = JSON.parse(
await promises.readFile(traceFile, 'utf8')
)
let includes: string[] = []

if (includeGlobs?.length) {
for (const includeGlob of includeGlobs) {
const results = await glob(includeGlob)
includes.push(
...results.map((file) => {
return path.relative(pageDir, path.join(dir, file))
// TODO: move this inside of webpack so it can be cached
// between builds. Should only need to be re-run on lockfile change
await nextBuildSpan
.traceChild('trace-next-server')
.traceAsyncFn(async () => {
let cacheKey: string | undefined
// consider all lockFiles in tree in case user accidentally
// has both package-lock.json and yarn.lock
const lockFiles: string[] = (
await Promise.all(
['package-lock.json', 'yarn.lock', 'pnpm-lock.yaml'].map((file) =>
findUp(file, { cwd: dir })
)
)
).filter(Boolean) as any // TypeScript doesn't like this filter

const nextServerTraceOutput = path.join(
distDir,
'next-server.js.nft.json'
)
const cachedTracePath = path.join(
distDir,
'cache/next-server.js.nft.json'
)

if (lockFiles.length > 0) {
const cacheHash = (
require('crypto') as typeof import('crypto')
).createHash('sha256')

cacheHash.update(require('next/package').version)

await Promise.all(
lockFiles.map(async (lockFile) => {
cacheHash.update(await promises.readFile(lockFile))
})
)
cacheKey = cacheHash.digest('hex')

try {
const existingTrace = JSON.parse(
await promises.readFile(cachedTracePath, 'utf8')
)

if (existingTrace.cacheKey === cacheKey) {
await promises.copyFile(cachedTracePath, nextServerTraceOutput)
return
}
} catch (_) {}
}
}
const combined = new Set([...traceContent.files, ...includes])

if (excludeGlobs?.length) {
const resolvedGlobs = excludeGlobs.map((exclude) =>
path.join(dir, exclude)
const root = path.parse(dir).root
const serverResult = await nodeFileTrace(
[require.resolve('next/dist/server/next-server')],
{
base: root,
processCwd: dir,
ignore: [
'**/next/dist/pages/**/*',
'**/next/dist/server/image-optimizer.js',
'**/next/dist/compiled/@ampproject/toolbox-optimizer/**/*',
'**/next/dist/server/lib/squoosh/**/*.wasm',
'**/next/dist/compiled/webpack/(bundle4|bundle5).js',
'**/node_modules/react/**/*.development.js',
'**/node_modules/react-dom/**/*.development.js',
'**/node_modules/use-subscription/**/*.development.js',
'**/node_modules/sharp/**/*',
'**/node_modules/webpack5/**/*',
],
}
)
combined.forEach((file) => {
if (isMatch(path.join(pageDir, file), resolvedGlobs)) {
combined.delete(file)

const tracedFiles = new Set()

serverResult.fileList.forEach((file) => {
const reason = serverResult.reasons.get(file)

if (reason?.type === 'initial') {
return
}
tracedFiles.add(
path.relative(distDir, path.join(root, file)).replace(/\\/g, '/')
)
})
}

await promises.writeFile(
traceFile,
JSON.stringify({
version: traceContent.version,
files: [...combined],
})
)
}
await promises.writeFile(
nextServerTraceOutput,
JSON.stringify({
version: 1,
cacheKey,
files: [...tracedFiles],
} as {
version: number
files: string[]
})
)
await promises.unlink(cachedTracePath).catch(() => {})
await promises
.copyFile(nextServerTraceOutput, cachedTracePath)
.catch(() => {})
})
}

if (serverPropsPages.size > 0 || ssgPages.size > 0) {
Expand Down
12 changes: 12 additions & 0 deletions packages/next/build/swc/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/next/build/swc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ crate-type = ["cdylib", "rlib"]
[dependencies]
anyhow = "1.0"
backtrace = "0.3"
chrono = "0.4"
easy-error = "1.0.0"
napi = { version = "1", features = ["serde-json"] }
napi-derive = "1"
Expand Down
11 changes: 11 additions & 0 deletions packages/next/build/swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub mod hook_optimizer;
pub mod minify;
pub mod next_dynamic;
pub mod next_ssg;
pub mod page_config;
pub mod styled_jsx;
mod transform;
mod util;
Expand All @@ -60,8 +61,14 @@ pub struct TransformOptions {
#[serde(default)]
pub disable_next_ssg: bool,

#[serde(default)]
pub disable_page_config: bool,

#[serde(default)]
pub pages_dir: Option<PathBuf>,

#[serde(default)]
pub is_development: bool,
}

pub fn custom_before_pass(name: &FileName, opts: &TransformOptions) -> impl Fold {
Expand All @@ -71,6 +78,10 @@ pub fn custom_before_pass(name: &FileName, opts: &TransformOptions) -> impl Fold
Optional::new(next_ssg::next_ssg(), !opts.disable_next_ssg),
amp_attributes::amp_attributes(),
next_dynamic::next_dynamic(name.clone(), opts.pages_dir.clone()),
Optional::new(
page_config::page_config(opts.is_development),
!opts.disable_page_config
)
)
}

Expand Down

0 comments on commit a19e31b

Please sign in to comment.