lvyjs 0.2.2 → 0.2.3

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.
@@ -1,4 +1,4 @@
1
- const assetsReg = /\.(png|jpg|jpeg|gif|svg|webp|ico)$/
2
- const cssReg = /\.(css|scss|less|sass|less)$/
1
+ const assetsReg = /\.(png|jpg|jpeg|gif|svg|webp|ico)$/;
2
+ const cssReg = /\.(css|scss|less|sass|less)$/;
3
3
 
4
- export { assetsReg, cssReg }
4
+ export { assetsReg, cssReg };
@@ -3,6 +3,6 @@
3
3
  * @param input
4
4
  * @returns
5
5
  */
6
- declare const ESBuild: (input: string) => Promise<string>
6
+ declare const ESBuild: (input: string) => Promise<string>;
7
7
 
8
- export { ESBuild }
8
+ export { ESBuild };
@@ -1,62 +1,64 @@
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'
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
5
 
6
6
  // 插件
7
- const plugins = []
7
+ const plugins = [];
8
8
  /**
9
9
  *
10
10
  */
11
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
- }
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
22
  /**
23
23
  *
24
24
  * @param input
25
25
  * @returns
26
26
  */
27
- const ESBuild = async input => {
28
- // 如果没有配置
29
- if (!global.lvyConfig) global.lvyConfig = {}
30
- if (!global.lvyConfig.esbuild) global.lvyConfig.esbuild = {}
31
- // 没有插件时,检查是否有可用插件。
32
- if (plugins.length === 0) {
33
- // init plugisn
34
- await initPlugins()
35
- }
36
- const options = global.lvyConfig?.esbuild?.options ?? {}
37
- const pl = options?.plugins ?? []
38
- // 构建
39
- const result = await esbuild.build({
40
- // 入口文件
41
- entryPoints: [input],
42
- //
43
- bundle: true,
44
- // 平台
45
- platform: 'node',
46
- // 输出格式
47
- format: 'esm',
48
- // 不写入文件
49
- write: false,
50
- // 忽略所有外部依赖
51
- external: ['*'],
52
- ...options,
53
- plugins: [...plugins, ...pl]
54
- })
55
- if (!result.outputFiles) {
56
- return ''
57
- }
58
- // 返回结果
59
- return result.outputFiles.map(file => new TextDecoder().decode(file.contents)).join('\n')
60
- }
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
+ };
61
63
 
62
- export { ESBuild }
64
+ export { ESBuild };
@@ -1,26 +1,26 @@
1
- import { assetsReg } from '../config.js'
2
- import { generateModuleContent } from '../utils/content.js'
1
+ import { assetsReg } from '../config.js';
2
+ import { generateModuleContent } from '../utils/content.js';
3
3
 
4
4
  /**
5
5
  *
6
6
  * @param param0
7
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'
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
+ });
20
22
  }
21
- })
22
- }
23
- }
24
- }
23
+ };
24
+ };
25
25
 
26
- export { esBuildAsstes }
26
+ export { esBuildAsstes };
@@ -1,41 +1,73 @@
1
- import { join, resolve } from 'path'
1
+ import { join, resolve } from 'path';
2
+ import { existsSync } from 'fs';
2
3
 
3
4
  /**
4
5
  *
5
6
  * @param alias
6
7
  * @returns
7
8
  */
