@marcuwynu23/webpack-plugin-alias-resolver
Version:
A Webpack plugin that rewrites path aliases like @/ into relative paths during build time.
94 lines (63 loc) โข 2.44 kB
Markdown
# webpack-plugin-alias-resolver
๐ง A Webpack plugin that rewrites alias-based imports (like `@/`) into relative paths during build time. Useful for TypeScript or Babel outputs that retain unresolved alias paths.
## ๐ฆ Installation
```bash
npm install --save-dev @marcuwynu23/webpack-plugin-alias-resolver
```
> Webpack is a peer dependency. Ensure it's installed:
```bash
npm install --save-dev webpack
```
## ๐ Usage
### Add to your `webpack.config.js`:
```ts
import AliasResolverPlugin from "@marcuwynu23/webpack-plugin-alias-resolver";
export default {
// ...your config
plugins: [
new AliasResolverPlugin({
alias: "@/",
baseDir: "js-generated", // folder where alias actually resolves to
targetDir: "js-generated", // folder to scan and fix
fileTypes: ["js", "ts", "json"], // file extensions to rewrite
}),
],
};
```
## โ๏ธ Options
| Option | Type | Default | Description |
| ----------- | -------------------- | -------- | -------------------------------------------------------------------------------------- |
| `alias` | `string` | `"@/"` | The alias prefix used in your imports (`@/`, `~/`, etc.) |
| `baseDir` | `string` | `"dist"` | Where the alias path actually resolves to (usually output dir) |
| `targetDir` | `string` | `"dist"` | Directory to scan and rewrite imports in |
| `fileTypes` | `string \| string[]` | `"js"` | File types to rewrite. Use `"js"`, `"ts"`, `"both"`, or an array like `["js", "json"]` |
## โจ What It Does
This plugin searches files inside `targetDir` and rewrites lines like:
```ts
import { thing } from "@/utils/helper";
```
โก๏ธ into:
```ts
import { thing } from "../../utils/helper";
```
This is useful when you're compiling code and the final output still contains unresolved aliases.
## ๐ก Example Project Structure
```
project/
โโโ src/
โ โโโ utils/helper.ts
โโโ js-generated/
โ โโโ utils/helper.js โ compiled output
โโโ webpack.config.js
```
## ๐งช TypeScript Support
This plugin is fully written in TypeScript and ships with type declarations.
## ๐ License
MIT ยฉ 2025 [Your Name]