vitek-plugin 0.2.0-beta → 0.2.1-beta

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
@@ -5,7 +5,7 @@
5
5
 
6
6
  **File-based HTTP API generation for Vite**
7
7
 
8
- [![Version](https://img.shields.io/badge/version-0.1.2--beta-orange.svg)](https://github.com/martinsbicudo/vitek-plugin)
8
+ [![Version](https://img.shields.io/badge/version-0.2.1--beta-orange.svg)](https://github.com/martinsbicudo/vitek-plugin)
9
9
  [![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
10
10
  [![Vite](https://img.shields.io/badge/vite-^5.0.0%20%7C%7C%20^6.0.0-646CFF.svg)](https://vitejs.dev/)
11
11
 
@@ -1 +1 @@
1
- {"version":3,"file":"vitek-resolve.d.ts","sourceRoot":"","sources":["../../src/plugin/vitek-resolve.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAOnC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAElD,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,aAAa,GAAG,MAAM,CAiB9D"}
1
+ {"version":3,"file":"vitek-resolve.d.ts","sourceRoot":"","sources":["../../src/plugin/vitek-resolve.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAOnC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAElD,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,aAAa,GAAG,MAAM,CAkB9D"}
@@ -9,10 +9,11 @@ export function createResolvePlugin(ctx) {
9
9
  name: 'vitek:resolve',
10
10
  enforce: 'pre',
11
11
  resolveId(id, importer) {
12
- if (!id.startsWith('.') || !importer || !ctx.root)
12
+ const root = ctx.root ?? process.cwd();
13
+ if (!id.startsWith('.') || !importer)
13
14
  return null;
14
- const fullApiDir = path.resolve(ctx.root, ctx.apiDirOption);
15
- const importerPath = normalizeImporterPath(importer, ctx.root);
15
+ const fullApiDir = path.resolve(root, ctx.apiDirOption);
16
+ const importerPath = normalizeImporterPath(importer, root);
16
17
  const normalizedApiDir = path.resolve(fullApiDir);
17
18
  const normalizedImporter = path.resolve(importerPath);
18
19
  if (!normalizedImporter.startsWith(normalizedApiDir))
@@ -1,5 +1,5 @@
1
1
  /**
2
- * vitek:transform — rewrite relative imports to root-relative paths
2
+ * vitek:transform — rewrite relative and alias imports to root-relative paths
3
3
  */
4
4
  import type { Plugin } from 'vite';
5
5
  import type { PluginContext } from './context.js';
@@ -1 +1 @@
1
- {"version":3,"file":"vitek-transform.d.ts","sourceRoot":"","sources":["../../src/plugin/vitek-transform.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAOnC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAElD,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,aAAa,GAAG,MAAM,CA2ChE"}
1
+ {"version":3,"file":"vitek-transform.d.ts","sourceRoot":"","sources":["../../src/plugin/vitek-transform.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAOnC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAMlD,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,aAAa,GAAG,MAAM,CAmEhE"}
@@ -1,14 +1,16 @@
1
1
  /**
2
- * vitek:transform — rewrite relative imports to root-relative paths
2
+ * vitek:transform — rewrite relative and alias imports to root-relative paths
3
3
  */
4
4
  import * as path from 'path';
5
5
  import MagicString from 'magic-string';
6
6
  import { normalizeModuleIdPath, resolveWithExtension, } from '../adapters/vite/path-utils.js';
7
+ function escapeRegex(s) {
8
+ return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
9
+ }
7
10
  export function createTransformPlugin(ctx) {
8
11
  return {
9
12
  name: 'vitek:transform',
10
13
  enforce: 'pre',
11
- // filter is supported in Vite 6.3+; ignored in Vite 5 (handler still works)
12
14
  transform: {
13
15
  filter: {
14
16
  id: { include: /\.(tsx?|jsx?|mjs)$/, exclude: /node_modules/ },
@@ -16,7 +18,6 @@ export function createTransformPlugin(ctx) {
16
18
  handler(code, id) {
17
19
  if (!ctx.root)
18
20
  return null;
19
- // Early return for Vite 5 (filter ignored); filter handles this in Vite 6.3+
20
21
  if (id.includes('node_modules') || !/\.(tsx?|jsx?|mjs)$/.test(id))
21
22
  return null;
22
23
  const srcDir = path.resolve(ctx.root, ctx.options.srcDir ?? 'src');
@@ -25,10 +26,10 @@ export function createTransformPlugin(ctx) {
25
26
  return null;
26
27
  const dir = path.dirname(normalizedId);
27
28
  const rootSlash = path.resolve(ctx.root) + path.sep;
28
- const relImportRe = /from\s+['"](\.\.?[^'"]+)['"]/g;
29
- let match;
30
29
  const s = new MagicString(code);
31
30
  let hasChanges = false;
31
+ const relImportRe = /from\s+['"](\.\.?[^'"]+)['"]/g;
32
+ let match;
32
33
  while ((match = relImportRe.exec(code)) !== null) {
33
34
  const specifier = match[1];
34
35
  const candidate = path.resolve(dir, specifier);
@@ -43,6 +44,30 @@ export function createTransformPlugin(ctx) {
43
44
  s.overwrite(match.index, match.index + match[0].length, `from ${quote}${newSpecifier}${quote}`);
44
45
  hasChanges = true;
45
46
  }
47
+ const alias = ctx.options.alias;
48
+ if (alias && Object.keys(alias).length > 0) {
49
+ const entries = Object.entries(alias)
50
+ .filter(([, v]) => v != null && v !== '')
51
+ .sort(([a], [b]) => b.length - a.length);
52
+ for (const [key, replacement] of entries) {
53
+ const escapedKey = escapeRegex(key);
54
+ const aliasRe = new RegExp(`from\\s+(['"])(${escapedKey})(/[^'"]*)?\\1`, 'g');
55
+ while ((match = aliasRe.exec(code)) !== null) {
56
+ const quote = match[1];
57
+ const rest = (match[3] || '').replace(/^\//, '');
58
+ const base = path.isAbsolute(replacement)
59
+ ? path.join(replacement, rest)
60
+ : path.join(ctx.root, replacement, rest);
61
+ const target = resolveWithExtension(base);
62
+ if (!target)
63
+ continue;
64
+ const rootRelative = path.relative(ctx.root, target).replace(/\\/g, '/');
65
+ const newSpecifier = `/${rootRelative}`;
66
+ s.overwrite(match.index, match.index + match[0].length, `from ${quote}${newSpecifier}${quote}`);
67
+ hasChanges = true;
68
+ }
69
+ }
70
+ }
46
71
  if (!hasChanges)
47
72
  return null;
48
73
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vitek-plugin",
3
- "version": "0.2.0-beta",
3
+ "version": "0.2.1-beta",
4
4
  "description": "Vite plugin for file-based HTTP API generation",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -14,6 +14,27 @@
14
14
  "files": [
15
15
  "dist"
16
16
  ],
17
+ "scripts": {
18
+ "prepare": "husky",
19
+ "build": "tsc",
20
+ "dev": "concurrently \"pnpm:dev:tsc\" \"pnpm:docs:dev\"",
21
+ "dev:tsc": "tsc --watch",
22
+ "prepublishOnly": "npm run build",
23
+ "docs:dev": "vitepress dev docs",
24
+ "docs:build": "vitepress build docs",
25
+ "docs:preview": "vitepress preview docs",
26
+ "test": "vitest run",
27
+ "test:watch": "vitest",
28
+ "test:coverage": "vitest run --coverage",
29
+ "test:e2e": "node scripts/e2e.mjs",
30
+ "test:e2e:socket": "node scripts/e2e-socket.mjs",
31
+ "bench": "node scripts/bench.mjs",
32
+ "example:bench": "node scripts/bench.mjs --with-example",
33
+ "examples:build": "bash examples/build-all.sh",
34
+ "examples:test": "bash examples/run-all-tests.sh",
35
+ "examples:build-and-test": "bash examples/build-and-test.sh",
36
+ "check": "pnpm i && pnpm run build && pnpm test && pnpm run examples:build-and-test && pnpm run test:e2e && pnpm run test:e2e:socket && pnpm run example:bench"
37
+ },
17
38
  "keywords": [
18
39
  "vite",
19
40
  "plugin",
@@ -61,24 +82,5 @@
61
82
  "bin": {
62
83
  "vitek": "./dist/cli/cli.js",
63
84
  "vitek-serve": "./dist/cli/serve.js"
64
- },
65
- "scripts": {
66
- "build": "tsc",
67
- "dev": "concurrently \"pnpm:dev:tsc\" \"pnpm:docs:dev\"",
68
- "dev:tsc": "tsc --watch",
69
- "docs:dev": "vitepress dev docs",
70
- "docs:build": "vitepress build docs",
71
- "docs:preview": "vitepress preview docs",
72
- "test": "vitest run",
73
- "test:watch": "vitest",
74
- "test:coverage": "vitest run --coverage",
75
- "test:e2e": "node scripts/e2e.mjs",
76
- "test:e2e:socket": "node scripts/e2e-socket.mjs",
77
- "bench": "node scripts/bench.mjs",
78
- "example:bench": "node scripts/bench.mjs --with-example",
79
- "examples:build": "bash examples/build-all.sh",
80
- "examples:test": "bash examples/run-all-tests.sh",
81
- "examples:build-and-test": "bash examples/build-and-test.sh",
82
- "check": "pnpm i && pnpm run build && pnpm test && pnpm run examples:build-and-test && pnpm run test:e2e && pnpm run test:e2e:socket && pnpm run example:bench"
83
85
  }
84
- }
86
+ }