lvyjs 0.2.17 → 0.2.19

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 CHANGED
@@ -1,5 +1,5 @@
1
- # LVY
1
+ # LYVJS
2
2
 
3
- 一款为 node 构建的开发工具
3
+ 一款为 NodeJS 构建的开发工具
4
4
 
5
- https://github.com/lemonade-lab/lvyjs
5
+ https://lvyjs.dev
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
- const alias = {}
10
- // 遍历 entries 数组
11
- val.entries.forEach(entry => {
12
- alias[entry.find] = entry.replacement
13
- })
14
- return alias
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
- return ['win32'].includes(process.platform)
18
- }
19
- const convertPath = inputPath => {
20
- return isWin32() ? inputPath.replace(/\\/g, '/') : inputPath
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/content.js CHANGED
@@ -1,48 +1,50 @@
1
- import { join } from 'path'
2
- import crypto from 'node:crypto'
3
- import { convertPath } from './config.js'
1
+ import crypto from 'node:crypto';
2
+ import { convertPath } from './config.js';
3
+ import { fileURLToPath } from 'url';
4
+ import { dirname, join } from 'path';
4
5
 
6
+ const __dirname = dirname(fileURLToPath(import.meta.url));
5
7
  /**
6
8
  * 生成模块内容
7
9
  * @param {string} relativePath 相对路径
8
10
  */
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 = {}
11
+ const generateModuleContent = (relativePath) => {
12
+ const baseURL = decodeURIComponent(relativePath);
13
+ const contents = [
14
+ `const reg = ['win32'].includes(process.platform) ? /^file:\\/\\/\\// : /^file:\\/\\// ;`,
15
+ `const fileUrl = import.meta.resolve('${convertPath(baseURL)}').replace(reg, '');`,
16
+ 'export default fileUrl;'
17
+ ].join('\n');
18
+ return contents;
19
+ };
20
+ const getRandomName = (str) => {
21
+ // 使用 MD5 算法创建哈希对象
22
+ const hash = crypto.createHash('md5');
23
+ // 更新哈希对象内容
24
+ hash.update(str);
25
+ return hash.digest('hex');
26
+ };
27
+ const chache = {};
25
28
  /**
26
29
  *
27
30
  * @param fileUrl
28
31
  * @returns
29
32
  */
30
- const generateCSSModuleContent = pathURL => {
31
- const fileName = getRandomName(pathURL)
32
- const outputFileURL = convertPath(
33
- join(process.cwd(), 'node_modules', 'lvyjs', 'assets', `${fileName}.css`)
34
- )
35
- if (!chache[pathURL]) {
36
- global.lvyWorkerProt.postMessage({
37
- type: 'CSS_MODULE_GENERATED',
38
- payload: {
39
- from: pathURL,
40
- to: outputFileURL
41
- }
42
- })
43
- chache[pathURL] = true
44
- }
45
- return `export default "${outputFileURL}";`
46
- }
33
+ const generateCSSModuleContent = (relativePath) => {
34
+ const inputURL = decodeURIComponent(relativePath);
35
+ const fileName = getRandomName(inputURL);
36
+ const outputURL = convertPath(join(__dirname, 'assets', `${fileName}.css`));
37
+ if (!chache[inputURL]) {
38
+ global.lvyWorkerProt.postMessage({
39
+ type: 'CSS_MODULE_GENERATED',
40
+ payload: {
41
+ from: inputURL,
42
+ to: outputURL
43
+ }
44
+ });
45
+ chache[inputURL] = true;
46
+ }
47
+ return `export default "${outputURL}";`;
48
+ };
47
49
 
48
- export { generateCSSModuleContent, generateModuleContent }
50
+ export { generateCSSModuleContent, generateModuleContent };
package/lib/index.d.ts CHANGED
@@ -1,11 +1,3 @@
1
- export {
2
- Options,
3
- PluginsCallBack,
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
- 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))
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
- // 执行loader
22
- await import('./main.js')
23
- //
24
- for (const app of apps) {
25
- if (!app) {
26
- continue
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
- if (typeof app == 'function') {
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
- const apps = []
40
- if (Array.isArray(global.lvyConfig?.plugins)) {
41
- // 修改config
42
- for (const plugin of global.lvyConfig.plugins) {
43
- if (!plugin) {
44
- continue
45
- }
46
- await apps.push(plugin(global.lvyConfig))
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
- if (typeof app != 'function' && typeof app.build == 'function') {
55
- await app.build(global.lvyConfig)
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
- if (process.argv.includes('--lvy-dev')) {
61
- await initConfig()
62
- await onDev()
63
- } else if (process.argv.includes('--lvy-build')) {
64
- await initConfig()
65
- await onBuild()
66
- buildAndRun()
67
- }
68
- }
69
- main()
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/loader.d.ts CHANGED
@@ -1,25 +1,31 @@
1
- import { MessagePort } from 'worker_threads'
1
+ import { MessagePort } from 'worker_threads';
2
2
 
3
3
  declare global {
4
- var lvyWorkerProt: MessagePort
4
+ var lvyWorkerProt: MessagePort;
5
5
  }
6
6
  /**
7
+ * 初始化时
7
8
  * @param param0
8
9
  */
9
- declare function initialize({ port, lvyConfig }: { port: any; lvyConfig: any }): Promise<void>
10
+ declare function initialize({ port, lvyConfig }: {
11
+ port: any;
12
+ lvyConfig: any;
13
+ }): Promise<void>;
10
14
  /**
15
+ * 启动时
11
16
  * @param specifier
12
17
  * @param context
13
18
  * @param nextResolve
14
19
  * @returns
15
20
  */
16
- declare function resolve(specifier: any, context: any, nextResolve: any): Promise<any>
21
+ declare function resolve(specifier: any, context: any, nextResolve: any): Promise<any>;
17
22
  /**
23
+ * 加载脚本时
18
24
  * @param url
19
25
  * @param context
20
26
  * @param defaultLoad
21
27
  * @returns
22
28
  */
23
- declare const load: (url: any, context: any, nextLoad: any) => any
29
+ declare const load: (url: any, context: any, nextLoad: any) => any;
24
30
 
25
- export { initialize, load, resolve }
31
+ export { initialize, load, resolve };
package/lib/loader.js CHANGED
@@ -1,74 +1,82 @@
1
- import { generateModuleContent, generateCSSModuleContent } from './content.js'
2
- import { isWin32, convertPath } from './config.js'
1
+ import { generateModuleContent, generateCSSModuleContent } from './content.js';
2
+ import { isWin32, convertPath } from './config.js';
3
3
 
4
- const reg = isWin32() ? /^file:\/\/\// : /^file:\/\//
5
- const baseURL = isWin32() ? 'file:///' : 'file://'
6
- const nodeReg = /(node_modules|node_|node:)/
4
+ // 用来消去 路径中的 file:///
5
+ const reg = isWin32() ? /^file:\/\/\// : /^file:\/\//;
6
+ const baseURL = isWin32() ? 'file:///' : 'file://';
7
+ const nodeReg = /(node_modules|node_|node:)/;
7
8
  /**
9
+ * 初始化时
8
10
  * @param param0
9
11
  */
10
12
  async function initialize({ port, lvyConfig }) {
11
- global.lvyConfig = lvyConfig
12
- global.lvyWorkerProt = port
13
+ global.lvyConfig = lvyConfig;
14
+ global.lvyWorkerProt = port;
13
15
  }
14
16
  /**
17
+ * 启动时
15
18
  * @param specifier
16
19
  * @param context
17
20
  * @param nextResolve
18
21
  * @returns
19
22
  */
20
23
  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
- }
24
+ if (!global.lvyConfig?.alias) {
25
+ global.lvyConfig.alias = {};
33
26
  }
34
- }
35
- return nextResolve(specifier, context)
27
+ if (global.lvyConfig.alias?.entries) {
28
+ for (const { find, replacement } of global.lvyConfig.alias?.entries) {
29
+ if (specifier.startsWith(find)) {
30
+ const parentURL = `${baseURL}${convertPath(specifier.replace(find, replacement))}`;
31
+ return nextResolve(specifier, {
32
+ ...context,
33
+ parentURL: parentURL
34
+ });
35
+ }
36
+ }
37
+ }
38
+ return nextResolve(specifier, context);
36
39
  }
37
40
  /**
41
+ * 加载脚本时
38
42
  * @param url
39
43
  * @param context
40
44
  * @param defaultLoad
41
45
  * @returns
42
46
  */
43
47
  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
- }
48
+ if (nodeReg.test(url)) {
49
+ return nextLoad(url, context);
50
+ }
51
+ const urls = url.split('?');
52
+ const baseURL = urls[0];
53
+ // 得到静态资源。转为普通的文件引用。
54
+ if (!global.lvyConfig.assets) {
55
+ global.lvyConfig.assets = {};
56
+ }
57
+ // 匹配静态资源
58
+ if (global.lvyConfig.assets?.filter && global.lvyConfig.assets?.filter.test(baseURL)) {
59
+ // 消除 file:///
60
+ const url = baseURL.replace(reg, '');
61
+ const code = generateModuleContent(url);
62
+ return nextLoad(baseURL, {
63
+ format: 'module',
64
+ source: code
65
+ });
66
+ }
67
+ if (!global.lvyConfig.styles) {
68
+ global.lvyConfig.styles = {};
69
+ }
70
+ if (global.lvyConfig.styles?.filter && global.lvyConfig.styles?.filter.test(baseURL)) {
71
+ // 消除 file:///
72
+ const url = baseURL.replace(reg, '');
73
+ const code = generateCSSModuleContent(url);
74
+ return nextLoad(baseURL, {
75
+ format: 'module',
76
+ source: code
77
+ });
78
+ }
79
+ return nextLoad(url, context);
80
+ };
73
81
 
