@plumeria/compiler 0.25.1 → 0.25.3
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/dist/extract.js +57 -66
- package/dist/index.js +167 -152
- package/package.json +3 -3
package/dist/extract.js
CHANGED
|
@@ -5,10 +5,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.generatedTsMap = void 0;
|
|
7
7
|
exports.extractTSFile = extractTSFile;
|
|
8
|
-
exports.restoreAllOriginals = restoreAllOriginals;
|
|
9
8
|
exports.extractVueAndSvelte = extractVueAndSvelte;
|
|
10
9
|
const core_1 = require("@swc/core");
|
|
11
|
-
const
|
|
10
|
+
const promises_1 = require("fs/promises");
|
|
12
11
|
const path_1 = __importDefault(require("path"));
|
|
13
12
|
const generatedTsMap = new Map();
|
|
14
13
|
exports.generatedTsMap = generatedTsMap;
|
|
@@ -159,11 +158,14 @@ function expressionToString(expr) {
|
|
|
159
158
|
console.warn(`css.props: Argument unsupported ${expr.type}: Use css.create instead.`);
|
|
160
159
|
return '';
|
|
161
160
|
}
|
|
162
|
-
function extractCssProps(ast) {
|
|
161
|
+
async function extractCssProps(ast, code) {
|
|
163
162
|
const propsMatches = [];
|
|
163
|
+
if (code && !code.includes('css.props')) {
|
|
164
|
+
return propsMatches;
|
|
165
|
+
}
|
|
164
166
|
try {
|
|
165
|
-
visit(ast, {
|
|
166
|
-
CallExpression: (node) => {
|
|
167
|
+
await visit(ast, {
|
|
168
|
+
CallExpression: async (node) => {
|
|
167
169
|
if (node.callee.type === 'MemberExpression' &&
|
|
168
170
|
node.callee.object.type === 'Identifier' &&
|
|
169
171
|
node.callee.object.value === 'css' &&
|
|
@@ -176,7 +178,7 @@ function extractCssProps(ast) {
|
|
|
176
178
|
if (arg.expression.type === 'ConditionalExpression' ||
|
|
177
179
|
(arg.expression.type === 'BinaryExpression' &&
|
|
178
180
|
arg.expression.operator === '&&')) {
|
|
179
|
-
const styles = extractStyleObjectsFromExpression(arg.expression);
|
|
181
|
+
const styles = await extractStyleObjectsFromExpression(arg.expression);
|
|
180
182
|
conditionalStyleObjects.push(...styles);
|
|
181
183
|
}
|
|
182
184
|
else {
|
|
@@ -237,23 +239,23 @@ function extractStyleObjectsFromExpression(expression) {
|
|
|
237
239
|
}
|
|
238
240
|
return [];
|
|
239
241
|
}
|
|
240
|
-
function extractStaticStringLiteralVariable(ast) {
|
|
242
|
+
async function extractStaticStringLiteralVariable(ast) {
|
|
241
243
|
const matches = [];
|
|
242
244
|
try {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
+
for (const node of ast.body) {
|
|
246
|
+
if (node.type === 'VariableDeclaration') {
|
|
245
247
|
const allStringLiterals = node.declarations.length > 0 &&
|
|
246
248
|
node.declarations.every((decl) => decl.init && decl.init.type === 'StringLiteral');
|
|
247
249
|
if (allStringLiterals) {
|
|
248
|
-
const { code: extractedCode } = (0, core_1.
|
|
250
|
+
const { code: extractedCode } = await (0, core_1.print)({
|
|
249
251
|
type: 'Module',
|
|
250
252
|
body: [node],
|
|
251
253
|
span: { start: 0, end: 0, ctxt: 0 },
|
|
252
254
|
});
|
|
253
255
|
matches.push(extractedCode.trim());
|
|
254
256
|
}
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
257
259
|
}
|
|
258
260
|
catch (e) {
|
|
259
261
|
console.error(`Failed to parse code to extract static string literal variables: ${e}`);
|
|
@@ -286,22 +288,22 @@ function extractCssPropsFromTemplate(code) {
|
|
|
286
288
|
}
|
|
287
289
|
return matches;
|
|
288
290
|
}
|
|
289
|
-
function visit(node, visitor) {
|
|
291
|
+
async function visit(node, visitor) {
|
|
290
292
|
if (!node)
|
|
291
293
|
return;
|
|
292
294
|
const visitorFunc = visitor[node.type];
|
|
293
295
|
if (visitorFunc) {
|
|
294
|
-
visitorFunc(node);
|
|
296
|
+
await visitorFunc(node);
|
|
295
297
|
}
|
|
296
298
|
for (const key in node) {
|
|
297
299
|
if (typeof node[key] === 'object' && node[key] !== null) {
|
|
298
300
|
if (Array.isArray(node[key])) {
|
|
299
301
|
for (const child of node[key]) {
|
|
300
|
-
visit(child, visitor);
|
|
302
|
+
await visit(child, visitor);
|
|
301
303
|
}
|
|
302
304
|
}
|
|
303
305
|
else {
|
|
304
|
-
visit(node[key], visitor);
|
|
306
|
+
await visit(node[key], visitor);
|
|
305
307
|
}
|
|
306
308
|
}
|
|
307
309
|
}
|
|
@@ -336,10 +338,10 @@ function importDeclarationToString(node) {
|
|
|
336
338
|
}
|
|
337
339
|
return `import '${source}';`;
|
|
338
340
|
}
|
|
339
|
-
function extractImportDeclarations(ast) {
|
|
341
|
+
async function extractImportDeclarations(ast) {
|
|
340
342
|
const importDeclarations = [];
|
|
341
343
|
try {
|
|
342
|
-
visit(ast, {
|
|
344
|
+
await visit(ast, {
|
|
343
345
|
ImportDeclaration: (node) => {
|
|
344
346
|
importDeclarations.push(importDeclarationToString(node));
|
|
345
347
|
},
|
|
@@ -350,11 +352,14 @@ function extractImportDeclarations(ast) {
|
|
|
350
352
|
}
|
|
351
353
|
return importDeclarations.join('\n');
|
|
352
354
|
}
|
|
353
|
-
function extractCssMethod(ast, methodName) {
|
|
355
|
+
async function extractCssMethod(ast, methodName, code) {
|
|
356
|
+
if (!code.includes(`css.${methodName}`)) {
|
|
357
|
+
return '';
|
|
358
|
+
}
|
|
354
359
|
const matches = [];
|
|
355
360
|
try {
|
|
356
|
-
|
|
357
|
-
|
|
361
|
+
for (const node of ast.body) {
|
|
362
|
+
if (node.type === 'VariableDeclaration') {
|
|
358
363
|
const containsCssMethod = node.declarations.some((decl) => decl.init &&
|
|
359
364
|
decl.init.type === 'CallExpression' &&
|
|
360
365
|
decl.init.callee.type === 'MemberExpression' &&
|
|
@@ -363,15 +368,15 @@ function extractCssMethod(ast, methodName) {
|
|
|
363
368
|
decl.init.callee.property.type === 'Identifier' &&
|
|
364
369
|
decl.init.callee.property.value === methodName);
|
|
365
370
|
if (containsCssMethod && node.span) {
|
|
366
|
-
const { code: extractedCode } = (0, core_1.
|
|
371
|
+
const { code: extractedCode } = await (0, core_1.print)({
|
|
367
372
|
type: 'Module',
|
|
368
373
|
body: [node],
|
|
369
374
|
span: { start: 0, end: 0, ctxt: 0 },
|
|
370
375
|
});
|
|
371
376
|
matches.push(extractedCode.trim());
|
|
372
377
|
}
|
|
373
|
-
}
|
|
374
|
-
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
375
380
|
}
|
|
376
381
|
catch (e) {
|
|
377
382
|
console.error(`Failed to parse code to extract css.${methodName}: ${e}`);
|
|
@@ -382,7 +387,7 @@ async function extractVueAndSvelte(filePath) {
|
|
|
382
387
|
const ext = path_1.default.extname(filePath);
|
|
383
388
|
if (!(ext === '.svelte' || ext === '.vue'))
|
|
384
389
|
return filePath;
|
|
385
|
-
const code =
|
|
390
|
+
const code = await (0, promises_1.readFile)(filePath, 'utf8');
|
|
386
391
|
const lines = code.split(/\r?\n/);
|
|
387
392
|
let inScript = false;
|
|
388
393
|
const contentLines = [];
|
|
@@ -401,30 +406,29 @@ async function extractVueAndSvelte(filePath) {
|
|
|
401
406
|
}
|
|
402
407
|
}
|
|
403
408
|
const tsCode = contentLines.join('\n');
|
|
409
|
+
const tsPath = filePath.replace(ext, '.ts');
|
|
404
410
|
if (!tsCode.trim()) {
|
|
405
|
-
|
|
406
|
-
fs_1.default.writeFileSync(tsPath, '', 'utf8');
|
|
407
|
-
generatedTsMap.set(filePath, tsPath);
|
|
411
|
+
generatedTsMap.set(filePath, '');
|
|
408
412
|
return tsPath;
|
|
409
413
|
}
|
|
410
|
-
const ast = (0, core_1.
|
|
414
|
+
const ast = await (0, core_1.parse)(tsCode, {
|
|
411
415
|
syntax: 'typescript',
|
|
412
416
|
tsx: true,
|
|
413
417
|
});
|
|
414
|
-
const propsFromScript = extractCssProps(ast);
|
|
418
|
+
const propsFromScript = await extractCssProps(ast, code);
|
|
415
419
|
const propsFromTemplate = extractCssPropsFromTemplate(code);
|
|
416
420
|
const propsMatches = [...new Set([...propsFromScript, ...propsFromTemplate])];
|
|
417
421
|
const calls = propsMatches
|
|
418
422
|
.filter(Boolean)
|
|
419
423
|
.map((call) => `${call};`)
|
|
420
424
|
.join('\n');
|
|
421
|
-
const importSection = extractImportDeclarations(ast);
|
|
422
|
-
const staticVariableSection = extractStaticStringLiteralVariable(ast);
|
|
423
|
-
const cssCreateSection = extractCssMethod(ast, 'create');
|
|
424
|
-
const cssKeyframesSection = extractCssMethod(ast, 'keyframes');
|
|
425
|
-
const cssViewTransitionSection = extractCssMethod(ast, 'viewTransition');
|
|
426
|
-
const cssDefineConstsSection = extractCssMethod(ast, 'defineConsts');
|
|
427
|
-
const cssDefineTokensSection = extractCssMethod(ast, 'defineTokens');
|
|
425
|
+
const importSection = await extractImportDeclarations(ast);
|
|
426
|
+
const staticVariableSection = await extractStaticStringLiteralVariable(ast);
|
|
427
|
+
const cssCreateSection = await extractCssMethod(ast, 'create', code);
|
|
428
|
+
const cssKeyframesSection = await extractCssMethod(ast, 'keyframes', code);
|
|
429
|
+
const cssViewTransitionSection = await extractCssMethod(ast, 'viewTransition', code);
|
|
430
|
+
const cssDefineConstsSection = await extractCssMethod(ast, 'defineConsts', code);
|
|
431
|
+
const cssDefineTokensSection = await extractCssMethod(ast, 'defineTokens', code);
|
|
428
432
|
let finalCode = '';
|
|
429
433
|
if (importSection)
|
|
430
434
|
finalCode += importSection + '\n';
|
|
@@ -442,25 +446,23 @@ async function extractVueAndSvelte(filePath) {
|
|
|
442
446
|
finalCode += cssCreateSection + '\n';
|
|
443
447
|
if (calls)
|
|
444
448
|
finalCode += calls + '\n';
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
generatedTsMap.set(filePath, tsPath);
|
|
448
|
-
return tsPath;
|
|
449
|
+
generatedTsMap.set(filePath, finalCode);
|
|
450
|
+
return filePath;
|
|
449
451
|
}
|
|
450
452
|
async function extractTSFile(filePath) {
|
|
451
|
-
const code =
|
|
452
|
-
const ast = (0, core_1.
|
|
453
|
+
const code = await (0, promises_1.readFile)(filePath, 'utf8');
|
|
454
|
+
const ast = await (0, core_1.parse)(code, {
|
|
453
455
|
syntax: 'typescript',
|
|
454
456
|
tsx: true,
|
|
455
457
|
});
|
|
456
|
-
const importSection = extractImportDeclarations(ast);
|
|
457
|
-
const staticVariableSection = extractStaticStringLiteralVariable(ast);
|
|
458
|
-
const cssCreateSection = extractCssMethod(ast, 'create');
|
|
459
|
-
const cssKeyframesSection = extractCssMethod(ast, 'keyframes');
|
|
460
|
-
const cssViewTransitionSection = extractCssMethod(ast, 'viewTransition');
|
|
461
|
-
const cssDefineConstsSection = extractCssMethod(ast, 'defineConsts');
|
|
462
|
-
const cssDefineTokensSection = extractCssMethod(ast, 'defineTokens');
|
|
463
|
-
const propsMatches = extractCssProps(ast);
|
|
458
|
+
const importSection = await extractImportDeclarations(ast);
|
|
459
|
+
const staticVariableSection = await extractStaticStringLiteralVariable(ast);
|
|
460
|
+
const cssCreateSection = await extractCssMethod(ast, 'create', code);
|
|
461
|
+
const cssKeyframesSection = await extractCssMethod(ast, 'keyframes', code);
|
|
462
|
+
const cssViewTransitionSection = await extractCssMethod(ast, 'viewTransition', code);
|
|
463
|
+
const cssDefineConstsSection = await extractCssMethod(ast, 'defineConsts', code);
|
|
464
|
+
const cssDefineTokensSection = await extractCssMethod(ast, 'defineTokens', code);
|
|
465
|
+
const propsMatches = await extractCssProps(ast, code);
|
|
464
466
|
const calls = propsMatches
|
|
465
467
|
.filter(Boolean)
|
|
466
468
|
.map((call) => `${call};`)
|
|
@@ -481,27 +483,16 @@ async function extractTSFile(filePath) {
|
|
|
481
483
|
if (cssCreateSection)
|
|
482
484
|
finalCode += cssCreateSection + '\n';
|
|
483
485
|
finalCode += calls;
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
fs_1.default.writeFileSync(tempFilePath, finalCode, 'utf8');
|
|
487
|
-
generatedTsMap.set(filePath, tempFilePath);
|
|
488
|
-
return tempFilePath;
|
|
489
|
-
}
|
|
490
|
-
async function restoreAllOriginals() {
|
|
491
|
-
for (const [originalPath, genPath] of generatedTsMap.entries()) {
|
|
492
|
-
if (genPath !== originalPath && fs_1.default.existsSync(genPath)) {
|
|
493
|
-
fs_1.default.unlinkSync(genPath);
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
generatedTsMap.clear();
|
|
486
|
+
generatedTsMap.set(filePath, finalCode);
|
|
487
|
+
return filePath;
|
|
497
488
|
}
|
|
498
489
|
process.on('uncaughtException', async (error) => {
|
|
499
490
|
console.error('Uncaught Exception:', error);
|
|
500
|
-
|
|
491
|
+
generatedTsMap.clear();
|
|
501
492
|
process.exit(1);
|
|
502
493
|
});
|
|
503
494
|
process.on('unhandledRejection', async (reason, promise) => {
|
|
504
495
|
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
|
|
505
|
-
|
|
496
|
+
generatedTsMap.clear();
|
|
506
497
|
process.exit(1);
|
|
507
498
|
});
|
package/dist/index.js
CHANGED
|
@@ -4,7 +4,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const path_1 = __importDefault(require("path"));
|
|
7
|
-
const fs_1 = require("fs");
|
|
8
7
|
const promises_1 = require("fs/promises");
|
|
9
8
|
const postcss_1 = __importDefault(require("postcss"));
|
|
10
9
|
const postcss_combine_media_query_1 = __importDefault(require("postcss-combine-media-query"));
|
|
@@ -14,7 +13,7 @@ const core_1 = require("@swc/core");
|
|
|
14
13
|
const find_up_1 = require("find-up");
|
|
15
14
|
const processors_1 = require("@plumeria/core/processors");
|
|
16
15
|
const extract_1 = require("./extract");
|
|
17
|
-
async function generateStats(buildTime) {
|
|
16
|
+
async function generateStats(buildTime, coreFilePath) {
|
|
18
17
|
const cssCode = await (0, promises_1.readFile)(coreFilePath, 'utf8');
|
|
19
18
|
const cssSize = Buffer.byteLength(cssCode, 'utf8');
|
|
20
19
|
let rules = 0;
|
|
@@ -48,177 +47,193 @@ async function generateStats(buildTime) {
|
|
|
48
47
|
console.log(`Build time: ${buildTime.toFixed(2)}s`);
|
|
49
48
|
console.log('────────────────────────────\n');
|
|
50
49
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
50
|
+
async function main() {
|
|
51
|
+
let projectRoot;
|
|
52
|
+
const workspaceRootFile = await (0, find_up_1.findUp)(async (directory) => {
|
|
53
|
+
const pnpmWsPath = path_1.default.join(directory, 'pnpm-workspace.yaml');
|
|
54
|
+
try {
|
|
55
|
+
await (0, promises_1.access)(pnpmWsPath);
|
|
56
|
+
return pnpmWsPath;
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
}
|
|
60
|
+
const pkgJsonPath = path_1.default.join(directory, 'package.json');
|
|
59
61
|
try {
|
|
60
|
-
|
|
62
|
+
await (0, promises_1.access)(pkgJsonPath);
|
|
63
|
+
const pkgJson = JSON.parse(await (0, promises_1.readFile)(pkgJsonPath, 'utf-8'));
|
|
61
64
|
if (pkgJson.workspaces) {
|
|
62
65
|
return pkgJsonPath;
|
|
63
66
|
}
|
|
64
67
|
}
|
|
65
|
-
catch
|
|
66
|
-
console.error(err);
|
|
68
|
+
catch {
|
|
67
69
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
projectRoot = path_1.default.dirname(workspaceRootFile);
|
|
73
|
-
}
|
|
74
|
-
else {
|
|
75
|
-
const singleProjectRootFile = (0, find_up_1.findUpSync)('package.json');
|
|
76
|
-
if (singleProjectRootFile) {
|
|
77
|
-
projectRoot = path_1.default.dirname(singleProjectRootFile);
|
|
70
|
+
return undefined;
|
|
71
|
+
});
|
|
72
|
+
if (workspaceRootFile) {
|
|
73
|
+
projectRoot = path_1.default.dirname(workspaceRootFile);
|
|
78
74
|
}
|
|
79
75
|
else {
|
|
80
|
-
|
|
76
|
+
const singleProjectRootFile = await (0, find_up_1.findUp)('package.json');
|
|
77
|
+
if (singleProjectRootFile) {
|
|
78
|
+
projectRoot = path_1.default.dirname(singleProjectRootFile);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
projectRoot = process.cwd();
|
|
82
|
+
}
|
|
81
83
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
if (
|
|
95
|
-
|
|
96
|
-
|
|
84
|
+
let coreFilePath;
|
|
85
|
+
const coreSourcePackageJsonPath = path_1.default.join(process.cwd(), 'package.json');
|
|
86
|
+
const coreSourcePackageJson = JSON.parse(await (0, promises_1.readFile)(coreSourcePackageJsonPath, 'utf-8'));
|
|
87
|
+
const dependencies = {
|
|
88
|
+
...coreSourcePackageJson.dependencies,
|
|
89
|
+
...coreSourcePackageJson.devDependencies,
|
|
90
|
+
};
|
|
91
|
+
const coreVersion = dependencies['@plumeria/core'];
|
|
92
|
+
const resolvedCorePackageJsonPath = require.resolve('@plumeria/core/package.json', {
|
|
93
|
+
paths: [projectRoot, process.cwd()],
|
|
94
|
+
});
|
|
95
|
+
if (workspaceRootFile) {
|
|
96
|
+
if (coreVersion.includes('workspace')) {
|
|
97
|
+
coreFilePath = path_1.default.join(path_1.default.dirname(resolvedCorePackageJsonPath), 'stylesheet.css');
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
const corePackageJson = JSON.parse(await (0, promises_1.readFile)(resolvedCorePackageJsonPath, 'utf-8'));
|
|
101
|
+
const exactCoreVersion = corePackageJson.version;
|
|
102
|
+
coreFilePath = path_1.default.join(projectRoot, 'node_modules', '.pnpm', `@plumeria+core@${exactCoreVersion}`, 'node_modules', '@plumeria', 'core', 'stylesheet.css');
|
|
103
|
+
}
|
|
97
104
|
}
|
|
98
105
|
else {
|
|
99
|
-
|
|
100
|
-
const exactCoreVersion = corePackageJson.version;
|
|
101
|
-
coreFilePath = path_1.default.join(projectRoot, 'node_modules', '.pnpm', `@plumeria+core@${exactCoreVersion}`, 'node_modules', '@plumeria', 'core', 'stylesheet.css');
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
else {
|
|
105
|
-
coreFilePath = path_1.default.join(path_1.default.dirname(resolvedCorePackageJsonPath), 'stylesheet.css');
|
|
106
|
-
}
|
|
107
|
-
const cleanUp = async () => {
|
|
108
|
-
if (process.env.CI && (0, fs_1.existsSync)(coreFilePath)) {
|
|
109
|
-
(0, fs_1.unlinkSync)(coreFilePath);
|
|
110
|
-
console.log('File deleted successfully');
|
|
111
|
-
}
|
|
112
|
-
try {
|
|
113
|
-
await (0, promises_1.writeFile)(coreFilePath, '', 'utf-8');
|
|
114
|
-
}
|
|
115
|
-
catch (err) {
|
|
116
|
-
console.error('An error occurred:', err);
|
|
117
|
-
}
|
|
118
|
-
};
|
|
119
|
-
function isCSS(filePath) {
|
|
120
|
-
if ((0, fs_1.statSync)(filePath).isDirectory()) {
|
|
121
|
-
return false;
|
|
106
|
+
coreFilePath = path_1.default.join(path_1.default.dirname(resolvedCorePackageJsonPath), 'stylesheet.css');
|
|
122
107
|
}
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
if (node.object?.type === 'Identifier' && node.object.value === 'css') {
|
|
134
|
-
if (node.property.value === 'props') {
|
|
135
|
-
found = true;
|
|
108
|
+
const cleanUp = async () => {
|
|
109
|
+
if (process.env.CI) {
|
|
110
|
+
try {
|
|
111
|
+
await (0, promises_1.access)(coreFilePath);
|
|
112
|
+
await (0, promises_1.unlink)(coreFilePath);
|
|
113
|
+
console.log('File deleted successfully');
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
if (error.code !== 'ENOENT') {
|
|
117
|
+
console.error(`Error deleting ${coreFilePath}:`, error);
|
|
136
118
|
}
|
|
137
119
|
}
|
|
138
120
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
121
|
+
try {
|
|
122
|
+
await (0, promises_1.writeFile)(coreFilePath, '', 'utf-8');
|
|
123
|
+
}
|
|
124
|
+
catch (err) {
|
|
125
|
+
console.error('An error occurred:', err);
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
async function isCSS(code, filePath) {
|
|
129
|
+
if (!code.includes('css.props')) {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
const ast = await (0, core_1.parse)(code, {
|
|
133
|
+
syntax: 'typescript',
|
|
134
|
+
tsx: filePath.endsWith('.tsx'),
|
|
135
|
+
decorators: false,
|
|
136
|
+
dynamicImport: true,
|
|
137
|
+
});
|
|
138
|
+
let found = false;
|
|
139
|
+
function visit(node) {
|
|
140
|
+
if (node.type === 'MemberExpression' && node.property?.value) {
|
|
141
|
+
if (node.object?.type === 'Identifier' && node.object.value === 'css') {
|
|
142
|
+
if (node.property.value === 'props') {
|
|
143
|
+
found = true;
|
|
145
144
|
}
|
|
146
145
|
}
|
|
147
|
-
|
|
148
|
-
|
|
146
|
+
}
|
|
147
|
+
for (const key in node) {
|
|
148
|
+
const value = node[key];
|
|
149
|
+
if (value && typeof value === 'object') {
|
|
150
|
+
if (Array.isArray(value)) {
|
|
151
|
+
for (const item of value) {
|
|
152
|
+
visit(item);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
visit(value);
|
|
157
|
+
}
|
|
149
158
|
}
|
|
150
159
|
}
|
|
151
160
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
filesSupportExtensions.push(tsFile);
|
|
191
|
-
}
|
|
192
|
-
else {
|
|
193
|
-
const tempFile = await (0, extract_1.extractTSFile)(file);
|
|
194
|
-
filesSupportExtensions.push(tempFile);
|
|
161
|
+
visit(ast);
|
|
162
|
+
return found;
|
|
163
|
+
}
|
|
164
|
+
async function optimizeCSS() {
|
|
165
|
+
const cssCode = await (0, promises_1.readFile)(coreFilePath, 'utf8');
|
|
166
|
+
const merged = (0, postcss_1.default)([(0, postcss_combine_media_query_1.default)()]).process(cssCode, {
|
|
167
|
+
from: coreFilePath,
|
|
168
|
+
to: coreFilePath,
|
|
169
|
+
});
|
|
170
|
+
const light = (0, lightningcss_1.transform)({
|
|
171
|
+
filename: coreFilePath,
|
|
172
|
+
code: Buffer.from(merged.css),
|
|
173
|
+
minify: process.env.NODE_ENV === 'production',
|
|
174
|
+
targets: {
|
|
175
|
+
safari: 16,
|
|
176
|
+
edge: 110,
|
|
177
|
+
firefox: 110,
|
|
178
|
+
chrome: 110,
|
|
179
|
+
},
|
|
180
|
+
});
|
|
181
|
+
const optimizedCss = Buffer.from(light.code).toString('utf-8');
|
|
182
|
+
await (0, promises_1.writeFile)(coreFilePath, optimizedCss, 'utf-8');
|
|
183
|
+
}
|
|
184
|
+
(async () => {
|
|
185
|
+
const startTime = performance.now();
|
|
186
|
+
await cleanUp();
|
|
187
|
+
const scanRoot = process.cwd();
|
|
188
|
+
const files = [];
|
|
189
|
+
for await (const entry of (0, promises_1.glob)('**/*.{js,jsx,ts,tsx,vue,svelte}', {
|
|
190
|
+
cwd: scanRoot,
|
|
191
|
+
exclude: [
|
|
192
|
+
'**/node_modules/**',
|
|
193
|
+
'**/dist/**',
|
|
194
|
+
'**/build/**',
|
|
195
|
+
'**/.next/**',
|
|
196
|
+
],
|
|
197
|
+
})) {
|
|
198
|
+
files.push(entry);
|
|
195
199
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
const
|
|
208
|
-
|
|
200
|
+
const projectName = path_1.default.basename(projectRoot);
|
|
201
|
+
const filesSupportExtensions = await Promise.all(files.map(async (file) => {
|
|
202
|
+
const ext = path_1.default.extname(file);
|
|
203
|
+
if (ext === '.vue' || ext === '.svelte') {
|
|
204
|
+
return await (0, extract_1.extractVueAndSvelte)(file);
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
return await (0, extract_1.extractTSFile)(file);
|
|
208
|
+
}
|
|
209
|
+
}));
|
|
210
|
+
const styleFiles = await Promise.all(filesSupportExtensions.map(async (file) => {
|
|
211
|
+
const code = extract_1.generatedTsMap.get(file);
|
|
212
|
+
const isCssFile = code ? await isCSS(code, file) : false;
|
|
213
|
+
return isCssFile ? file : null;
|
|
214
|
+
}))
|
|
215
|
+
.then((results) => results.filter(Boolean))
|
|
216
|
+
.then((results) => results.sort());
|
|
217
|
+
for (const file of styleFiles) {
|
|
218
|
+
const code = extract_1.generatedTsMap.get(file);
|
|
219
|
+
if (code) {
|
|
220
|
+
const ext = path_1.default.extname(file);
|
|
221
|
+
const tsPath = ext === '.vue' || ext === '.svelte' ? file.replace(ext, '.ts') : file;
|
|
222
|
+
await (0, execute_1.executeCode)(code, { filePath: tsPath });
|
|
223
|
+
if (process.argv.includes('--paths')) {
|
|
224
|
+
console.log(`✅: ${projectName}/${path_1.default.relative(projectRoot, file)}`);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
209
227
|
}
|
|
210
|
-
}
|
|
211
|
-
for (let i = 0; i < styleFiles.length; i++) {
|
|
212
228
|
await (0, processors_1.buildGlobal)(coreFilePath);
|
|
213
|
-
}
|
|
214
|
-
for (let i = 0; i < styleFiles.length; i++) {
|
|
215
229
|
await (0, processors_1.buildProps)(coreFilePath);
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
}
|
|
224
|
-
}
|
|
230
|
+
await optimizeCSS();
|
|
231
|
+
extract_1.generatedTsMap.clear();
|
|
232
|
+
if (process.argv.includes('--stats')) {
|
|
233
|
+
const endTime = performance.now();
|
|
234
|
+
const buildTime = (endTime - startTime) / 1000;
|
|
235
|
+
await generateStats(buildTime, coreFilePath);
|
|
236
|
+
}
|
|
237
|
+
})();
|
|
238
|
+
}
|
|
239
|
+
main();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plumeria/compiler",
|
|
3
|
-
"version": "0.25.
|
|
3
|
+
"version": "0.25.3",
|
|
4
4
|
"description": "Plumeria Rust-based compiler",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -16,12 +16,12 @@
|
|
|
16
16
|
"css": "./bin/css.js"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@swc/core": "1.15.
|
|
19
|
+
"@swc/core": "1.15.1",
|
|
20
20
|
"find-up": "^8.0.0",
|
|
21
21
|
"lightningcss": "^1.30.2",
|
|
22
22
|
"postcss": "^8.5.6",
|
|
23
23
|
"postcss-combine-media-query": "^2.1.0",
|
|
24
|
-
"rscute": "^1.
|
|
24
|
+
"rscute": "^1.1.1"
|
|
25
25
|
},
|
|
26
26
|
"publishConfig": {
|
|
27
27
|
"access": "public"
|