Skip to content

Commit

Permalink
Modify plugin formats:
Browse files Browse the repository at this point in the history
- Jest now has string and array format options
- Jest now outputs strings by default
- Rollup now outputs objects by default
- Fix test:plugins script
  • Loading branch information
davestewart committed Sep 18, 2020
1 parent bd8a98f commit fe9031a
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 10 deletions.
6 changes: 5 additions & 1 deletion cli/modules/integrations/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ const { indent, makeJson } = require('../../utils/text')

function printConfig (data) {
console.log()
console.log(indent(makeJson(data)))
const text = data === true
? 'This plugin does not generate any output'.red
: makeJson(data)

console.log(indent(text))
}

// ---------------------------------------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion docs/cli/paths.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Folders:
- Any `paths` **must** resolve from the `baseUrl`, so if you need to go up a level from `src`, something like this is fine: `../packages`
- The format supports [multiple paths](https://www.typescriptlang.org/tsconfig#paths), though
- the CLI will only write a single path
- Jest is the only library to utilise this
- Jest v25+ is the only library to utilise this

Content:

Expand Down
12 changes: 9 additions & 3 deletions docs/integrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module.exports = {

## Rollup

If bundling using Rollup and @rollup/plugin-alias, you can [add the aliases](https://github.com/rollup/plugins/tree/master/packages/alias#usage) using the `plugins.alias` configuration option:
If bundling using Rollup and [@rollup/plugin-alias](https://github.com/rollup/plugins/tree/master/packages/alias) you can [add the aliases](https://github.com/rollup/plugins/tree/master/packages/alias#usage) using the `plugins.alias` configuration option:

```js
// rollup.config.js
Expand All @@ -70,10 +70,10 @@ module.exports = {
}
```

You can request rollup paths in `object` or `array` (the default) format:
You can request paths in `object` (the default) or `array` format:

```js
hq.get('rollup', { format: 'object' })
hq.get('rollup', { format: 'array' })
```

## Jest
Expand All @@ -90,6 +90,12 @@ module.exports = {
}
```

You can request paths in `string` (the default) or `array` (Jest v25+) format:

```js
hq.get('rollup', { format: 'array' })
```

## Vue

If using Vue CLI, you can [add the aliases](https://cli.vuejs.org/guide/webpack.html#simple-configuration) using the `configureWebpack` or `chainWebpack` option.
Expand Down
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.1.5",
"version": "5.1.6",
"description": "The end-to-end solution for configuring, refactoring, maintaining and using path aliases",
"main": "src/index.js",
"bin": "bin/alias-hq",
Expand All @@ -13,7 +13,7 @@
"cli": "node cli",
"setup": "node cli -- setup",
"test": "jest --verbose",
"test:plugins": "jest --watch -f plugins.spec.js -t 'available plugins:'",
"test:plugins": "jest --watch -f plugins/core.spec.js -t 'core plugins:'",
"test:coverage": "jest --coverage"
},
"repository": {
Expand Down
9 changes: 7 additions & 2 deletions src/plugins/jest/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const { toObject, join } = require('../../utils')

const defaults = {
format: 'string'
}

// @see https://jestjs.io/docs/en/configuration#modulenamemapper-objectstring-string--arraystring
function callback (name, config) {
function callback (name, config, options) {
options = { ...defaults, ...options }
const { baseUrl, paths } = config
name = name
.replace(/\*/, '(.*)')
Expand All @@ -11,7 +16,7 @@ function callback (name, config) {
.replace(/\*/, '$1')
return join('<rootDir>', baseUrl, path)
})
if (path.length === 1) {
if (options.format === 'string' || path.length === 1) {
path = path[0]
}
return {
Expand Down
23 changes: 22 additions & 1 deletion src/plugins/jest/tests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
module.exports = [
function () {
const label = 'string'
const options = {
format: 'string'
}
const expected = {
'^@/(.*)$': '<rootDir>/src/$1',
'^@packages/(.*)$': '<rootDir>/packages/$1',
'^@classes/(.*)$': '<rootDir>/src/classes/$1',
'^@app/(.*)$': '<rootDir>/src/app/$1',
'^@data/(.*)$': '<rootDir>/src/app/data/$1',
'^@services/(.*)$': '<rootDir>/src/app/services/$1',
'^@views/(.*)$': '<rootDir>/src/app/views/$1'
}
return { label, options, expected }
},

function () {
const label = 'array'
const options = {
format: 'array'
}
const expected = {
'^@/(.*)$': '<rootDir>/src/$1',
'^@packages/(.*)$': '<rootDir>/packages/$1',
Expand All @@ -12,6 +33,6 @@ module.exports = [
],
'^@views/(.*)$': '<rootDir>/src/app/views/$1'
}
return { expected }
return { label, options, expected }
},
]
5 changes: 5 additions & 0 deletions src/plugins/rollup/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const { toArray, toObject, resolve, join } = require('../../utils')

const defaults = {
format: 'object'
}

// @see https://github.com/rollup/plugins/tree/master/packages/alias
function callback (name, config, options) {
options = { ...defaults, ...options }
const { rootUrl, baseUrl } = config
name = name
.replace(/\/\*$/, '')
Expand Down

0 comments on commit fe9031a

Please sign in to comment.