Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Fallbacks for Webpack and Babel #80

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [6.3.0] - 2024-10-29

Added:

- Add fallbacks support for Babel and Webpack (#80) - closes #79

## [6.2.3] - 2023-09-12

Fixed:
Expand Down
12 changes: 12 additions & 0 deletions docs/integrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ module.exports = {
}
```

The `format` option controls support for [fallbacks](https://www.typescriptlang.org/docs/handbook/modules/reference.html#fallbacks). Instead of `string` (the default), you can set `array` (Webpack v5+) to enable it:

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

## Rollup

#### Basic setup
Expand Down Expand Up @@ -160,6 +166,12 @@ module.exports = {
}
```

The `format` option controls support for [fallbacks](https://www.typescriptlang.org/docs/handbook/modules/reference.html#fallbacks). Instead of `string` (the default), you can set `array` (Module Resolver v4+) to enable it:

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

For more info, check the plugin's [docs](https://github.com/tleunen/babel-plugin-module-resolver/blob/master/DOCS.md).

# Frameworks
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "alias-hq",
"version": "6.2.4",
"version": "6.3.0",
"description": "The end-to-end solution for configuring, refactoring, maintaining and using path aliases",
"bin": "bin/alias-hq",
"exports": {
Expand Down
14 changes: 12 additions & 2 deletions src/plugins/babel/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
const { toObject } = require('../../utils')

const defaults = {
format: 'string'
}

// https://github.com/tleunen/babel-plugin-module-resolver/blob/HEAD/DOCS.md#alias
function callback (alias, paths) {
function callback (alias, paths, config, options) {
// for babel to use a regex, the alias must start with a ^
const prefix = alias.includes('*') ? '^' : ''
const name = prefix + alias.replace('*', '(.*)')
const path = paths[0].replace('*', '\\1')
let path = paths.map(path => {
return path.replace('*', '\\1')
})
if (options.format === 'string' || path.length === 1) {
path = path[0]
}
return {
name,
path,
}
}

module.exports = function (config, options) {
options = { ...defaults, ...options }
return toObject(callback, config, options)
}
28 changes: 27 additions & 1 deletion src/plugins/babel/tests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module.exports = [
function () {
const label = 'string'
const options = {
format: 'string'
}
const expected = {
'^@/(.*)': '/\\1',
'^@packages/(.*)': '../packages/\\1',
Expand All @@ -11,6 +15,28 @@ module.exports = [
'^@views/(.*)': 'app/views/\\1',
'^@alias-hq/(.*)': '../../src/\\1',
}
return { expected }
return { label, options, expected }
},

function () {
const label = 'array'
const options = {
format: 'array'
}
const expected = {
'^@/(.*)': '/\\1',
'^@packages/(.*)': '../packages/\\1',
'^@classes/(.*)': 'classes/\\1',
'^@app/(.*)': 'app/\\1',
'^@data/(.*)': 'app/data/\\1',
'@settings': 'app/settings.js',
'^@services/(.*)': [
'app/services/\\1',
'../packages/services/\\1',
],
'^@views/(.*)': 'app/views/\\1',
'^@alias-hq/(.*)': '../../src/\\1',
}
return { label, options, expected }
},
]
16 changes: 13 additions & 3 deletions src/plugins/webpack/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
const { toObject, resolve } = require('../../utils')

const defaults = {
format: 'string'
}

// @see https://webpack.js.org/configuration/resolve/#resolvealias
function callback (name, [path], config) {
function callback (name, paths, config, options) {
const { root, base } = config
name = name.replace(/\/\*$/, '')
path = path.replace(/\*$/, '')
path = resolve(root, base, path)
let path = paths.map(path => {
path = path.replace(/\*$/, '')
return resolve(root, base, path)
})
if (options.format === 'string' || path.length === 1) {
path = path[0]
}
return {
name,
path,
}
}

module.exports = function (config, options) {
options = { ...defaults, ...options }
return toObject(callback, config, options)
}
28 changes: 27 additions & 1 deletion src/plugins/webpack/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ const { abs } = require('../../utils')

module.exports = [
function () {
const label = 'string'
const options = {
format: 'string',
}
const expected = {
'@': abs(''),
'@packages': abs('../packages'),
Expand All @@ -13,6 +17,28 @@ module.exports = [
'@settings': abs('app/settings.js'),
'@alias-hq': abs('../../src'),
}
return { expected }
return { label, options, expected }
},

function () {
const label = 'array'
const options = {
format: 'array',
}
const expected = {
'@': abs(''),
'@packages': abs('../packages'),
'@classes': abs('classes'),
'@app': abs('app'),
'@data': abs('app/data'),
'@services': [
abs('app/services'),
abs('../packages/services'),
],
'@views': abs('app/views'),
'@settings': abs('app/settings.js'),
'@alias-hq': abs('../../src'),
}
return { label, options, expected }
},
]