Skip to content

Commit

Permalink
Merge branch 'canary' into get-stream
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] committed Dec 8, 2021
2 parents b4b470d + 1a6a1e5 commit ef22faa
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build_test_deploy.yml
Expand Up @@ -735,6 +735,7 @@ jobs:
path: packages/next-swc/crates/wasm/pkg-nodejs

- run: ls packages/next-swc/crates/wasm
if: ${{needs.build.outputs.docsChange != 'docs only change'}}

- uses: actions/download-artifact@v2
if: ${{needs.build.outputs.docsChange != 'docs only change'}}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/stale.yml
Expand Up @@ -8,7 +8,7 @@ jobs:
stale:
runs-on: ubuntu-latest
steps:
- uses: actions/stale@main
- uses: actions/stale@v4
id: stale
name: 'Close stale issues with no reproduction'
with:
Expand Down
30 changes: 30 additions & 0 deletions docs/advanced-features/output-file-tracing.md
Expand Up @@ -18,7 +18,37 @@ Next.js' production server is also traced for its needed files and output at `.n

To leverage the `.nft.json` files emitted to the `.next` output directory, you can read the list of files in each trace which are relative to the `.nft.json` file and then copy them to your deployment location.

## Automatically Copying Traced Files (experimental)

Next.js can automatically create a `standalone` folder which copies only the necessary files for a production deployment including select files in `node_modules`.

To leverage this automatic copying you can enable it in your `next.config.js`:

```js
module.exports = {
experimental: {
outputStandalone: true,
},
}
```

This will create a folder at `.next/standalone` which can then be deployed on it's own without installing `node_modules`.

Additionally, a minimal `server.js` file is also output which can be used instead of `next start`. This minimal server does not handle serving the `static` directory as this should be handled by a CDN instead.

## Caveats

- While tracing in monorepo setups, the project directory is used for tracing by default. For `next build packages/web-app`, `packages/web-app` would be the tracing root and any files outside of that folder will not be included. To include files outside of this folder you can set `experimental.outputFileTracingRoot` in your `next.config.js`.

```js
// packages/web-app/next.config.js
module.exports = {
experimental: {
// this includes files from the monorepo base two directories up
outputFileTracingRoot: path.join(__dirname, '../../'),
},
}
```

- There are some cases that Next.js might fail to include required files, or might incorrectly include unused files. In those cases, you can export page configs props `unstable_includeFiles` and `unstable_excludeFiles` respectively. Each prop accepts an array of [globs](<https://en.wikipedia.org/wiki/Glob_(programming)>) relative to the project's root to either include or exclude in the trace.
- Currently, Next.js does not do anything with the emitted `.nft.json` files. The files must be read by your deployment platform, for example [Vercel](https://vercel.com), to create a minimal deployment. In a future release, a new command is planned to utilize these `.nft.json` files.
20 changes: 12 additions & 8 deletions packages/eslint-plugin-next/lib/rules/no-page-custom-font.js
Expand Up @@ -24,7 +24,7 @@ module.exports = {
page.startsWith(`${posix.sep}_document`)

let documentImportName
let localDefaultExportName
let localDefaultExportId
let exportDeclarationType

return {
Expand All @@ -43,7 +43,7 @@ module.exports = {
exportDeclarationType = node.declaration.type

if (node.declaration.type === 'FunctionDeclaration') {
localDefaultExportName = node.declaration.id.name
localDefaultExportId = node.declaration.id
return
}

Expand All @@ -52,7 +52,7 @@ module.exports = {
node.declaration.superClass &&
node.declaration.superClass.name === documentImportName
) {
localDefaultExportName = node.declaration.id.name
localDefaultExportId = node.declaration.id
}
},

Expand All @@ -66,15 +66,15 @@ module.exports = {
// if `export default <name>` is further down within the file after the
// currently traversed component, then `localDefaultExportName` will
// still be undefined
if (!localDefaultExportName) {
if (!localDefaultExportId) {
// find the top level of the module
const program = ancestors.find(
(ancestor) => ancestor.type === 'Program'
)

// go over each token to find the combination of `export default <name>`
for (let i = 0; i <= program.tokens.length - 1; i++) {
if (localDefaultExportName) {
if (localDefaultExportId) {
break
}

Expand All @@ -91,7 +91,7 @@ module.exports = {
const maybeIdentifier = program.tokens[i + 2]

if (maybeIdentifier && maybeIdentifier.type === 'Identifier') {
localDefaultExportName = maybeIdentifier.value
localDefaultExportId = { name: maybeIdentifier.value }
}
}
}
Expand All @@ -112,13 +112,13 @@ module.exports = {
if (exportDeclarationType === 'FunctionDeclaration') {
return (
ancestor.type === exportDeclarationType &&
ancestor.id.name === localDefaultExportName
isIdentifierMatch(ancestor.id, localDefaultExportId)
)
}

// function ...() {} export default ...
// class ... extends ...; export default ...
return ancestor.id && ancestor.id.name === localDefaultExportName
return isIdentifierMatch(ancestor.id, localDefaultExportId)
})

// file starts with _document and this <link /> is within the default export
Expand Down Expand Up @@ -153,3 +153,7 @@ module.exports = {
}
},
}

function isIdentifierMatch(id1, id2) {
return (id1 === null && id2 === null) || (id1 && id2 && id1.name === id2.name)
}
21 changes: 20 additions & 1 deletion packages/next/server/config.ts
@@ -1,6 +1,6 @@
import chalk from 'chalk'
import findUp from 'next/dist/compiled/find-up'
import { basename, extname, relative } from 'path'
import { basename, extname, relative, isAbsolute, resolve } from 'path'
import { pathToFileURL } from 'url'
import { Agent as HttpAgent } from 'http'
import { Agent as HttpsAgent } from 'https'
Expand Down Expand Up @@ -371,6 +371,25 @@ function assignDefaults(userConfig: { [key: string]: any }) {
)
}

if (
result.experimental?.outputFileTracingRoot &&
!isAbsolute(result.experimental.outputFileTracingRoot)
) {
result.experimental.outputFileTracingRoot = resolve(
result.experimental.outputFileTracingRoot
)
Log.warn(
`experimental.outputFileTracingRoot should be absolute, using: ${result.experimental.outputFileTracingRoot}`
)
}

if (result.experimental?.outputStandalone && !result.outputFileTracing) {
Log.warn(
`experimental.outputStandalone requires outputFileTracing not be disabled please enable it to leverage the standalone build`
)
result.experimental.outputStandalone = false
}

// TODO: Change defaultConfig type to NextConfigComplete
// so we don't need "!" here.
setHttpAgentOptions(
Expand Down
19 changes: 17 additions & 2 deletions test/unit/eslint-plugin-next/no-page-custom-font.test.ts
Expand Up @@ -105,10 +105,25 @@ ruleTester.run('no-page-custom-font', rule, {
);
}
}
export default MyDocument;`,
filename,
},
{
code: `export default function() {
return (
<Html>
<Head>
<link
href="https://fonts.googleapis.com/css2?family=Krona+One&display=swap"
rel="stylesheet"
/>
</Head>
</Html>
)
}`,
filename,
},
],

invalid: [
Expand Down Expand Up @@ -153,7 +168,7 @@ ruleTester.run('no-page-custom-font', rule, {
<link
href="https://fonts.googleapis.com/css2?family=Open+Sans"
rel="stylesheet"
/>
/>
</>
)
}
Expand Down

0 comments on commit ef22faa

Please sign in to comment.