lvyjs 0.2.7 → 0.2.9

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.
@@ -0,0 +1,12 @@
1
+ declare const assetsRegExp: RegExp;
2
+ declare const stylesRegExp: RegExp;
3
+ /**
4
+ *
5
+ * @param val
6
+ * @returns
7
+ */
8
+ declare const createAlias: (val: any) => {};
9
+ declare const isWin32: () => boolean;
10
+ declare const convertPath: (inputPath: string) => string;
11
+
12
+ export { assetsRegExp, convertPath, createAlias, isWin32, stylesRegExp };
package/lib/config.js CHANGED
@@ -1,5 +1,5 @@
1
- const assetsReg = /\.(png|jpg|jpeg|gif|svg|webp|ico)$/;
2
- const cssReg = /\.(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
@@ -13,5 +13,11 @@ const createAlias = val => {
13
13
  });
14
14
  return alias;
15
15
  };
16
+ const isWin32 = () => {
17
+ return ['win32'].includes(process.platform);
18
+ };
19
+ const convertPath = (inputPath) => {
20
+ return isWin32() ? inputPath.replace(/\\/g, '/') : inputPath;
21
+ };
16
22
 
17
- export { assetsReg, createAlias, cssReg };
23
+ export { assetsRegExp, convertPath, createAlias, isWin32, stylesRegExp };
package/lib/content.js CHANGED
@@ -1,13 +1,7 @@
1
1
  import { join } from 'path';
2
2
  import crypto from 'node:crypto';
3
+ import { convertPath } from './config.js';
3
4
 
4
- /**
5
- * @param inputPath
6
- * @returns
7
- */
8
- const convertPath = (inputPath) => {
9
- return ['win32'].includes(process.platform) ? inputPath : inputPath.replace(/\\/g, '/');
10
- };
11
5
  /**
12
6
  * 生成模块内容
13
7
  * @param {string} relativePath 相对路径
package/lib/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
- export { Options, defineConfig, getOptions, initConfig, usePlugin } from './store.js';
1
+ export { Options, PluginsCallBack, PluginsOptions, PluginsValue, defineConfig, getOptions, initConfig } from './store.js';
2
2
  export { buildAndRun, buildJS } from './rullup/index.js';
3
+ export { assetsRegExp, convertPath, createAlias, isWin32, stylesRegExp } from './config.js';
package/lib/index.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import { buildAndRun } from './rullup/index.js';
2
2
  export { buildJS } from './rullup/index.js';
3
3
  import { initConfig } from './store.js';
4
- export { defineConfig, getOptions, usePlugin } from './store.js';
4
+ export { defineConfig, getOptions } from './store.js';
5
+ export { assetsRegExp, convertPath, createAlias, isWin32, stylesRegExp } from './config.js';
5
6
 
6
7
  /**
7
8
  * @param input
@@ -14,7 +15,7 @@ const onDev = async () => {
14
15
  if (!plugin) {
15
16
  continue;
16
17
  }
17
- apps.push(plugin(global.lvyConfig));
18
+ await apps.push(plugin(global.lvyConfig));
18
19
  }
19
20
  }
20
21
  // 执行loader
@@ -24,17 +25,51 @@ const onDev = async () => {
24
25
  if (!app) {
25
26
  continue;
26
27
  }
27
- if (typeof app == 'function')
28
- app(global.lvyConfig);
28
+ if (typeof app == 'function') {
29
+ await app(global.lvyConfig);
30
+ }
31
+ else if (typeof app.load == 'function') {
32
+ app.load(global.lvyConfig);
33
+ }
34
+ }
35
+ };
36
+ /**
37
+ * @param input
38
+ */
39
+ 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
+ // 执行loader
51
+ await import('./main.js');
52
+ //
53
+ for (const app of apps) {
54
+ if (!app) {
55
+ continue;
56
+ }
57
+ if (typeof app == 'function') {
58
+ await app(global.lvyConfig);
59
+ }
60
+ else if (typeof app.build == 'function') {
61
+ app.build(global.lvyConfig);
62
+ }
29
63
  }
30
64
  };
31
65
  const main = async () => {
32
66
  if (process.argv.includes('--lvy-dev')) {
33
67
  await initConfig();
34
- onDev();
68
+ await onDev();
35
69
  }
36
70
  else if (process.argv.includes('--lvy-build')) {
37
71
  await initConfig();
72
+ await onBuild();
38
73
  buildAndRun();
39
74
  }
40
75
  };
package/lib/loader.d.ts CHANGED
@@ -4,7 +4,6 @@ declare global {
4
4
  var lvyWorkerProt: MessagePort;
5
5
  }
6
6
  /**
7
- *
8
7
  * @param param0
9
8
  */
