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 CHANGED
@@ -3,23 +3,15 @@
3
3
  import { fork } from 'child_process'
4
4
  import { join, dirname, relative } from 'path'
5
5
  import { fileURLToPath } from 'node:url'
6
- import { existsSync } from 'fs'
6
+ import { createRequire } from 'node:module'
7
7
  const args = [...process.argv.slice(2)]
8
8
  const currentFilePath = fileURLToPath(import.meta.url)
9
9
  const currentDirPath = dirname(currentFilePath)
10
10
  const pkgFilr = join(currentDirPath, '../package.json')
11
11
  const jsFile = join(currentDirPath, '../lib/index.js')
12
12
  const jsdir = relative(process.cwd(), jsFile)
13
- let tsxDir = join(currentDirPath, '../../tsx/dist/cli.mjs')
14
- if (!existsSync(tsxDir)) {
15
- tsxDir = join(currentDirPath, '../node_modules/tsx/dist/cli.mjs')
16
- }
17
- if (!existsSync(tsxDir)) {
18
- tsxDir = join(process.cwd(), 'node_modules/tsx/dist/cli.mjs')
19
- }
20
- if (!existsSync(tsxDir)) {
21
- new Error('tsx not found')
22
- }
13
+ const require = createRequire(import.meta.url)
14
+ const tsxDir = require.resolve('tsx/cli')
23
15
  if (args.includes('build')) {
24
16
  const argsx = args.filter(arg => arg !== 'build')
25
17
  const msg = fork(tsxDir, [jsdir, '--lvy-build', ...argsx], {
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,46 +1,48 @@
1
- import { join } from 'path';
2
- import crypto from 'node:crypto';
3
- import { convertPath } from './config.js';
1
+ import { join } from 'path'
2
+ import crypto from 'node:crypto'
3
+ import { convertPath } from './config.js'
4
4
 
5
5
  /**
6
6
  * 生成模块内容
7
7
  * @param {string} relativePath 相对路径
8
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 = {};
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
25
  /**
26
26
  *
27
27
  * @param fileUrl
28
28
  * @returns
29
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
- };
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
+ }
45
47
 
46
- export { generateCSSModuleContent, generateModuleContent };
48
+ export { generateCSSModuleContent, generateModuleContent }
package/lib/index.d.ts CHANGED
@@ -1,3 +1,11 @@
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';
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'
package/lib/index.js CHANGED
@@ -1,73 +1,71 @@
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));
19
- }
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))
20
19
  }
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
- }
20
+ }
21
+ // 执行loader
22
+ await import('./main.js')
23
+ //
24
+ for (const app of apps) {
25
+ if (!app) {
26
+ continue
34
27
  }
35
- };
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
+ }
36
35
  /**
37
36
  * @param input
38
37
  */
39
38
  const onBuild = async () => {
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
- }
49
- }
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
- }
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))
58
47
  }
59
- };
60
- const main = async () => {
61
- if (process.argv.includes('--lvy-dev')) {
62
- await initConfig();
63
- await onDev();
48
+ }
49
+ //
50
+ for (const app of apps) {
51
+ if (!app) {
52
+ continue
64
53
  }
65
- else if (process.argv.includes('--lvy-build')) {
66
- await initConfig();
67
- await onBuild();
68
- buildAndRun();
54
+ if (typeof app != 'function' && typeof app.build == 'function') {
55
+ await app.build(global.lvyConfig)
69
56
  }
70
- };
71
- main();
57
+ }
58
+ }
59
+ 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()
72
70
 
73
- export { buildAndRun, initConfig };
71
+ export { buildAndRun, initConfig }
package/lib/loader.d.ts CHANGED
@@ -1,28 +1,25 @@
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
  * @param param0
8
8
  */
9
- declare function initialize({ port, lvyConfig }: {
10
- port: any;
11
- lvyConfig: any;
12
- }): Promise<void>;
9
+ declare function initialize({ port, lvyConfig }: { port: any; lvyConfig: any }): Promise<void>
13
10
  /**
14
11
  * @param specifier
15
12
  * @param context
16
13
  * @param nextResolve
17
14
  * @returns
18
15
  */
19
- declare function resolve(specifier: any, context: any, nextResolve: any): Promise<any>;
16
+ declare function resolve(specifier: any, context: any, nextResolve: any): Promise<any>
20
17
  /**
21
18
  * @param url
22
19
  * @param context
23
20
  * @param defaultLoad
24
21
  * @returns
25
22
  */
26
- declare const load: (url: any, context: any, nextLoad: any) => any;
23
+ declare const load: (url: any, context: any, nextLoad: any) => any
27
24
 
28
- export { initialize, load, resolve };
25
+ export { initialize, load, resolve }
package/lib/loader.js CHANGED
@@ -1,15 +1,15 @@
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
+ const reg = isWin32() ? /^file:\/\/\// : /^file:\/\//
5
+ const baseURL = isWin32() ? 'file:///' : 'file://'
6
+ const nodeReg = /(node_modules|node_|node:)/
7
7
  /**
8
8
  * @param param0
9
9
  */
10
10
  async function initialize({ port, lvyConfig }) {
11
- global.lvyConfig = lvyConfig;
12
- global.lvyWorkerProt = port;
11
+ global.lvyConfig = lvyConfig
12
+ global.lvyWorkerProt = port
13
13
  }
14
14
  /**
15
15
  * @param specifier
@@ -18,21 +18,21 @@ async function initialize({ port, lvyConfig }) {
18
18
  * @returns
19
19
  */
20
20
  async function resolve(specifier, context, nextResolve) {
21
- if (!global.lvyConfig?.alias) {
22
- global.lvyConfig.alias = {};
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
+ }
23
33
  }
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);
34
+ }
35
+ return nextResolve(specifier, context)
36
36
  }
37
37
  /**
38
38
  * @param url
@@ -41,34 +41,34 @@ async function resolve(specifier, context, nextResolve) {
41
41
  * @returns
42
42
  */
43
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
- };
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
73
 
74
- export { initialize, load, resolve };
74
+ export { initialize, load, resolve }
package/lib/main.js CHANGED
@@ -1,36 +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(`This version of Node.js (${process.version}) does not support module.register(). Please upgrade to Node v18.19 or v20.6 and above.`);
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
+ )
8
10
  }
9
- const { port1, port2 } = new MessageChannel();
10
- const cache = {};
11
+ const { port1, port2 } = new MessageChannel()
12
+ const cache = {}
11
13
  port1.on('message', msg => {
12
- if (msg.type == 'CSS_MODULE_GENERATED') {
13
- const { from, to } = msg.payload;
14
- if (!cache[from]) {
15
- postCSS(from, to);
16
- cache[from] = true;
17
- }
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
18
19
  }
19
- });
20
+ }
21
+ })
20
22
  // port1.unref()
21
23
  module.register('./loader.js', {
22
- parentURL: import.meta.url,
23
- data: {
24
- port: port2,
25
- lvyConfig: {
26
- alias: global.lvyConfig?.alias,
27
- assets: global.lvyConfig?.assets ?? {
28
- filter: assetsRegExp
29
- },
30
- styles: global.lvyConfig?.styles ?? {
31
- filter: stylesRegExp
32
- }
33
- }
34
- },
35
- transferList: [port2]
36
- });
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
+ })