lvyjs 0.2.3 → 0.2.5
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/env.d.ts +8 -0
- package/lib/esbuild/plugins/alias.js +25 -22
- package/lib/postcss.js +74 -10
- package/package.json +2 -10
- package/lib/esbuild/plugins/css.d.ts +0 -6
package/env.d.ts
CHANGED
|
@@ -7,6 +7,14 @@ declare module '*.scss' {
|
|
|
7
7
|
const src: string
|
|
8
8
|
export default src
|
|
9
9
|
}
|
|
10
|
+
declare module '*.less' {
|
|
11
|
+
const src: string
|
|
12
|
+
export default src
|
|
13
|
+
}
|
|
14
|
+
declare module '*.sass' {
|
|
15
|
+
const src: string
|
|
16
|
+
export default src
|
|
17
|
+
}
|
|
10
18
|
|
|
11
19
|
// images
|
|
12
20
|
declare module '*.png' {
|
|
@@ -19,37 +19,23 @@ const esBuildAlias = (alias) => {
|
|
|
19
19
|
if (typeof find === 'string' && url.startsWith(find)) {
|
|
20
20
|
const resolvedPath = join(replacement, url.slice(find.length));
|
|
21
21
|
const absolutePath = resolve(resolvedPath);
|
|
22
|
-
if (existsSync(absolutePath)) {
|
|
23
|
-
return {
|
|
24
|
-
path: absolutePath
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
22
|
const value = checkFileExtensions(absolutePath);
|
|
28
23
|
if (!value)
|
|
29
24
|
return;
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
};
|
|
34
|
-
}
|
|
25
|
+
return {
|
|
26
|
+
path: value
|
|
27
|
+
};
|
|
35
28
|
}
|
|
36
29
|
else if (find instanceof RegExp && find.test(url)) {
|
|
37
30
|
// 正则匹配
|
|
38
31
|
const resolvedPath = url.replace(find, replacement);
|
|
39
32
|
const absolutePath = resolve(resolvedPath);
|
|
40
|
-
if (existsSync(absolutePath)) {
|
|
41
|
-
return {
|
|
42
|
-
path: absolutePath
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
33
|
const value = checkFileExtensions(absolutePath);
|
|
46
34
|
if (!value)
|
|
47
35
|
return;
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
};
|
|
52
|
-
}
|
|
36
|
+
return {
|
|
37
|
+
path: value
|
|
38
|
+
};
|
|
53
39
|
}
|
|
54
40
|
}
|
|
55
41
|
return null;
|
|
@@ -58,8 +44,25 @@ const esBuildAlias = (alias) => {
|
|
|
58
44
|
}
|
|
59
45
|
};
|
|
60
46
|
};
|
|
61
|
-
|
|
62
|
-
|
|
47
|
+
/**
|
|
48
|
+
*
|
|
49
|
+
* @param basePath
|
|
50
|
+
* @returns
|
|
51
|
+
*/
|
|
52
|
+
const checkFileExtensions = (basePath) => {
|
|
53
|
+
const extensions = ['.js', '.jsx', '.ts', '.tsx'];
|
|
54
|
+
if (existsSync(basePath)) {
|
|
55
|
+
// const name = basename(basePath)
|
|
56
|
+
// const rest = name.split('.')
|
|
57
|
+
// // 该路径是文件命名法。
|
|
58
|
+
// if (rest.length > 1) {
|
|
59
|
+
// const fileName = rest[rest.length - 1]
|
|
60
|
+
// if (fileName) return basePath
|
|
61
|
+
// }
|
|
62
|
+
// // 不是文件命名,路径存在,推测是路径引用
|
|
63
|
+
// basePath = `${basePath}/index`
|
|
64
|
+
return basePath;
|
|
65
|
+
}
|
|
63
66
|
for (const ext of extensions) {
|
|
64
67
|
const filePath = `${basePath}${ext}`;
|
|
65
68
|
if (existsSync(filePath)) {
|
package/lib/postcss.js
CHANGED
|
@@ -4,12 +4,25 @@ import { createRequire } from 'module';
|
|
|
4
4
|
import { join, resolve, dirname } from 'path';
|
|
5
5
|
|
|
6
6
|
const require = createRequire(import.meta.url);
|
|
7
|
-
const
|
|
7
|
+
const config = {
|
|
8
|
+
css: null,
|
|
9
|
+
sass: null,
|
|
10
|
+
less: null,
|
|
11
|
+
scss: null
|
|
12
|
+
};
|
|
13
|
+
const loadPostcssConfig = (configPath, typing) => {
|
|
14
|
+
if (config[typing]) {
|
|
15
|
+
return {
|
|
16
|
+
plugins: config[typing]
|
|
17
|
+
};
|
|
18
|
+
}
|
|
8
19
|
const plugins = [];
|
|
9
20
|
let aliasEntries = [];
|
|
10
21
|
if (typeof global.lvyConfig?.alias != 'boolean') {
|
|
11
22
|
aliasEntries = global.lvyConfig.alias?.entries || [];
|
|
12
23
|
}
|
|
24
|
+
const includeKeys = ['postcss-import', 'postcss-url', 'autoprefixer'];
|
|
25
|
+
//
|
|
13
26
|
if (aliasEntries.length > 0) {
|
|
14
27
|
// 创建 postcss-import 插件并配置别名解析
|
|
15
28
|
try {
|
|
@@ -29,6 +42,26 @@ const loadPostcssConfig = configPath => {
|
|
|
29
42
|
catch (err) {
|
|
30
43
|
console.error(err);
|
|
31
44
|
}
|
|
45
|
+
try {
|
|
46
|
+
plugins.push(require('postcss-url')({
|
|
47
|
+
url: asset => {
|
|
48
|
+
// 使用 resolve 逻辑处理 URL
|
|
49
|
+
for (const entry of aliasEntries) {
|
|
50
|
+
if (asset.url.startsWith(entry.find)) {
|
|
51
|
+
const aliasedPath = asset.url.replace(entry.find, entry.replacement);
|
|
52
|
+
return aliasedPath; // 返回解析后的 URL
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return asset.url; // 返回原始路径
|
|
56
|
+
}
|
|
57
|
+
}));
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
console.error(err);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
for (let i = 2; i < includeKeys.length; i++) {
|
|
64
|
+
plugins.push(require(includeKeys[i])({}));
|
|
32
65
|
}
|
|
33
66
|
try {
|
|
34
67
|
if (fs.existsSync(configPath)) {
|
|
@@ -36,8 +69,10 @@ const loadPostcssConfig = configPath => {
|
|
|
36
69
|
// 添加其他插件
|
|
37
70
|
if (!Array.isArray(cfg.plugins)) {
|
|
38
71
|
const keys = Object.keys(cfg.plugins);
|
|
39
|
-
|
|
72
|
+
for (const key of keys) {
|
|
40
73
|
try {
|
|
74
|
+
if (includeKeys.includes(key))
|
|
75
|
+
continue;
|
|
41
76
|
const pluginConfig = cfg.plugins[key];
|
|
42
77
|
const plugin = require(key);
|
|
43
78
|
if (typeof plugin === 'function') {
|
|
@@ -50,29 +85,58 @@ const loadPostcssConfig = configPath => {
|
|
|
50
85
|
catch (err) {
|
|
51
86
|
console.error(`加载 PostCSS 插件 ${key} 失败:`, err);
|
|
52
87
|
}
|
|
53
|
-
}
|
|
88
|
+
}
|
|
54
89
|
}
|
|
55
90
|
else {
|
|
56
91
|
plugins.push(...cfg.plugins);
|
|
57
92
|
}
|
|
58
|
-
return { plugins };
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
throw new Error(`未找到 PostCSS 配置文件: ${configPath}`);
|
|
62
93
|
}
|
|
63
94
|
}
|
|
64
95
|
catch (err) {
|
|
65
96
|
console.error('加载 PostCSS 配置失败:', err);
|
|
66
|
-
return { plugins };
|
|
67
97
|
}
|
|
98
|
+
config[typing] = plugins;
|
|
99
|
+
return {
|
|
100
|
+
plugins: config[typing]
|
|
101
|
+
};
|
|
68
102
|
};
|
|
69
103
|
const postCSS = (inputPath, outputPath) => {
|
|
70
104
|
const configPath = join(process.cwd(), 'postcss.config.cjs');
|
|
71
|
-
|
|
105
|
+
let typing = 'css';
|
|
106
|
+
let parser = undefined;
|
|
107
|
+
if (/\.sass$/.test(inputPath)) {
|
|
108
|
+
typing = 'sass';
|
|
109
|
+
// parser = require('postcss-sass')
|
|
110
|
+
}
|
|
111
|
+
else if (/\.less$/.test(inputPath)) {
|
|
112
|
+
typing = 'less';
|
|
113
|
+
// parser = require('postcss-less')
|
|
114
|
+
}
|
|
115
|
+
else if (/\.scss$/.test(inputPath)) {
|
|
116
|
+
typing = 'scss';
|
|
117
|
+
// parser = require('postcss-scss')
|
|
118
|
+
}
|
|
119
|
+
const postcssConfig = loadPostcssConfig(configPath, 'css');
|
|
120
|
+
if (!postcssConfig)
|
|
121
|
+
return;
|
|
72
122
|
const readAndProcessCSS = async () => {
|
|
73
|
-
|
|
123
|
+
let css = '';
|
|
124
|
+
if (typing === 'less') {
|
|
125
|
+
const less = require('less');
|
|
126
|
+
const lessResult = await less.render(fs.readFileSync(inputPath, 'utf-8'));
|
|
127
|
+
css = lessResult.css;
|
|
128
|
+
}
|
|
129
|
+
else if (typing === 'sass' || typing === 'scss') {
|
|
130
|
+
const sass = require('sass');
|
|
131
|
+
const sassResult = sass.renderSync({ file: inputPath });
|
|
132
|
+
css = sassResult.css.toString();
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
css = fs.readFileSync(inputPath, 'utf-8');
|
|
136
|
+
}
|
|
74
137
|
fs.mkdirSync(dirname(outputPath), { recursive: true });
|
|
75
138
|
const result = await postcss(postcssConfig.plugins).process(css, {
|
|
139
|
+
parser: parser,
|
|
76
140
|
from: inputPath,
|
|
77
141
|
to: outputPath
|
|
78
142
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lvyjs",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.5",
|
|
4
4
|
"description": "tsx compile script",
|
|
5
5
|
"author": "lemonade",
|
|
6
6
|
"license": "MIT",
|
|
@@ -11,23 +11,15 @@
|
|
|
11
11
|
"@rollup/plugin-commonjs": "^28.0.0",
|
|
12
12
|
"@rollup/plugin-json": "^6.1.0",
|
|
13
13
|
"@rollup/plugin-typescript": "^11.1.6",
|
|
14
|
+
"@rollup/pluginutils": "^5.1.3",
|
|
14
15
|
"autoprefixer": "^10.4.20",
|
|
15
|
-
"cssnano": "^7.0.6",
|
|
16
16
|
"esbuild": "^0.24.0",
|
|
17
17
|
"postcss": "^8.4.47",
|
|
18
|
-
"postcss-cli": "^11.0.0",
|
|
19
18
|
"postcss-import": "^16.1.0",
|
|
20
|
-
"postcss-nested": "^6.2.0",
|
|
21
|
-
"postcss-sass": "^0.5.0",
|
|
22
|
-
"postcss-scss": "^4.0.9",
|
|
23
|
-
"postcss-simple-vars": "^7.0.1",
|
|
24
|
-
"postcss-styled": "^0.34.0",
|
|
25
|
-
"postcss-syntax": "^0.36.2",
|
|
26
19
|
"postcss-url": "^10.1.3",
|
|
27
20
|
"rollup": "^4.18.1",
|
|
28
21
|
"rollup-plugin-dts": "^6.1.1",
|
|
29
22
|
"rollup-plugin-styles": "^4.0.0",
|
|
30
|
-
"sass": "^1.79.4",
|
|
31
23
|
"tailwindcss": "^3.4.3",
|
|
32
24
|
"tsx": "^4.19.1",
|
|
33
25
|
"typescript": "^5.5.3"
|