8
- const esBuildAlias = alias => {
9
- const entries = alias?.entries
10
- return {
11
- name: 'alias',
12
- setup(build) {
13
- // 解析路径时使用 entries
14
- if (entries) {
15
- build.onResolve({ filter: /.*/ }, args => {
16
- const url = args.path
17
- for (const { find, replacement } of entries) {
18
- if (typeof find === 'string' && url.startsWith(find)) {
19
- // 字符串匹配
20
- const resolvedPath = join(replacement, url.slice(find.length))
21
- const absolutePath = resolve(resolvedPath)
22
- return {
23
- path: absolutePath
24
- }
25
- } else if (find instanceof RegExp && find.test(url)) {
26
- // 正则匹配
27
- const resolvedPath = url.replace(find, replacement)
28
- const absolutePath = resolve(resolvedPath)
29
- return {
30
- path: absolutePath
31
- }
9
+ const esBuildAlias = (alias) => {
10
+ const entries = alias?.entries;
11
+ return {
12
+ name: 'alias',
13
+ setup(build) {
14
+ // 解析路径时使用 entries
15
+ if (entries) {
16
+ build.onResolve({ filter: /.*/ }, args => {
17
+ const url = args.path;
18
+ for (const { find, replacement } of entries) {
19
+ if (typeof find === 'string' && url.startsWith(find)) {
20
+ const resolvedPath = join(replacement, url.slice(find.length));
21
+ const absolutePath = resolve(resolvedPath);
22
+ if (existsSync(absolutePath)) {
23
+ return {
24
+ path: absolutePath
25
+ };
26
+ }
27
+ const value = checkFileExtensions(absolutePath);
28
+ if (!value)
29
+ return;
30
+ if (value) {
31
+ return {
32
+ path: value
33
+ };
34
+ }
35
+ }
36
+ else if (find instanceof RegExp && find.test(url)) {
37
+ // 正则匹配
38
+ const resolvedPath = url.replace(find, replacement);
39
+ const absolutePath = resolve(resolvedPath);
40
+ if (existsSync(absolutePath)) {
41
+ return {
42
+ path: absolutePath
43
+ };
44
+ }
45
+ const value = checkFileExtensions(absolutePath);
46
+ if (!value)
47
+ return;
48
+ if (value) {
49
+ return {
50
+ path: value
51
+ };
52
+ }
53
+ }
54
+ }
55
+ return null;
56
+ });
32
57
  }
33
- }
34
- return null
35
- })
36
- }
58
+ }
59
+ };
60
+ };
61
+ const checkFileExtensions = basePath => {
62
+ const extensions = ['.js', '.ts', '.jsx', '.tsx'];
63
+ for (const ext of extensions) {
64
+ const filePath = `${basePath}${ext}`;
65
+ if (existsSync(filePath)) {
66
+ return filePath;
67
+ }
37
68
  }
38
- }
39
- }
69
+ // 如果没有找到文件,返回 null
70
+ return null;
71
+ };
40
72
 
41
- export { esBuildAlias }
73
+ export { esBuildAlias };
@@ -1,27 +1,27 @@
1
- import { generateCSSModuleContent } from '../utils/content.js'
2
- import { cssReg } from '../config.js'
1
+ import { generateCSSModuleContent } from '../utils/content.js';
2
+ import { cssReg } from '../config.js';
3
3
 
4
4
  /**
5
5
  * css资源处理插件
6
6
  * @param param0
7
7
  * @returns
8
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'
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
+ });
21
23
  }
22
- })
23
- }
24
- }
25
- }
24
+ };
25
+ };
26
26
 
27
- export { esBuildCSS }
27
+ export { esBuildCSS };
@@ -1,59 +1,57 @@
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 = inputPath => {
9
- return process.platform === 'win32' ? inputPath.replace(/\\/g, '/') : inputPath
10
- }
8
+ const convertPath = (inputPath) => {
9
+ return process.platform === 'win32' ? inputPath.replace(/\\/g, '/') : inputPath;
10
+ };
11
11
  /**
12
12
  * 生成模块内容
13
13
  * @param {string} relativePath 相对路径
14
14
  */
15
- const generateModuleContent = relativePath => {
16
- const contents = [
17
- 'const createUrl = (basePath, path) => {',
18
- "const platform = ['linux', 'android', 'darwin'];",
19
- 'const T = platform.includes(process.platform);',
20
- 'const reg = T ? /^file:\\/\\// : /^file:\\/\\/\\//;',
21
- "return new URL(path, basePath).href.replace(reg, '');",
22
- '};',
23
- `const fileUrl = createUrl(import.meta.url, '${convertPath(relativePath)}');`,
24
- 'export default fileUrl;'
25
- ].join('\n')
26
- return contents
27
- }
28
- const getRandomName = str => {
29
- // 使用 MD5 算法创建哈希对象
30
- const hash = crypto.createHash('md5')
31
- // 更新哈希对象内容
32
- hash.update(str)
33
- return hash.digest('hex')
34
- }
35
- const chache = {}
15
+ const generateModuleContent = (relativePath) => {
16
+ const contents = [
17
+ 'const createUrl = (basePath, path) => {',
18
+ "const platform = ['linux', 'android', 'darwin'];",
19
+ 'const T = platform.includes(process.platform);',
20
+ 'const reg = T ? /^file:\\/\\// : /^file:\\/\\/\\//;',
21
+ "return new URL(path, basePath).href.replace(reg, '');",
22
+ '};',
23
+ `const fileUrl = createUrl(import.meta.url, '${convertPath(relativePath)}');`,
24
+ 'export default fileUrl;'
25
+ ].join('\n');
26
+ return contents;
27
+ };
28
+ const getRandomName = (str) => {
29
+ // 使用 MD5 算法创建哈希对象
30
+ const hash = crypto.createHash('md5');
31
+ // 更新哈希对象内容
32
+ hash.update(str);
33
+ return hash.digest('hex');
34
+ };
35
+ const chache = {};
36
36
  /**
37
37
  *
38
38
  * @param fileUrl
39
39
  * @returns
40
40
  */
41
- const generateCSSModuleContent = pathURL => {
42
- const fileName = getRandomName(pathURL)
43
- const outputFileURL = convertPath(
44
- join(process.cwd(), 'node_modules', 'lvyjs', 'assets', `${fileName}.css`)
45
- )
46
- if (!chache[pathURL]) {
47
- global.lvyWorkerProt.postMessage({
48
- type: 'CSS_MODULE_GENERATED',
49
- payload: {
50
- from: pathURL,
51
- to: outputFileURL
52
- }
53
- })
54
- chache[pathURL] = true
55
- }
56
- return `export default "${outputFileURL}";`
57
- }
41
+ const generateCSSModuleContent = (pathURL) => {
42
+ const fileName = getRandomName(pathURL);
43
+ const outputFileURL = convertPath(join(process.cwd(), 'node_modules', 'lvyjs', 'assets', `${fileName}.css`));
44
+ if (!chache[pathURL]) {
45
+ global.lvyWorkerProt.postMessage({
46
+ type: 'CSS_MODULE_GENERATED',
47
+ payload: {
48
+ from: pathURL,
49
+ to: outputFileURL
50
+ }
51
+ });
52
+ chache[pathURL] = true;
53
+ }
54
+ return `export default "${outputFileURL}";`;
55
+ };
58
56
 
59
- export { generateCSSModuleContent, generateModuleContent }
57
+ export { generateCSSModuleContent, generateModuleContent };
package/lib/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { Options, defineConfig, getOptions, initConfig, usePlugin } from './store.js'
2
- export { buildAndRun, buildJS } from './rullup/index.js'
3
- export { ESBuild } from './esbuild/index.js'
1
+ export { Options, defineConfig, getOptions, initConfig, usePlugin } from './store.js';
2
+ export { buildAndRun, buildJS } from './rullup/index.js';
3
+ export { ESBuild } from './esbuild/index.js';
package/lib/index.js CHANGED
@@ -1,42 +1,44 @@
1
- import { buildAndRun } from './rullup/index.js'
2
- export { buildJS } from './rullup/index.js'
3
- import { initConfig } from './store.js'
4
- export { defineConfig, getOptions, usePlugin } from './store.js'
5
- export { ESBuild } from './esbuild/index.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, usePlugin } from './store.js';
5
+ export { ESBuild } from './esbuild/index.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
- 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
+ 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
+ app(global.lvyConfig);
27
30
  }
28
- if (typeof app == 'function') app(global.lvyConfig)
29
- }
30
- }
31
+ };
31
32
  const main = async () => {
32
- if (process.argv.includes('--lvy-dev')) {
33
- await initConfig()
34
- onDev()
35
- } else if (process.argv.includes('--lvy-build')) {
36
- await initConfig()
37
- buildAndRun()
38
- }
39
- }
40
- main()
33
+ if (process.argv.includes('--lvy-dev')) {
34
+ await initConfig();
35
+ onDev();
36
+ }
37
+ else if (process.argv.includes('--lvy-build')) {
38
+ await initConfig();
39
+ buildAndRun();
40
+ }
41
+ };
42
+ main();
41
43
 
