From 60b0267400826c6900ec4de0143d38a6269bc541 Mon Sep 17 00:00:00 2001 From: MMT-LD <28092872+MMT-LD@users.noreply.github.com> Date: Fri, 22 Jan 2021 11:16:50 +0000 Subject: [PATCH 001/100] feature/vanilla-emotion - add an example of vanilla emotion (#20228) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Mateusz Burzyński Co-authored-by: Tim Neutkens --- examples/with-emotion-vanilla/.babelrc | 4 + examples/with-emotion-vanilla/.gitignore | 34 ++++++ examples/with-emotion-vanilla/README.md | 24 +++++ examples/with-emotion-vanilla/package.json | 20 ++++ .../with-emotion-vanilla/pages/_document.js | 34 ++++++ examples/with-emotion-vanilla/pages/index.js | 30 ++++++ .../with-emotion-vanilla/shared/renderer.js | 12 +++ .../with-emotion-vanilla/shared/styles.js | 102 ++++++++++++++++++ 8 files changed, 260 insertions(+) create mode 100644 examples/with-emotion-vanilla/.babelrc create mode 100644 examples/with-emotion-vanilla/.gitignore create mode 100644 examples/with-emotion-vanilla/README.md create mode 100644 examples/with-emotion-vanilla/package.json create mode 100644 examples/with-emotion-vanilla/pages/_document.js create mode 100644 examples/with-emotion-vanilla/pages/index.js create mode 100644 examples/with-emotion-vanilla/shared/renderer.js create mode 100644 examples/with-emotion-vanilla/shared/styles.js diff --git a/examples/with-emotion-vanilla/.babelrc b/examples/with-emotion-vanilla/.babelrc new file mode 100644 index 0000000000000..d69237b3888a2 --- /dev/null +++ b/examples/with-emotion-vanilla/.babelrc @@ -0,0 +1,4 @@ +{ + "presets": [["next/babel"]], + "plugins": ["@emotion/babel-plugin"] +} diff --git a/examples/with-emotion-vanilla/.gitignore b/examples/with-emotion-vanilla/.gitignore new file mode 100644 index 0000000000000..1437c53f70bc2 --- /dev/null +++ b/examples/with-emotion-vanilla/.gitignore @@ -0,0 +1,34 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js + +# testing +/coverage + +# next.js +/.next/ +/out/ + +# production +/build + +# misc +.DS_Store +*.pem + +# debug +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# local env files +.env.local +.env.development.local +.env.test.local +.env.production.local + +# vercel +.vercel diff --git a/examples/with-emotion-vanilla/README.md b/examples/with-emotion-vanilla/README.md new file mode 100644 index 0000000000000..f43e633dfc36b --- /dev/null +++ b/examples/with-emotion-vanilla/README.md @@ -0,0 +1,24 @@ +# Emotion Vanilla Example + +Extract and inline critical css with +[emotion](https://github.com/emotion-js/emotion/tree/master/packages/emotion), +[@emotion/server](https://github.com/emotion-js/emotion/tree/master/packages/server), +[@emotion/css](https://github.com/emotion-js/emotion/tree/master/packages/css) + +## Deploy your own + +Deploy the example using [Vercel](https://vercel.com): + +[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/import/project?template=https://github.com/vercel/next.js/tree/canary/examples/with-emotion-vanilla) + +## How to use + +Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example: + +```bash +npx create-next-app --example with-emotion-vanilla with-emotion-vanilla-app +# or +yarn create next-app --example with-emotion-vanilla with-emotion-vanilla-app +``` + +Deploy it to the cloud with [Vercel](https://vercel.com/import?filter=next.js&utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)). diff --git a/examples/with-emotion-vanilla/package.json b/examples/with-emotion-vanilla/package.json new file mode 100644 index 0000000000000..4d271b1761be4 --- /dev/null +++ b/examples/with-emotion-vanilla/package.json @@ -0,0 +1,20 @@ +{ + "name": "with-emotion-vanilla", + "version": "1.0.0", + "scripts": { + "dev": "next", + "build": "next build", + "start": "next start" + }, + "license": "MIT", + "devDependencies": { + "@emotion/babel-plugin": "11.0.0" + }, + "dependencies": { + "@emotion/css": "11.0.0", + "@emotion/server": "11.0.0", + "next": "latest", + "react": "^17.0.1", + "react-dom": "^17.0.1" + } +} diff --git a/examples/with-emotion-vanilla/pages/_document.js b/examples/with-emotion-vanilla/pages/_document.js new file mode 100644 index 0000000000000..e34725067736d --- /dev/null +++ b/examples/with-emotion-vanilla/pages/_document.js @@ -0,0 +1,34 @@ +import Document, { Html, Head, Main, NextScript } from 'next/document' +import * as React from 'react' +import { renderStatic } from '../shared/renderer' +export default class AppDocument extends Document { + static async getInitialProps(ctx) { + const page = await ctx.renderPage() + const { css, ids } = await renderStatic(page.html) + const initialProps = await Document.getInitialProps(ctx) + return { + ...initialProps, + styles: ( + + {initialProps.styles} + ` + `` ) } } diff --git a/test/integration/font-optimization/pages/nonce.js b/test/integration/font-optimization/pages/nonce.js new file mode 100644 index 0000000000000..0dae87f35be2c --- /dev/null +++ b/test/integration/font-optimization/pages/nonce.js @@ -0,0 +1,19 @@ +import React from 'react' +import Head from 'next/head' + +const Page = () => { + return ( + <> + + + +
Hi!
+ + ) +} + +export default Page diff --git a/test/integration/font-optimization/test/index.test.js b/test/integration/font-optimization/test/index.test.js index 4c7d1f8abf95c..358e48c1dd643 100644 --- a/test/integration/font-optimization/test/index.test.js +++ b/test/integration/font-optimization/test/index.test.js @@ -52,6 +52,17 @@ function runTests() { ) }) + it('should pass nonce to the inlined font definition', async () => { + const html = await renderViaHTTP(appPort, '/nonce') + expect(await fsExists(builtPage('font-manifest.json'))).toBe(true) + expect(html).toContain( + '' + ) + expect(html).toMatch( + /