lvyjs 0.2.7 → 0.2.8

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 相对路径
@@ -1,52 +1,54 @@
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 ['win32'].includes(process.platform) ? inputPath : inputPath.replace(/\\/g, '/');
10
- };
8
+ const convertPath = inputPath => {
9
+ return ['win32'].includes(process.platform) ? inputPath : inputPath.replace(/\\/g, '/')
10
+ }
11
11
  /**
12
12
  * 生成模块内容
13
13
  * @param {string} relativePath 相对路径
14
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 = {};
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
31
  /**
32
32
  *
33
33
  * @param fileUrl
34
34
  * @returns
35
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
- };
36
+ const generateCSSModuleContent = pathURL => {
37
+ const fileName = getRandomName(pathURL)
38
+ const outputFileURL = convertPath(
39
+ join(process.cwd(), 'node_modules', 'lvyjs', 'assets', `${fileName}.css`)
40
+ )
41
+ if (!chache[pathURL]) {
42
+ global.lvyWorkerProt.postMessage({
43
+ type: 'CSS_MODULE_GENERATED',
44
+ payload: {
45
+ from: pathURL,
46
+ to: outputFileURL
47
+ }
48
+ })
49
+ chache[pathURL] = true
50
+ }
51
+ return `export default "${outputFileURL}";`
52
+ }
51
53
 
52
- export { generateCSSModuleContent, generateModuleContent };
54
+ export { generateCSSModuleContent, generateModuleContent }
package/lib/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export { Options, defineConfig, getOptions, initConfig, usePlugin } 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
@@ -2,6 +2,7 @@ import { buildAndRun } from './rullup/index.js';
2
2
  export { buildJS } from './rullup/index.js';
3
3
  import { initConfig } from './store.js';
4
4
  export { defineConfig, getOptions, usePlugin } from './store.js';
5
+ export { assetsRegExp, convertPath, createAlias, isWin32, stylesRegExp } from './config.js';
5
6
 
6
7
  /**
7
8
  * @param input
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/server.js CHANGED
@@ -1,11 +1,11 @@
1
- import Koa from 'koa';
2
- import KoaStatic from 'koa-static';
1
+ import Koa from 'koa'
2
+ import KoaStatic from 'koa-static'
3
3
 
4
- const app = new Koa();
4
+ const app = new Koa()
5
5
  // 使用 koa-static 中间件来提供静态文件
6
- app.use(KoaStatic(process.cwd()));
6
+ app.use(KoaStatic(process.cwd()))
7
7
  // 设定端口并启动服务器
8
- const PORT = 8989;
8
+ const PORT = 8989
9
9
  app.listen(8989, () => {
10
- console.log(`Server running at http://localhost:${PORT}`);
11
- });
10
+ console.log(`Server running at http://localhost:${PORT}`)
11
+ })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lvyjs",
3
- "version": "0.2.7",
3
+ "version": "0.2.8",
4
4
  "description": "tsx compile script",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",