42
- export { buildAndRun, initConfig }
44
+ export { buildAndRun, initConfig };
package/lib/loader.d.ts CHANGED
@@ -1,15 +1,18 @@
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
- declare function initialize({ port, lvyConfig }: { port: any; lvyConfig: any }): Promise<void>
6
+ declare function initialize({ port, lvyConfig }: {
7
+ port: any;
8
+ lvyConfig: any;
9
+ }): Promise<void>;
7
10
  /**
8
11
  * @param url
9
12
  * @param context
10
13
  * @param defaultLoad
11
14
  * @returns
12
15
  */
13
- declare const load: (url: any, context: any, defaultLoad: any) => Promise<any>
16
+ declare const load: (url: any, context: any, defaultLoad: any) => Promise<any>;
14
17
 
15
- export { initialize, load }
18
+ export { initialize, load };
package/lib/loader.js CHANGED
@@ -1,12 +1,12 @@
1
- import { ESBuild } from './esbuild/index.js'
1
+ import { ESBuild } from './esbuild/index.js';
2
2
 
3
- const platform = ['win32'].includes(process.platform)
4
- const reg = platform ? /^file:\/\/\// : /^file:\/\//
5
- const nodeReg = /(node_modules|node_|node:)/
6
- const jsReg = /\.(js|ts|jsx|tsx)$/
3
+ const platform = ['win32'].includes(process.platform);
4
+ const reg = platform ? /^file:\/\/\// : /^file:\/\//;
5
+ const nodeReg = /(node_modules|node_|node:)/;
6
+ const jsReg = /\.(js|ts|jsx|tsx)$/;
7
7
  async function initialize({ port, lvyConfig }) {
8
- global.lvyConfig = lvyConfig
9
- global.lvyWorkerProt = port
8
+ global.lvyConfig = lvyConfig;
9
+ global.lvyWorkerProt = port;
10
10
  }
