From f9f78580531808c5dbb278a39c55a13fd85b81ab Mon Sep 17 00:00:00 2001 From: Jane Chu <7559015+janechu@users.noreply.github.com> Date: Tue, 15 Feb 2022 13:13:51 -0800 Subject: [PATCH] Add the fast-style web component (#5600) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Pull Request ## 📖 Description This branch adds the following: - A FAST Style element that will hydrate styles client side - A webpack config that will build the minimal JS for the logic for this web component so that the file `fast-style.min.js` can be referred to in server side generated HTML ### 🎫 Issues Closes #5575 ## 👩💻 Reviewer Notes If you run the test server after building all the necessary files, you can navigate to `localhost:8080/fast-style` and see the component working there. One thing is the fixture uses `shadowroot="open"` to be able to see which styles are applied to nested elements during Playwright testing. This does not need the case for actual implementation, the shadowroot can be closed, this has been manually verified. ## 📑 Test Plan The added tests are Playwright tests, I'll let reviewers comment as to whether more tests are needed, this however tests the base case of applying styles to the container of the fast-style web component. ## ✅ Checklist ### General - [ ] I have included a change request file using `$ yarn change` - [x] I have added tests for my changes. - [x] I have tested my changes. - [ ] I have updated the project documentation to reflect my changes. - [ ] I have read the [CONTRIBUTING](https://github.com/Microsoft/fast/blob/master/CONTRIBUTING.md) documentation and followed the [standards](https://www.fast.design/docs/community/code-of-conduct/#our-standards) for this project. ## ⏭ Next Steps - Integrate the FASTStyle wc to the HTML renderer #5576 --- .../web-components/fast-ssr/server/server.ts | 47 ++ .../src/fast-style/index.fixture.html | 436 ++++++++++++++++++ .../fast-ssr/src/fast-style/index.ts | 79 ++++ .../fast-ssr/test/fast-style.spec.ts | 62 +++ 4 files changed, 624 insertions(+) create mode 100644 packages/web-components/fast-ssr/src/fast-style/index.fixture.html create mode 100644 packages/web-components/fast-ssr/src/fast-style/index.ts create mode 100644 packages/web-components/fast-ssr/test/fast-style.spec.ts diff --git a/packages/web-components/fast-ssr/server/server.ts b/packages/web-components/fast-ssr/server/server.ts index e47e01fad16..866fa10b5e8 100644 --- a/packages/web-components/fast-ssr/server/server.ts +++ b/packages/web-components/fast-ssr/server/server.ts @@ -1,6 +1,9 @@ import { Readable } from "stream"; import express, { Request, Response } from "express"; +import fs from "fs"; +import path from "path"; +const __dirname = path.resolve(path.dirname("")); const PORT = 8080; function handleRequest(req: Request, res: Response) { res.set("Content-Type", "text/html"); @@ -20,6 +23,50 @@ function handleRequest(req: Request, res: Response) { }); } +function handleStyleRequest(req: Request, res: Response) { + res.set("Content-Type", "text/html"); + fs.readFile( + path.resolve(__dirname, "./src/fast-style/index.fixture.html"), + { encoding: "utf8" }, + (err, data) => { + const stream = (Readable as any).from(data); + stream.on("readable", function (this: any) { + while ((data = this.read())) { + res.write(data); + } + }); + stream.on("close", () => res.end()); + stream.on("error", (e: Error) => { + console.error(e); + process.exit(1); + }); + } + ); +} + +function handleStyleScriptRequest(req: Request, res: Response) { + res.set("Content-Type", "application/javascript"); + fs.readFile( + path.resolve(__dirname, "./dist/fast-style/index.js"), + { encoding: "utf8" }, + (err, data) => { + const stream = (Readable as any).from(data); + stream.on("readable", function (this: any) { + while ((data = this.read())) { + res.write(data); + } + }); + stream.on("close", () => res.end()); + stream.on("error", (e: Error) => { + console.error(e); + process.exit(1); + }); + } + ); +} + const app = express(); app.get("/", handleRequest); +app.get("/fast-style", handleStyleRequest); +app.get("/fast-style.js", handleStyleScriptRequest); app.listen(PORT); diff --git a/packages/web-components/fast-ssr/src/fast-style/index.fixture.html b/packages/web-components/fast-ssr/src/fast-style/index.fixture.html new file mode 100644 index 00000000000..92820fa5ab9 --- /dev/null +++ b/packages/web-components/fast-ssr/src/fast-style/index.fixture.html @@ -0,0 +1,436 @@ + + +
+ ++ Lorem ipsum dolor sit amet, consectetur adipiscing elit. + Maurisscelerisque varius ornare. Etiam convallis sollicitudin + scelerisque.Maecenas in velit vehicula, aliquet orci et, consequat + purus. Donec egetsodales lectus, vel sollicitudin ligula. + Suspendisse volutpat auctor diam,vel mattis lorem venenatis in. +
++ Lorem ipsum dolor sit amet, consectetur adipiscing elit. + Maurisscelerisque varius ornare. Etiam convallis sollicitudin + scelerisque.Maecenas in velit vehicula, aliquet orci et, consequat + purus. Donec egetsodales lectus, vel sollicitudin ligula. + Suspendisse volutpat auctor diam,vel mattis lorem venenatis in. +
++ Lorem ipsum dolor sit amet, consectetur adipiscing elit. + Maurisscelerisque varius ornare. Etiam convallis sollicitudin + scelerisque.Maecenas in velit vehicula, aliquet orci et, consequat + purus. Donec egetsodales lectus, vel sollicitudin ligula. + Suspendisse volutpat auctor diam,vel mattis lorem venenatis in. +
++ Lorem ipsum dolor sit amet, consectetur adipiscing elit. + Maurisscelerisque varius ornare. Etiam convallis sollicitudin + scelerisque.Maecenas in velit vehicula, aliquet orci et, consequat + purus. Donec egetsodales lectus, vel sollicitudin ligula. + Suspendisse volutpat auctor diam,vel mattis lorem venenatis in. +
++ Lorem ipsum dolor sit amet, consectetur adipiscing elit. + Maurisscelerisque varius ornare. Etiam convallis sollicitudin + scelerisque.Maecenas in velit vehicula, aliquet orci et, consequat + purus. Donec egetsodales lectus, vel sollicitudin ligula. + Suspendisse volutpat auctor diam,vel mattis lorem venenatis in. +
++ Lorem ipsum dolor sit amet, consectetur adipiscing elit. + Maurisscelerisque varius ornare. Etiam convallis sollicitudin + scelerisque.Maecenas in velit vehicula, aliquet orci et, consequat + purus. Donec egetsodales lectus, vel sollicitudin ligula. + Suspendisse volutpat auctor diam,vel mattis lorem venenatis in. +
++ Lorem ipsum dolor sit amet, consectetur adipiscing elit. + Maurisscelerisque varius ornare. Etiam convallis sollicitudin + scelerisque.Maecenas in velit vehicula, aliquet orci et, consequat + purus. Donec egetsodales lectus, vel sollicitudin ligula. + Suspendisse volutpat auctor diam,vel mattis lorem venenatis in. +
++ Lorem ipsum dolor sit amet, consectetur adipiscing elit. + Maurisscelerisque varius ornare. Etiam convallis sollicitudin + scelerisque.Maecenas in velit vehicula, aliquet orci et, consequat + purus. Donec egetsodales lectus, vel sollicitudin ligula. + Suspendisse volutpat auctor diam,vel mattis lorem venenatis in. +
++ Lorem ipsum dolor sit amet, consectetur adipiscing elit. + Maurisscelerisque varius ornare. Etiam convallis sollicitudin + scelerisque.Maecenas in velit vehicula, aliquet orci et, consequat + purus. Donec egetsodales lectus, vel sollicitudin ligula. + Suspendisse volutpat auctor diam,vel mattis lorem venenatis in. +
++ Lorem ipsum dolor sit amet, consectetur adipiscing elit. + Maurisscelerisque varius ornare. Etiam convallis sollicitudin + scelerisque.Maecenas in velit vehicula, aliquet orci et, consequat + purus. Donec egetsodales lectus, vel sollicitudin ligula. + Suspendisse volutpat auctor diam,vel mattis lorem venenatis in. +
+