compile-css 1.0.1 → 2.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.
- package/README.md +28 -54
- package/compileCss.js +25 -39
- package/package.json +5 -2
- package/.vscode.example/launch.json +0 -14
- package/.vscode.example/sessions.json +0 -30
- package/.vscode.example/settings.json +0 -6
- package/compileCssConfig.js +0 -36
package/README.md
CHANGED
|
@@ -24,32 +24,7 @@ compile-css
|
|
|
24
24
|
|
|
25
25
|
在项目根目录创建 `compileCssConfig.js` 文件。
|
|
26
26
|
|
|
27
|
-
###
|
|
28
|
-
|
|
29
|
-
支持两种配置格式,**推荐使用 `presets` 多套配置**。
|
|
30
|
-
|
|
31
|
-
### 单套配置(`paths`)
|
|
32
|
-
|
|
33
|
-
适合只有一个 HTML 模板的简单项目:
|
|
34
|
-
|
|
35
|
-
```javascript
|
|
36
|
-
const path = require('path');
|
|
37
|
-
|
|
38
|
-
module.exports = {
|
|
39
|
-
paths: {
|
|
40
|
-
scss: path.resolve('./scss'), // SCSS 源文件目录
|
|
41
|
-
template: path.resolve('./html'), // HTML 模板目录
|
|
42
|
-
temp: path.resolve('./temp'), // 编译临时目录
|
|
43
|
-
dist: path.resolve('./html', 'css'), // 编译输出目录
|
|
44
|
-
scssIgnore: ['scss/common.scss'], // 忽略的 SCSS 文件
|
|
45
|
-
templateIgnore: [] // 忽略的 HTML 文件
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
### 多套配置(`presets`,推荐)
|
|
51
|
-
|
|
52
|
-
适合 PC 端 + 手机端等多套模板的项目,每套配置独立编译互不干扰:
|
|
27
|
+
### 配置示例
|
|
53
28
|
|
|
54
29
|
```javascript
|
|
55
30
|
const path = require('path');
|
|
@@ -57,22 +32,22 @@ const path = require('path');
|
|
|
57
32
|
module.exports = {
|
|
58
33
|
presets: [
|
|
59
34
|
{
|
|
60
|
-
name: 'pc',
|
|
61
|
-
scss: path.resolve('./scss/pc'),
|
|
62
|
-
|
|
63
|
-
temp: path.resolve('./temp/pc'),
|
|
64
|
-
dist: path.resolve('./html/pc', 'css'),
|
|
65
|
-
scssIgnore: [],
|
|
66
|
-
|
|
35
|
+
name: 'pc', // 标识名称,用于日志输出
|
|
36
|
+
scss: path.resolve('./scss/pc'), // SCSS 源文件目录
|
|
37
|
+
content: path.resolve('./html/pc'), // TailwindCSS 扫描目录
|
|
38
|
+
temp: path.resolve('./temp/pc'), // 编译临时目录
|
|
39
|
+
dist: path.resolve('./html/pc', 'css'), // 编译输出目录
|
|
40
|
+
scssIgnore: [], // 忽略的 SCSS 文件
|
|
41
|
+
watchIgnore: [] // 忽略的监听文件
|
|
67
42
|
},
|
|
68
43
|
{
|
|
69
|
-
name: 'mobile',
|
|
44
|
+
name: 'mobile',
|
|
70
45
|
scss: path.resolve('./scss/mobile'),
|
|
71
|
-
|
|
46
|
+
content: path.resolve('./html/mobile'),
|
|
72
47
|
temp: path.resolve('./temp/mobile'),
|
|
73
48
|
dist: path.resolve('./html/mobile', 'css'),
|
|
74
49
|
scssIgnore: [],
|
|
75
|
-
|
|
50
|
+
watchIgnore: []
|
|
76
51
|
}
|
|
77
52
|
]
|
|
78
53
|
};
|
|
@@ -82,17 +57,17 @@ module.exports = {
|
|
|
82
57
|
|
|
83
58
|
| 配置项 | 类型 | 必填 | 说明 |
|
|
84
59
|
|--------|------|------|------|
|
|
85
|
-
| `name` | `string` | 否 |
|
|
86
|
-
| `scss` | `string` | 是 | SCSS
|
|
87
|
-
| `
|
|
88
|
-
| `temp` | `string` | 是 |
|
|
89
|
-
| `dist` | `string` | 是 |
|
|
90
|
-
| `scssIgnore` | `string[]` | 否 | SCSS
|
|
91
|
-
| `
|
|
60
|
+
| `name` | `string` | 否 | 标识名称,用于控制台日志输出,方便区分不同 preset 的编译信息 |
|
|
61
|
+
| `scss` | `string` | 是 | SCSS 源文件目录。递归扫描该目录下所有非 `_` 开头的 `.scss` 文件作为入口文件编译 |
|
|
62
|
+
| `content` | `string` | 是 | TailwindCSS 扫描目录。该目录下的文件变化会触发 `common.css` 重新编译。会与 `tailwind.config.js` 中的 `content` 合并 |
|
|
63
|
+
| `temp` | `string` | 是 | 编译临时目录。SCSS 先编译为普通 CSS 存放到此,再交给 PostCSS 处理。**每次启动时自动清空** |
|
|
64
|
+
| `dist` | `string` | 是 | 最终输出目录。经过 tailwindcss、autoprefixer、cssnano 处理后的 CSS 文件输出到这里 |
|
|
65
|
+
| `scssIgnore` | `string[]` | 否 | SCSS 忽略列表。支持文件名、相对路径或正则表达式。被忽略的文件不会作为入口文件编译 |
|
|
66
|
+
| `watchIgnore` | `string[]` | 否 | 监听忽略列表。`content` 目录中匹配的文件变化不会触发重新编译 |
|
|
92
67
|
|
|
93
|
-
###
|
|
68
|
+
### 忽略规则
|
|
94
69
|
|
|
95
|
-
`scssIgnore` 和 `
|
|
70
|
+
`scssIgnore` 和 `watchIgnore` 支持四种匹配方式:
|
|
96
71
|
|
|
97
72
|
- **完整相对路径**:`'scss/common.scss'` — 匹配项目根目录下 `scss/common.scss`
|
|
98
73
|
- **目录前缀**:`'scss/lib'` — 匹配 `scss/lib` 目录下的所有文件
|
|
@@ -102,10 +77,10 @@ module.exports = {
|
|
|
102
77
|
### 注意事项
|
|
103
78
|
|
|
104
79
|
- `scss` 目录中以 `_` 开头的文件(如 `_variables.scss`)被视为模块文件,不会作为入口文件编译。但当模块文件发生变化时,会触发所有入口文件重新编译
|
|
105
|
-
- 每个
|
|
106
|
-
- 每个
|
|
107
|
-
- `tailwind.config.js`
|
|
108
|
-
|
|
80
|
+
- 每个 preset 的 `temp` 目录必须唯一,不能与其他 preset 共享
|
|
81
|
+
- 每个 preset 的 `temp` 目录启动时自动清空重建
|
|
82
|
+
- `tailwind.config.js` 必须放在项目根目录
|
|
83
|
+
- `content` 字段与 `tailwind.config.js` 中的 `content` 会合并,无需重复配置
|
|
109
84
|
|
|
110
85
|
## 编译流程
|
|
111
86
|
|
|
@@ -113,14 +88,13 @@ module.exports = {
|
|
|
113
88
|
SCSS 文件 ──sass编译──▶ temp 目录(临时 CSS) ──tailwindcss + autoprefixer + cssnano──▶ dist 目录(最终 CSS)
|
|
114
89
|
```
|
|
115
90
|
|
|
116
|
-
1.
|
|
117
|
-
2.
|
|
118
|
-
3.
|
|
91
|
+
1. SCSS 文件变化 → `sass` 编译到 `temp` 目录
|
|
92
|
+
2. `temp` 目录 CSS 文件变化 → PostCSS(tailwindcss + autoprefixer + cssnano)编译到 `dist` 目录
|
|
93
|
+
3. `content` 目录文件变化 → 重新编译 `common.css`,更新 TailwindCSS 工具类
|
|
119
94
|
|
|
120
95
|
## 功能特性
|
|
121
|
-
|
|
122
96
|
- SCSS 编译
|
|
123
97
|
- PostCSS 处理(tailwindcss、autoprefixer、cssnano)
|
|
124
98
|
- 文件变化自动监听编译
|
|
125
|
-
-
|
|
99
|
+
- 支持多套配置(PC、手机等多端项目)
|
|
126
100
|
- 启动时自动清理临时目录
|
package/compileCss.js
CHANGED
|
@@ -21,34 +21,13 @@ function loadUserConfig(configName) {
|
|
|
21
21
|
return null;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
//
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
template: path.resolve('./html'),
|
|
29
|
-
temp: path.resolve('./temp'),
|
|
30
|
-
dist: path.resolve('./html', 'css')
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
// 兼容旧版单套 paths 配置,自动转换为 presets
|
|
35
|
-
let presets;
|
|
36
|
-
if (rawConfig.presets) {
|
|
37
|
-
presets = rawConfig.presets;
|
|
38
|
-
} else if (rawConfig.paths) {
|
|
39
|
-
presets = [{
|
|
40
|
-
name: 'default',
|
|
41
|
-
scss: rawConfig.paths.scss,
|
|
42
|
-
template: rawConfig.paths.template,
|
|
43
|
-
temp: rawConfig.paths.temp,
|
|
44
|
-
dist: rawConfig.paths.dist,
|
|
45
|
-
scssIgnore: rawConfig.paths.scssIgnore || [],
|
|
46
|
-
templateIgnore: rawConfig.paths.templateIgnore || []
|
|
47
|
-
}];
|
|
48
|
-
} else {
|
|
49
|
-
console.error('错误:配置文件格式不正确,请提供 paths 或 presets 配置。');
|
|
24
|
+
// 加载编译配置
|
|
25
|
+
const config = loadUserConfig('compileCssConfig.js');
|
|
26
|
+
if (!config || !config.presets) {
|
|
27
|
+
console.error('错误:未找到 compileCssConfig.js 或配置缺少 presets 字段。');
|
|
50
28
|
process.exit(1);
|
|
51
29
|
}
|
|
30
|
+
var presets = config.presets;
|
|
52
31
|
|
|
53
32
|
// 加载Tailwind配置(必须由用户提供)
|
|
54
33
|
const toption = loadUserConfig('tailwind.config.js');
|
|
@@ -137,7 +116,10 @@ function postcssCompile(file, output, tcontent) {
|
|
|
137
116
|
|
|
138
117
|
let option = { ...toption };
|
|
139
118
|
if (tcontent) {
|
|
140
|
-
option.content =
|
|
119
|
+
option.content = [
|
|
120
|
+
...(Array.isArray(option.content) ? option.content : []),
|
|
121
|
+
...tcontent
|
|
122
|
+
];
|
|
141
123
|
}
|
|
142
124
|
let css = `@tailwind base;
|
|
143
125
|
@tailwind components;
|
|
@@ -174,13 +156,19 @@ function postcssCompile(file, output, tcontent) {
|
|
|
174
156
|
// 为每个 preset 启动独立的 watcher
|
|
175
157
|
presets.forEach(function (preset) {
|
|
176
158
|
var scssDir = preset.scss;
|
|
177
|
-
var
|
|
159
|
+
var contentDir = preset.content;
|
|
178
160
|
var tempDir = preset.temp;
|
|
179
161
|
var distDir = preset.dist;
|
|
180
162
|
var scssIgnore = preset.scssIgnore || [];
|
|
181
|
-
var
|
|
163
|
+
var watchIgnore = preset.watchIgnore || [];
|
|
182
164
|
var presetName = preset.name || 'default';
|
|
183
165
|
|
|
166
|
+
// 构建 TailwindCSS content 路径
|
|
167
|
+
var tailwindContent = [];
|
|
168
|
+
if (contentDir) {
|
|
169
|
+
tailwindContent.push(path.resolve(contentDir, '**/*'));
|
|
170
|
+
}
|
|
171
|
+
|
|
184
172
|
// 启动时清理并重新创建 temp 目录
|
|
185
173
|
if (fs.existsSync(tempDir)) {
|
|
186
174
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
@@ -188,15 +176,17 @@ presets.forEach(function (preset) {
|
|
|
188
176
|
fs.mkdirSync(tempDir, { recursive: true });
|
|
189
177
|
|
|
190
178
|
var watchPaths = [
|
|
191
|
-
path.resolve(templateDir),
|
|
192
179
|
path.resolve(scssDir),
|
|
193
180
|
path.resolve(tempDir),
|
|
194
181
|
];
|
|
182
|
+
if (contentDir) {
|
|
183
|
+
watchPaths.push(path.resolve(contentDir));
|
|
184
|
+
}
|
|
195
185
|
|
|
196
186
|
chokidar.watch(watchPaths, {
|
|
197
187
|
ignored: [
|
|
198
188
|
...scssIgnore.map(function (i) { return path.resolve(process.cwd(), i); }),
|
|
199
|
-
...
|
|
189
|
+
...watchIgnore.map(function (i) { return path.resolve(process.cwd(), i); }),
|
|
200
190
|
],
|
|
201
191
|
persistent: true,
|
|
202
192
|
})
|
|
@@ -221,18 +211,14 @@ presets.forEach(function (preset) {
|
|
|
221
211
|
}
|
|
222
212
|
|
|
223
213
|
if (pathfile.endsWith('.css')) {
|
|
224
|
-
//输出的目录结构应该保持和scss目录结构 或者 temp目录结构一致
|
|
225
|
-
//如果来源是scssDir目录,那么输出的目录应该是 distDir
|
|
226
|
-
var tcontent = [path.resolve(templateDir, '**/*.{html,htm}')];
|
|
227
214
|
var output = pathfile.replace(tempDir, distDir);
|
|
228
|
-
postcssCompile(pathfile, output,
|
|
215
|
+
postcssCompile(pathfile, output, tailwindContent);
|
|
229
216
|
}
|
|
230
217
|
|
|
231
|
-
if (
|
|
232
|
-
var tcontent = [path.resolve(templateDir, '**/*.{html,htm}')];
|
|
218
|
+
if (contentDir && !isIgnored(pathfile, watchIgnore)) {
|
|
233
219
|
var input = path.resolve(tempDir, 'common.css');
|
|
234
220
|
var output = path.resolve(distDir, 'common.css');
|
|
235
|
-
postcssCompile(input, output,
|
|
221
|
+
postcssCompile(input, output, tailwindContent);
|
|
236
222
|
}
|
|
237
223
|
}
|
|
238
224
|
} catch (e) {
|
|
@@ -240,5 +226,5 @@ presets.forEach(function (preset) {
|
|
|
240
226
|
}
|
|
241
227
|
});
|
|
242
228
|
|
|
243
|
-
console.log('[' + presetName + '] 监听已启动: scss=' + scssDir + ',
|
|
229
|
+
console.log('[' + presetName + '] 监听已启动: scss=' + scssDir + ', content=' + (contentDir || '未设置'));
|
|
244
230
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "compile-css",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "A CSS compilation tool that supports SCSS, PostCSS, and TailwindCSS",
|
|
5
5
|
"main": "compileCss.js",
|
|
6
6
|
"bin": {
|
|
@@ -25,7 +25,10 @@
|
|
|
25
25
|
"url": "https://gitee.com/coder_home/compile-css/issues"
|
|
26
26
|
},
|
|
27
27
|
"scripts": {
|
|
28
|
-
"compileCss": "node compileCss.js"
|
|
28
|
+
"compileCss": "node compileCss.js",
|
|
29
|
+
"publish-major": "npm version major && git push --follow-tags && npm publish --registry=https://registry.npmjs.org/",
|
|
30
|
+
"publish-minor": "npm version minor && git push --follow-tags && npm publish --registry=https://registry.npmjs.org/",
|
|
31
|
+
"publish-patch": "npm version patch && git push --follow-tags && npm publish --registry=https://registry.npmjs.org/"
|
|
29
32
|
},
|
|
30
33
|
"dependencies": {
|
|
31
34
|
"chokidar": "^4.0.3",
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
// 使用 IntelliSense 了解相关属性。
|
|
3
|
-
// 悬停以查看现有属性的描述。
|
|
4
|
-
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
|
|
5
|
-
"version": "0.2.0",
|
|
6
|
-
"configurations": [
|
|
7
|
-
{
|
|
8
|
-
"name": "Listen for Xdebug",
|
|
9
|
-
"type": "php",
|
|
10
|
-
"request": "launch",
|
|
11
|
-
"port": 9003
|
|
12
|
-
}
|
|
13
|
-
]
|
|
14
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "https://cdn.statically.io/gh/nguyenngoclongdev/cdn/main/schema/v11/terminal-keeper.json",
|
|
3
|
-
"theme": "tribe",
|
|
4
|
-
"active": "default",
|
|
5
|
-
"activateOnStartup": true,
|
|
6
|
-
"keepExistingTerminals": false,
|
|
7
|
-
"sessions": {
|
|
8
|
-
"default": [
|
|
9
|
-
{
|
|
10
|
-
"name": "cmd",
|
|
11
|
-
"autoExecuteCommands": false,
|
|
12
|
-
"icon": "person",
|
|
13
|
-
"color": "terminal.ansiGreen",
|
|
14
|
-
"commands": []
|
|
15
|
-
},
|
|
16
|
-
[
|
|
17
|
-
{
|
|
18
|
-
"name": "compileCss",
|
|
19
|
-
"commands": [
|
|
20
|
-
"npm run compileCss"
|
|
21
|
-
]
|
|
22
|
-
},
|
|
23
|
-
{
|
|
24
|
-
"name": "cmd",
|
|
25
|
-
"commands": []
|
|
26
|
-
}
|
|
27
|
-
]
|
|
28
|
-
]
|
|
29
|
-
}
|
|
30
|
-
}
|
package/compileCssConfig.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
const path = require('path');
|
|
2
|
-
|
|
3
|
-
// module.exports = {
|
|
4
|
-
// paths: {
|
|
5
|
-
// scss: path.resolve('./scss'), // scss文件目录
|
|
6
|
-
// template: path.resolve('./html'), // html文件目录
|
|
7
|
-
// temp: path.resolve('./temp'), //编译过程中的临时文件目录
|
|
8
|
-
// dist: path.resolve('./html', 'css'), //编译后的css文件目录
|
|
9
|
-
// scssIgnore: ['scss/common.scss'], // 忽略的scss文件名(可以是文件名或目录名)
|
|
10
|
-
// templateIgnore: [] // 忽略的html文件(可以是文件名或目录名)
|
|
11
|
-
// }
|
|
12
|
-
// };
|
|
13
|
-
|
|
14
|
-
// 多套配置
|
|
15
|
-
module.exports = {
|
|
16
|
-
presets: [
|
|
17
|
-
{
|
|
18
|
-
name: 'pc', // 标识名称,用于日志
|
|
19
|
-
scss: path.resolve('./scss/'),
|
|
20
|
-
template: path.resolve('./html/'),
|
|
21
|
-
temp: path.resolve('./temp/pc'),
|
|
22
|
-
dist: path.resolve('html/css'),
|
|
23
|
-
scssIgnore: [],
|
|
24
|
-
templateIgnore: []
|
|
25
|
-
},
|
|
26
|
-
{
|
|
27
|
-
name: 'mobile',
|
|
28
|
-
scss: path.resolve('./scss/mobile'),
|
|
29
|
-
template: path.resolve('./html/mobile'),
|
|
30
|
-
temp: path.resolve('./temp/mobile'),
|
|
31
|
-
dist: path.resolve('html', 'mobile/css'),
|
|
32
|
-
scssIgnore: [],
|
|
33
|
-
templateIgnore: []
|
|
34
|
-
}
|
|
35
|
-
]
|
|
36
|
-
};
|