11
11
  /**
12
12
  * @param url
@@ -15,17 +15,17 @@ async function initialize({ port, lvyConfig }) {
15
15
  * @returns
16
16
  */
17
17
  const load = async (url, context, defaultLoad) => {
18
- if (nodeReg.test(url)) {
19
- return defaultLoad(url, context)
20
- }
21
- if (!jsReg.test(url)) {
22
- return defaultLoad(url, context)
23
- }
24
- const code = await ESBuild(url.replace(reg, ''))
25
- return defaultLoad(url, {
26
- format: 'module',
27
- source: code
28
- })
29
- }
18
+ if (nodeReg.test(url)) {
19
+ return defaultLoad(url, context);
20
+ }
21
+ if (!jsReg.test(url)) {
22
+ return defaultLoad(url, context);
23
+ }
24
+ const code = await ESBuild(url.replace(reg, ''));
25
+ return defaultLoad(url, {
26
+ format: 'module',
27
+ source: code
28
+ });
29
+ };
30
30
 
31
- export { initialize, load }
31
+ export { initialize, load };
package/lib/main.js CHANGED
@@ -1,33 +1,32 @@
1
- import module from 'node:module'
2
- import { MessageChannel } from 'node:worker_threads'
3
- import { postCSS } from './postcss.js'
1
+ import module from 'node:module';
2
+ import { MessageChannel } from 'node:worker_threads';
3
+ import { postCSS } from './postcss.js';
4
4
 
5
5
  if (!module.register) {
6
- throw new Error(
7
- `This version of Node.js (${process.version}) does not support module.register(). Please upgrade to Node v18.19 or v20.6 and above.`
8
- )
6
+ 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.`);
9
7
  }
10
- const { port1, port2 } = new MessageChannel()
11
- const cache = {}
8
+ const { port1, port2 } = new MessageChannel();
9
+ const cache = {};
12
10
  port1.on('message', msg => {
13
- if (msg.type == 'CSS_MODULE_GENERATED') {
14
- const { from, to } = msg.payload
15
- if (!cache[from]) {
16
- postCSS(from, to)
17
- cache[from] = true
11
+ if (msg.type == 'CSS_MODULE_GENERATED') {
12
+ const { from, to } = msg.payload;
13
+ if (!cache[from]) {
14
+ postCSS(from, to);
15
+ cache[from] = true;
16
+ }
18
17
  }
19
- } else if (msg.type == 'JS_MODULE_GENERATED');
20
- })
18
+ else if (msg.type == 'JS_MODULE_GENERATED') ;
19
+ });
21
20
  // port1.unref()
22
21
  module.register('./loader.js', {
23
- parentURL: import.meta.url,
24
- data: {
25
- port: port2,
26
- lvyConfig: {
27
- alias: global.lvyConfig.alias,
28
- assets: global.lvyConfig.assets,
29
- styles: global.lvyConfig.styles
30
- }
31
- },
32
- transferList: [port2]
33
- })
22
+ parentURL: import.meta.url,
23
+ data: {
24
+ port: port2,
25
+ lvyConfig: {
26
+ alias: global.lvyConfig.alias,
27
+ assets: global.lvyConfig.assets,
28
+ styles: global.lvyConfig.styles
29
+ }
30
+ },
31
+ transferList: [port2]
32
+ });