74
- export { initialize, load, resolve }
82
+ export { initialize, load, resolve };
package/lib/main.js CHANGED
@@ -1,38 +1,38 @@
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'
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
- throw new Error(
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
- if (msg.type == 'CSS_MODULE_GENERATED') {
15
- const { from, to } = msg.payload
16
- if (!cache[from]) {
17
- postCSS(from, to)
18
- cache[from] = true
12
+ if (msg.type == 'CSS_MODULE_GENERATED') {
13
+ const { from, to } = msg.payload;
14
+ if (!cache[from]) {
15
+ const formPath = decodeURIComponent(from);
16
+ postCSS(formPath, to);
17
+ // postCSS(from, to)
18
+ cache[from] = true;
19
+ }
19
20
  }
20
- }
21
- })
21
+ });
22
22
  // port1.unref()
23
23
  module.register('./loader.js', {
24
- parentURL: import.meta.url,
25
- data: {
26
- port: port2,
27
- lvyConfig: {
28
- alias: global.lvyConfig?.alias,
29
- assets: global.lvyConfig?.assets ?? {
30
- filter: assetsRegExp
31
- },
32
- styles: global.lvyConfig?.styles ?? {
33
- filter: stylesRegExp
34
- }
35
- }
36
- },
37
- transferList: [port2]
38
- })
24
+ parentURL: import.meta.url,
25
+ data: {
26
+ port: port2,
27
+ lvyConfig: {
28
+ alias: global.lvyConfig?.alias,
29
+ assets: global.lvyConfig?.assets ?? {
30
+ filter: assetsRegExp
31
+ },
32
+ styles: global.lvyConfig?.styles ?? {
33
+ filter: stylesRegExp
34
+ }
35
+ }
36
+ },
37
+ transferList: [port2]
38
+ });