@plumeria/compiler 3.0.1 → 4.0.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.
@@ -0,0 +1,7 @@
1
+ interface CompilerOptions {
2
+ include: string;
3
+ exclude: string[];
4
+ cwd?: string;
5
+ }
6
+ export declare function compileCSS(options: CompilerOptions): string;
7
+ export {};
package/dist/index.js CHANGED
@@ -3,14 +3,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.compileCSS = compileCSS;
6
7
  const core_1 = require("@swc/core");
7
8
  const zss_engine_1 = require("zss-engine");
8
- const path_1 = __importDefault(require("path"));
9
9
  const fs_1 = __importDefault(require("fs"));
10
10
  const utils_1 = require("@plumeria/utils");
11
- const optimizer_1 = require("./optimizer");
12
- async function compile(options) {
13
- const { pattern, output, cwd = process.cwd() } = options;
11
+ function compileCSS(options) {
12
+ const { include, exclude, cwd = process.cwd() } = options;
14
13
  let staticTable = null;
15
14
  let keyframesData = null;
16
15
  let viewTransitionData = null;
@@ -77,56 +76,63 @@ async function compile(options) {
77
76
  utils_1.t.isIdentifier(callee.object, { name: 'css' }) &&
78
77
  utils_1.t.isIdentifier(callee.property)) {
79
78
  const args = node.arguments;
80
- if (callee.property.value === 'props') {
81
- const merged = {};
82
- let allStatic = true;
83
- args.forEach((arg) => {
84
- const expr = arg.expression;
85
- if (utils_1.t.isObjectExpression(expr)) {
86
- const obj = (0, utils_1.objectExpressionToObject)(expr, utils_1.tables.staticTable, utils_1.tables.keyframesHashTable, utils_1.tables.viewTransitionHashTable, utils_1.tables.themeTable);
87
- if (obj) {
88
- Object.assign(merged, obj);
89
- }
90
- else {
91
- allStatic = false;
92
- }
93
- }
94
- else if (utils_1.t.isMemberExpression(expr)) {
95
- if (utils_1.t.isIdentifier(expr.object) &&
96
- utils_1.t.isIdentifier(expr.property)) {
97
- const varName = expr.object.value;
98
- const propName = expr.property.value;
99
- const styleSet = localCreateStyles[varName];
100
- if (styleSet && styleSet[propName]) {
101
- Object.assign(merged, styleSet[propName]);
102
- }
103
- else {
104
- allStatic = false;
105
- }
106
- }
107
- else {
108
- allStatic = false;
79
+ const extractStylesFromExpression = (expr) => {
80
+ const results = [];
81
+ if (utils_1.t.isObjectExpression(expr)) {
82
+ const obj = (0, utils_1.objectExpressionToObject)(expr, utils_1.tables.staticTable, utils_1.tables.keyframesHashTable, utils_1.tables.viewTransitionHashTable, utils_1.tables.themeTable);
83
+ if (obj)
84
+ results.push(obj);
85
+ }
86
+ else if (utils_1.t.isMemberExpression(expr)) {
87
+ if (utils_1.t.isIdentifier(expr.object) &&
88
+ utils_1.t.isIdentifier(expr.property)) {
89
+ const varName = expr.object.value;
90
+ const propName = expr.property.value;
91
+ const styleSet = localCreateStyles[varName];
92
+ if (styleSet && styleSet[propName]) {
93
+ results.push(styleSet[propName]);
109
94
  }
110
95
  }
111
- else if (utils_1.t.isIdentifier(expr)) {
112
- const obj = localCreateStyles[expr.value];
113
- if (obj) {
114
- Object.assign(merged, obj);
115
- }
116
- else {
117
- allStatic = false;
96
+ else if (utils_1.t.isIdentifier(expr.object) &&
97
+ expr.property.type === 'Computed') {
98
+ const varName = expr.object.value;
99
+ const styleSet = localCreateStyles[varName];
100
+ if (styleSet) {
101
+ Object.values(styleSet).forEach((s) => results.push(s));
118
102
  }
119
103
  }
120
- else {
121
- allStatic = false;
104
+ }
105
+ else if (utils_1.t.isIdentifier(expr)) {
106
+ const obj = localCreateStyles[expr.value];
107
+ if (obj) {
108
+ results.push(obj);
122
109
  }
123
- });
124
- if (allStatic && Object.keys(merged).length > 0) {
125
- (0, utils_1.extractOndemandStyles)(merged, extractedSheets);
126
- const hash = (0, zss_engine_1.genBase36Hash)(merged, 1, 8);
127
- const records = (0, utils_1.getStyleRecords)(hash, merged, 1);
128
- records.forEach((r) => extractedSheets.push(r.sheet));
129
110
  }
111
+ else if (utils_1.t.isConditionalExpression(expr)) {
112
+ results.push(...extractStylesFromExpression(expr.consequent));
113
+ results.push(...extractStylesFromExpression(expr.alternate));
114
+ }
115
+ else if (utils_1.t.isBinaryExpression(expr) &&
116
+ (expr.operator === '&&' ||
117
+ expr.operator === '||' ||
118
+ expr.operator === '??')) {
119
+ results.push(...extractStylesFromExpression(expr.left));
120
+ results.push(...extractStylesFromExpression(expr.right));
121
+ }
122
+ return results;
123
+ };
124
+ const processStyle = (style) => {
125
+ (0, utils_1.extractOndemandStyles)(style, extractedSheets);
126
+ const hash = (0, zss_engine_1.genBase36Hash)(style, 1, 8);
127
+ const records = (0, utils_1.getStyleRecords)(hash, style, 1);
128
+ records.forEach((r) => extractedSheets.push(r.sheet));
129
+ };
130
+ if (callee.property.value === 'props') {
131
+ args.forEach((arg) => {
132
+ const expr = arg.expression;
133
+ const styles = extractStylesFromExpression(expr);
134
+ styles.forEach((s) => processStyle(s));
135
+ });
130
136
  }
131
137
  else if (callee.property.value === 'keyframes' &&
132
138
  args.length > 0 &&
@@ -141,6 +147,8 @@ async function compile(options) {
141
147
  const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, utils_1.tables.staticTable, utils_1.tables.keyframesHashTable, utils_1.tables.viewTransitionHashTable, utils_1.tables.themeTable);
142
148
  const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
143
149
  utils_1.tables.viewTransitionObjectTable[hash] = obj;
150
+ (0, utils_1.extractOndemandStyles)(obj, extractedSheets);
151
+ (0, utils_1.extractOndemandStyles)({ vt: `vt-${hash}` }, extractedSheets);
144
152
  }
145
153
  else if (callee.property.value === 'createTheme' &&
146
154
  args.length > 0 &&
@@ -154,27 +162,15 @@ async function compile(options) {
154
162
  });
155
163
  return extractedSheets;
156
164
  };
157
- const files = fs_1.default.globSync(pattern, {
165
+ const files = fs_1.default.globSync(include, {
158
166
  cwd,
159
- exclude: ['**/node_modules/**', '**/dist/**', '**/.next/**'],
167
+ exclude: exclude,
160
168
  });
161
- files.forEach((file) => {
169
+ for (const file of files) {
162
170
  const sheets = processFile(file);
163
- sheets.forEach((sheet) => allSheets.add(sheet));
164
- });
165
- const outputPath = path_1.default.resolve(cwd, output);
166
- const css = Array.from(allSheets).join('\n');
167
- const outputDir = path_1.default.dirname(outputPath);
168
- if (!fs_1.default.existsSync(outputDir)) {
169
- fs_1.default.mkdirSync(outputDir, { recursive: true });
171
+ for (const sheet of sheets) {
172
+ allSheets.add(sheet);
173
+ }
170
174
  }
171
- const optCSS = await (0, optimizer_1.optimizeCSS)(css);
172
- fs_1.default.writeFileSync(outputPath, optCSS);
173
- }
174
- async function main() {
175
- const coreFilePath = require.resolve('@plumeria/core/stylesheet.css');
176
- await compile({ pattern: '**/*.{js,jsx,ts,tsx}', output: coreFilePath });
175
+ return Array.from(allSheets).join('\n');
177
176
  }
178
- main().catch((err) => {
179
- console.error(err);
180
- });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@plumeria/compiler",
3
- "version": "3.0.1",
4
- "description": "Plumeria swc based compiler",
3
+ "version": "4.0.0",
4
+ "description": "Plumeria swc based compiler for statically extracting css",
5
5
  "author": "Refirst 11",
6
6
  "license": "MIT",
7
7
  "funding": "https://github.com/sponsors/refirst11",
@@ -15,30 +15,24 @@
15
15
  "url": "https://github.com/zss-in-js/plumeria/issues"
16
16
  },
17
17
  "sideEffects": false,
18
+ "main": "dist/index.js",
19
+ "types": "dist/index.d.ts",
18
20
  "files": [
19
- "bin/",
20
21
  "dist/"
21
22
  ],
22
- "bin": {
23
- "css": "./bin/css.js"
24
- },
25
23
  "dependencies": {
26
- "rscute": "^1.1.5",
27
- "lightningcss": "^1.30.2",
28
- "postcss": "^8.5.6",
29
- "postcss-combine-media-query": "^2.1.0",
30
- "@plumeria/utils": "^3.0.1"
24
+ "@plumeria/utils": "^4.0.0"
31
25
  },
32
26
  "devDependencies": {
33
27
  "@swc/core": "1.15.2",
34
- "zss-engine": "2.1.2",
35
- "@plumeria/core": "^3.0.1"
28
+ "zss-engine": "2.1.2"
36
29
  },
37
30
  "publishConfig": {
38
31
  "access": "public",
39
32
  "provenance": true
40
33
  },
41
34
  "scripts": {
42
- "build": "rimraf dist && tsc"
35
+ "build": "rimraf dist && pnpm cjs",
36
+ "cjs": "tsc --project tsconfig.cjs.json"
43
37
  }
44
38
  }
package/bin/css.js DELETED
@@ -1,4 +0,0 @@
1
- #!/usr/bin/env node
2
- const path = require('path');
3
- require('rscute/register').register();
4
- require(path.resolve(__dirname, '../dist/index.js'));
package/dist/optimizer.js DELETED
@@ -1,26 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.optimizeCSS = optimizeCSS;
7
- const postcss_1 = __importDefault(require("postcss"));
8
- const postcss_combine_media_query_1 = __importDefault(require("postcss-combine-media-query"));
9
- const lightningcss_1 = require("lightningcss");
10
- async function optimizeCSS(cssCode) {
11
- const merged = await (0, postcss_1.default)([(0, postcss_combine_media_query_1.default)()]).process(cssCode, {
12
- from: undefined,
13
- });
14
- const light = (0, lightningcss_1.transform)({
15
- filename: 'stylesheet.css',
16
- code: Buffer.from(merged.css),
17
- minify: process.env.NODE_ENV === 'production',
18
- targets: {
19
- safari: 16,
20
- edge: 110,
21
- firefox: 110,
22
- chrome: 110,
23
- },
24
- });
25
- return Buffer.from(light.code).toString('utf-8');
26
- }