alemonjs 2.0.0-rc.13 → 2.0.0-rc.14
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 +13 -6
- package/env.d.ts +12 -45
- package/index.js +2 -0
- package/lib/build/rullup.js +15 -50
- package/lib/index.d.ts +1 -21
- package/lib/index.js +26 -51
- package/lib/loader/index.d.ts +10 -0
- package/lib/loader/index.js +143 -0
- package/lib/loader/main.d.ts +2 -0
- package/lib/loader/main.js +3 -0
- package/lib/main.js +1 -1
- package/lib/plugins/index.d.ts +27 -0
- package/lib/plugins/index.js +29 -0
- package/lib/{build → plugins}/loader-files.js +0 -1
- package/lib/store.d.ts +25 -0
- package/lib/store.js +40 -0
- package/package.json +11 -2
- package/dist/loader.mjs +0 -76
package/bin/index.js
CHANGED
|
@@ -5,18 +5,20 @@ import { fileURLToPath } from 'node:url'
|
|
|
5
5
|
const args = [...process.argv.slice(2)]
|
|
6
6
|
const currentFilePath = fileURLToPath(import.meta.url)
|
|
7
7
|
const currentDirPath = dirname(currentFilePath)
|
|
8
|
-
const js = join(currentDirPath, '../lib/index.js')
|
|
9
8
|
// 启动模式
|
|
10
9
|
if (args.includes('start')) {
|
|
10
|
+
console.log('start')
|
|
11
|
+
const js = join(currentDirPath, '../lib/index.js')
|
|
11
12
|
const argsx = args.filter(arg => arg !== 'start')
|
|
12
|
-
const msg = spawnSync('node', [js, '--start', ...argsx], { stdio: 'inherit' })
|
|
13
|
+
const msg = spawnSync('node', [js, '--alemonjs-start', ...argsx], { stdio: 'inherit' })
|
|
13
14
|
if (msg.error) {
|
|
14
15
|
console.error(msg.error)
|
|
15
16
|
process.exit()
|
|
16
17
|
}
|
|
17
18
|
} else if (args.includes('build')) {
|
|
19
|
+
const js = join(currentDirPath, '../lib/index.js')
|
|
18
20
|
const argsx = args.filter(arg => arg !== 'build')
|
|
19
|
-
const msg = spawnSync('npx', ['tsx', js, '--build', ...argsx], {
|
|
21
|
+
const msg = spawnSync('npx', ['tsx', js, '--alemonjs-build', ...argsx], {
|
|
20
22
|
stdio: 'inherit'
|
|
21
23
|
})
|
|
22
24
|
if (msg.error) {
|
|
@@ -24,10 +26,15 @@ if (args.includes('start')) {
|
|
|
24
26
|
process.exit()
|
|
25
27
|
}
|
|
26
28
|
} else if (args.includes('dev')) {
|
|
29
|
+
const js = join(currentDirPath, '../index.js')
|
|
27
30
|
const argsx = args.filter(arg => arg !== 'dev')
|
|
28
|
-
const msg = spawnSync(
|
|
29
|
-
|
|
30
|
-
|
|
31
|
+
const msg = spawnSync(
|
|
32
|
+
'npx',
|
|
33
|
+
['tsx', 'watch', '--clear-screen=false', js, '---alemonjs-dev', ...argsx],
|
|
34
|
+
{
|
|
35
|
+
stdio: 'inherit'
|
|
36
|
+
}
|
|
37
|
+
)
|
|
31
38
|
if (msg.error) {
|
|
32
39
|
console.error(msg.error)
|
|
33
40
|
process.exit()
|
package/env.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
// CSS
|
|
2
2
|
declare module '*.css' {
|
|
3
|
-
const
|
|
4
|
-
export default
|
|
3
|
+
const src: string
|
|
4
|
+
export default src
|
|
5
5
|
}
|
|
6
|
-
|
|
7
|
-
// images
|
|
8
|
-
declare module '*.apng' {
|
|
6
|
+
declare module '*.scss' {
|
|
9
7
|
const src: string
|
|
10
8
|
export default src
|
|
11
9
|
}
|
|
10
|
+
|
|
11
|
+
// images
|
|
12
12
|
declare module '*.png' {
|
|
13
13
|
const src: string
|
|
14
14
|
export default src
|
|
@@ -21,18 +21,7 @@ declare module '*.jpeg' {
|
|
|
21
21
|
const src: string
|
|
22
22
|
export default src
|
|
23
23
|
}
|
|
24
|
-
|
|
25
|
-
const src: string
|
|
26
|
-
export default src
|
|
27
|
-
}
|
|
28
|
-
declare module '*.pjpeg' {
|
|
29
|
-
const src: string
|
|
30
|
-
export default src
|
|
31
|
-
}
|
|
32
|
-
declare module '*.pjp' {
|
|
33
|
-
const src: string
|
|
34
|
-
export default src
|
|
35
|
-
}
|
|
24
|
+
|
|
36
25
|
declare module '*.gif' {
|
|
37
26
|
const src: string
|
|
38
27
|
export default src
|
|
@@ -41,20 +30,16 @@ declare module '*.svg' {
|
|
|
41
30
|
const src: string
|
|
42
31
|
export default src
|
|
43
32
|
}
|
|
44
|
-
declare module '*.ico' {
|
|
45
|
-
const src: string
|
|
46
|
-
export default src
|
|
47
|
-
}
|
|
48
33
|
declare module '*.webp' {
|
|
49
34
|
const src: string
|
|
50
35
|
export default src
|
|
51
36
|
}
|
|
52
|
-
declare module '*.
|
|
37
|
+
declare module '*.ico' {
|
|
53
38
|
const src: string
|
|
54
39
|
export default src
|
|
55
40
|
}
|
|
56
41
|
|
|
57
|
-
//
|
|
42
|
+
// vidoe
|
|
58
43
|
declare module '*.mp4' {
|
|
59
44
|
const src: string
|
|
60
45
|
export default src
|
|
@@ -63,10 +48,12 @@ declare module '*.webm' {
|
|
|
63
48
|
const src: string
|
|
64
49
|
export default src
|
|
65
50
|
}
|
|
66
|
-
declare module '*.
|
|
51
|
+
declare module '*.ogv' {
|
|
67
52
|
const src: string
|
|
68
53
|
export default src
|
|
69
54
|
}
|
|
55
|
+
|
|
56
|
+
// audio
|
|
70
57
|
declare module '*.mp3' {
|
|
71
58
|
const src: string
|
|
72
59
|
export default src
|
|
@@ -75,27 +62,7 @@ declare module '*.wav' {
|
|
|
75
62
|
const src: string
|
|
76
63
|
export default src
|
|
77
64
|
}
|
|
78
|
-
declare module '*.
|
|
79
|
-
const src: string
|
|
80
|
-
export default src
|
|
81
|
-
}
|
|
82
|
-
declare module '*.aac' {
|
|
83
|
-
const src: string
|
|
84
|
-
export default src
|
|
85
|
-
}
|
|
86
|
-
declare module '*.opus' {
|
|
87
|
-
const src: string
|
|
88
|
-
export default src
|
|
89
|
-
}
|
|
90
|
-
declare module '*.mov' {
|
|
91
|
-
const src: string
|
|
92
|
-
export default src
|
|
93
|
-
}
|
|
94
|
-
declare module '*.m4a' {
|
|
95
|
-
const src: string
|
|
96
|
-
export default src
|
|
97
|
-
}
|
|
98
|
-
declare module '*.vtt' {
|
|
65
|
+
declare module '*.ogg' {
|
|
99
66
|
const src: string
|
|
100
67
|
export default src
|
|
101
68
|
}
|
package/index.js
ADDED
package/lib/build/rullup.js
CHANGED
|
@@ -4,28 +4,7 @@ import { readdirSync } from 'fs';
|
|
|
4
4
|
import typescript from '@rollup/plugin-typescript';
|
|
5
5
|
import commonjs from '@rollup/plugin-commonjs';
|
|
6
6
|
import json from '@rollup/plugin-json';
|
|
7
|
-
import { rollupNodeFiles } from './loader-files.js';
|
|
8
7
|
|
|
9
|
-
// 尝试从参数中,得到更高优先级的配置
|
|
10
|
-
const argv = [...process.argv].slice(2);
|
|
11
|
-
/**
|
|
12
|
-
* @param key 参数
|
|
13
|
-
* @returns 参数值
|
|
14
|
-
*/
|
|
15
|
-
const getArgvValue = key => {
|
|
16
|
-
const v = argv.indexOf(key);
|
|
17
|
-
if (v === -1)
|
|
18
|
-
return null;
|
|
19
|
-
const next = argv[v + 1];
|
|
20
|
-
if (typeof next == 'string') {
|
|
21
|
-
// 如果是参数
|
|
22
|
-
if (next.startsWith('-'))
|
|
23
|
-
return null;
|
|
24
|
-
// 如果是值
|
|
25
|
-
return next;
|
|
26
|
-
}
|
|
27
|
-
return null;
|
|
28
|
-
};
|
|
29
8
|
/**
|
|
30
9
|
* 获取指定目录下的所有 ts、js、jsx、tsx 文件
|
|
31
10
|
* @param dir 目录路径
|
|
@@ -37,7 +16,7 @@ const getFiles = (dir) => {
|
|
|
37
16
|
list.forEach(item => {
|
|
38
17
|
const fullPath = join(dir, item.name);
|
|
39
18
|
if (item.isDirectory()) {
|
|
40
|
-
results.push(...getFiles(fullPath));
|
|
19
|
+
results.push(...getFiles(fullPath));
|
|
41
20
|
}
|
|
42
21
|
else if (item.isFile() &&
|
|
43
22
|
/\.(ts|js|jsx|tsx)$/.test(item.name) &&
|
|
@@ -57,39 +36,28 @@ const onwarn = (warning, warn) => {
|
|
|
57
36
|
return;
|
|
58
37
|
warn(warning);
|
|
59
38
|
};
|
|
60
|
-
/**
|
|
61
|
-
*
|
|
62
|
-
* @returns
|
|
63
|
-
*/
|
|
64
|
-
const getRexs = () => {
|
|
65
|
-
if (argv.includes('--esms-no-import')) {
|
|
66
|
-
return [];
|
|
67
|
-
}
|
|
68
|
-
const i = getArgvValue('--esms-image-import');
|
|
69
|
-
const i_rex = i ? new RegExp(i) : /(\.png|\.jpg|\.jpeg|\.gif|\.svg|.webp)$/;
|
|
70
|
-
const s = getArgvValue('--esms-css-import');
|
|
71
|
-
const s_rex2 = s ? new RegExp(s) : /(\.css)$/;
|
|
72
|
-
const v = getArgvValue('--esms-vidoe-import');
|
|
73
|
-
const v_rex3 = v ? new RegExp(v) : /(\.mp4|\.webm|\.ogv)$/;
|
|
74
|
-
const v2 = getArgvValue('--esms-aodio-import');
|
|
75
|
-
const v_rex4 = v2 ? new RegExp(v2) : /(\.mp3|\.wav|\.ogg)$/;
|
|
76
|
-
return [i_rex, s_rex2, v_rex3, v_rex4];
|
|
77
|
-
};
|
|
78
39
|
/**
|
|
79
40
|
*
|
|
80
41
|
* @param inputs
|
|
81
42
|
* @param output
|
|
82
43
|
*/
|
|
83
44
|
const buildJs = async (inputs, output) => {
|
|
84
|
-
|
|
45
|
+
// 从全局变量中,拿到 rollup 的配置
|
|
46
|
+
const plguins = [];
|
|
47
|
+
// 存在build配置
|
|
48
|
+
if (global?.alemonjsconfig?.build) {
|
|
49
|
+
const p = global.alemonjsconfig.build?.plugins;
|
|
50
|
+
if (p && Array.isArray(p)) {
|
|
51
|
+
// 遍历插件
|
|
52
|
+
const ps = p.filter(item => item.belong == 'rollup');
|
|
53
|
+
for (const item of ps) {
|
|
54
|
+
plguins.push(item.load());
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
85
58
|
const bundle = await rollup({
|
|
86
59
|
input: inputs,
|
|
87
|
-
plugins: [
|
|
88
|
-
...rexs.map(rex => rollupNodeFiles({ filter: rex })),
|
|
89
|
-
commonjs(),
|
|
90
|
-
json(),
|
|
91
|
-
typescript()
|
|
92
|
-
],
|
|
60
|
+
plugins: [...plguins, commonjs(), json(), typescript()],
|
|
93
61
|
onwarn: onwarn
|
|
94
62
|
});
|
|
95
63
|
// 写入输出文件
|
|
@@ -106,10 +74,7 @@ const buildJs = async (inputs, output) => {
|
|
|
106
74
|
*/
|
|
107
75
|
async function buildAndRun(input, output) {
|
|
108
76
|
const inputFiles = getFiles(join(process.cwd(), input));
|
|
109
|
-
// output js
|
|
110
77
|
await buildJs(inputFiles, output);
|
|
111
|
-
// output d.ts
|
|
112
|
-
// await buildDts(inputFiles, output)
|
|
113
78
|
}
|
|
114
79
|
|
|
115
80
|
export { buildAndRun };
|
package/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { BuildOptions } from 'esbuild';
|
|
2
|
-
import { RollupOptions } from 'rollup';
|
|
3
1
|
export { Config, argv, getArgvValue } from './config.js';
|
|
4
2
|
export { useOberver, useParse, useSend } from './hook/use-api.js';
|
|
5
3
|
export { OnObserver, OnResponse, ResponseConfig } from './app/event-utlis.js';
|
|
@@ -9,23 +7,5 @@ export { OnProcessor, pushAppsFiles } from './app/event-processor.js';
|
|
|
9
7
|
export { getAppsFiles } from './app/event-files.js';
|
|
10
8
|
export { defineBot } from './app/event-bot.js';
|
|
11
9
|
export { defineChildren } from './app/event-chidren.js';
|
|
10
|
+
export { defineConfig } from './store.js';
|
|
12
11
|
export { Ark, At, BtBox, Button, Embed, Emoji, Files, Image, Link, Text, Video, Voice } from './hook/message-format.js';
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* 编译配置
|
|
16
|
-
*/
|
|
17
|
-
type Options = {
|
|
18
|
-
input: string;
|
|
19
|
-
build?: {
|
|
20
|
-
esBuildOptions?: BuildOptions;
|
|
21
|
-
rollupOptions?: RollupOptions;
|
|
22
|
-
};
|
|
23
|
-
};
|
|
24
|
-
/**
|
|
25
|
-
*
|
|
26
|
-
* @param param0
|
|
27
|
-
* @returns
|
|
28
|
-
*/
|
|
29
|
-
declare const defineConfig: (optoins?: Options) => Promise<Options>;
|
|
30
|
-
|
|
31
|
-
export { defineConfig };
|
package/lib/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import './main.js';
|
|
2
1
|
import { argv, getArgvValue, Config } from './config.js';
|
|
3
2
|
import { pushAppsFiles } from './app/event-processor.js';
|
|
4
3
|
export { OnProcessor } from './app/event-processor.js';
|
|
@@ -6,18 +5,14 @@ import { getAppsFiles } from './app/event-files.js';
|
|
|
6
5
|
import { dirname, join } from 'path';
|
|
7
6
|
import { existsSync, readFileSync } from 'fs';
|
|
8
7
|
import { buildAndRun } from './build/rullup.js';
|
|
8
|
+
import { initConfig } from './store.js';
|
|
9
|
+
export { defineConfig } from './store.js';
|
|
9
10
|
export { useOberver, useParse, useSend } from './hook/use-api.js';
|
|
10
11
|
export { OnObserver, OnResponse, ResponseConfig } from './app/event-utlis.js';
|
|
11
12
|
export { defineBot } from './app/event-bot.js';
|
|
12
13
|
export { defineChildren } from './app/event-chidren.js';
|
|
13
14
|
export { Ark, At, BtBox, Button, Embed, Emoji, Files, Image, Link, Text, Video, Voice } from './hook/message-format.js';
|
|
14
15
|
|
|
15
|
-
/**
|
|
16
|
-
*
|
|
17
|
-
* @param param0
|
|
18
|
-
* @returns
|
|
19
|
-
*/
|
|
20
|
-
const defineConfig = async (optoins) => optoins;
|
|
21
16
|
const cwd = process.cwd();
|
|
22
17
|
/**
|
|
23
18
|
*
|
|
@@ -79,7 +74,6 @@ const onDev = async (input) => {
|
|
|
79
74
|
const dir = join(cwd, input);
|
|
80
75
|
// src/apps/**/*
|
|
81
76
|
const mainDir = dirname(dir);
|
|
82
|
-
console.log('mainDir', mainDir);
|
|
83
77
|
const app = await import(`file://${dir}`);
|
|
84
78
|
const c = app?.default?.(cfg.values, cfg.value);
|
|
85
79
|
runChildren(mainDir);
|
|
@@ -147,70 +141,51 @@ const onStart = async (input) => {
|
|
|
147
141
|
}
|
|
148
142
|
await import(`${prefix}${cfg.values.login}`);
|
|
149
143
|
};
|
|
150
|
-
let options = null;
|
|
151
|
-
/**
|
|
152
|
-
*
|
|
153
|
-
*/
|
|
154
|
-
const initConfig = async () => {
|
|
155
|
-
const files = [
|
|
156
|
-
'alemon.config.ts',
|
|
157
|
-
'alemon.config.js',
|
|
158
|
-
'alemon.config.mjs',
|
|
159
|
-
'alemon.config.cjs',
|
|
160
|
-
'alemon.config.tsx'
|
|
161
|
-
];
|
|
162
|
-
let configDir = '';
|
|
163
|
-
for (const file of files) {
|
|
164
|
-
if (existsSync(file)) {
|
|
165
|
-
configDir = file;
|
|
166
|
-
break;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
if (configDir !== '') {
|
|
170
|
-
const v = await import(`file://${configDir}`);
|
|
171
|
-
if (v?.default) {
|
|
172
|
-
options = v.default;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
};
|
|
176
144
|
const main = async () => {
|
|
177
|
-
if (argv.includes('--dev')) {
|
|
178
|
-
await initConfig()
|
|
145
|
+
if (argv.includes('--alemonjs-dev')) {
|
|
146
|
+
// await initConfig()
|
|
179
147
|
// 开发模式
|
|
180
148
|
let input = getArgvValue('--input');
|
|
181
149
|
if (!input) {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
input =
|
|
150
|
+
input = 'src/index.ts';
|
|
151
|
+
}
|
|
152
|
+
if (!existsSync(input)) {
|
|
153
|
+
input = 'src/index.js';
|
|
186
154
|
}
|
|
187
155
|
if (!existsSync(input)) {
|
|
188
|
-
throw new Error('
|
|
156
|
+
throw new Error('src/index.js is required');
|
|
189
157
|
}
|
|
190
158
|
onDev(input);
|
|
191
159
|
}
|
|
192
|
-
else if (argv.includes('--build')) {
|
|
160
|
+
else if (argv.includes('--alemonjs-build')) {
|
|
193
161
|
await initConfig();
|
|
194
|
-
//
|
|
162
|
+
// 开发模式
|
|
195
163
|
let input = getArgvValue('--input');
|
|
196
164
|
if (!input) {
|
|
197
|
-
|
|
198
|
-
throw new Error('input is required');
|
|
199
|
-
}
|
|
200
|
-
input = options?.input;
|
|
165
|
+
input = 'src/index.ts';
|
|
201
166
|
}
|
|
202
167
|
if (!existsSync(input)) {
|
|
203
|
-
|
|
168
|
+
input = 'src/index.js';
|
|
169
|
+
}
|
|
170
|
+
if (!existsSync(input)) {
|
|
171
|
+
throw new Error('src/index.js is required');
|
|
204
172
|
}
|
|
205
173
|
const ouput = getArgvValue('--ouput') ?? 'lib';
|
|
206
174
|
onBuild(input, ouput);
|
|
207
175
|
}
|
|
208
|
-
else if (argv.includes('--
|
|
176
|
+
else if (argv.includes('--alemonjs-start')) {
|
|
209
177
|
// start 模式 没有config 没有ts环境
|
|
210
|
-
|
|
178
|
+
let input = getArgvValue('--input');
|
|
179
|
+
if (!input) {
|
|
180
|
+
// 存在lib/index.js
|
|
181
|
+
if (existsSync('lib/index.js')) {
|
|
182
|
+
input = 'lib/index.js';
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
// getOptions
|
|
211
186
|
onStart(input);
|
|
212
187
|
}
|
|
213
188
|
};
|
|
214
189
|
main();
|
|
215
190
|
|
|
216
|
-
export { Config, argv,
|
|
191
|
+
export { Config, argv, getAppsFiles, getArgvValue, pushAppsFiles };
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
4
|
+
// 尝试从参数中,得到更高优先级的配置
|
|
5
|
+
const argv = [...process.argv].slice(2);
|
|
6
|
+
/**
|
|
7
|
+
* @param key 参数
|
|
8
|
+
* @returns 参数值
|
|
9
|
+
*/
|
|
10
|
+
const getArgvValue = key => {
|
|
11
|
+
const v = argv.indexOf(key);
|
|
12
|
+
if (v === -1)
|
|
13
|
+
return null;
|
|
14
|
+
const next = argv[v + 1];
|
|
15
|
+
if (typeof next === 'string' && !next.startsWith('-')) {
|
|
16
|
+
return next;
|
|
17
|
+
}
|
|
18
|
+
return null;
|
|
19
|
+
};
|
|
20
|
+
const getRexs = () => {
|
|
21
|
+
const i = getArgvValue('--esms-image-import');
|
|
22
|
+
const i_rex = i ? new RegExp(i) : /(\.png|\.jpg|\.jpeg|\.gif|\.svg|.webp|.ico)$/;
|
|
23
|
+
const s = getArgvValue('--esms-css-import');
|
|
24
|
+
const s_rex2 = s ? new RegExp(s) : /(\.css|\.scss)$/;
|
|
25
|
+
const v = getArgvValue('--esms-vidoe-import');
|
|
26
|
+
const v_rex3 = v ? new RegExp(v) : /(\.mp4|\.webm|\.ogv)$/;
|
|
27
|
+
const v2 = getArgvValue('--esms-aodio-import');
|
|
28
|
+
const v_rex4 = v2 ? new RegExp(v2) : /(\.mp3|\.wav|\.ogg)$/;
|
|
29
|
+
const f = getArgvValue('--esms-font-import');
|
|
30
|
+
const f_rex5 = f ? new RegExp(f) : /(\.woff|\.woff2|\.eot|\.ttf|\.otf)$/;
|
|
31
|
+
const fi = getArgvValue('--esms-files-import');
|
|
32
|
+
const fi_rex6 = fi ? new RegExp(fi) : /(\.md)$/;
|
|
33
|
+
return [i_rex, s_rex2, v_rex3, v_rex4, f_rex5, fi_rex6];
|
|
34
|
+
};
|
|
35
|
+
let tsconfig = null;
|
|
36
|
+
let aliases = {};
|
|
37
|
+
// 初始化函数,读取并解析 tsconfig.json 文件
|
|
38
|
+
const init = () => {
|
|
39
|
+
try {
|
|
40
|
+
const tsconfigPath = path.resolve(process.cwd(), 'tsconfig.json');
|
|
41
|
+
if (fs.existsSync(tsconfigPath)) {
|
|
42
|
+
const tsconfigContent = fs.readFileSync(tsconfigPath, 'utf-8');
|
|
43
|
+
tsconfig = JSON.parse(tsconfigContent);
|
|
44
|
+
aliases = tsconfig.compilerOptions?.paths || {};
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
tsconfig = 'error';
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
tsconfig = 'error';
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
// 初始化配置
|
|
55
|
+
init();
|
|
56
|
+
/**
|
|
57
|
+
* 生成模块内容
|
|
58
|
+
* @param {string} relativePath 相对路径
|
|
59
|
+
* @returns {string} 模块内容
|
|
60
|
+
*/
|
|
61
|
+
const generateModuleContent = relativePath => `
|
|
62
|
+
const createUrl = (basePath, path) => {
|
|
63
|
+
const platform = ['linux', 'android', 'darwin'];
|
|
64
|
+
const T = platform.includes(process.platform);
|
|
65
|
+
const reg = T ? /^file:\\/\\// : /^file:\\/\\/\\//;
|
|
66
|
+
return new URL(path, basePath).href.replace(reg, '');
|
|
67
|
+
};
|
|
68
|
+
export default createUrl(import.meta.url, '${relativePath}');
|
|
69
|
+
`;
|
|
70
|
+
/**
|
|
71
|
+
* 处理非js文件的加载
|
|
72
|
+
* @param {string} url URL
|
|
73
|
+
* @param {object} context 上下文
|
|
74
|
+
* @returns {object|null} 加载结果
|
|
75
|
+
*/
|
|
76
|
+
const handleNonJsFiles = (url, context, defaultLoad) => {
|
|
77
|
+
if (argv.includes('--esms-no-import')) {
|
|
78
|
+
return defaultLoad(url, context);
|
|
79
|
+
}
|
|
80
|
+
const rexs = getRexs();
|
|
81
|
+
for (const rex of rexs) {
|
|
82
|
+
if (rex.test(url)) {
|
|
83
|
+
const outputDir = path.dirname(context.parentURL || 'index.js');
|
|
84
|
+
const relativePath = path.relative(outputDir, url);
|
|
85
|
+
return {
|
|
86
|
+
format: 'module',
|
|
87
|
+
source: generateModuleContent(relativePath),
|
|
88
|
+
shortCircuit: true
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return defaultLoad(url, context);
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* 处理路径别名的加载
|
|
96
|
+
* @param {string} url URL
|
|
97
|
+
* @returns {object|null} 加载结果
|
|
98
|
+
*/
|
|
99
|
+
const handlePathAliases = url => {
|
|
100
|
+
for (const alias in aliases) {
|
|
101
|
+
const aliasPattern = alias.replace('/*', '');
|
|
102
|
+
if (url.startsWith(aliasPattern)) {
|
|
103
|
+
const aliasPaths = aliases[alias];
|
|
104
|
+
for (const aliasPath of aliasPaths) {
|
|
105
|
+
const resolvedPath = path.resolve(process.cwd(), aliasPath.replace('/*', ''), url.replace(aliasPattern, ''));
|
|
106
|
+
const fileUrl = new URL(resolvedPath, import.meta.url).href;
|
|
107
|
+
return {
|
|
108
|
+
format: 'module',
|
|
109
|
+
source: `
|
|
110
|
+
const module = await import(${fileUrl});
|
|
111
|
+
export default module;
|
|
112
|
+
export * from ${fileUrl};
|
|
113
|
+
`,
|
|
114
|
+
shortCircuit: true
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return null;
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* 主加载函数
|
|
123
|
+
* @param {string} url URL
|
|
124
|
+
* @param {object} context 上下文
|
|
125
|
+
* @param {function} defaultLoad 默认加载函数
|
|
126
|
+
* @returns {object} 加载结果
|
|
127
|
+
*/
|
|
128
|
+
async function load(url, context, defaultLoad) {
|
|
129
|
+
// 禁用路径别名处理或 tsconfig.json 文件读取错误
|
|
130
|
+
if (argv.includes('--esms-no-path') || tsconfig === 'error') {
|
|
131
|
+
// 尝试调用非别名文件加载处理 ,否则调用默认加载函数
|
|
132
|
+
return handleNonJsFiles(url, context, defaultLoad);
|
|
133
|
+
}
|
|
134
|
+
// 处理路径别名的加载
|
|
135
|
+
const aliasResult = handlePathAliases(url);
|
|
136
|
+
if (aliasResult) {
|
|
137
|
+
return aliasResult;
|
|
138
|
+
}
|
|
139
|
+
// 处理非js文件的加载
|
|
140
|
+
return handleNonJsFiles(url, context, defaultLoad);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export { load };
|
package/lib/main.js
CHANGED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import * as rollup from 'rollup';
|
|
2
|
+
import { RollupAliasOptions } from '@rollup/plugin-alias';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* ruleup别名插件
|
|
6
|
+
* @param options
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
declare const alias: (options?: RollupAliasOptions) => {
|
|
10
|
+
belong: "rollup" | "esbuild" | "other";
|
|
11
|
+
name: string;
|
|
12
|
+
load: () => rollup.Plugin<any>;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* ruleup插件:对指定类型文件进行字符串引用处理
|
|
16
|
+
* @param options
|
|
17
|
+
* @returns
|
|
18
|
+
*/
|
|
19
|
+
declare const files: (options: {
|
|
20
|
+
filter: RegExp;
|
|
21
|
+
}) => {
|
|
22
|
+
belong: "rollup" | "esbuild" | "other";
|
|
23
|
+
name: string;
|
|
24
|
+
load: () => rollup.InputPluginOption;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export { alias, files };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import aliasx from '@rollup/plugin-alias';
|
|
2
|
+
import { rollupNodeFiles } from './loader-files.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* ruleup别名插件
|
|
6
|
+
* @param options
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
const alias = (options) => {
|
|
10
|
+
return {
|
|
11
|
+
belong: 'rollup',
|
|
12
|
+
name: 'alias',
|
|
13
|
+
load: () => aliasx(options)
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* ruleup插件:对指定类型文件进行字符串引用处理
|
|
18
|
+
* @param options
|
|
19
|
+
* @returns
|
|
20
|
+
*/
|
|
21
|
+
const files = (options) => {
|
|
22
|
+
return {
|
|
23
|
+
belong: 'rollup',
|
|
24
|
+
name: 'import-files',
|
|
25
|
+
load: () => rollupNodeFiles(options)
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export { alias, files };
|
package/lib/store.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { RollupOptions } from 'rollup';
|
|
2
|
+
|
|
3
|
+
type Options = {
|
|
4
|
+
build?: {
|
|
5
|
+
plugins?: {
|
|
6
|
+
belong: 'rollup' | 'esbuild' | 'other';
|
|
7
|
+
name: string;
|
|
8
|
+
load: any;
|
|
9
|
+
}[];
|
|
10
|
+
rollupOptions?: {
|
|
11
|
+
input?: string | string[];
|
|
12
|
+
} & RollupOptions;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
declare global {
|
|
16
|
+
var alemonjsconfig: Options;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
*
|
|
20
|
+
* @param param0
|
|
21
|
+
* @returns
|
|
22
|
+
*/
|
|
23
|
+
declare const defineConfig: (optoins?: Options) => Options;
|
|
24
|
+
|
|
25
|
+
export { type Options, defineConfig };
|
package/lib/store.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { existsSync } from 'fs';
|
|
2
|
+
import { join } from 'path';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
*/
|
|
7
|
+
const initConfig = async () => {
|
|
8
|
+
if (!global.alemonjsconfig) {
|
|
9
|
+
// init
|
|
10
|
+
global.alemonjsconfig = {};
|
|
11
|
+
}
|
|
12
|
+
const files = [
|
|
13
|
+
'alemon.config.ts',
|
|
14
|
+
'alemon.config.js',
|
|
15
|
+
'alemon.config.mjs',
|
|
16
|
+
'alemon.config.cjs',
|
|
17
|
+
'alemon.config.tsx'
|
|
18
|
+
];
|
|
19
|
+
let configDir = '';
|
|
20
|
+
for (const file of files) {
|
|
21
|
+
if (existsSync(file)) {
|
|
22
|
+
configDir = file;
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (configDir !== '') {
|
|
27
|
+
const v = await import(`file://${join(process.cwd(), configDir)}`);
|
|
28
|
+
if (v?.default) {
|
|
29
|
+
global.alemonjsconfig = v.default;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
*
|
|
35
|
+
* @param param0
|
|
36
|
+
* @returns
|
|
37
|
+
*/
|
|
38
|
+
const defineConfig = (optoins) => optoins;
|
|
39
|
+
|
|
40
|
+
export { defineConfig, initConfig };
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "alemonjs",
|
|
3
|
-
"version": "2.0.0-rc.
|
|
3
|
+
"version": "2.0.0-rc.14",
|
|
4
4
|
"description": "啊柠檬脚本",
|
|
5
5
|
"author": "ningmengchongshui",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"main": "lib/index.js",
|
|
9
9
|
"dependencies": {
|
|
10
|
+
"@rollup/plugin-node-resolve": "^15.3.0",
|
|
10
11
|
"@rollup/plugin-alias": "^5.1.0",
|
|
11
12
|
"@rollup/plugin-commonjs": "^28.0.0",
|
|
12
13
|
"@rollup/plugin-json": "^6.1.0",
|
|
@@ -26,11 +27,19 @@
|
|
|
26
27
|
"types": "./lib/index.d.ts"
|
|
27
28
|
},
|
|
28
29
|
"./loader": {
|
|
29
|
-
"import": "./
|
|
30
|
+
"import": "./lib/loader/index.js",
|
|
31
|
+
"types": "./lib/loader/index.d.ts"
|
|
32
|
+
},
|
|
33
|
+
"./register": {
|
|
34
|
+
"import": "./lib/main.js"
|
|
30
35
|
},
|
|
31
36
|
"./env": {
|
|
32
37
|
"types": "./env.d.ts"
|
|
33
38
|
},
|
|
39
|
+
"./plugins": {
|
|
40
|
+
"import": "./lib/plugins/index.js",
|
|
41
|
+
"types": "./lib/plugins/index.d.ts"
|
|
42
|
+
},
|
|
34
43
|
"./tsconfig.json": {
|
|
35
44
|
"import": "./tsconfig.json",
|
|
36
45
|
"require": "./tsconfig.json",
|
package/dist/loader.mjs
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import path from 'path'
|
|
2
|
-
|
|
3
|
-
// 尝试从参数中,得到更高优先级的配置
|
|
4
|
-
const argv = [...process.argv].slice(2)
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @param key 参数
|
|
8
|
-
* @returns 参数值
|
|
9
|
-
*/
|
|
10
|
-
const getArgvValue = key => {
|
|
11
|
-
const v = argv.indexOf(key)
|
|
12
|
-
if (v === -1) return null
|
|
13
|
-
const next = argv[v + 1]
|
|
14
|
-
if (typeof next == 'string') {
|
|
15
|
-
// 如果是参数
|
|
16
|
-
if (next.startsWith('-')) return null
|
|
17
|
-
// 如果是值
|
|
18
|
-
return next
|
|
19
|
-
}
|
|
20
|
-
return null
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const getRexs = () => {
|
|
24
|
-
const i = getArgvValue('--esms-image-import')
|
|
25
|
-
const i_rex = i ? new RegExp(i) : /(\.png|\.jpg|\.jpeg|\.gif|\.svg|.webp)$/
|
|
26
|
-
const s = getArgvValue('--esms-css-import')
|
|
27
|
-
const s_rex2 = s ? new RegExp(s) : /(\.css)$/
|
|
28
|
-
const v = getArgvValue('--esms-vidoe-import')
|
|
29
|
-
const v_rex3 = v ? new RegExp(v) : /(\.mp4|\.webm|\.ogv)$/
|
|
30
|
-
const v2 = getArgvValue('--esms-aodio-import')
|
|
31
|
-
const v_rex4 = v2 ? new RegExp(v2) : /(\.mp3|\.wav|\.ogg)$/
|
|
32
|
-
return [i_rex, s_rex2, v_rex3, v_rex4]
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* 处理非js文件的加载
|
|
37
|
-
* @param {*} url
|
|
38
|
-
* @param {*} context
|
|
39
|
-
* @param {*} defaultLoad
|
|
40
|
-
* @returns
|
|
41
|
-
*/
|
|
42
|
-
export async function load(url, context, defaultLoad) {
|
|
43
|
-
// 禁用loader
|
|
44
|
-
if (argv.includes('--esms-no-import')) {
|
|
45
|
-
return defaultLoad(url, context)
|
|
46
|
-
}
|
|
47
|
-
const getImport = () => {
|
|
48
|
-
// 获取输出目录
|
|
49
|
-
const outputDir = path.dirname(context.parentURL || 'index.js')
|
|
50
|
-
// 计算相对路径
|
|
51
|
-
const relativePath = path.relative(outputDir, url)
|
|
52
|
-
// 生成模块内容
|
|
53
|
-
const contents = `
|
|
54
|
-
const createUrl = (basePath, path) => {
|
|
55
|
-
const platform = ['linux', 'android', 'darwin'];
|
|
56
|
-
const T = platform.includes(process.platform);
|
|
57
|
-
const reg = T ? /^file:\\/\\// : /^file:\\/\\/\\//;
|
|
58
|
-
return new URL(path, basePath).href.replace(reg, '');
|
|
59
|
-
};
|
|
60
|
-
export default createUrl(import.meta.url, '${relativePath}');
|
|
61
|
-
`
|
|
62
|
-
return {
|
|
63
|
-
format: 'module',
|
|
64
|
-
source: contents,
|
|
65
|
-
// 防止进一步处理
|
|
66
|
-
shortCircuit: true
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
const rexs = getRexs()
|
|
70
|
-
for(const rex of rexs) {
|
|
71
|
-
if (rex.test(url)) {
|
|
72
|
-
return getImport()
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
return defaultLoad(url, context)
|
|
76
|
-
}
|