Skip to content

Commit

Permalink
Improve plugin utils callback signature (#59)
Browse files Browse the repository at this point in the history
* Update plugin utils callback

* Update plugins

* Update docs and version

* Update changelog
  • Loading branch information
davestewart authored Dec 6, 2022
1 parent 3f6e7ac commit cde1ee0
Show file tree
Hide file tree
Showing 8 changed files with 11,460 additions and 6,581 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [6.0.0] - 2022-12-06
### Changed
- Improve plugin utils callback signature (#59) - closes #58

## [5.4.0] - 2022-04-22
### Added
- Added support to initialize Alias HQ with a Node require flag (#48) - closes #46
Expand Down
10 changes: 5 additions & 5 deletions docs/api/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Your plugin will always receive the loaded `config` as the first parameter:
Your plugin may receive user options (in a format of your choosing) with which you can decide how to customise the transformation:

```js
function (config, options = 'bar') {
function plugin (config, options = 'bar') {
return options.format === 'foo'
? fooify(config.paths)
: barify(config.paths)
Expand Down Expand Up @@ -196,11 +196,11 @@ This simplifies the writing of the overall conversion function, allowing you to
```js
const { toArray } = require('../../utils')

// process a single `name => paths` entry
function callback (name, config, options) {
const { rootUrl, baseUrl, paths } = config // note, these are the *alias* paths!
// process a single `alias => paths` entry
function callback (alias, paths, urls, options) {
const { root, base } = urls
return {
name: name.replace('/*', ''),
name: alias.replace('/*', ''),
path: paths[0].replace('/*', '')
}
}
Expand Down
17,982 changes: 11,430 additions & 6,552 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "alias-hq",
"version": "5.4.0",
"version": "6.0.0",
"description": "The end-to-end solution for configuring, refactoring, maintaining and using path aliases",
"bin": "bin/alias-hq",
"exports": {
Expand All @@ -18,7 +18,7 @@
"cli": "node cli",
"setup": "node cli -- setup",
"lint": "eslint . --fix",
"test": "jest --verbose",
"test": "jest --verbose --detectOpenHandles",
"test:plugins": "jest --watch -f plugins/core.spec.js -t 'core plugins:'",
"test:coverage": "jest --coverage",
"prepare": "rimraf types && tsc"
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/jest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ const defaults = {
}

// @see https://jestjs.io/docs/en/configuration#modulenamemapper-objectstring-string--arraystring
function callback (name, config, options) {
const { baseUrl, paths } = config
function callback (name, paths, config, options) {
const { base } = config
name = name
.replace(/\*/, '(.*)')
let path = paths.map(path => {
path = path
.replace(/^\//, '')
.replace(/\*/, '$1')
return join('<rootDir>', baseUrl, path)
return join('<rootDir>', base, path)
})
if (options.format === 'string' || path.length === 1) {
path = path[0]
Expand Down
12 changes: 5 additions & 7 deletions src/plugins/rollup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ const defaults = {
}

// @see https://github.com/rollup/plugins/tree/master/packages/alias
function callback (name, config, options) {
const { rootUrl, baseUrl } = config
name = name
.replace(/\/\*$/, '')
let path = config.paths[0]
.replace(/\/\*$/, '')
path = resolve(rootUrl, baseUrl, path)
function callback (name, [path], config, options) {
const { root, base } = config
name = name.replace(/\/\*$/, '')
path = path.replace(/\/\*$/, '')
path = resolve(root, base, path)
if (options.format === 'object') {
return {
name,
Expand Down
12 changes: 5 additions & 7 deletions src/plugins/webpack/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
const { toObject, resolve } = require('../../utils')

// @see https://webpack.js.org/configuration/resolve/#resolvealias
function callback (name, config) {
const { rootUrl, baseUrl, paths } = config
name = name
.replace(/\/\*$/, '')
let path = paths[0]
.replace(/\*$/, '')
path = resolve(rootUrl, baseUrl, path)
function callback (name, [path], config) {
const { root, base } = config
name = name.replace(/\/\*$/, '')
path = path.replace(/\*$/, '')
path = resolve(root, base, path)
return {
name,
path,
Expand Down
11 changes: 6 additions & 5 deletions src/utils/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ function toArray (callback, config, options) {
return Object
.keys(config.paths)
.map(name => {
return callback(name, {
rootUrl: config.rootUrl,
baseUrl: config.baseUrl,
paths: config.paths[name],
}, options)
const paths = config.paths[name]
const urls = {
root: config.rootUrl,
base: config.baseUrl,
}
return callback(name, paths, urls, options)
})
}

Expand Down

0 comments on commit cde1ee0

Please sign in to comment.