Skip to content

Commit

Permalink
Merge branch 'canary' into x-rm-use-deferred-content-hook
Browse files Browse the repository at this point in the history
  • Loading branch information
devknoll committed Feb 18, 2022
2 parents 6edc1a7 + 924b71c commit 1e59b09
Show file tree
Hide file tree
Showing 28 changed files with 327 additions and 517 deletions.
616 changes: 151 additions & 465 deletions .github/workflows/build_test_deploy.yml

Large diffs are not rendered by default.

16 changes: 7 additions & 9 deletions docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ module.exports = {

// Handle image imports
// https://jestjs.io/docs/webpack#handling-static-assets
'^.+\\.(jpg|jpeg|png|gif|webp|avif|svg)$': `<rootDir>/__mocks__/fileMock.js`,
'^.+\\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|svg)$/i': `<rootDir>/__mocks__/fileMock.js`,

// Handle module aliases
'^@/components/(.*)$': '<rootDir>/components/$1',
Expand Down Expand Up @@ -354,21 +354,19 @@ Stylesheets and images aren't used in the tests but importing them may cause err

```js
// __mocks__/fileMock.js
module.exports = 'test-file-stub'
module.exports = {
src: '/img.jpg',
height: 24,
width: 24,
blurDataURL: 'data:image/png;base64,imagedata',
}
```

```js
// __mocks__/styleMock.js
module.exports = {}
```

If you're running into the issue `"Failed to parse src "test-file-stub" on 'next/image'"`, add a '/' to your fileMock.

```js
// __mocks__/fileMock.js
module.exports = '/test-file-stub'
```

For more information on handling static assets, please refer to the [Jest Docs](https://jestjs.io/docs/webpack#handling-static-assets).

**Optional: Extend Jest with custom matchers**
Expand Down
2 changes: 1 addition & 1 deletion errors/no-stylesheets-in-head-component.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Add the stylesheet in a custom `Document` component.

```jsx
// pages/_document.js
import Document, { Html, Head, Main, NextScript } from 'next/document'
import { Html, Head, Main, NextScript } from 'next/document'

export default function Document() {
return (
Expand Down
7 changes: 6 additions & 1 deletion examples/with-jest-babel/__mocks__/fileMock.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// Read more at "Handling stylesheets and image imports" on https://nextjs.org/docs/testing

module.exports = 'test-file-stub'
module.exports = {
src: '/img.jpg',
height: 24,
width: 24,
blurDataURL: 'data:image/png;base64,imagedata',
}
2 changes: 1 addition & 1 deletion examples/with-jest-babel/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {

// Handle image imports
// https://jestjs.io/docs/webpack#handling-static-assets
'^.+\\.(jpg|jpeg|png|gif|webp|avif|svg)$': `<rootDir>/__mocks__/fileMock.js`,
'^.+\\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|svg)$': `<rootDir>/__mocks__/fileMock.js`,

// Handle module aliases
'^@/components/(.*)$': '<rootDir>/components/$1',
Expand Down
2 changes: 1 addition & 1 deletion packages/next-swc/.cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ rustflags = [
]

[target.aarch64-unknown-linux-musl]
linker = "aarch64-linux-gnu-gcc"
linker = "aarch64-linux-musl-gcc"
rustflags = [
"-C",
"target-feature=-crt-static",
Expand Down
3 changes: 3 additions & 0 deletions packages/next-swc/crates/napi/npm/android-arm-eabi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `@next/swc-android-arm-eabi`

This is the **android-arm-eabi** binary for `@next/swc`
18 changes: 18 additions & 0 deletions packages/next-swc/crates/napi/npm/android-arm-eabi/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@next/swc-android-arm-eabi",
"version": "0.0.0",
"os": [
"android"
],
"cpu": [
"arm"
],
"main": "next-swc.android-arm-eabi.node",
"files": [
"next-swc.android-arm-eabi.node"
],
"license": "MIT",
"engines": {
"node": ">= 10"
}
}
1 change: 1 addition & 0 deletions packages/next-swc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"armv7-unknown-linux-gnueabihf",
"aarch64-apple-darwin",
"aarch64-linux-android",
"arm-linux-androideabi",
"x86_64-unknown-freebsd",
"x86_64-unknown-linux-musl",
"aarch64-unknown-linux-musl",
Expand Down
3 changes: 3 additions & 0 deletions packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1910,6 +1910,9 @@ export default async function build(
})
}

// ensure the worker is not left hanging
staticWorkers.close()

const analysisEnd = process.hrtime(analysisBegin)
telemetry.record(
eventBuildOptimize(pagePaths, {
Expand Down
7 changes: 6 additions & 1 deletion packages/next/build/jest/__mocks__/fileMock.js
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
module.exports = 'test-file-stub'
module.exports = {
src: '/img.jpg',
height: 24,
width: 24,
blurDataURL: 'data:image/png;base64,imagedata',
}
2 changes: 1 addition & 1 deletion packages/next/build/jest/jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default function nextJest(options: { dir?: string } = {}) {
'^.+\\.(css|sass|scss)$': require.resolve('./__mocks__/styleMock.js'),

// Handle image imports
'^.+\\.(jpg|jpeg|png|gif|webp|avif|svg)$': require.resolve(
'^.+\\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|svg)$': require.resolve(
`./__mocks__/fileMock.js`
),

Expand Down
7 changes: 7 additions & 0 deletions packages/next/build/webpack/loaders/next-middleware-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ export default function middlewareLoader(this: any) {
return `
import { adapter } from 'next/dist/server/web/adapter'
// The condition is true when the "process" module is provided
if (process !== global.process) {
// prefer local process but global.process has correct "env"
process.env = global.process.env;
global.process = process;
}
var mod = require(${stringifiedPagePath})
var handler = mod.middleware || mod.default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ export default async function middlewareSSRLoader(this: any) {
const reactLoadableManifest = self.__REACT_LOADABLE_MANIFEST
const rscManifest = self.__RSC_MANIFEST
if (typeof pageMod.default !== 'function') {
throw new Error('Your page must export a \`default\` component')
}
// Set server context
self.__server_context = {
page: ${JSON.stringify(page)},
Expand Down
9 changes: 9 additions & 0 deletions packages/next/lib/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,13 @@ export class Worker {
this._worker = undefined
return worker.end()
}

/**
* Quietly end the worker if it exists
*/
close(): void {
if (this._worker) {
this._worker.end()
}
}
}
2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
"@babel/traverse": "7.15.0",
"@babel/types": "7.15.0",
"@hapi/accept": "5.0.2",
"@napi-rs/cli": "1.2.1",
"@napi-rs/cli": "2.4.4",
"@napi-rs/triples": "1.0.3",
"@next/polyfill-module": "12.1.0",
"@next/polyfill-nomodule": "12.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/next/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ export default abstract class Server {
const isSSG = !!components.getStaticProps
const hasServerProps = !!components.getServerSideProps
const hasStaticPaths = !!components.getStaticPaths
const hasGetInitialProps = !!(components.Component as any).getInitialProps
const hasGetInitialProps = !!components.Component?.getInitialProps

// Toggle whether or not this is a Data request
const isDataReq = !!query._nextDataReq && (isSSG || hasServerProps)
Expand Down
8 changes: 6 additions & 2 deletions packages/next/server/load-components.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import type {
AppType,
DocumentType,
NextComponentType,
} from '../shared/lib/utils'
import {
BUILD_MANIFEST,
REACT_LOADABLE_MANIFEST,
} from '../shared/lib/constants'
import { join } from 'path'
import { requirePage } from './require'
import { BuildManifest } from './get-page-files'
import { AppType, DocumentType } from '../shared/lib/utils'
import { interopDefault } from '../lib/interop-default'
import {
PageConfig,
Expand All @@ -22,7 +26,7 @@ export type ManifestItem = {
export type ReactLoadableManifest = { [moduleId: string]: ManifestItem }

export type LoadComponentsReturnType = {
Component: React.ComponentType
Component: NextComponentType
pageConfig: PageConfig
buildManifest: BuildManifest
reactLoadableManifest: ReactLoadableManifest
Expand Down
4 changes: 2 additions & 2 deletions packages/next/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ export async function renderToHTML(
const defaultAppGetInitialProps =
App.getInitialProps === (App as any).origGetInitialProps

const hasPageGetInitialProps = !!(Component as any).getInitialProps
const hasPageGetInitialProps = !!(Component as any)?.getInitialProps

const pageIsDynamic = isDynamicRoute(pathname)

Expand All @@ -561,7 +561,7 @@ export async function renderToHTML(
'getServerSideProps',
'getStaticPaths',
]) {
if ((Component as any)[methodName]) {
if ((Component as any)?.[methodName]) {
throw new Error(
`page ${pathname} ${methodName} ${GSSP_COMPONENT_MEMBER_ERROR}`
)
Expand Down
1 change: 1 addition & 0 deletions packages/next/server/web/sandbox/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ function createContext(options: {
File,
FormData,
process: {
...polyfills.process,
env: buildEnvironmentVariablesFrom(options.env),
},
ReadableStream: polyfills.ReadableStream,
Expand Down
3 changes: 2 additions & 1 deletion packages/next/server/web/sandbox/polyfills.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Crypto as WebCrypto } from 'next/dist/compiled/@peculiar/webcrypto'
import { CryptoKey } from 'next/dist/compiled/@peculiar/webcrypto'
import { v4 as uuid } from 'next/dist/compiled/uuid'
import processPolyfill from 'next/dist/compiled/process'
import { ReadableStream } from './readable-stream'

import crypto from 'crypto'
Expand All @@ -13,7 +14,7 @@ export function btoa(str: string) {
return Buffer.from(str, 'binary').toString('base64')
}

export { CryptoKey, ReadableStream }
export { CryptoKey, ReadableStream, processPolyfill as process }

export class Crypto extends WebCrypto {
// @ts-ignore Remove once types are updated and we deprecate node 12
Expand Down
5 changes: 5 additions & 0 deletions packages/next/types/misc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@ declare module 'next/dist/compiled/comment-json' {
export = m
}

declare module 'next/dist/compiled/process' {
import m from 'process'
export = m
}

declare module 'pnp-webpack-plugin' {
import webpack from 'webpack4'

Expand Down
1 change: 1 addition & 0 deletions scripts/install-native.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import fs from 'fs-extra'
version: '1.0.0',
optionalDependencies: {
'@next/swc-android-arm64': 'canary',
'@next/swc-android-arm-eabi': 'canary',
'@next/swc-darwin-arm64': 'canary',
'@next/swc-darwin-x64': 'canary',
'@next/swc-linux-arm-gnueabihf': 'canary',
Expand Down
12 changes: 12 additions & 0 deletions test/integration/middleware/core/pages/global/_middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { NextResponse } from 'next/server'

export async function middleware(request, ev) {
console.log(process.env.MIDDLEWARE_TEST)

return NextResponse.json({
process: {
env: process.env,
nextTick: typeof process.nextTick,
},
})
}
24 changes: 24 additions & 0 deletions test/integration/middleware/core/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,30 @@ describe('Middleware base tests', () => {
}
})
})

describe('global', () => {
beforeAll(async () => {
context.appPort = await findPort()
context.app = await launchApp(context.appDir, context.appPort, {
env: {
MIDDLEWARE_TEST: 'asdf',
},
})
})

it('should contains process polyfill', async () => {
const res = await fetchViaHTTP(context.appPort, `/global`)
const json = await res.json()
expect(json).toEqual({
process: {
env: {
MIDDLEWARE_TEST: 'asdf',
},
nextTick: 'function',
},
})
})
})
})

function urlTests(_log, locale = '') {
Expand Down
Loading

0 comments on commit 1e59b09

Please sign in to comment.