lvyjs 0.2.16 → 0.2.17
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/bin/index.js +3 -11
- package/lib/config.d.ts +6 -6
- package/lib/config.js +15 -15
- package/lib/content.js +37 -35
- package/lib/index.d.ts +11 -3
- package/lib/index.js +56 -58
- package/lib/loader.d.ts +6 -9
- package/lib/loader.js +51 -51
- package/lib/main.js +31 -29
- package/lib/postcss.js +170 -172
- package/lib/rullup/index.d.ts +3 -3
- package/lib/rullup/index.js +115 -119
- package/lib/rullup/plugins/loader-css.js +44 -46
- package/lib/rullup/plugins/loader-files.js +30 -30
- package/lib/rullup/utils/files.js +20 -19
- package/lib/store.d.ts +66 -48
- package/lib/store.js +25 -26
- package/lib/typing.d.ts +3 -3
- package/package.json +1 -1
package/lib/postcss.js
CHANGED
|
@@ -1,36 +1,42 @@
|
|
|
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
|
-
|
|
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(
|
|
28
|
+
this,
|
|
29
|
+
fullPath,
|
|
30
|
+
currentDirectory,
|
|
31
|
+
options,
|
|
32
|
+
environment
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
// 注册自定义文件管理器
|
|
36
|
+
pluginManager.addFileManager(AliasFileManager)
|
|
37
|
+
},
|
|
38
|
+
minVersion: [3, 0] // 支持的最低 LESS 版本
|
|
39
|
+
}
|
|
34
40
|
}
|
|
35
41
|
/**
|
|
36
42
|
*
|
|
@@ -39,95 +45,92 @@ function LessAliasPlugin(aliases) {
|
|
|
39
45
|
* @returns
|
|
40
46
|
*/
|
|
41
47
|
const loadPostcssConfig = (configPath, typing) => {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
};
|
|
46
|
-
}
|
|
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
|
-
}
|
|
48
|
+
if (config[typing]) {
|
|
49
|
+
return {
|
|
50
|
+
plugins: config[typing]
|
|
90
51
|
}
|
|
91
|
-
|
|
92
|
-
|
|
52
|
+
}
|
|
53
|
+
const plugins = []
|
|
54
|
+
let aliasEntries = []
|
|
55
|
+
if (typeof global.lvyConfig?.alias != 'boolean') {
|
|
56
|
+
aliasEntries = global.lvyConfig.alias?.entries || []
|
|
57
|
+
}
|
|
58
|
+
const includeKeys = ['postcss-import', 'postcss-url', 'autoprefixer']
|
|
59
|
+
//
|
|
60
|
+
if (aliasEntries.length > 0) {
|
|
61
|
+
// 创建 postcss-import 插件并配置别名解析
|
|
62
|
+
try {
|
|
63
|
+
plugins.push(
|
|
64
|
+
require('postcss-import')({
|
|
65
|
+
resolve: (id, basedir) => {
|
|
66
|
+
// 检查别名
|
|
67
|
+
for (const entry of aliasEntries) {
|
|
68
|
+
if (id.startsWith(entry.find)) {
|
|
69
|
+
const aliasedPath = id.replace(entry.find, entry.replacement)
|
|
70
|
+
return convertPath(resolve(basedir, aliasedPath))
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return id // 默认返回原始路径
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
)
|
|
77
|
+
} catch (err) {
|
|
78
|
+
console.error(err)
|
|
93
79
|
}
|
|
94
80
|
try {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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
|
-
}
|
|
81
|
+
plugins.push(
|
|
82
|
+
require('postcss-url')({
|
|
83
|
+
url: asset => {
|
|
84
|
+
// 使用 resolve 逻辑处理 URL
|
|
85
|
+
for (const entry of aliasEntries) {
|
|
86
|
+
if (asset.url.startsWith(entry.find)) {
|
|
87
|
+
const aliasedPath = asset.url.replace(entry.find, entry.replacement)
|
|
88
|
+
return convertPath(aliasedPath)
|
|
89
|
+
}
|
|
117
90
|
}
|
|
118
|
-
|
|
119
|
-
|
|
91
|
+
return convertPath(asset.url)
|
|
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 插件函数`)
|
|
120
117
|
}
|
|
118
|
+
} catch (err) {
|
|
119
|
+
console.error(`加载 PostCSS 插件 ${key} 失败:`, err)
|
|
120
|
+
}
|
|
121
121
|
}
|
|
122
|
+
} else {
|
|
123
|
+
plugins.push(...cfg.plugins)
|
|
124
|
+
}
|
|
122
125
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
126
|
+
} catch (err) {
|
|
127
|
+
console.error('加载 PostCSS 配置失败:', err)
|
|
128
|
+
}
|
|
129
|
+
config[typing] = plugins
|
|
130
|
+
return {
|
|
131
|
+
plugins: config[typing]
|
|
132
|
+
}
|
|
133
|
+
}
|
|
131
134
|
/**
|
|
132
135
|
*
|
|
133
136
|
* @param inputPath
|
|
@@ -135,68 +138,63 @@ const loadPostcssConfig = (configPath, typing) => {
|
|
|
135
138
|
* @returns
|
|
136
139
|
*/
|
|
137
140
|
const postCSS = (inputPath, outputPath) => {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
141
|
+
const configPath = join(process.cwd(), 'postcss.config.cjs')
|
|
142
|
+
let typing = 'css'
|
|
143
|
+
let parser = undefined
|
|
144
|
+
if (/\.sass$/.test(inputPath)) {
|
|
145
|
+
typing = 'sass'
|
|
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')
|
|
143
168
|
}
|
|
144
|
-
|
|
145
|
-
|
|
169
|
+
fs.mkdirSync(dirname(outputPath), { recursive: true })
|
|
170
|
+
const result = await postcss(postcssConfig.plugins).process(css, {
|
|
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
|
+
})
|
|
146
180
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
const
|
|
151
|
-
|
|
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 => {
|
|
181
|
+
const dependencies = result.messages
|
|
182
|
+
.filter(msg => msg.type === 'dependency')
|
|
183
|
+
.map(msg => msg.file)
|
|
184
|
+
for (const dep of dependencies) {
|
|
185
|
+
fs.watch(dep, eventType => {
|
|
196
186
|
if (eventType === 'change') {
|
|
197
|
-
|
|
187
|
+
readAndProcessCSS()
|
|
198
188
|
}
|
|
199
|
-
|
|
200
|
-
}
|
|
189
|
+
})
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
readAndProcessCSS()
|
|
193
|
+
fs.watch(inputPath, eventType => {
|
|
194
|
+
if (eventType === 'change') {
|
|
195
|
+
readAndProcessCSS()
|
|
196
|
+
}
|
|
197
|
+
})
|
|
198
|
+
}
|
|
201
199
|
|
|
202
|
-
export { LessAliasPlugin as default, postCSS }
|
|
200
|
+
export { LessAliasPlugin as default, postCSS }
|
package/lib/rullup/index.d.ts
CHANGED
|
@@ -7,11 +7,11 @@
|
|
|
7
7
|
* @param inputs
|
|
8
8
|
* @param output
|
|
9
9
|
*/
|
|
10
|
-
declare const buildJS: (inputs: string[]) => Promise<void
|
|
10
|
+
declare const buildJS: (inputs: string[]) => Promise<void>
|
|
11
11
|
/**
|
|
12
12
|
*
|
|
13
13
|
* @param script
|
|
14
14
|
*/
|
|
15
|
-
declare function buildAndRun(): Promise<void
|
|
15
|
+
declare function buildAndRun(): Promise<void>
|
|
16
16
|
|
|
17
|
-
export { buildAndRun, buildJS }
|
|
17
|
+
export { buildAndRun, buildJS }
|
package/lib/rullup/index.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { rollup } from 'rollup'
|
|
2
|
-
import { join } from 'path'
|
|
3
|
-
import typescript from '@rollup/plugin-typescript'
|
|
4
|
-
import commonjs from '@rollup/plugin-commonjs'
|
|
5
|
-
import json from '@rollup/plugin-json'
|
|
6
|
-
import styles from 'rollup-plugin-styles'
|
|
7
|
-
import { getScriptFiles } from './utils/files.js'
|
|
8
|
-
import alias from '@rollup/plugin-alias'
|
|
9
|
-
import { rollupStylesCSSImport } from './plugins/loader-css.js'
|
|
10
|
-
import { rollupAssets } from './plugins/loader-files.js'
|
|
11
|
-
import { createAlias } from '../config.js'
|
|
12
|
-
import { statSync, readFileSync } from 'fs'
|
|
13
|
-
import zlib from 'zlib'
|
|
1
|
+
import { rollup } from 'rollup'
|
|
2
|
+
import { join } from 'path'
|
|
3
|
+
import typescript from '@rollup/plugin-typescript'
|
|
4
|
+
import commonjs from '@rollup/plugin-commonjs'
|
|
5
|
+
import json from '@rollup/plugin-json'
|
|
6
|
+
import styles from 'rollup-plugin-styles'
|
|
7
|
+
import { getScriptFiles } from './utils/files.js'
|
|
8
|
+
import alias from '@rollup/plugin-alias'
|
|
9
|
+
import { rollupStylesCSSImport } from './plugins/loader-css.js'
|
|
10
|
+
import { rollupAssets } from './plugins/loader-files.js'
|
|
11
|
+
import { createAlias } from '../config.js'
|
|
12
|
+
import { statSync, readFileSync } from 'fs'
|
|
13
|
+
import zlib from 'zlib'
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* 用于忽略警告
|
|
@@ -18,10 +18,9 @@ import zlib from 'zlib';
|
|
|
18
18
|
* @param warn
|
|
19
19
|
*/
|
|
20
20
|
const onwarn = (warning, warn) => {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
21
|
+
if (warning.code === 'UNRESOLVED_IMPORT') return
|
|
22
|
+
warn(warning)
|
|
23
|
+
}
|
|
25
24
|
/**
|
|
26
25
|
* 打包 JS
|
|
27
26
|
* *** 注意 **
|
|
@@ -31,114 +30,111 @@ const onwarn = (warning, warn) => {
|
|
|
31
30
|
* @param inputs
|
|
32
31
|
* @param output
|
|
33
32
|
*/
|
|
34
|
-
const buildJS = async
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
else if (key == 'RollupOptions') {
|
|
73
|
-
continue;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
// 如果不存在这些配置
|
|
77
|
-
const keys = ['commonjs', 'typescript'];
|
|
78
|
-
for (const key of keys) {
|
|
79
|
-
// 如果是布尔值
|
|
80
|
-
if (typeof global.lvyConfig.build[key] == 'boolean') {
|
|
81
|
-
continue;
|
|
82
|
-
}
|
|
83
|
-
// 存在这些配置
|
|
84
|
-
if (global.lvyConfig.build[key]) {
|
|
85
|
-
continue;
|
|
86
|
-
}
|
|
87
|
-
//
|
|
88
|
-
if (key === 'commonjs') {
|
|
89
|
-
plugins.push(commonjs());
|
|
90
|
-
}
|
|
91
|
-
else if (key === 'typescript') {
|
|
92
|
-
plugins.push(typescript());
|
|
93
|
-
}
|
|
94
|
-
}
|
|
33
|
+
const buildJS = async inputs => {
|
|
34
|
+
if (!global.lvyConfig) global.lvyConfig = {}
|
|
35
|
+
if (!global.lvyConfig.build) global.lvyConfig.build = {}
|
|
36
|
+
// 插件
|
|
37
|
+
const plugins = []
|
|
38
|
+
if (typeof global.lvyConfig?.alias !== 'boolean') {
|
|
39
|
+
plugins.push(alias(global.lvyConfig?.alias ?? {}))
|
|
40
|
+
}
|
|
41
|
+
if (typeof global.lvyConfig?.assets !== 'boolean') {
|
|
42
|
+
plugins.push(rollupAssets(global.lvyConfig?.assets ?? {}))
|
|
43
|
+
}
|
|
44
|
+
if (typeof global.lvyConfig?.styles !== 'boolean') {
|
|
45
|
+
if (!global.lvyConfig.alias) global.lvyConfig.alias = {}
|
|
46
|
+
plugins.push(
|
|
47
|
+
styles({
|
|
48
|
+
alias: createAlias(global.lvyConfig?.alias),
|
|
49
|
+
mode: ['inject', () => '']
|
|
50
|
+
})
|
|
51
|
+
)
|
|
52
|
+
plugins.push(rollupStylesCSSImport(global.lvyConfig.styles))
|
|
53
|
+
}
|
|
54
|
+
plugins.push(json())
|
|
55
|
+
if (typeof global.lvyConfig.build != 'boolean') {
|
|
56
|
+
//
|
|
57
|
+
for (const key in global.lvyConfig.build) {
|
|
58
|
+
if (typeof global.lvyConfig.build[key] == 'boolean') {
|
|
59
|
+
continue
|
|
60
|
+
}
|
|
61
|
+
if (key == 'commonjs' && !global.lvyConfig.build['@rollup/plugin-commonjs']) {
|
|
62
|
+
plugins.push(commonjs(global.lvyConfig.build[key]))
|
|
63
|
+
} else if (key == 'typescript' && !global.lvyConfig.build['@rollup/plugin-typescript']) {
|
|
64
|
+
plugins.push(typescript(global.lvyConfig.build[key]))
|
|
65
|
+
} else if (key == 'OutputOptions') {
|
|
66
|
+
continue
|
|
67
|
+
} else if (key == 'RollupOptions') {
|
|
68
|
+
continue
|
|
69
|
+
}
|
|
95
70
|
}
|
|
96
|
-
|
|
97
|
-
const
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
sourcemap: false,
|
|
114
|
-
preserveModules: true,
|
|
115
|
-
assetFileNames: 'assets/[name]-[hash][extname]',
|
|
116
|
-
...OutputOptions
|
|
117
|
-
});
|
|
118
|
-
// 打印产出地址和文件大小
|
|
119
|
-
console.log(`✓ ${output.length} modules transformed.`);
|
|
120
|
-
for (const file of output) {
|
|
121
|
-
const filePath = join(outputDir, file.fileName);
|
|
122
|
-
const fileSize = statSync(filePath).size;
|
|
123
|
-
const fileContent = readFileSync(filePath);
|
|
124
|
-
const gzipFileSize = zlib.gzipSync(fileContent).length;
|
|
125
|
-
console.log(`${filePath.padEnd(40)} ${(fileSize / 1024).toFixed(2)} kB │ gzip: ${(gzipFileSize / 1024).toFixed(2)} kB`);
|
|
71
|
+
// 如果不存在这些配置
|
|
72
|
+
const keys = ['commonjs', 'typescript']
|
|
73
|
+
for (const key of keys) {
|
|
74
|
+
// 如果是布尔值
|
|
75
|
+
if (typeof global.lvyConfig.build[key] == 'boolean') {
|
|
76
|
+
continue
|
|
77
|
+
}
|
|
78
|
+
// 存在这些配置
|
|
79
|
+
if (global.lvyConfig.build[key]) {
|
|
80
|
+
continue
|
|
81
|
+
}
|
|
82
|
+
//
|
|
83
|
+
if (key === 'commonjs') {
|
|
84
|
+
plugins.push(commonjs())
|
|
85
|
+
} else if (key === 'typescript') {
|
|
86
|
+
plugins.push(typescript())
|
|
87
|
+
}
|
|
126
88
|
}
|
|
127
|
-
|
|
128
|
-
}
|
|
89
|
+
}
|
|
90
|
+
const RollupOptions = global.lvyConfig?.build?.RollupOptions ?? {}
|
|
91
|
+
const plg = await RollupOptions?.plugins
|
|
92
|
+
const pl = plg && typeof plg != 'boolean' ? plg : []
|
|
93
|
+
// build
|
|
94
|
+
const bundle = await rollup({
|
|
95
|
+
input: inputs,
|
|
96
|
+
onwarn: onwarn,
|
|
97
|
+
...RollupOptions,
|
|
98
|
+
plugins: Array.isArray(pl) ? [...plugins, ...pl] : pl
|
|
99
|
+
})
|
|
100
|
+
const OutputOptions = global.lvyConfig?.build?.OutputOptions ?? []
|
|
101
|
+
// 获取 dir 的值
|
|
102
|
+
const outputDir = OutputOptions['dir'] || 'lib'
|
|
103
|
+
// 写入输出文件
|
|
104
|
+
const { output } = await bundle.write({
|
|
105
|
+
dir: outputDir,
|
|
106
|
+
format: 'es',
|
|
107
|
+
sourcemap: false,
|
|
108
|
+
preserveModules: true,
|
|
109
|
+
assetFileNames: 'assets/[name]-[hash][extname]',
|
|
110
|
+
...OutputOptions
|
|
111
|
+
})
|
|
112
|
+
// 打印产出地址和文件大小
|
|
113
|
+
console.log(`✓ ${output.length} modules transformed.`)
|
|
114
|
+
for (const file of output) {
|
|
115
|
+
const filePath = join(outputDir, file.fileName)
|
|
116
|
+
const fileSize = statSync(filePath).size
|
|
117
|
+
const fileContent = readFileSync(filePath)
|
|
118
|
+
const gzipFileSize = zlib.gzipSync(fileContent).length
|
|
119
|
+
console.log(
|
|
120
|
+
`${filePath.padEnd(40)} ${(fileSize / 1024).toFixed(2)} kB │ gzip: ${(
|
|
121
|
+
gzipFileSize / 1024
|
|
122
|
+
).toFixed(2)} kB`
|
|
123
|
+
)
|
|
124
|
+
}
|
|
125
|
+
//
|
|
126
|
+
}
|
|
129
127
|
/**
|
|
130
128
|
*
|
|
131
129
|
* @param script
|
|
132
130
|
*/
|
|
133
131
|
async function buildAndRun() {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
const inputFiles = getScriptFiles(join(process.cwd(), inputDir));
|
|
141
|
-
await buildJS(inputFiles);
|
|
132
|
+
if (!global.lvyConfig) global.lvyConfig = {}
|
|
133
|
+
if (!global.lvyConfig.build) global.lvyConfig.build = {}
|
|
134
|
+
// rollup 配置
|
|
135
|
+
let inputDir = global.lvyConfig?.build?.OutputOptions?.input ?? 'src'
|
|
136
|
+
const inputFiles = getScriptFiles(join(process.cwd(), inputDir))
|
|
137
|
+
await buildJS(inputFiles)
|
|
142
138
|
}
|
|
143
139
|
|
|
144
|
-
export { buildAndRun, buildJS }
|
|
140
|
+
export { buildAndRun, buildJS }
|