1.0.2 โข Published 8 months ago
@marcuwynu23/webpack-plugin-alias-resolver v1.0.2
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
npm install --save-dev @marcuwynu23/webpack-plugin-alias-resolverWebpack is a peer dependency. Ensure it's installed:
npm install --save-dev webpack๐ Usage
Add to your webpack.config.js:
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:
import { thing } from "@/utils/helper";โก๏ธ into:
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