lvyjs 0.2.15 → 0.2.16

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/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 };
@@ -0,0 +1,28 @@
1
+ import { MessagePort } from 'worker_threads';
2
+
3
+ declare global {
4
+ var lvyWorkerProt: MessagePort;
5
+ }
6
+ /**
7
+ * @param param0
8
+ */
9
+ declare function initialize({ port, lvyConfig }: {
10
+ port: any;
11
+ lvyConfig: any;
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>;
20
+ /**
21
+ * @param url
22
+ * @param context
23
+ * @param defaultLoad
24
+ * @returns
25
+ */
26
+ declare const load: (url: any, context: any, nextLoad: any) => any;
27
+
28
+ export { initialize, load, resolve };
package/lib/loader.js ADDED
@@ -0,0 +1,74 @@
1
+ import { generateModuleContent, generateCSSModuleContent } from './content.js';
2
+ import { isWin32, convertPath } from './config.js';
3
+
4
+ const reg = isWin32() ? /^file:\/\/\// : /^file:\/\//;
5
+ const baseURL = isWin32() ? 'file:///' : 'file://';
6
+ const nodeReg = /(node_modules|node_|node:)/;
7
+ /**
8
+ * @param param0
9
+ */
10
+ async function initialize({ port, lvyConfig }) {
11
+ global.lvyConfig = lvyConfig;
12
+ global.lvyWorkerProt = port;
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
+ }
37
+ /**
38
+ * @param url
39
+ * @param context
40
+ * @param defaultLoad
41
+ * @returns
42
+ */
43
+ const load = (url, context, nextLoad) => {
44
+ if (nodeReg.test(url)) {
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 = {};
63
+ }
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
+ });
70
+ }
71
+ return nextLoad(url, context);
72
+ };
73
+
74
+ export { initialize, load, resolve };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lvyjs",
3
- "version": "0.2.15",
3
+ "version": "0.2.16",
4
4
  "description": "tsx compile script",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",