lvyjs 0.2.6 → 0.2.8
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 +4 -3
- package/lib/config.d.ts +12 -0
- package/lib/config.js +22 -3
- package/lib/content.js +46 -0
- package/lib/esbuild/utils/content.js +39 -42
- package/lib/index.d.ts +1 -1
- package/lib/index.js +1 -1
- package/lib/loader.d.ts +12 -2
- package/lib/loader.js +57 -14
- package/lib/main.js +0 -1
- package/lib/postcss.js +40 -6
- package/lib/rullup/index.js +7 -12
- package/lib/rullup/plugins/loader-css.js +7 -11
- package/lib/rullup/plugins/loader-files.js +7 -11
- package/lib/server.js +11 -0
- package/lib/store.d.ts +1 -14
- package/lib/typing.d.ts +6 -0
- package/package.json +1 -3
- package/lib/esbuild/config.js +0 -4
- package/lib/esbuild/index.d.ts +0 -8
- package/lib/esbuild/index.js +0 -64
- package/lib/esbuild/plugins/Asstes.js +0 -26
- package/lib/esbuild/plugins/alias.js +0 -44
- package/lib/esbuild/plugins/css.js +0 -27
- package/lib/rullup/plugins/config.js +0 -4
package/bin/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
|
|
2
3
|
import { fork } from 'child_process'
|
|
3
4
|
import { join, dirname, relative } from 'path'
|
|
4
5
|
import { fileURLToPath } from 'node:url'
|
|
@@ -7,10 +8,8 @@ const args = [...process.argv.slice(2)]
|
|
|
7
8
|
const currentFilePath = fileURLToPath(import.meta.url)
|
|
8
9
|
const currentDirPath = dirname(currentFilePath)
|
|
9
10
|
const pkgFilr = join(currentDirPath, '../package.json')
|
|
10
|
-
|
|
11
11
|
const jsFile = join(currentDirPath, '../lib/index.js')
|
|
12
12
|
const jsdir = relative(process.cwd(), jsFile)
|
|
13
|
-
|
|
14
13
|
let tsxDir = join(currentDirPath, '../../tsx/dist/cli.mjs')
|
|
15
14
|
if (!existsSync(tsxDir)) {
|
|
16
15
|
tsxDir = join(currentDirPath, '../node_modules/tsx/dist/cli.mjs')
|
|
@@ -18,8 +17,10 @@ if (!existsSync(tsxDir)) {
|
|
|
18
17
|
if (!existsSync(tsxDir)) {
|
|
19
18
|
tsxDir = join(process.cwd(), 'node_modules/tsx/dist/cli.mjs')
|
|
20
19
|
}
|
|
20
|
+
if (!existsSync(tsxDir)) {
|
|
21
|
+
new Error('无法搜寻tsx')
|
|
22
|
+
}
|
|
21
23
|
|
|
22
|
-
// 启动模式
|
|
23
24
|
if (args.includes('build')) {
|
|
24
25
|
const argsx = args.filter(arg => arg !== 'build')
|
|
25
26
|
const msg = fork(tsxDir, [jsdir, '--lvy-build', ...argsx], {
|
package/lib/config.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
declare const assetsRegExp: RegExp;
|
|
2
|
+
declare const stylesRegExp: RegExp;
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @param val
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
8
|
+
declare const createAlias: (val: any) => {};
|
|
9
|
+
declare const isWin32: () => boolean;
|
|
10
|
+
declare const convertPath: (inputPath: string) => string;
|
|
11
|
+
|
|
12
|
+
export { assetsRegExp, convertPath, createAlias, isWin32, stylesRegExp };
|
package/lib/config.js
CHANGED
|
@@ -1,4 +1,23 @@
|
|
|
1
|
-
const
|
|
2
|
-
const
|
|
1
|
+
const assetsRegExp = /\.(png|jpg|jpeg|gif|svg|webp|ico)$/;
|
|
2
|
+
const stylesRegExp = /\.(css|scss|less|sass|less)$/;
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @param val
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
8
|
+
const createAlias = val => {
|
|
9
|
+
const alias = {};
|
|
10
|
+
// 遍历 entries 数组
|
|
11
|
+
val.entries.forEach(entry => {
|
|
12
|
+
alias[entry.find] = entry.replacement;
|
|
13
|
+
});
|
|
14
|
+
return alias;
|
|
15
|
+
};
|
|
16
|
+
const isWin32 = () => {
|
|
17
|
+
return ['win32'].includes(process.platform);
|
|
18
|
+
};
|
|
19
|
+
const convertPath = (inputPath) => {
|
|
20
|
+
return isWin32() ? inputPath.replace(/\\/g, '/') : inputPath;
|
|
21
|
+
};
|
|
3
22
|
|
|
4
|
-
export {
|
|
23
|
+
export { assetsRegExp, convertPath, createAlias, isWin32, stylesRegExp };
|
package/lib/content.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { join } from 'path';
|
|
2
|
+
import crypto from 'node:crypto';
|
|
3
|
+
import { convertPath } from './config.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 生成模块内容
|
|
7
|
+
* @param {string} relativePath 相对路径
|
|
8
|
+
*/
|
|
9
|
+
const generateModuleContent = (relativePath) => {
|
|
10
|
+
const contents = [
|
|
11
|
+
`const reg = ['win32'].includes(process.platform) ? /^file:\\/\\/\\// : /^file:\\/\\// ;`,
|
|
12
|
+
`const fileUrl = import.meta.resolve('${convertPath(relativePath)}').replace(reg, '');`,
|
|
13
|
+
'export default fileUrl;'
|
|
14
|
+
].join('\n');
|
|
15
|
+
return contents;
|
|
16
|
+
};
|
|
17
|
+
const getRandomName = (str) => {
|
|
18
|
+
// 使用 MD5 算法创建哈希对象
|
|
19
|
+
const hash = crypto.createHash('md5');
|
|
20
|
+
// 更新哈希对象内容
|
|
21
|
+
hash.update(str);
|
|
22
|
+
return hash.digest('hex');
|
|
23
|
+
};
|
|
24
|
+
const chache = {};
|
|
25
|
+
/**
|
|
26
|
+
*
|
|
27
|
+
* @param fileUrl
|
|
28
|
+
* @returns
|
|
29
|
+
*/
|
|
30
|
+
const generateCSSModuleContent = (pathURL) => {
|
|
31
|
+
const fileName = getRandomName(pathURL);
|
|
32
|
+
const outputFileURL = convertPath(join(process.cwd(), 'node_modules', 'lvyjs', 'assets', `${fileName}.css`));
|
|
33
|
+
if (!chache[pathURL]) {
|
|
34
|
+
global.lvyWorkerProt.postMessage({
|
|
35
|
+
type: 'CSS_MODULE_GENERATED',
|
|
36
|
+
payload: {
|
|
37
|
+
from: pathURL,
|
|
38
|
+
to: outputFileURL
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
chache[pathURL] = true;
|
|
42
|
+
}
|
|
43
|
+
return `export default "${outputFileURL}";`;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export { generateCSSModuleContent, generateModuleContent };
|
|
@@ -1,57 +1,54 @@
|
|
|
1
|
-
import { join } from 'path'
|
|
2
|
-
import crypto from 'node:crypto'
|
|
1
|
+
import { join } from 'path'
|
|
2
|
+
import crypto from 'node:crypto'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @param inputPath
|
|
6
6
|
* @returns
|
|
7
7
|
*/
|
|
8
|
-
const convertPath =
|
|
9
|
-
|
|
10
|
-
}
|
|
8
|
+
const convertPath = inputPath => {
|
|
9
|
+
return ['win32'].includes(process.platform) ? inputPath : inputPath.replace(/\\/g, '/')
|
|
10
|
+
}
|
|
11
11
|
/**
|
|
12
12
|
* 生成模块内容
|
|
13
13
|
* @param {string} relativePath 相对路径
|
|
14
14
|
*/
|
|
15
|
-
const generateModuleContent =
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
// 更新哈希对象内容
|
|
32
|
-
hash.update(str);
|
|
33
|
-
return hash.digest('hex');
|
|
34
|
-
};
|
|
35
|
-
const chache = {};
|
|
15
|
+
const generateModuleContent = relativePath => {
|
|
16
|
+
const contents = [
|
|
17
|
+
`const reg = ['win32'].includes(process.platform) ? /^file:\\/\\/\\// : /^file:\\/\\// ;`,
|
|
18
|
+
`const fileUrl = import.meta.resolve('${convertPath(relativePath)}').replace(reg, '');`,
|
|
19
|
+
'export default fileUrl;'
|
|
20
|
+
].join('\n')
|
|
21
|
+
return contents
|
|
22
|
+
}
|
|
23
|
+
const getRandomName = str => {
|
|
24
|
+
// 使用 MD5 算法创建哈希对象
|
|
25
|
+
const hash = crypto.createHash('md5')
|
|
26
|
+
// 更新哈希对象内容
|
|
27
|
+
hash.update(str)
|
|
28
|
+
return hash.digest('hex')
|
|
29
|
+
}
|
|
30
|
+
const chache = {}
|
|
36
31
|
/**
|
|
37
32
|
*
|
|
38
33
|
* @param fileUrl
|
|
39
34
|
* @returns
|
|
40
35
|
*/
|
|
41
|
-
const generateCSSModuleContent =
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
}
|
|
36
|
+
const generateCSSModuleContent = pathURL => {
|
|
37
|
+
const fileName = getRandomName(pathURL)
|
|
38
|
+
const outputFileURL = convertPath(
|
|
39
|
+
join(process.cwd(), 'node_modules', 'lvyjs', 'assets', `${fileName}.css`)
|
|
40
|
+
)
|
|
41
|
+
if (!chache[pathURL]) {
|
|
42
|
+
global.lvyWorkerProt.postMessage({
|
|
43
|
+
type: 'CSS_MODULE_GENERATED',
|
|
44
|
+
payload: {
|
|
45
|
+
from: pathURL,
|
|
46
|
+
to: outputFileURL
|
|
47
|
+
}
|
|
48
|
+
})
|
|
49
|
+
chache[pathURL] = true
|
|
50
|
+
}
|
|
51
|
+
return `export default "${outputFileURL}";`
|
|
52
|
+
}
|
|
56
53
|
|
|
57
|
-
export { generateCSSModuleContent, generateModuleContent }
|
|
54
|
+
export { generateCSSModuleContent, generateModuleContent }
|
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { Options, defineConfig, getOptions, initConfig, usePlugin } from './store.js';
|
|
2
2
|
export { buildAndRun, buildJS } from './rullup/index.js';
|
|
3
|
-
export {
|
|
3
|
+
export { assetsRegExp, convertPath, createAlias, isWin32, stylesRegExp } from './config.js';
|
package/lib/index.js
CHANGED
|
@@ -2,7 +2,7 @@ import { buildAndRun } from './rullup/index.js';
|
|
|
2
2
|
export { buildJS } from './rullup/index.js';
|
|
3
3
|
import { initConfig } from './store.js';
|
|
4
4
|
export { defineConfig, getOptions, usePlugin } from './store.js';
|
|
5
|
-
export {
|
|
5
|
+
export { assetsRegExp, convertPath, createAlias, isWin32, stylesRegExp } from './config.js';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* @param input
|
package/lib/loader.d.ts
CHANGED
|
@@ -3,16 +3,26 @@ import { MessagePort } from 'worker_threads';
|
|
|
3
3
|
declare global {
|
|
4
4
|
var lvyWorkerProt: MessagePort;
|
|
5
5
|
}
|
|
6
|
+
/**
|
|
7
|
+
* @param param0
|
|
8
|
+
*/
|
|
6
9
|
declare function initialize({ port, lvyConfig }: {
|
|
7
10
|
port: any;
|
|
8
11
|
lvyConfig: any;
|
|
9
12
|
}): Promise<void>;
|
|
13
|
+
/**
|
|
14
|
+
* @param specifier
|
|
15
|
+
* @param context
|
|
16
|
+
* @param nextResolve
|
|
17
|
+
* @returns
|
|
18
|
+
*/
|
|
19
|
+
declare function resolve(specifier: any, context: any, nextResolve: any): Promise<any>;
|
|
10
20
|
/**
|
|
11
21
|
* @param url
|
|
12
22
|
* @param context
|
|
13
23
|
* @param defaultLoad
|
|
14
24
|
* @returns
|
|
15
25
|
*/
|
|
16
|
-
declare const load: (url: any, context: any,
|
|
26
|
+
declare const load: (url: any, context: any, nextLoad: any) => any;
|
|
17
27
|
|
|
18
|
-
export { initialize, load };
|
|
28
|
+
export { initialize, load, resolve };
|
package/lib/loader.js
CHANGED
|
@@ -1,31 +1,74 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { generateModuleContent, generateCSSModuleContent } from './content.js';
|
|
2
|
+
import { isWin32, convertPath } from './config.js';
|
|
2
3
|
|
|
3
|
-
const
|
|
4
|
-
const
|
|
4
|
+
const reg = isWin32() ? /^file:\/\/\// : /^file:\/\//;
|
|
5
|
+
const baseURL = isWin32() ? 'file:///' : 'file://';
|
|
5
6
|
const nodeReg = /(node_modules|node_|node:)/;
|
|
6
|
-
|
|
7
|
+
/**
|
|
8
|
+
* @param param0
|
|
9
|
+
*/
|
|
7
10
|
async function initialize({ port, lvyConfig }) {
|
|
8
11
|
global.lvyConfig = lvyConfig;
|
|
9
12
|
global.lvyWorkerProt = port;
|
|
10
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* @param specifier
|
|
16
|
+
* @param context
|
|
17
|
+
* @param nextResolve
|
|
18
|
+
* @returns
|
|
19
|
+
*/
|
|
20
|
+
async function resolve(specifier, context, nextResolve) {
|
|
21
|
+
if (!global.lvyConfig?.alias) {
|
|
22
|
+
global.lvyConfig.alias = {};
|
|
23
|
+
}
|
|
24
|
+
if (global.lvyConfig.alias?.entries) {
|
|
25
|
+
for (const { find, replacement } of global.lvyConfig.alias?.entries) {
|
|
26
|
+
if (specifier.startsWith(find)) {
|
|
27
|
+
const parentURL = `${baseURL}${convertPath(specifier.replace(find, replacement))}`;
|
|
28
|
+
return nextResolve(specifier, {
|
|
29
|
+
...context,
|
|
30
|
+
parentURL: parentURL
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return nextResolve(specifier, context);
|
|
36
|
+
}
|
|
11
37
|
/**
|
|
12
38
|
* @param url
|
|
13
39
|
* @param context
|
|
14
40
|
* @param defaultLoad
|
|
15
41
|
* @returns
|
|
16
42
|
*/
|
|
17
|
-
const load =
|
|
43
|
+
const load = (url, context, nextLoad) => {
|
|
18
44
|
if (nodeReg.test(url)) {
|
|
19
|
-
return
|
|
45
|
+
return nextLoad(url, context);
|
|
46
|
+
}
|
|
47
|
+
const urls = url.split('?');
|
|
48
|
+
const baseURL = urls[0];
|
|
49
|
+
// 得到静态资源。转为普通的文件引用。
|
|
50
|
+
if (!global.lvyConfig.assets) {
|
|
51
|
+
global.lvyConfig.assets = {};
|
|
52
|
+
}
|
|
53
|
+
// 匹配静态资源
|
|
54
|
+
if (global.lvyConfig.assets?.filter && global.lvyConfig.assets?.filter.test(baseURL)) {
|
|
55
|
+
const code = generateModuleContent(baseURL.replace(reg, ''));
|
|
56
|
+
return nextLoad(baseURL, {
|
|
57
|
+
format: 'module',
|
|
58
|
+
source: code
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
if (!global.lvyConfig.styles) {
|
|
62
|
+
global.lvyConfig.styles = {};
|
|
20
63
|
}
|
|
21
|
-
if (
|
|
22
|
-
|
|
64
|
+
if (global.lvyConfig.styles?.filter && global.lvyConfig.styles?.filter.test(baseURL)) {
|
|
65
|
+
const code = generateCSSModuleContent(baseURL.replace(reg, ''));
|
|
66
|
+
return nextLoad(baseURL, {
|
|
67
|
+
format: 'module',
|
|
68
|
+
source: code
|
|
69
|
+
});
|
|
23
70
|
}
|
|
24
|
-
|
|
25
|
-
return defaultLoad(url, {
|
|
26
|
-
format: 'module',
|
|
27
|
-
source: code
|
|
28
|
-
});
|
|
71
|
+
return nextLoad(url, context);
|
|
29
72
|
};
|
|
30
73
|
|
|
31
|
-
export { initialize, load };
|
|
74
|
+
export { initialize, load, resolve };
|
package/lib/main.js
CHANGED
package/lib/postcss.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import postcss from 'postcss';
|
|
3
3
|
import { createRequire } from 'module';
|
|
4
|
-
import { join, resolve, dirname } from 'path';
|
|
4
|
+
import { join, resolve, dirname, isAbsolute } from 'path';
|
|
5
|
+
import { createAlias } from './config.js';
|
|
5
6
|
|
|
6
7
|
const require = createRequire(import.meta.url);
|
|
7
8
|
const config = {
|
|
@@ -10,6 +11,33 @@ const config = {
|
|
|
10
11
|
less: null,
|
|
11
12
|
scss: null
|
|
12
13
|
};
|
|
14
|
+
function LessAliasPlugin(aliases) {
|
|
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
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
*
|
|
37
|
+
* @param configPath
|
|
38
|
+
* @param typing
|
|
39
|
+
* @returns
|
|
40
|
+
*/
|
|
13
41
|
const loadPostcssConfig = (configPath, typing) => {
|
|
14
42
|
if (config[typing]) {
|
|
15
43
|
return {
|
|
@@ -100,21 +128,24 @@ const loadPostcssConfig = (configPath, typing) => {
|
|
|
100
128
|
plugins: config[typing]
|
|
101
129
|
};
|
|
102
130
|
};
|
|
131
|
+
/**
|
|
132
|
+
*
|
|
133
|
+
* @param inputPath
|
|
134
|
+
* @param outputPath
|
|
135
|
+
* @returns
|
|
136
|
+
*/
|
|
103
137
|
const postCSS = (inputPath, outputPath) => {
|
|
104
138
|
const configPath = join(process.cwd(), 'postcss.config.cjs');
|
|
105
139
|
let typing = 'css';
|
|
106
140
|
let parser = undefined;
|
|
107
141
|
if (/\.sass$/.test(inputPath)) {
|
|
108
142
|
typing = 'sass';
|
|
109
|
-
// parser = require('postcss-sass')
|
|
110
143
|
}
|
|
111
144
|
else if (/\.less$/.test(inputPath)) {
|
|
112
145
|
typing = 'less';
|
|
113
|
-
// parser = require('postcss-less')
|
|
114
146
|
}
|
|
115
147
|
else if (/\.scss$/.test(inputPath)) {
|
|
116
148
|
typing = 'scss';
|
|
117
|
-
// parser = require('postcss-scss')
|
|
118
149
|
}
|
|
119
150
|
const postcssConfig = loadPostcssConfig(configPath, 'css');
|
|
120
151
|
if (!postcssConfig)
|
|
@@ -123,7 +154,10 @@ const postCSS = (inputPath, outputPath) => {
|
|
|
123
154
|
let css = '';
|
|
124
155
|
if (typing === 'less') {
|
|
125
156
|
const less = require('less');
|
|
126
|
-
const lessResult = await less.render(fs.readFileSync(inputPath, 'utf-8')
|
|
157
|
+
const lessResult = await less.render(fs.readFileSync(inputPath, 'utf-8'), {
|
|
158
|
+
filename: inputPath,
|
|
159
|
+
plugins: [LessAliasPlugin(createAlias(global.lvyConfig?.alias))] // 使用插件
|
|
160
|
+
});
|
|
127
161
|
css = lessResult.css;
|
|
128
162
|
}
|
|
129
163
|
else if (typing === 'sass' || typing === 'scss') {
|
|
@@ -165,4 +199,4 @@ const postCSS = (inputPath, outputPath) => {
|
|
|
165
199
|
});
|
|
166
200
|
};
|
|
167
201
|
|
|
168
|
-
export { postCSS };
|
|
202
|
+
export { LessAliasPlugin as default, postCSS };
|
package/lib/rullup/index.js
CHANGED
|
@@ -8,6 +8,7 @@ import { getScriptFiles } from './utils/files.js';
|
|
|
8
8
|
import alias from '@rollup/plugin-alias';
|
|
9
9
|
import { rollupStylesCSSImport } from './plugins/loader-css.js';
|
|
10
10
|
import { rollupAssets } from './plugins/loader-files.js';
|
|
11
|
+
import { createAlias } from '../config.js';
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* 用于忽略警告
|
|
@@ -44,16 +45,8 @@ const buildJS = async (inputs) => {
|
|
|
44
45
|
if (typeof global.lvyConfig?.styles !== 'boolean') {
|
|
45
46
|
if (!global.lvyConfig.alias)
|
|
46
47
|
global.lvyConfig.alias = {};
|
|
47
|
-
const newAlias = val => {
|
|
48
|
-
const alias = {};
|
|
49
|
-
// 遍历 entries 数组
|
|
50
|
-
val.entries.forEach(entry => {
|
|
51
|
-
alias[entry.find] = entry.replacement;
|
|
52
|
-
});
|
|
53
|
-
return alias;
|
|
54
|
-
};
|
|
55
48
|
plugins.push(styles({
|
|
56
|
-
alias:
|
|
49
|
+
alias: createAlias(global.lvyConfig?.alias),
|
|
57
50
|
mode: ['inject', () => '']
|
|
58
51
|
}));
|
|
59
52
|
plugins.push(rollupStylesCSSImport(global.lvyConfig.styles));
|
|
@@ -99,14 +92,16 @@ const buildJS = async (inputs) => {
|
|
|
99
92
|
}
|
|
100
93
|
}
|
|
101
94
|
const RollupOptions = global.lvyConfig?.build?.RollupOptions ?? {};
|
|
102
|
-
const
|
|
95
|
+
const plg = await RollupOptions?.plugins;
|
|
96
|
+
const pl = plg && typeof plg != 'boolean' ? plg : [];
|
|
103
97
|
// build
|
|
104
98
|
const bundle = await rollup({
|
|
105
99
|
input: inputs,
|
|
106
|
-
plugins: [...plugins],
|
|
107
100
|
onwarn: onwarn,
|
|
108
|
-
...RollupOptions
|
|
101
|
+
...RollupOptions,
|
|
102
|
+
plugins: Array.isArray(pl) ? [...plugins, ...pl] : pl
|
|
109
103
|
});
|
|
104
|
+
const OutputOptions = global.lvyConfig?.build?.OutputOptions ?? [];
|
|
110
105
|
// 写入输出文件
|
|
111
106
|
await bundle.write({
|
|
112
107
|
dir: 'lib',
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { createFilter } from '@rollup/pluginutils';
|
|
2
2
|
import { resolve, dirname, basename } from 'node:path';
|
|
3
|
-
import {
|
|
3
|
+
import { stylesRegExp } from '../../config.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
*
|
|
7
7
|
* @returns
|
|
8
8
|
*/
|
|
9
9
|
const rollupStylesCSSImport = (options) => {
|
|
10
|
-
const include = options?.filter ??
|
|
10
|
+
const include = options?.filter ?? stylesRegExp;
|
|
11
11
|
const filter = createFilter(include, null);
|
|
12
12
|
return {
|
|
13
13
|
name: 'c-css',
|
|
@@ -43,15 +43,11 @@ const rollupStylesCSSImport = (options) => {
|
|
|
43
43
|
// 内容
|
|
44
44
|
source: csscode
|
|
45
45
|
});
|
|
46
|
-
const contents =
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
return import.meta.ROLLUP_FILE_URL_${refeId}.replace(reg, '')
|
|
52
|
-
};
|
|
53
|
-
export default createUrl();
|
|
54
|
-
`;
|
|
46
|
+
const contents = [
|
|
47
|
+
`const reg = ['win32'].includes(process.platform) ? /^file:\\/\\/\\// : /^file:\\/\\// ;`,
|
|
48
|
+
`const fileUrl = import.meta.ROLLUP_FILE_URL_${refeId}.replace(reg, '');`,
|
|
49
|
+
'export default fileUrl;'
|
|
50
|
+
].join('\n');
|
|
55
51
|
return contents;
|
|
56
52
|
}
|
|
57
53
|
};
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { resolve, dirname, basename } from 'path';
|
|
2
2
|
import { readFileSync } from 'fs';
|
|
3
|
-
import {
|
|
3
|
+
import { assetsRegExp } from '../../config.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* @param {Object} options
|
|
7
7
|
* @returns {Object}
|
|
8
8
|
*/
|
|
9
9
|
const rollupAssets = (options) => {
|
|
10
|
-
const { filter =
|
|
10
|
+
const { filter = assetsRegExp } = options ?? {};
|
|
11
11
|
return {
|
|
12
12
|
name: 'rollup-node-files',
|
|
13
13
|
resolveId(source, importer) {
|
|
@@ -22,16 +22,12 @@ const rollupAssets = (options) => {
|
|
|
22
22
|
name: basename(id),
|
|
23
23
|
source: readFileSync(id)
|
|
24
24
|
});
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
'
|
|
29
|
-
'const reg = T ? /^file:\\/\\// : /^file:\\/\\/\\//',
|
|
30
|
-
'return import.meta.ROLLUP_FILE_URL_' + referenceId + ".replace(reg, '')",
|
|
31
|
-
'};',
|
|
32
|
-
'export default createUrl();'
|
|
25
|
+
const contents = [
|
|
26
|
+
`const reg = ['win32'].includes(process.platform) ? /^file:\\/\\/\\// : /^file:\\/\\// ;`,
|
|
27
|
+
`const fileUrl = import.meta.ROLLUP_FILE_URL_${referenceId}.replace(reg, '');`,
|
|
28
|
+
'export default fileUrl;'
|
|
33
29
|
].join('\n');
|
|
34
|
-
return
|
|
30
|
+
return contents;
|
|
35
31
|
}
|
|
36
32
|
}
|
|
37
33
|
};
|
package/lib/server.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import Koa from 'koa'
|
|
2
|
+
import KoaStatic from 'koa-static'
|
|
3
|
+
|
|
4
|
+
const app = new Koa()
|
|
5
|
+
// 使用 koa-static 中间件来提供静态文件
|
|
6
|
+
app.use(KoaStatic(process.cwd()))
|
|
7
|
+
// 设定端口并启动服务器
|
|
8
|
+
const PORT = 8989
|
|
9
|
+
app.listen(8989, () => {
|
|
10
|
+
console.log(`Server running at http://localhost:${PORT}`)
|
|
11
|
+
})
|
package/lib/store.d.ts
CHANGED
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
import { RollupCommonJSOptions } from '@rollup/plugin-commonjs';
|
|
2
2
|
import { RollupTypescriptOptions } from '@rollup/plugin-typescript';
|
|
3
|
-
import { SameShape, BuildOptions } from 'esbuild';
|
|
4
3
|
import { RollupOptions, OutputOptions } from 'rollup';
|
|
4
|
+
import { Alias } from './typing.js';
|
|
5
5
|
|
|
6
|
-
interface Alias {
|
|
7
|
-
find: string | RegExp;
|
|
8
|
-
replacement: string;
|
|
9
|
-
}
|
|
10
6
|
type Options = {
|
|
11
7
|
/**
|
|
12
8
|
* 配置调整机及其回调插件
|
|
@@ -36,15 +32,6 @@ type Options = {
|
|
|
36
32
|
*/
|
|
37
33
|
filter?: RegExp;
|
|
38
34
|
} | false;
|
|
39
|
-
/**
|
|
40
|
-
* 运行时配置
|
|
41
|
-
*/
|
|
42
|
-
esbuild?: {
|
|
43
|
-
/**
|
|
44
|
-
*
|
|
45
|
-
*/
|
|
46
|
-
options?: SameShape<BuildOptions, BuildOptions>;
|
|
47
|
-
};
|
|
48
35
|
/**
|
|
49
36
|
* 打包时配置
|
|
50
37
|
*/
|
package/lib/typing.d.ts
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lvyjs",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.8",
|
|
4
4
|
"description": "tsx compile script",
|
|
5
5
|
"author": "lemonade",
|
|
6
6
|
"license": "MIT",
|
|
@@ -13,14 +13,12 @@
|
|
|
13
13
|
"@rollup/plugin-typescript": "^11.1.6",
|
|
14
14
|
"@rollup/pluginutils": "^5.1.3",
|
|
15
15
|
"autoprefixer": "^10.4.20",
|
|
16
|
-
"esbuild": "^0.24.0",
|
|
17
16
|
"postcss": "^8.4.47",
|
|
18
17
|
"postcss-import": "^16.1.0",
|
|
19
18
|
"postcss-url": "^10.1.3",
|
|
20
19
|
"rollup": "^4.18.1",
|
|
21
20
|
"rollup-plugin-dts": "^6.1.1",
|
|
22
21
|
"rollup-plugin-styles": "^4.0.0",
|
|
23
|
-
"tailwindcss": "^3.4.3",
|
|
24
22
|
"tsx": "^4.19.1",
|
|
25
23
|
"typescript": "^5.5.3"
|
|
26
24
|
},
|
package/lib/esbuild/config.js
DELETED
package/lib/esbuild/index.d.ts
DELETED
package/lib/esbuild/index.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import esbuild from 'esbuild';
|
|
2
|
-
import { esBuildCSS } from './plugins/css.js';
|
|
3
|
-
import { esBuildAlias } from './plugins/alias.js';
|
|
4
|
-
import { esBuildAsstes } from './plugins/Asstes.js';
|
|
5
|
-
|
|
6
|
-
// 插件
|
|
7
|
-
const plugins = [];
|
|
8
|
-
/**
|
|
9
|
-
*
|
|
10
|
-
*/
|
|
11
|
-
const initPlugins = () => {
|
|
12
|
-
if (typeof global.lvyConfig?.alias != 'boolean') {
|
|
13
|
-
plugins.push(esBuildAlias(global.lvyConfig.alias));
|
|
14
|
-
}
|
|
15
|
-
if (typeof global.lvyConfig?.assets != 'boolean') {
|
|
16
|
-
plugins.push(esBuildAsstes(global.lvyConfig.assets));
|
|
17
|
-
}
|
|
18
|
-
if (typeof global.lvyConfig?.styles != 'boolean') {
|
|
19
|
-
plugins.push(esBuildCSS(global.lvyConfig?.styles ?? {}));
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
/**
|
|
23
|
-
*
|
|
24
|
-
* @param input
|
|
25
|
-
* @returns
|
|
26
|
-
*/
|
|
27
|
-
const ESBuild = async (input) => {
|
|
28
|
-
// 如果没有配置
|
|
29
|
-
if (!global.lvyConfig)
|
|
30
|
-
global.lvyConfig = {};
|
|
31
|
-
if (!global.lvyConfig.esbuild)
|
|
32
|
-
global.lvyConfig.esbuild = {};
|
|
33
|
-
// 没有插件时,检查是否有可用插件。
|
|
34
|
-
if (plugins.length === 0) {
|
|
35
|
-
// init plugisn
|
|
36
|
-
await initPlugins();
|
|
37
|
-
}
|
|
38
|
-
const options = global.lvyConfig?.esbuild?.options ?? {};
|
|
39
|
-
const pl = options?.plugins ?? [];
|
|
40
|
-
// 构建
|
|
41
|
-
const result = await esbuild.build({
|
|
42
|
-
// 入口文件
|
|
43
|
-
entryPoints: [input],
|
|
44
|
-
//
|
|
45
|
-
bundle: true,
|
|
46
|
-
// 平台
|
|
47
|
-
platform: 'node',
|
|
48
|
-
// 输出格式
|
|
49
|
-
format: 'esm',
|
|
50
|
-
// 不写入文件
|
|
51
|
-
write: false,
|
|
52
|
-
// 忽略所有外部依赖
|
|
53
|
-
external: ['*'],
|
|
54
|
-
...options,
|
|
55
|
-
plugins: [...plugins, ...pl]
|
|
56
|
-
});
|
|
57
|
-
if (!result.outputFiles) {
|
|
58
|
-
return '';
|
|
59
|
-
}
|
|
60
|
-
// 返回结果
|
|
61
|
-
return result.outputFiles.map(file => new TextDecoder().decode(file.contents)).join('\n');
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
export { ESBuild };
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { assetsReg } from '../../config.js';
|
|
2
|
-
import { generateModuleContent } from '../utils/content.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
*
|
|
6
|
-
* @param param0
|
|
7
|
-
*/
|
|
8
|
-
const esBuildAsstes = (optoins) => {
|
|
9
|
-
// 默认配置
|
|
10
|
-
const filter = optoins?.filter ?? assetsReg;
|
|
11
|
-
// 返回插件
|
|
12
|
-
return {
|
|
13
|
-
name: 'assets',
|
|
14
|
-
setup(build) {
|
|
15
|
-
build.onLoad({ filter }, args => {
|
|
16
|
-
const content = generateModuleContent(args.path);
|
|
17
|
-
return {
|
|
18
|
-
contents: content,
|
|
19
|
-
loader: 'js'
|
|
20
|
-
};
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
export { esBuildAsstes };
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { assetsReg, cssReg } from '../../config.js';
|
|
2
|
-
import { resolve } from 'path';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* esbuild alias 插件
|
|
6
|
-
* @param alias 配置对象
|
|
7
|
-
* @returns 插件
|
|
8
|
-
*/
|
|
9
|
-
const esBuildAlias = (alias) => {
|
|
10
|
-
const entries = alias?.entries;
|
|
11
|
-
let assets = null;
|
|
12
|
-
if (typeof global.lvyConfig?.assets != 'boolean') {
|
|
13
|
-
assets = global.lvyConfig?.assets?.filter ?? assetsReg;
|
|
14
|
-
}
|
|
15
|
-
let styles = null;
|
|
16
|
-
if (typeof global.lvyConfig?.styles != 'boolean') {
|
|
17
|
-
styles = global.lvyConfig?.styles?.filter ?? cssReg;
|
|
18
|
-
}
|
|
19
|
-
return {
|
|
20
|
-
name: 'alias',
|
|
21
|
-
setup(build) {
|
|
22
|
-
if (entries) {
|
|
23
|
-
build.onResolve({ filter: /.*/ }, args => {
|
|
24
|
-
const url = args.path;
|
|
25
|
-
const resolveDir = args.resolveDir;
|
|
26
|
-
if (assets?.test(url) || styles?.test(url)) {
|
|
27
|
-
for (const { find, replacement } of entries) {
|
|
28
|
-
if ((find instanceof RegExp && find.test(url)) ||
|
|
29
|
-
(typeof find === 'string' && url.startsWith(find))) {
|
|
30
|
-
let resolvedPath = url.replace(find, replacement);
|
|
31
|
-
return { path: resolvedPath };
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
return {
|
|
35
|
-
path: resolve(resolveDir, url)
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
export { esBuildAlias };
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { generateCSSModuleContent } from '../utils/content.js';
|
|
2
|
-
import { cssReg } from '../../config.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* css资源处理插件
|
|
6
|
-
* @param param0
|
|
7
|
-
* @returns
|
|
8
|
-
*/
|
|
9
|
-
const esBuildCSS = (optoins) => {
|
|
10
|
-
const filter = optoins?.filter || cssReg;
|
|
11
|
-
// 返回插件
|
|
12
|
-
return {
|
|
13
|
-
name: 'css-loader',
|
|
14
|
-
setup(build) {
|
|
15
|
-
// 加载 CSS/SCSS 文件
|
|
16
|
-
build.onLoad({ filter }, args => {
|
|
17
|
-
const contents = generateCSSModuleContent(args.path);
|
|
18
|
-
return {
|
|
19
|
-
contents: contents,
|
|
20
|
-
loader: 'js'
|
|
21
|
-
};
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export { esBuildCSS };
|