vite-plugin-simple-tsconfig-alias 0.0.1 → 0.1.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Ray <https://github.com/so1ve>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,45 +1,80 @@
1
1
  # vite-plugin-simple-tsconfig-alias
2
2
 
3
- ## ⚠️ IMPORTANT NOTICE ⚠️
4
-
5
- **This package is created solely for the purpose of setting up OIDC (OpenID Connect) trusted publishing with npm.**
6
-
7
- This is **NOT** a functional package and contains **NO** code or functionality beyond the OIDC setup configuration.
8
-
9
- ## Purpose
10
-
11
- This package exists to:
12
- 1. Configure OIDC trusted publishing for the package name `vite-plugin-simple-tsconfig-alias`
13
- 2. Enable secure, token-less publishing from CI/CD workflows
14
- 3. Establish provenance for packages published under this name
15
-
16
- ## What is OIDC Trusted Publishing?
17
-
18
- OIDC trusted publishing allows package maintainers to publish packages directly from their CI/CD workflows without needing to manage npm access tokens. Instead, it uses OpenID Connect to establish trust between the CI/CD provider (like GitHub Actions) and npm.
19
-
20
- ## Setup Instructions
21
-
22
- To properly configure OIDC trusted publishing for this package:
23
-
24
- 1. Go to [npmjs.com](https://www.npmjs.com/) and navigate to your package settings
25
- 2. Configure the trusted publisher (e.g., GitHub Actions)
26
- 3. Specify the repository and workflow that should be allowed to publish
27
- 4. Use the configured workflow to publish your actual package
28
-
29
- ## DO NOT USE THIS PACKAGE
30
-
31
- This package is a placeholder for OIDC configuration only. It:
32
- - Contains no executable code
33
- - Provides no functionality
34
- - Should not be installed as a dependency
35
- - Exists only for administrative purposes
36
-
37
- ## More Information
38
-
39
- For more details about npm's trusted publishing feature, see:
40
- - [npm Trusted Publishing Documentation](https://docs.npmjs.com/generating-provenance-statements)
41
- - [GitHub Actions OIDC Documentation](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect)
42
-
43
- ---
44
-
45
- **Maintained for OIDC setup purposes only**
3
+ [![NPM version](https://img.shields.io/npm/v/vite-plugin-simple-tsconfig-alias?color=a1b858&label=)](https://www.npmjs.com/package/vite-plugin-simple-tsconfig-alias)
4
+
5
+ A simple Vite plugin to resolve tsconfig paths as aliases.
6
+
7
+ ## 💎 Features
8
+
9
+ - Reads `compilerOptions.paths` from one or more tsconfig files.
10
+ - Supports exact mappings (no `*`), e.g. `"@": ["./src"]`.
11
+ - Supports single-star wildcard mappings, e.g. `"@/*": ["./src/*"]`.
12
+ - The `*` is converted to a RegExp capture group on `find` and `$1` replacements.
13
+ - Merges aliases into `config.resolve.alias` early (`enforce: "pre"`).
14
+ - Aliases from tsconfig are prepended so they take precedence over existing ones.
15
+ - `baseUrl` behavior:
16
+ - If `baseUrl` is set in a tsconfig, targets are resolved relative to it.
17
+ - If not set, targets are resolved relative to that tsconfig's directory.
18
+
19
+ ## 📦 Installation
20
+
21
+ ```bash
22
+ $ npm install vite-plugin-simple-tsconfig-alias
23
+ $ yarn add vite-plugin-simple-tsconfig-alias
24
+ $ pnpm add vite-plugin-simple-tsconfig-alias
25
+ ```
26
+
27
+ ## 🚀 Usage
28
+
29
+ ### Vite Plugin
30
+
31
+ ```ts
32
+ // vite.config.ts
33
+ import { defineConfig } from "vite";
34
+ import TsconfigAlias from "vite-plugin-simple-tsconfig-alias";
35
+
36
+ export default defineConfig({
37
+ plugins: [
38
+ TsconfigAlias({
39
+ // Optional: root directory of the project (default: process.cwd())
40
+ root: process.cwd(),
41
+ // Optional: tsconfig file names to read (default: ["tsconfig.json"])
42
+ configNames: ["tsconfig.json", "tsconfig.paths.json"],
43
+ }),
44
+ ],
45
+ });
46
+ ```
47
+
48
+ ### Programmatic API
49
+
50
+ You can also use the underlying functions to parse and merge aliases manually. This makes it possible to be used in other contexts beyond Vite plugins, like other bundlers that doesn't have a `config` hook.
51
+
52
+ ```ts
53
+ import {
54
+ mergeAliases,
55
+ parseTsconfigAliases,
56
+ } from "vite-plugin-simple-tsconfig-alias";
57
+
58
+ // Parse aliases from tsconfig files
59
+ const aliases = parseTsconfigAliases(process.cwd(), ["tsconfig.json"]);
60
+
61
+ // Merge with existing Vite aliases
62
+ const finalAliases = mergeAliases(
63
+ [{ find: "@custom", replacement: "./src/custom" }],
64
+ aliases,
65
+ );
66
+ ```
67
+
68
+ ## ⚠️ Limitations
69
+
70
+ - Only the first target in a `paths` entry is used. If you rely on fallbacks like `"@/*": ["./src/*", "./generated/*"]`, only `./src/*` will be applied.
71
+ - Path mapping transforms are only injected into Vite's resolver.
72
+ - TypeScript / vue-tsc still use tsconfig directly; keep tsconfig correct.
73
+ - Does not support Vite's `\0` virtual module prefix use cases.
74
+ - Wildcard handling is generic regex-based and may be looser than TypeScript's own matching in edge cases (multiple `*` segments, unusual patterns).
75
+ - If the tsconfig file can't be read/parsed, it is silently skipped.
76
+ - Windows paths: Replacements are absolute filesystem paths produced by Node's `path.resolve`.
77
+
78
+ ## 📝 License
79
+
80
+ [MIT](./LICENSE). Made with ❤️ by [Ray](https://github.com/so1ve)
@@ -0,0 +1,19 @@
1
+ import { Plugin } from "vite";
2
+
3
+ //#region src/types.d.ts
4
+ interface Alias {
5
+ find: string | RegExp;
6
+ replacement: string;
7
+ }
8
+ type AliasOptions = readonly Alias[] | Record<string, string>;
9
+ //#endregion
10
+ //#region src/index.d.ts
11
+ interface TsconfigAliasPluginOptions {
12
+ root?: string;
13
+ configNames?: string[];
14
+ }
15
+ declare function parseTsconfigAliases(projectRoot: string, configNames: string[]): Alias[];
16
+ declare function mergeAliases(existing: AliasOptions | undefined, incoming: Alias[]): Alias[];
17
+ declare function TsconfigAlias(options?: TsconfigAliasPluginOptions): Plugin;
18
+ //#endregion
19
+ export { TsconfigAliasPluginOptions, TsconfigAlias as default, mergeAliases, parseTsconfigAliases };
package/dist/index.mjs ADDED
@@ -0,0 +1,80 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import ts from "typescript";
4
+
5
+ //#region src/index.ts
6
+ const escapeRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
7
+ function buildStarRegex(pattern) {
8
+ const parts = pattern.split("*");
9
+ const starCount = parts.length - 1;
10
+ const source = `^${parts.map(escapeRegExp).join("(.*)")}$`;
11
+ return {
12
+ find: new RegExp(source),
13
+ starCount
14
+ };
15
+ }
16
+ function buildReplacement(target, starCount) {
17
+ let replacement = target;
18
+ for (let i = 1; i <= starCount; i += 1) replacement = replacement.replace("*", `$${i}`);
19
+ return replacement;
20
+ }
21
+ function readTsconfig(filePath) {
22
+ if (!fs.existsSync(filePath)) return null;
23
+ const read = ts.readConfigFile(filePath, ts.sys.readFile);
24
+ if (read.error) return null;
25
+ return ts.parseJsonConfigFileContent(read.config, ts.sys, path.dirname(filePath), void 0, filePath);
26
+ }
27
+ function parseTsconfigAliases(projectRoot, configNames) {
28
+ const aliases = [];
29
+ for (const configName of configNames) {
30
+ const configPath = path.resolve(projectRoot, configName);
31
+ const parsed = readTsconfig(configPath);
32
+ if (!parsed) continue;
33
+ const baseUrl = parsed.options.baseUrl ?? path.dirname(configPath);
34
+ const paths = parsed.options.paths ?? {};
35
+ for (const [from, [firstTarget]] of Object.entries(paths)) {
36
+ if (!firstTarget) continue;
37
+ const absoluteTarget = path.resolve(baseUrl, firstTarget);
38
+ if (!from.includes("*")) {
39
+ aliases.push({
40
+ find: from,
41
+ replacement: absoluteTarget
42
+ });
43
+ continue;
44
+ }
45
+ const { find, starCount } = buildStarRegex(from);
46
+ aliases.push({
47
+ find,
48
+ replacement: buildReplacement(absoluteTarget, starCount)
49
+ });
50
+ }
51
+ }
52
+ aliases.sort((a, b) => {
53
+ const aLen = typeof a.find === "string" ? a.find.length : a.find.source.length;
54
+ return (typeof b.find === "string" ? b.find.length : b.find.source.length) - aLen;
55
+ });
56
+ return aliases;
57
+ }
58
+ function mergeAliases(existing, incoming) {
59
+ const normalizedExisting = existing ? Array.isArray(existing) ? [...existing] : Object.entries(existing).map(([find, replacement]) => ({
60
+ find,
61
+ replacement
62
+ })) : [];
63
+ return [...incoming, ...normalizedExisting];
64
+ }
65
+ function TsconfigAlias(options = {}) {
66
+ const root = options.root ?? process.cwd();
67
+ const configNames = options.configNames ?? ["tsconfig.json"];
68
+ return {
69
+ name: "vite-plugin-simple-tsconfig-alias",
70
+ enforce: "pre",
71
+ config(config) {
72
+ const aliases = parseTsconfigAliases(root, configNames);
73
+ config.resolve ??= {};
74
+ config.resolve.alias = mergeAliases(config.resolve.alias, aliases);
75
+ }
76
+ };
77
+ }
78
+
79
+ //#endregion
80
+ export { TsconfigAlias as default, mergeAliases, parseTsconfigAliases };
package/package.json CHANGED
@@ -1,10 +1,71 @@
1
1
  {
2
2
  "name": "vite-plugin-simple-tsconfig-alias",
3
- "version": "0.0.1",
4
- "description": "OIDC trusted publishing setup package for vite-plugin-simple-tsconfig-alias",
3
+ "version": "0.1.1",
4
+ "author": "Ray <i@mk1.io> (@so1ve)",
5
+ "type": "module",
6
+ "description": "A simple Vite plugin to resolve tsconfig paths as aliases.",
5
7
  "keywords": [
6
- "oidc",
7
- "trusted-publishing",
8
- "setup"
9
- ]
10
- }
8
+ "vite",
9
+ "vite-plugin",
10
+ "tsconfig",
11
+ "alias",
12
+ "path-mapping"
13
+ ],
14
+ "homepage": "https://github.com/so1ve/vite-plugin-simple-tsconfig-alias#readme",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/so1ve/vite-plugin-simple-tsconfig-alias.git"
18
+ },
19
+ "bugs": {
20
+ "url": "https://github.com/so1ve/vite-plugin-simple-tsconfig-alias/issues"
21
+ },
22
+ "license": "MIT",
23
+ "sideEffects": false,
24
+ "exports": {
25
+ ".": "./dist/index.mjs",
26
+ "./package.json": "./package.json"
27
+ },
28
+ "main": "./dist/index.mjs",
29
+ "module": "./dist/index.mjs",
30
+ "types": "./dist/index.d.mts",
31
+ "files": [
32
+ "dist"
33
+ ],
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "devDependencies": {
38
+ "@antfu/ni": "^28.0.0",
39
+ "@so1ve/eslint-config": "^4.1.2",
40
+ "@so1ve/prettier-config": "^4.1.2",
41
+ "@types/node": "^25.0.3",
42
+ "@typescript/native-preview": "7.0.0-dev.20251029.1",
43
+ "bumpp": "^10.3.2",
44
+ "eslint": "^9.39.2",
45
+ "prettier": "^3.7.4",
46
+ "tsdown": "^0.18.3",
47
+ "typescript": "^5.9.3",
48
+ "vite": "^7.3.0",
49
+ "vitest": "^4.0.16"
50
+ },
51
+ "peerDependencies": {
52
+ "typescript": "^5",
53
+ "vite": "^4 || ^5 || ^6 || ^7"
54
+ },
55
+ "peerDependenciesMeta": {
56
+ "vite": {
57
+ "optional": true
58
+ }
59
+ },
60
+ "scripts": {
61
+ "build": "tsdown",
62
+ "lint": "eslint . && prettier . --check",
63
+ "lint:fix": "eslint . --fix && prettier . --write",
64
+ "release": "bumpp --commit --push --tag",
65
+ "test": "vitest",
66
+ "typecheck": "nr typecheck:isolated && nr typecheck:non-isolated",
67
+ "typecheck:isolated": "tsc --noEmit --project tsconfig.isolated.json",
68
+ "typecheck:non-isolated": "tsgo --noEmit --project tsconfig.non-isolated.json",
69
+ "watch": "tsdown --watch"
70
+ }
71
+ }