Skip to content

Commit 618ed15

Browse files
committed
Add additional example
1 parent 3815bf5 commit 618ed15

27 files changed

Lines changed: 521 additions & 4 deletions

lib/esbuild-plugin-react18-css/__tests__/esbuild.test.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from "node:fs";
22
import path from "node:path";
3-
import { describe, test, beforeAll, afterAll } from "vitest";
3+
import { describe, test, beforeAll } from "vitest";
44
import esbuild from "esbuild";
55
import cssPlugin from "../src";
66
import glob from "tiny-glob";
@@ -28,7 +28,7 @@ describe("Test plugin with esbuild", async () => {
2828
});
2929

3030
test("Should contain -moz-", ({ expect }) => {
31-
const test = fs.readFileSync(path.resolve(exampleBuildDir, "server", "index.css"), "utf-8");
31+
const test = fs.readFileSync(path.resolve(exampleBuildDir, "index.css"), "utf-8");
3232
expect(/-moz-/.test(test)).toBe(true);
3333
});
3434
});
@@ -66,3 +66,31 @@ describe("Test plugin with esbuild and options", async () => {
6666
expect(/-moz-/.test(test)).toBe(false);
6767
});
6868
});
69+
70+
describe("Only server styles", () => {
71+
const exampleBuildDir = path.resolve(process.cwd(), "test-build1");
72+
73+
beforeAll(async () => {
74+
await esbuild.build({
75+
format: "cjs",
76+
target: "es2019",
77+
sourcemap: false,
78+
bundle: true,
79+
minify: true,
80+
plugins: [cssPlugin()],
81+
entryPoints: await glob("../example2/src/**/*.*"),
82+
external: ["react", "react-dom"],
83+
outdir: "./test-build1",
84+
});
85+
});
86+
87+
test(`Test CSS Class Hash`, ({ expect }) => {
88+
const text = fs.readFileSync(path.resolve(exampleBuildDir, "server", "index.js"), "utf-8");
89+
expect(/{fork:["'][^"']*fork[^"']*["']/.test(text)).toBe(true);
90+
});
91+
92+
test("Should contain -moz-", ({ expect }) => {
93+
const test = fs.readFileSync(path.resolve(exampleBuildDir, "index.css"), "utf-8");
94+
expect(/-moz-/.test(test)).toBe(true);
95+
});
96+
});

lib/esbuild-plugin-react18-css/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ function generateCombinedCSS(result: BuildResult) {
3737
indexCSSFilePath = serverCSSFile?.path.replace(`server${path.sep}`, "") ?? "";
3838
}
3939

40-
console.log({ indexCSSFilePath });
41-
4240
if (indexCSSFile) indexCSSFile.contents = new TextEncoder().encode(combinedCSS);
4341
else
4442
result.outputFiles?.push({

lib/example2/.eslintrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
extends: ["custom/react-internal"],
3+
};

lib/example2/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# tsup build
2+
dist

lib/example2/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Fork Me React.js component
2+
3+
A simple fork me component for React.js projects!
4+
5+
## Install
6+
7+
```bash
8+
$ pnpm add @mayank1513/fork-me
9+
# or
10+
$ npm install @mayank1513/fork-me
11+
# or
12+
$ yarn add @mayank1513/fork-me
13+
```
14+
15+
## Add on your page
16+
17+
```ts
18+
<ForkMe gitHubUrl="https://github.com/mayank1513/turborepo-template" />
19+
// and
20+
<StarMe gitHubUrl="https://github.com/mayank1513/turborepo-template" />
21+
```
22+
23+
You need to import styles for ForkMe component
24+
25+
```ts
26+
import "@mayank1513/fork-me/server/index.css";
27+
```
28+
29+
## Optional parameters
30+
31+
```ts
32+
text?: string; // replace the Fork me on GitHub text
33+
width?: string | number; // if you need to adjust the width of the ribbon (length)
34+
height?: string | number; // to adjust height of the ribbon
35+
bgColor?: string; // background color
36+
textColor?: string; // text color
37+
```
38+
39+
> This package also serves as an example to demonstrate how to build and publish `React.js` library compatible with React Server Components

lib/example2/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"use client";
2+
3+
/** to make sure import statements remain same for monorepo setup and install via npm */
4+
export * from "./src/client";

lib/example2/package.json

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"name": "esbuild-plugin-react18-css-example2",
3+
"author": "Mayank Kumar Chaudhari <https://mayank-chaudhari.vercel.app>",
4+
"private": true,
5+
"version": "0.0.0",
6+
"description": "An intuitive React.js fork me ribbon component for promoting open source project forks.",
7+
"main": "./index.ts",
8+
"types": "./index.ts",
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/react18-tools/esbuild-plugin-react18-css.git"
12+
},
13+
"bugs": {
14+
"url": "https://github.com/react18-tools/esbuild-plugin-react18-css/issues"
15+
},
16+
"homepage": "https://github.com/react18-tools/esbuild-plugin-react18-css/tree/main/lib/fork-me#readme",
17+
"sideEffects": false,
18+
"license": "MIT",
19+
"scripts": {
20+
"test": "vitest run --coverage",
21+
"build": "tsup src && tsc -p tsconfig-build.json && node touchup.js",
22+
"doc": "cp -f ../../README.md README.md && typedoc",
23+
"publish-package": "cd dist && npm publish --provenance --access public",
24+
"lint": "eslint ."
25+
},
26+
"devDependencies": {
27+
"@testing-library/react": "^14.2.1",
28+
"@turbo/gen": "^1.12.4",
29+
"@types/node": "^20.11.22",
30+
"@types/react": "^18.2.60",
31+
"@types/react-dom": "^18.2.19",
32+
"@vitejs/plugin-react": "^4.2.1",
33+
"@vitest/coverage-v8": "^1.3.1",
34+
"esbuild-plugin-css-module": "^0.0.1",
35+
"esbuild-plugin-react18": "^0.0.7",
36+
"eslint-config-custom": "workspace:*",
37+
"esbuild-plugin-react18-css": "workspace:*",
38+
"jsdom": "^24.0.0",
39+
"octokit": "^3.1.2",
40+
"react": "^18.2.0",
41+
"tsconfig": "workspace:*",
42+
"tsup": "^8.0.2",
43+
"typedoc": "^0.25.9",
44+
"typescript": "^5.3.3",
45+
"vite-tsconfig-paths": "^4.3.1",
46+
"vitest": "^1.3.1"
47+
},
48+
"peerDependencies": {
49+
"@types/react": "16.8 - 18",
50+
"next": "10 - 14",
51+
"react": "16.8 - 18"
52+
},
53+
"peerDependenciesMeta": {
54+
"next": {
55+
"optional": true
56+
}
57+
},
58+
"funding": {
59+
"type": "github",
60+
"url": "https://github.com/sponsors/mayank1513"
61+
},
62+
"keywords": [
63+
"fork-me",
64+
"GitHub",
65+
"nextjs",
66+
"nextjs13",
67+
"react",
68+
"react18",
69+
"react-server-components",
70+
"react-client-components",
71+
"typescript",
72+
"javascript",
73+
"mayank1513",
74+
"turborepo-trmplate"
75+
]
76+
}

lib/example2/server.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/** to make sure import statements remain same for monorepo setup and install via npm */
2+
3+
export * from "./src/server";

lib/example2/src/client/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use client";
2+
/**
3+
* need to export server components and client components from separate files as
4+
* directive on top of the file from which component is imported takes effect.
5+
* i.e., server component re-exported from file with "use client" will behave as client component
6+
* */
7+
8+
// client component exports
9+
export * from "./star-me";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use client";
2+
3+
export * from "./star-me";

0 commit comments

Comments
 (0)