@plumeria/compiler 0.1.0 → 0.1.1
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 +1 -1
- package/src/clean-up.ts +11 -7
- package/src/index.ts +21 -33
package/package.json
CHANGED
package/src/clean-up.ts
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
|
-
import { writeFileSync } from
|
|
2
|
-
import
|
|
1
|
+
import { writeFileSync } from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
3
|
|
|
4
4
|
export const cleanUp = async () => {
|
|
5
|
-
const
|
|
6
|
-
const
|
|
5
|
+
const projectRoot = process.cwd().split('node_modules')[0];
|
|
6
|
+
const directPath = path.join(projectRoot, 'node_modules/@plumeria/core');
|
|
7
|
+
|
|
8
|
+
const createFilePath = directPath + '/dist/styles/create.css';
|
|
9
|
+
const globalFilePath = directPath + '/dist/styles/global.css';
|
|
10
|
+
|
|
7
11
|
try {
|
|
8
|
-
writeFileSync(createFilePath,
|
|
9
|
-
writeFileSync(globalFilePath,
|
|
12
|
+
writeFileSync(createFilePath, '', 'utf-8');
|
|
13
|
+
writeFileSync(globalFilePath, '', 'utf-8');
|
|
10
14
|
} catch (err) {
|
|
11
|
-
console.error(
|
|
15
|
+
console.error('An error occurred:', err);
|
|
12
16
|
}
|
|
13
17
|
};
|
package/src/index.ts
CHANGED
|
@@ -1,29 +1,22 @@
|
|
|
1
|
-
import * as path from
|
|
2
|
-
import * as fs from
|
|
3
|
-
import ts from
|
|
4
|
-
import { globby } from
|
|
5
|
-
import { cleanUp } from
|
|
6
|
-
import { buildCreate } from
|
|
7
|
-
import { buildGlobal } from
|
|
8
|
-
import postcss from
|
|
9
|
-
import combineSelectors from
|
|
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 '@plumeria/core/dist/method/create-build-helper.js';
|
|
7
|
+
import { buildGlobal } from '@plumeria/core/dist/method/global-build-helper.js';
|
|
8
|
+
import postcss from 'postcss';
|
|
9
|
+
import combineSelectors from 'postcss-combine-duplicated-selectors';
|
|
10
10
|
|
|
11
11
|
function isCSS(filePath: string): boolean {
|
|
12
|
-
const content = fs.readFileSync(filePath,
|
|
13
|
-
const sourceFile = ts.createSourceFile(
|
|
14
|
-
filePath,
|
|
15
|
-
content,
|
|
16
|
-
ts.ScriptTarget.Latest,
|
|
17
|
-
true
|
|
18
|
-
);
|
|
12
|
+
const content = fs.readFileSync(filePath, 'utf8');
|
|
13
|
+
const sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true);
|
|
19
14
|
|
|
20
15
|
const checker = (node: ts.Node): boolean => {
|
|
21
16
|
if (ts.isPropertyAccessExpression(node) && ts.isIdentifier(node.name)) {
|
|
22
17
|
const expressionText = node.expression.getText(sourceFile);
|
|
23
18
|
const methodName = node.name.getText(sourceFile);
|
|
24
|
-
return (
|
|
25
|
-
expressionText === "css" && ["create", "global"].includes(methodName)
|
|
26
|
-
);
|
|
19
|
+
return expressionText === 'css' && ['create', 'global'].includes(methodName);
|
|
27
20
|
}
|
|
28
21
|
return ts.forEachChild(node, checker) || false;
|
|
29
22
|
};
|
|
@@ -32,31 +25,26 @@ function isCSS(filePath: string): boolean {
|
|
|
32
25
|
}
|
|
33
26
|
|
|
34
27
|
async function getAppRoot(): Promise<string> {
|
|
35
|
-
const threeLevelsUp = path.join(process.cwd(),
|
|
36
|
-
return fs.existsSync(path.join(threeLevelsUp,
|
|
37
|
-
? path.join(process.cwd(), "../../../../../")
|
|
38
|
-
: path.join(process.cwd(), "../../");
|
|
28
|
+
const threeLevelsUp = path.join(process.cwd(), '../../../../..');
|
|
29
|
+
return fs.existsSync(path.join(threeLevelsUp, 'node_modules/.pnpm')) ? path.join(process.cwd(), '../../../../../') : path.join(process.cwd(), '../../');
|
|
39
30
|
}
|
|
40
31
|
|
|
41
32
|
async function optimizeCSS() {
|
|
42
|
-
const
|
|
43
|
-
const
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
]).process(cssContent, { from: cssPath, to: cssPath });
|
|
33
|
+
const corePath = path.dirname(require.resolve('@plumeria/core/package.json'));
|
|
34
|
+
const cssPath = path.join(corePath, 'dist/styles/global.css');
|
|
35
|
+
const cssContent = fs.readFileSync(cssPath, 'utf8');
|
|
36
|
+
const result = postcss([combineSelectors({ removeDuplicatedProperties: true })]).process(cssContent, { from: cssPath, to: cssPath });
|
|
47
37
|
fs.writeFileSync(cssPath, result.css);
|
|
48
38
|
}
|
|
49
39
|
|
|
50
40
|
(async () => {
|
|
51
41
|
await cleanUp();
|
|
52
42
|
const appRoot = await getAppRoot();
|
|
53
|
-
const files = await globby([path.join(appRoot,
|
|
54
|
-
ignore: [
|
|
43
|
+
const files = await globby([path.join(appRoot, '**/*.{js,jsx,ts,tsx}')], {
|
|
44
|
+
ignore: ['**/main.{js,ts}/**', '**/dist/**', '**/.next/**', '**/node_modules/**'],
|
|
55
45
|
});
|
|
56
46
|
const styleFiles = files.filter(isCSS);
|
|
57
|
-
const importPromises = styleFiles.map(
|
|
58
|
-
(styleFile) => import(path.resolve(styleFile))
|
|
59
|
-
);
|
|
47
|
+
const importPromises = styleFiles.map(styleFile => import(path.resolve(styleFile)));
|
|
60
48
|
await Promise.all(importPromises);
|
|
61
49
|
|
|
62
50
|
for (let i = 0; i < styleFiles.length; i++) {
|