# directory-based-webpack-plugin

> Directory based webpack alias

Latest version **1.0.1** (published 2020-12-27) · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install directory-based-webpack-plugin
pnpm add directory-based-webpack-plugin
yarn add directory-based-webpack-plugin
bun add directory-based-webpack-plugin
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2020-12-27 |
| First published | 2020-12-27 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 6.8 KB |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| Author | Heniker |
| Maintainers | heniker |

## Links

- npm: https://www.npmjs.com/package/directory-based-webpack-plugin
- Repository: https://github.com/Heniker/directory-based-webpack-plugin
- Homepage: https://github.com/Heniker/directory-based-webpack-plugin#readme
- Issues: https://github.com/Heniker/directory-based-webpack-plugin/issues
- npm.io page: https://npm.io/package/directory-based-webpack-plugin

## Dependencies (3)

- [find-up](https://npm.io/package/find-up.md) ^5.0.0
- [loader-utils](https://npm.io/package/loader-utils.md) 2.0.0
- [lodash.memoize](https://npm.io/package/lodash.memoize.md) 4.1.2

## Recent versions

- 1.0.1 (latest) — 2020-12-27
- 1.0.0 — 2020-12-27

## README

# Directory based webpack alias

Use this to specify aliases for specific folder in your `webpack.config.js` and optionally define extractors for specific files (e.g. `paths` in `tsconfig.json`, or `moduleAliases` in `package.json`)

## Why?

Webpack doesn't allow you to create aliases for specific folder.

For example, imagine you have 2 entry points, _A_ and _B_ in your webpack config which are basically separate projects. They do not import anything from each other, but they share some files in `common` folder (standard real-life monorepo structure).

If you use aliases you might find it natural to use `@` symbol to reference the root of your project. But you can't specify `@` to mean project _A_ root and project _B_ root at the same time depending on where it referenced from in a single config file.

This plugin solves this issue by introducing file-relative aliases.

## How to use

Example creating aliases for specific glob pattern:

```js
const glob = require('glob')

new DirectoryBasedPlugin(
  {},
  {},
  new Map([
    ...glob
      .sync(`${project1Config.projectPath}/**/`, { ignore: '**/node_modules/**' })
      .map((it) => [it, {@: project1Config.projectPath}]),
    ...glob
      .sync(`${project2Config.projectPath}/**/`, { ignore: '**/node_modules/**' })
      .map((it) => [it, {@: project2Config.projectPath}]),
  ])
)
```

You can also specify names of special files that define your **project root** and extract your aliases from them.

```js
const plugin = new DirectoryBasedPlugin({}, { aliasRoots: ['package.json'] })

plugin.extractors['package.json'] = (packageJsonPath) => {
  const packageConfig = require(packageJsonPath)
  // return value format should be the same as webpack resolve.alias
  return packageConfig._moduleAliases
}
```

## Options

### DirectoryBasedPlugin(defaultAlias, options, pathToAliasMap?)

#### defaultAlias

Type: `object`

Default aliases that are used to resolve every compiled file. Uses the same format as webpack `resolve.alias`. Can be overridden by aliases found in `aliasRoots` and `pathToAliasMap`.

#### options

##### options.aliasRoots

Type: `Array<string>`

Default: [`tsconfig.json`]

Files that include extractable aliases. The plugin will use `plugin.extractors` to extract aliases from them and use extracted aliases to resolve all files under the directory where `aliasRoot` was found.

##### options.ignore

Type: `Array<string>`

Default: [`node_modules`]

Array of substrings that, if found in compiled file path, are excluded from this plugin aliasing.

#### pathToAliasMap

Type: `Map<string, string>`

Default: `new Map()`

Defines alias for specific file. Overrides all other options.

---
_Source: https://npm.io/package/directory-based-webpack-plugin · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