10
9
  declare function initialize({ port, lvyConfig }: {
package/lib/loader.js CHANGED
@@ -1,10 +1,10 @@
1
1
  import { generateModuleContent, generateCSSModuleContent } from './content.js';
2
+ import { isWin32, convertPath } from './config.js';
2
3
 
3
- const platform = ['win32'].includes(process.platform);
4
- const reg = platform ? /^file:\/\/\// : /^file:\/\//;
4
+ const reg = isWin32() ? /^file:\/\/\// : /^file:\/\//;
5
+ const baseURL = isWin32() ? 'file:///' : 'file://';
5
6
  const nodeReg = /(node_modules|node_|node:)/;
6
7
  /**
7
- *
8
8
  * @param param0
9
9
  */
10
10
  async function initialize({ port, lvyConfig }) {
@@ -18,17 +18,16 @@ async function initialize({ port, lvyConfig }) {
18
18
  * @returns
19
19
  */
20
20
  async function resolve(specifier, context, nextResolve) {
21
- const { parentURL = null } = context;
22
21
  if (!global.lvyConfig?.alias) {
23
22
  global.lvyConfig.alias = {};
24
23
  }
25
24
  if (global.lvyConfig.alias?.entries) {
26
25
  for (const { find, replacement } of global.lvyConfig.alias?.entries) {
27
26
  if (specifier.startsWith(find)) {
28
- const url = specifier.replace(find, replacement);
27
+ const parentURL = `${baseURL}${convertPath(specifier.replace(find, replacement))}`;
29
28
  return nextResolve(specifier, {
30
29
  ...context,
31
- parentURL: parentURL ? new URL(url, parentURL).href : new URL(url).href
30
+ parentURL: parentURL
32
31
  });
33
32
  }
34
33
  }
package/lib/postcss.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import fs from 'fs';
2
2
  import postcss from 'postcss';
3
3
  import { createRequire } from 'module';
4
- import { join, resolve, dirname } from 'path';
4
+ import { join, resolve, dirname, isAbsolute } from 'path';
5
+ import { createAlias } from './config.js';
5
6
 
6
7
  const require = createRequire(import.meta.url);
7
8
  const config = {
@@ -10,6 +11,27 @@ const config = {
10
11
  less: null,
11
12
  scss: null
12
13
  };
14
+ function LessAliasPlugin(aliases) {
15
+ return {
16
+ install: function (less, pluginManager) {
17
+ const AliasFileManager = new less.FileManager();
18
+ AliasFileManager.loadFile = function (filename, currentDirectory, options, environment) {
19
+ // 替换路径中的别名
20
+ for (const alias in aliases) {
21
+ if (filename.startsWith(alias)) {
22
+ filename = filename.replace(alias, aliases[alias]);
23
+ break;
24
+ }
25
+ }
26
+ const fullPath = isAbsolute(filename) ? filename : resolve(currentDirectory, filename);
27
+ return less.FileManager.prototype.loadFile.call(this, fullPath, currentDirectory, options, environment);
28
+ };
29
+ // 注册自定义文件管理器
30
+ pluginManager.addFileManager(AliasFileManager);
31
+ },
32
+ minVersion: [3, 0] // 支持的最低 LESS 版本
33
+ };
34
+ }
13
35
  /**
14
36
  *
15
37
  * @param configPath
@@ -132,7 +154,10 @@ const postCSS = (inputPath, outputPath) => {
132
154
  let css = '';
133
155
  if (typing === 'less') {
134
156
  const less = require('less');
135
- const lessResult = await less.render(fs.readFileSync(inputPath, 'utf-8'));
157
+ const lessResult = await less.render(fs.readFileSync(inputPath, 'utf-8'), {
158
+ filename: inputPath,
159
+ plugins: [LessAliasPlugin(createAlias(global.lvyConfig?.alias))] // 使用插件
160
+ });
136
161
  css = lessResult.css;
137
162
  }
138
163
  else if (typing === 'sass' || typing === 'scss') {
@@ -174,4 +199,4 @@ const postCSS = (inputPath, outputPath) => {
174
199
  });
175
200
  };
176
201
 
177
- export { postCSS };
202
+ export { LessAliasPlugin as default, postCSS };
@@ -1,13 +1,13 @@
1
1
  import { createFilter } from '@rollup/pluginutils';
2
2
  import { resolve, dirname, basename } from 'node:path';
3
- import { cssReg } from '../../config.js';
3
+ import { stylesRegExp } from '../../config.js';
4
4
 
5
5
  /**
6
6
  *
7
7
  * @returns
8
8
  */
9
9
  const rollupStylesCSSImport = (options) => {
10
- const include = options?.filter ?? cssReg;
10
+ const include = options?.filter ?? stylesRegExp;
11
11
  const filter = createFilter(include, null);
12
12
  return {
13
13
  name: 'c-css',
@@ -1,13 +1,13 @@
1
1
  import { resolve, dirname, basename } from 'path';
2
2
  import { readFileSync } from 'fs';
3
- import { assetsReg } from '../../config.js';
3
+ import { assetsRegExp } from '../../config.js';
4
4
 
5
5
  /**
6
6
  * @param {Object} options
7
7
  * @returns {Object}
8
8
  */
9
9
  const rollupAssets = (options) => {
10
- const { filter = assetsReg } = options ?? {};
10
+ const { filter = assetsRegExp } = options ?? {};
11
11
  return {
12
12
  name: 'rollup-node-files',
13
13
  resolveId(source, importer) {
package/lib/store.d.ts CHANGED
@@ -3,11 +3,17 @@ import { RollupTypescriptOptions } from '@rollup/plugin-typescript';
3
3
  import { RollupOptions, OutputOptions } from 'rollup';
4
4
  import { Alias } from './typing.js';
5
5
 
6
+ type PluginsValue = (options: Options) => void;
7
+ type PluginsCallBack = PluginsValue | {
8
+ load?: PluginsValue;
9
+ build?: PluginsValue;
10
+ };
11
+ type PluginsOptions = ((options: Options) => PluginsCallBack | void);
6
12
  type Options = {
7
13
  /**
8
14
  * 配置调整机及其回调插件
9
15
  */
10
- plugins?: ((options: Options) => ((options: Options) => void) | void | undefined | null)[];
16
+ plugins?: PluginsOptions[];
11
17
  /**
12
18
  * 别名
13
19
  */
@@ -59,12 +65,6 @@ type Options = {
59
65
  };
60
66
  } | false;
61
67
  };
62
- /**
63
- *
64
- * @param options
65
- * @returns
66
- */
67
- declare const usePlugin: (load: (options: Options) => ((options: Options) => void) | void | undefined | null) => (options: Options) => ((options: Options) => void) | void | undefined | null;
68
68
  /**
69
69
  *
70
70
  */
@@ -85,4 +85,4 @@ declare const getOptions: () => Options;
85
85
  */
86
86
  declare const defineConfig: (optoins?: Options) => Options | undefined;
87
87
 
88
- export { type Options, defineConfig, getOptions, initConfig, usePlugin };
88
+ export { type Options, type PluginsCallBack, type PluginsOptions, type PluginsValue, defineConfig, getOptions, initConfig };
package/lib/store.js CHANGED
@@ -1,12 +1,6 @@
1
1
  import { existsSync } from 'fs';
2
2
  import { join } from 'path';
3
3
 
4
- /**
5
- *
6
- * @param options
7
- * @returns
8
- */
9
- const usePlugin = (load) => load;
10
4
  /**
11
5
  *
12
6
  */
@@ -44,4 +38,4 @@ const getOptions = () => global.lvyConfig;
44
38
  */
45
39
  const defineConfig = (optoins) => optoins;
46
40
 
47
- export { defineConfig, getOptions, initConfig, usePlugin };
41
+ export { defineConfig, getOptions, initConfig };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lvyjs",
3
- "version": "0.2.7",
3
+ "version": "0.2.9",
4
4
  "description": "tsx compile script",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",
@@ -1,52 +0,0 @@
1
- import { join } from 'path';
2
- import crypto from 'node:crypto';
3
-
4
- /**
5
- * @param inputPath
6
- * @returns
7
- */
8
- const convertPath = (inputPath) => {
9
- return ['win32'].includes(process.platform) ? inputPath : inputPath.replace(/\\/g, '/');
10
- };
11
- /**
12
- * 生成模块内容
13
- * @param {string} relativePath 相对路径
14
- */
15
- const generateModuleContent = (relativePath) => {
16
- const contents = [
17
- `const reg = ['win32'].includes(process.platform) ? /^file:\\/\\/\\// : /^file:\\/\\// ;`,
18
- `const fileUrl = import.meta.resolve('${convertPath(relativePath)}').replace(reg, '');`,
19
- 'export default fileUrl;'
20
- ].join('\n');
21
- return contents;
22
- };
23
- const getRandomName = (str) => {
24
- // 使用 MD5 算法创建哈希对象
25
- const hash = crypto.createHash('md5');
26
- // 更新哈希对象内容
27
- hash.update(str);
28
- return hash.digest('hex');
29
- };
30
- const chache = {};
31
- /**
32
- *
33
- * @param fileUrl
34
- * @returns
35
- */
36
- const generateCSSModuleContent = (pathURL) => {
37
- const fileName = getRandomName(pathURL);
38
- const outputFileURL = convertPath(join(process.cwd(), 'node_modules', 'lvyjs', 'assets', `${fileName}.css`));
39
- if (!chache[pathURL]) {
40
- global.lvyWorkerProt.postMessage({
41
- type: 'CSS_MODULE_GENERATED',
42
- payload: {
43
- from: pathURL,
44
- to: outputFileURL
45
- }
46
- });
47
- chache[pathURL] = true;
48
- }
49
- return `export default "${outputFileURL}";`;
50
- };
51
-
52
- export { generateCSSModuleContent, generateModuleContent };
package/lib/server.js DELETED
@@ -1,11 +0,0 @@
1
- import Koa from 'koa';
2
- import KoaStatic from 'koa-static';
3
-
4
- const app = new Koa();
5
- // 使用 koa-static 中间件来提供静态文件
6
- app.use(KoaStatic(process.cwd()));
7
- // 设定端口并启动服务器
8
- const PORT = 8989;
9
- app.listen(8989, () => {
10
- console.log(`Server running at http://localhost:${PORT}`);
11
- });