@plumeria/compiler 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "@plumeria/compiler",
3
+ "version": "0.1.0",
4
+ "description": "Compiler for Plumeria that build and optimizes",
5
+ "keywords": [
6
+ "react",
7
+ "css",
8
+ "css-in-js",
9
+ "plumeria",
10
+ "styling"
11
+ ],
12
+ "author": "Refirst",
13
+ "repository": "github:zss-in-js/plumeria",
14
+ "license": "MIT",
15
+ "dependencies": {
16
+ "globby": "^14.0.2",
17
+ "postcss": "^8.4.49",
18
+ "postcss-combine-duplicated-selectors": "^10.0.3",
19
+ "tsx": "^4.19.2"
20
+ }
21
+ }
@@ -0,0 +1,13 @@
1
+ import { writeFileSync } from "fs";
2
+ import { join } from "path";
3
+
4
+ export const cleanUp = async () => {
5
+ const createFilePath = join(__dirname, "../../core/dist/styles/create.css");
6
+ const globalFilePath = join(__dirname, "../../core/dist/styles/global.css");
7
+ try {
8
+ writeFileSync(createFilePath, "", "utf-8");
9
+ writeFileSync(globalFilePath, "", "utf-8");
10
+ } catch (err) {
11
+ console.error("An error occurred:", err);
12
+ }
13
+ };
package/src/index.ts ADDED
@@ -0,0 +1,69 @@
1
+ import * as path from "path";
2
+ import * as fs from "fs";
3
+ import ts from "typescript";
4
+ import { globby } from "globby";
5
+ import { cleanUp } from "./clean-up";
6
+ import { buildCreate } from "../../core/dist/method/create-build-helper.js";
7
+ import { buildGlobal } from "../../core/dist/method/global-build-helper.js";
8
+ import postcss from "postcss";
9
+ import combineSelectors from "postcss-combine-duplicated-selectors";
10
+
11
+ function isCSS(filePath: string): boolean {
12
+ const content = fs.readFileSync(filePath, "utf8");
13
+ const sourceFile = ts.createSourceFile(
14
+ filePath,
15
+ content,
16
+ ts.ScriptTarget.Latest,
17
+ true
18
+ );
19
+
20
+ const checker = (node: ts.Node): boolean => {
21
+ if (ts.isPropertyAccessExpression(node) && ts.isIdentifier(node.name)) {
22
+ const expressionText = node.expression.getText(sourceFile);
23
+ const methodName = node.name.getText(sourceFile);
24
+ return (
25
+ expressionText === "css" && ["create", "global"].includes(methodName)
26
+ );
27
+ }
28
+ return ts.forEachChild(node, checker) || false;
29
+ };
30
+
31
+ return checker(sourceFile);
32
+ }
33
+
34
+ async function getAppRoot(): Promise<string> {
35
+ const threeLevelsUp = path.join(process.cwd(), "../../../../..");
36
+ return fs.existsSync(path.join(threeLevelsUp, "node_modules/.pnpm"))
37
+ ? path.join(process.cwd(), "../../../../../")
38
+ : path.join(process.cwd(), "../../");
39
+ }
40
+
41
+ async function optimizeCSS() {
42
+ const cssPath = path.join(__dirname, "../../core/dist/styles/global.css");
43
+ const cssContent = fs.readFileSync(cssPath, "utf8");
44
+ const result = postcss([
45
+ combineSelectors({ removeDuplicatedProperties: true }),
46
+ ]).process(cssContent, { from: cssPath, to: cssPath });
47
+ fs.writeFileSync(cssPath, result.css);
48
+ }
49
+
50
+ (async () => {
51
+ await cleanUp();
52
+ const appRoot = await getAppRoot();
53
+ const files = await globby([path.join(appRoot, "**/*.{js,jsx,ts,tsx}")], {
54
+ ignore: ["**/main.{js,ts}/**", "**/.next/**", "**/node_modules/**"],
55
+ });
56
+ const styleFiles = files.filter(isCSS);
57
+ const importPromises = styleFiles.map(
58
+ (styleFile) => import(path.resolve(styleFile))
59
+ );
60
+ await Promise.all(importPromises);
61
+
62
+ for (let i = 0; i < styleFiles.length; i++) {
63
+ await buildCreate();
64
+ }
65
+ for (let i = 0; i < styleFiles.length; i++) {
66
+ await buildGlobal();
67
+ }
68
+ await optimizeCSS();
69
+ })();
package/tsconfig.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "ESNext",
4
+ "moduleResolution": "Bundler"
5
+ }
6
+ }