Skip to content

Commit

Permalink
Add automatic Alias HQ registration for Node (#48)
Browse files Browse the repository at this point in the history
* Feat register script #46

* Use package exports to register node paths

* Update demo

Co-authored-by: Dave Stewart <info@davestewart.co.uk>
  • Loading branch information
diegoulloao and davestewart authored Apr 24, 2022
1 parent d3de862 commit cbeae70
Show file tree
Hide file tree
Showing 12 changed files with 6,294 additions and 7 deletions.
2 changes: 2 additions & 0 deletions demo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.reify-cache
29 changes: 29 additions & 0 deletions demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Alias HQ Demo

> Demo for CLI and Node
## Overview

This folder is here only to test the CLI and Node integration.

## CLI

Run the tests from the root package:

```bash
npm run test # from root
```

All tests should pass.

## Node

Run the script from the demo folder:

```bash
cd demo # if not already in demo
npm i
npm run node
```

The terminal should print dummy output.
6,226 changes: 6,226 additions & 0 deletions demo/package-lock.json

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions demo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "alias-hq-demo",
"description": "Demo for CLI (tests) and Node (script)",
"scripts": {
"node": "node -r alias-hq/init ./src/node.js"
},
"devDependencies": {
"alias-hq": "file:.."
},
"dependencies": {
"reify": "^0.20.12"
}
}
2 changes: 1 addition & 1 deletion demo/packages/fetch/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default function (url, payload) {
return Promise.resolve(JSON.stringify({ url, payload }))
return Promise.resolve({ url, payload })
}
2 changes: 1 addition & 1 deletion demo/src/app/data/models/user.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import User from '@/classes/user'
import User from '@/classes/User'
import config from '../../../config/api'

export const user = new User(config)
2 changes: 1 addition & 1 deletion demo/src/app/views/elements/profile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default `
<div>
<pre>{{ data }}</pre>
<output>{{ url }}</output>
</div>
`
2 changes: 1 addition & 1 deletion demo/src/app/views/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import profile from './elements/profile'
export default {
async render () {
const data = await user.load(settings.id)
return profile.replace('{{ data }}', data)
return profile.replace('{{ url }}', data.url)
}
}
2 changes: 2 additions & 0 deletions demo/src/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('reify')
require('./main')
13 changes: 12 additions & 1 deletion docs/integrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,23 @@ This short React guide is *not meant to be exhaustive*; for issues, use your com

Although Node [doesn't support](https://arunmichaeldsouza.com/blog/aliasing-module-paths-in-node-js) path aliases natively, the [module-alias](https://www.npmjs.com/package/module-alias) package modifies Node's `require()` to add support for path aliases. There are some [caveats](https://github.com/ilearnio/module-alias#using-within-another-npm-package) but Alias HQ supports it via a plugin.

Setup is simple; add the following line in your application's main file, before requiring any `@aliased` paths:
Either, add the following line in your application's main file, before requiring any `@aliased` paths:

```js
require('alias-hq').get('module-alias')
```

Or, register paths automatically using Node's [require](https://nodejs.org/api/cli.html#-r---require-module) flag and Alias's Node-specific `init` export:

```json
{
"scripts": {
"start": "node -r alias-hq/init src/index.js",
"watch": "nodemon --exec node -r alias-hq/init src/index.ts",
},
}
```

## JSON-only

For libraries or setups that require JSON, you can use the [CLI](cli/cli.md):
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{
"name": "alias-hq",
"version": "5.3.2",
"version": "5.4.0",
"description": "The end-to-end solution for configuring, refactoring, maintaining and using path aliases",
"main": "src/index.js",
"bin": "bin/alias-hq",
"exports": {
".": "./src/index.js",
"./init": "./src/plugins/module-alias/init.js"
},
"types": "types",
"files": [
"src/*",
Expand Down
1 change: 1 addition & 0 deletions src/plugins/module-alias/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('alias-hq').get('module-alias')

0 comments on commit cbeae70

Please sign in to comment.