lvyjs 0.2.14 → 0.2.15
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 +1 -1
- package/bin/index.js +1 -2
- package/lib/config.d.ts +6 -6
- package/lib/config.js +15 -15
- package/lib/index.d.ts +3 -11
- package/lib/index.js +58 -56
- package/lib/main.js +29 -31
- package/lib/postcss.js +172 -170
- package/lib/rullup/index.d.ts +3 -3
- package/lib/rullup/index.js +119 -97
- package/lib/rullup/plugins/loader-css.js +46 -44
- package/lib/rullup/plugins/loader-files.js +30 -30
- package/lib/rullup/utils/files.js +19 -20
- package/lib/store.d.ts +48 -66
- package/lib/store.js +26 -25
- package/lib/typing.d.ts +3 -3
- package/package.json +13 -4
- package/lib/content.js +0 -48
- package/lib/loader.d.ts +0 -25
- package/lib/loader.js +0 -74
package/README.md
CHANGED
package/bin/index.js
CHANGED
|
@@ -18,9 +18,8 @@ if (!existsSync(tsxDir)) {
|
|
|
18
18
|
tsxDir = join(process.cwd(), 'node_modules/tsx/dist/cli.mjs')
|
|
19
19
|
}
|
|
20
20
|
if (!existsSync(tsxDir)) {
|
|
21
|
-
new Error('
|
|
21
|
+
new Error('tsx not found')
|
|
22
22
|
}
|
|
23
|
-
|
|
24
23
|
if (args.includes('build')) {
|
|
25
24
|
const argsx = args.filter(arg => arg !== 'build')
|
|
26
25
|
const msg = fork(tsxDir, [jsdir, '--lvy-build', ...argsx], {
|
package/lib/config.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
declare const assetsRegExp: RegExp
|
|
2
|
-
declare const stylesRegExp: RegExp
|
|
1
|
+
declare const assetsRegExp: RegExp;
|
|
2
|
+
declare const stylesRegExp: RegExp;
|
|
3
3
|
/**
|
|
4
4
|
*
|
|
5
5
|
* @param val
|
|
6
6
|
* @returns
|
|
7
7
|
*/
|
|
8
|
-
declare const createAlias: (val: any) => {}
|
|
9
|
-
declare const isWin32: () => boolean
|
|
10
|
-
declare const convertPath: (inputPath: string) => string
|
|
8
|
+
declare const createAlias: (val: any) => {};
|
|
9
|
+
declare const isWin32: () => boolean;
|
|
10
|
+
declare const convertPath: (inputPath: string) => string;
|
|
11
11
|
|
|
12
|
-
export { assetsRegExp, convertPath, createAlias, isWin32, stylesRegExp }
|
|
12
|
+
export { assetsRegExp, convertPath, createAlias, isWin32, stylesRegExp };
|
package/lib/config.js
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
const assetsRegExp = /\.(png|jpg|jpeg|gif|svg|webp|ico)
|
|
2
|
-
const stylesRegExp = /\.(css|scss|less|sass|less)
|
|
1
|
+
const assetsRegExp = /\.(png|jpg|jpeg|gif|svg|webp|ico)$/;
|
|
2
|
+
const stylesRegExp = /\.(css|scss|less|sass|less)$/;
|
|
3
3
|
/**
|
|
4
4
|
*
|
|
5
5
|
* @param val
|
|
6
6
|
* @returns
|
|
7
7
|
*/
|
|
8
8
|
const createAlias = val => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
9
|
+
const alias = {};
|
|
10
|
+
// 遍历 entries 数组
|
|
11
|
+
val.entries.forEach(entry => {
|
|
12
|
+
alias[entry.find] = entry.replacement;
|
|
13
|
+
});
|
|
14
|
+
return alias;
|
|
15
|
+
};
|
|
16
16
|
const isWin32 = () => {
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
const convertPath = inputPath => {
|
|
20
|
-
|
|
21
|
-
}
|
|
17
|
+
return ['win32'].includes(process.platform);
|
|
18
|
+
};
|
|
19
|
+
const convertPath = (inputPath) => {
|
|
20
|
+
return isWin32() ? inputPath.replace(/\\/g, '/') : inputPath;
|
|
21
|
+
};
|
|
22
22
|
|
|
23
|
-
export { assetsRegExp, convertPath, createAlias, isWin32, stylesRegExp }
|
|
23
|
+
export { assetsRegExp, convertPath, createAlias, isWin32, stylesRegExp };
|
package/lib/index.d.ts
CHANGED
|
@@ -1,11 +1,3 @@
|
|
|
1
|
-
export {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
PluginsOptions,
|
|
5
|
-
PluginsValue,
|
|
6
|
-
defineConfig,
|
|
7
|
-
getOptions,
|
|
8
|
-
initConfig
|
|
9
|
-
} from './store.js'
|
|
10
|
-
export { buildAndRun, buildJS } from './rullup/index.js'
|
|
11
|
-
export { assetsRegExp, convertPath, createAlias, isWin32, stylesRegExp } from './config.js'
|
|
1
|
+
export { Options, PluginsCallBack, PluginsOptions, PluginsValue, defineConfig, getOptions, initConfig } from './store.js';
|
|
2
|
+
export { buildAndRun, buildJS } from './rullup/index.js';
|
|
3
|
+
export { assetsRegExp, convertPath, createAlias, isWin32, stylesRegExp } from './config.js';
|
package/lib/index.js
CHANGED
|
@@ -1,71 +1,73 @@
|
|
|
1
|
-
import { buildAndRun } from './rullup/index.js'
|
|
2
|
-
export { buildJS } from './rullup/index.js'
|
|
3
|
-
import { initConfig } from './store.js'
|
|
4
|
-
export { defineConfig, getOptions } from './store.js'
|
|
5
|
-
export { assetsRegExp, convertPath, createAlias, isWin32, stylesRegExp } from './config.js'
|
|
1
|
+
import { buildAndRun } from './rullup/index.js';
|
|
2
|
+
export { buildJS } from './rullup/index.js';
|
|
3
|
+
import { initConfig } from './store.js';
|
|
4
|
+
export { defineConfig, getOptions } from './store.js';
|
|
5
|
+
export { assetsRegExp, convertPath, createAlias, isWin32, stylesRegExp } from './config.js';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* @param input
|
|
9
9
|
*/
|
|
10
10
|
const onDev = async () => {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
11
|
+
const apps = [];
|
|
12
|
+
if (Array.isArray(global.lvyConfig?.plugins)) {
|
|
13
|
+
// 修改config
|
|
14
|
+
for (const plugin of global.lvyConfig.plugins) {
|
|
15
|
+
if (!plugin) {
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
await apps.push(plugin(global.lvyConfig));
|
|
19
|
+
}
|
|
19
20
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
21
|
+
// 执行loader
|
|
22
|
+
await import('./main.js');
|
|
23
|
+
//
|
|
24
|
+
for (const app of apps) {
|
|
25
|
+
if (!app) {
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
if (typeof app == 'function') {
|
|
29
|
+
await app(global.lvyConfig);
|
|
30
|
+
}
|
|
31
|
+
else if (typeof app.load == 'function') {
|
|
32
|
+
app.load(global.lvyConfig);
|
|
33
|
+
}
|
|
27
34
|
}
|
|
28
|
-
|
|
29
|
-
await app(global.lvyConfig)
|
|
30
|
-
} else if (typeof app.load == 'function') {
|
|
31
|
-
app.load(global.lvyConfig)
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
+
};
|
|
35
36
|
/**
|
|
36
37
|
* @param input
|
|
37
38
|
*/
|
|
38
39
|
const onBuild = async () => {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
//
|
|
50
|
-
for (const app of apps) {
|
|
51
|
-
if (!app) {
|
|
52
|
-
continue
|
|
40
|
+
const apps = [];
|
|
41
|
+
if (Array.isArray(global.lvyConfig?.plugins)) {
|
|
42
|
+
// 修改config
|
|
43
|
+
for (const plugin of global.lvyConfig.plugins) {
|
|
44
|
+
if (!plugin) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
await apps.push(plugin(global.lvyConfig));
|
|
48
|
+
}
|
|
53
49
|
}
|
|
54
|
-
|
|
55
|
-
|
|
50
|
+
//
|
|
51
|
+
for (const app of apps) {
|
|
52
|
+
if (!app) {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
if (typeof app != 'function' && typeof app.build == 'function') {
|
|
56
|
+
await app.build(global.lvyConfig);
|
|
57
|
+
}
|
|
56
58
|
}
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
+
};
|
|
59
60
|
const main = async () => {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
|
|
61
|
+
if (process.argv.includes('--lvy-dev')) {
|
|
62
|
+
await initConfig();
|
|
63
|
+
await onDev();
|
|
64
|
+
}
|
|
65
|
+
else if (process.argv.includes('--lvy-build')) {
|
|
66
|
+
await initConfig();
|
|
67
|
+
await onBuild();
|
|
68
|
+
buildAndRun();
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
main();
|
|
70
72
|
|
|
71
|
-
export { buildAndRun, initConfig }
|
|
73
|
+
export { buildAndRun, initConfig };
|
package/lib/main.js
CHANGED
|
@@ -1,38 +1,36 @@
|
|
|
1
|
-
import module from 'node:module'
|
|
2
|
-
import { MessageChannel } from 'node:worker_threads'
|
|
3
|
-
import { postCSS } from './postcss.js'
|
|
4
|
-
import {
|
|
1
|
+
import module from 'node:module';
|
|
2
|
+
import { MessageChannel } from 'node:worker_threads';
|
|
3
|
+
import { postCSS } from './postcss.js';
|
|
4
|
+
import { stylesRegExp, assetsRegExp } from './config.js';
|
|
5
5
|
|
|
6
6
|
if (!module.register) {
|
|
7
|
-
|
|
8
|
-
`This version of Node.js (${process.version}) does not support module.register(). Please upgrade to Node v18.19 or v20.6 and above.`
|
|
9
|
-
)
|
|
7
|
+
throw new Error(`This version of Node.js (${process.version}) does not support module.register(). Please upgrade to Node v18.19 or v20.6 and above.`);
|
|
10
8
|
}
|
|
11
|
-
const { port1, port2 } = new MessageChannel()
|
|
12
|
-
const cache = {}
|
|
9
|
+
const { port1, port2 } = new MessageChannel();
|
|
10
|
+
const cache = {};
|
|
13
11
|
port1.on('message', msg => {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
12
|
+
if (msg.type == 'CSS_MODULE_GENERATED') {
|
|
13
|
+
const { from, to } = msg.payload;
|
|
14
|
+
if (!cache[from]) {
|
|
15
|
+
postCSS(from, to);
|
|
16
|
+
cache[from] = true;
|
|
17
|
+
}
|
|
19
18
|
}
|
|
20
|
-
|
|
21
|
-
})
|
|
19
|
+
});
|
|
22
20
|
// port1.unref()
|
|
23
21
|
module.register('./loader.js', {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
})
|
|
22
|
+
parentURL: import.meta.url,
|
|
23
|
+
data: {
|
|
24
|
+
port: port2,
|
|
25
|
+
lvyConfig: {
|
|
26
|
+
alias: global.lvyConfig?.alias,
|
|
27
|
+
assets: global.lvyConfig?.assets ?? {
|
|
28
|
+
filter: assetsRegExp
|
|
29
|
+
},
|
|
30
|
+
styles: global.lvyConfig?.styles ?? {
|
|
31
|
+
filter: stylesRegExp
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
transferList: [port2]
|
|
36
|
+
});
|
package/lib/postcss.js
CHANGED
|
@@ -1,42 +1,36 @@
|
|
|
1
|
-
import fs from 'fs'
|
|
2
|
-
import postcss from 'postcss'
|
|
3
|
-
import { createRequire } from 'module'
|
|
4
|
-
import { join, resolve, dirname, isAbsolute } from 'path'
|
|
5
|
-
import { convertPath, createAlias } from './config.js'
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import postcss from 'postcss';
|
|
3
|
+
import { createRequire } from 'module';
|
|
4
|
+
import { join, resolve, dirname, isAbsolute } from 'path';
|
|
5
|
+
import { convertPath, createAlias } from './config.js';
|
|
6
6
|
|
|
7
|
-
const require = createRequire(import.meta.url)
|
|
7
|
+
const require = createRequire(import.meta.url);
|
|
8
8
|
const config = {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
9
|
+
css: null,
|
|
10
|
+
sass: null,
|
|
11
|
+
less: null,
|
|
12
|
+
scss: null
|
|
13
|
+
};
|
|
14
14
|
function LessAliasPlugin(aliases) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
// 注册自定义文件管理器
|
|
36
|
-
pluginManager.addFileManager(AliasFileManager)
|
|
37
|
-
},
|
|
38
|
-
minVersion: [3, 0] // 支持的最低 LESS 版本
|
|
39
|
-
}
|
|
15
|
+
return {
|
|
16
|
+
install: function (less, pluginManager) {
|
|
17
|
+
const AliasFileManager = new less.FileManager();
|
|
18
|
+
AliasFileManager.loadFile = function (filename, currentDirectory, options, environment) {
|
|
19
|
+
// 替换路径中的别名
|
|
20
|
+
for (const alias in aliases) {
|
|
21
|
+
if (filename.startsWith(alias)) {
|
|
22
|
+
filename = filename.replace(alias, aliases[alias]);
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const fullPath = isAbsolute(filename) ? filename : resolve(currentDirectory, filename);
|
|
27
|
+
return less.FileManager.prototype.loadFile.call(this, fullPath, currentDirectory, options, environment);
|
|
28
|
+
};
|
|
29
|
+
// 注册自定义文件管理器
|
|
30
|
+
pluginManager.addFileManager(AliasFileManager);
|
|
31
|
+
},
|
|
32
|
+
minVersion: [3, 0] // 支持的最低 LESS 版本
|
|
33
|
+
};
|
|
40
34
|
}
|
|
41
35
|
/**
|
|
42
36
|
*
|
|
@@ -45,92 +39,95 @@ function LessAliasPlugin(aliases) {
|
|
|
45
39
|
* @returns
|
|
46
40
|
*/
|
|
47
41
|
const loadPostcssConfig = (configPath, typing) => {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
42
|
+
if (config[typing]) {
|
|
43
|
+
return {
|
|
44
|
+
plugins: config[typing]
|
|
45
|
+
};
|
|
51
46
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
47
|
+
const plugins = [];
|
|
48
|
+
let aliasEntries = [];
|
|
49
|
+
if (typeof global.lvyConfig?.alias != 'boolean') {
|
|
50
|
+
aliasEntries = global.lvyConfig.alias?.entries || [];
|
|
51
|
+
}
|
|
52
|
+
const includeKeys = ['postcss-import', 'postcss-url', 'autoprefixer'];
|
|
53
|
+
//
|
|
54
|
+
if (aliasEntries.length > 0) {
|
|
55
|
+
// 创建 postcss-import 插件并配置别名解析
|
|
56
|
+
try {
|
|
57
|
+
plugins.push(require('postcss-import')({
|
|
58
|
+
resolve: (id, basedir) => {
|
|
59
|
+
// 检查别名
|
|
60
|
+
for (const entry of aliasEntries) {
|
|
61
|
+
if (id.startsWith(entry.find)) {
|
|
62
|
+
const aliasedPath = id.replace(entry.find, entry.replacement);
|
|
63
|
+
return convertPath(resolve(basedir, aliasedPath));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return id; // 默认返回原始路径
|
|
67
|
+
}
|
|
68
|
+
}));
|
|
69
|
+
}
|
|
70
|
+
catch (err) {
|
|
71
|
+
console.error(err);
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
plugins.push(require('postcss-url')({
|
|
75
|
+
url: asset => {
|
|
76
|
+
// 使用 resolve 逻辑处理 URL
|
|
77
|
+
for (const entry of aliasEntries) {
|
|
78
|
+
if (asset.url.startsWith(entry.find)) {
|
|
79
|
+
const aliasedPath = asset.url.replace(entry.find, entry.replacement);
|
|
80
|
+
return convertPath(aliasedPath);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return convertPath(asset.url);
|
|
84
|
+
}
|
|
85
|
+
}));
|
|
86
|
+
}
|
|
87
|
+
catch (err) {
|
|
88
|
+
console.error(err);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
for (let i = 2; i < includeKeys.length; i++) {
|
|
92
|
+
plugins.push(require(includeKeys[i])({}));
|
|
79
93
|
}
|
|
80
94
|
try {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
95
|
+
if (fs.existsSync(configPath)) {
|
|
96
|
+
const cfg = require(configPath);
|
|
97
|
+
// 添加其他插件
|
|
98
|
+
if (!Array.isArray(cfg.plugins)) {
|
|
99
|
+
const keys = Object.keys(cfg.plugins);
|
|
100
|
+
for (const key of keys) {
|
|
101
|
+
try {
|
|
102
|
+
if (includeKeys.includes(key))
|
|
103
|
+
continue;
|
|
104
|
+
const pluginConfig = cfg.plugins[key];
|
|
105
|
+
const plugin = require(key);
|
|
106
|
+
if (typeof plugin === 'function') {
|
|
107
|
+
plugins.push(plugin(pluginConfig));
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
throw new Error(`插件 ${key} 不是有效的 PostCSS 插件函数`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
catch (err) {
|
|
114
|
+
console.error(`加载 PostCSS 插件 ${key} 失败:`, err);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
90
117
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
})
|
|
94
|
-
)
|
|
95
|
-
} catch (err) {
|
|
96
|
-
console.error(err)
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
for (let i = 2; i < includeKeys.length; i++) {
|
|
100
|
-
plugins.push(require(includeKeys[i])({}))
|
|
101
|
-
}
|
|
102
|
-
try {
|
|
103
|
-
if (fs.existsSync(configPath)) {
|
|
104
|
-
const cfg = require(configPath)
|
|
105
|
-
// 添加其他插件
|
|
106
|
-
if (!Array.isArray(cfg.plugins)) {
|
|
107
|
-
const keys = Object.keys(cfg.plugins)
|
|
108
|
-
for (const key of keys) {
|
|
109
|
-
try {
|
|
110
|
-
if (includeKeys.includes(key)) continue
|
|
111
|
-
const pluginConfig = cfg.plugins[key]
|
|
112
|
-
const plugin = require(key)
|
|
113
|
-
if (typeof plugin === 'function') {
|
|
114
|
-
plugins.push(plugin(pluginConfig))
|
|
115
|
-
} else {
|
|
116
|
-
throw new Error(`插件 ${key} 不是有效的 PostCSS 插件函数`)
|
|
118
|
+
else {
|
|
119
|
+
plugins.push(...cfg.plugins);
|
|
117
120
|
}
|
|
118
|
-
} catch (err) {
|
|
119
|
-
console.error(`加载 PostCSS 插件 ${key} 失败:`, err)
|
|
120
|
-
}
|
|
121
121
|
}
|
|
122
|
-
} else {
|
|
123
|
-
plugins.push(...cfg.plugins)
|
|
124
|
-
}
|
|
125
122
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
}
|
|
123
|
+
catch (err) {
|
|
124
|
+
console.error('加载 PostCSS 配置失败:', err);
|
|
125
|
+
}
|
|
126
|
+
config[typing] = plugins;
|
|
127
|
+
return {
|
|
128
|
+
plugins: config[typing]
|
|
129
|
+
};
|
|
130
|
+
};
|
|
134
131
|
/**
|
|
135
132
|
*
|
|
136
133
|
* @param inputPath
|
|
@@ -138,63 +135,68 @@ const loadPostcssConfig = (configPath, typing) => {
|
|
|
138
135
|
* @returns
|
|
139
136
|
*/
|
|
140
137
|
const postCSS = (inputPath, outputPath) => {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
} else if (/\.less$/.test(inputPath)) {
|
|
147
|
-
typing = 'less'
|
|
148
|
-
} else if (/\.scss$/.test(inputPath)) {
|
|
149
|
-
typing = 'scss'
|
|
150
|
-
}
|
|
151
|
-
const postcssConfig = loadPostcssConfig(configPath, 'css')
|
|
152
|
-
if (!postcssConfig) return
|
|
153
|
-
const readAndProcessCSS = async () => {
|
|
154
|
-
let css = ''
|
|
155
|
-
if (typing === 'less') {
|
|
156
|
-
const less = require('less')
|
|
157
|
-
const lessResult = await less.render(fs.readFileSync(inputPath, 'utf-8'), {
|
|
158
|
-
filename: inputPath,
|
|
159
|
-
plugins: [LessAliasPlugin(createAlias(global.lvyConfig?.alias))] // 使用插件
|
|
160
|
-
})
|
|
161
|
-
css = lessResult.css
|
|
162
|
-
} else if (typing === 'sass' || typing === 'scss') {
|
|
163
|
-
const sass = require('sass')
|
|
164
|
-
const sassResult = sass.renderSync({ file: inputPath })
|
|
165
|
-
css = sassResult.css.toString()
|
|
166
|
-
} else {
|
|
167
|
-
css = fs.readFileSync(inputPath, 'utf-8')
|
|
138
|
+
const configPath = join(process.cwd(), 'postcss.config.cjs');
|
|
139
|
+
let typing = 'css';
|
|
140
|
+
let parser = undefined;
|
|
141
|
+
if (/\.sass$/.test(inputPath)) {
|
|
142
|
+
typing = 'sass';
|
|
168
143
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
parser: parser,
|
|
172
|
-
from: inputPath,
|
|
173
|
-
to: outputPath
|
|
174
|
-
})
|
|
175
|
-
fs.writeFileSync(outputPath, result.css)
|
|
176
|
-
if (result.warnings().length) {
|
|
177
|
-
result.warnings().forEach(warn => {
|
|
178
|
-
console.warn(warn.toString())
|
|
179
|
-
})
|
|
144
|
+
else if (/\.less$/.test(inputPath)) {
|
|
145
|
+
typing = 'less';
|
|
180
146
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
147
|
+
else if (/\.scss$/.test(inputPath)) {
|
|
148
|
+
typing = 'scss';
|
|
149
|
+
}
|
|
150
|
+
const postcssConfig = loadPostcssConfig(configPath, 'css');
|
|
151
|
+
if (!postcssConfig)
|
|
152
|
+
return;
|
|
153
|
+
const readAndProcessCSS = async () => {
|
|
154
|
+
let css = '';
|
|
155
|
+
if (typing === 'less') {
|
|
156
|
+
const less = require('less');
|
|
157
|
+
const lessResult = await less.render(fs.readFileSync(inputPath, 'utf-8'), {
|
|
158
|
+
filename: inputPath,
|
|
159
|
+
plugins: [LessAliasPlugin(createAlias(global.lvyConfig?.alias))] // 使用插件
|
|
160
|
+
});
|
|
161
|
+
css = lessResult.css;
|
|
162
|
+
}
|
|
163
|
+
else if (typing === 'sass' || typing === 'scss') {
|
|
164
|
+
const sass = require('sass');
|
|
165
|
+
const sassResult = sass.renderSync({ file: inputPath });
|
|
166
|
+
css = sassResult.css.toString();
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
css = fs.readFileSync(inputPath, 'utf-8');
|
|
170
|
+
}
|
|
171
|
+
fs.mkdirSync(dirname(outputPath), { recursive: true });
|
|
172
|
+
const result = await postcss(postcssConfig.plugins).process(css, {
|
|
173
|
+
parser: parser,
|
|
174
|
+
from: inputPath,
|
|
175
|
+
to: outputPath
|
|
176
|
+
});
|
|
177
|
+
fs.writeFileSync(outputPath, result.css);
|
|
178
|
+
if (result.warnings().length) {
|
|
179
|
+
result.warnings().forEach(warn => {
|
|
180
|
+
console.warn(warn.toString());
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
const dependencies = result.messages
|
|
184
|
+
.filter(msg => msg.type === 'dependency')
|
|
185
|
+
.map(msg => msg.file);
|
|
186
|
+
for (const dep of dependencies) {
|
|
187
|
+
fs.watch(dep, eventType => {
|
|
188
|
+
if (eventType === 'change') {
|
|
189
|
+
readAndProcessCSS();
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
readAndProcessCSS();
|
|
195
|
+
fs.watch(inputPath, eventType => {
|
|
186
196
|
if (eventType === 'change') {
|
|
187
|
-
|
|
197
|
+
readAndProcessCSS();
|
|
188
198
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
}
|
|
192
|
-
readAndProcessCSS()
|
|
193
|
-
fs.watch(inputPath, eventType => {
|
|
194
|
-
if (eventType === 'change') {
|
|
195
|
-
readAndProcessCSS()
|
|
196
|
-
}
|
|
197
|
-
})
|
|
198
|
-
}
|
|
199
|
+
});
|
|
200
|
+
};
|
|
199
201
|
|
|
200
|
-
export { LessAliasPlugin as default, postCSS }
|
|
202
|
+
export { LessAliasPlugin as default, postCSS };
|