Skip to content

Commit

Permalink
Merge pull request #7 from euberdeveloper/customize-matching
Browse files Browse the repository at this point in the history
Customize matching
  • Loading branch information
euberdeveloper authored Oct 31, 2023
2 parents 2e7279d + cb1ba1c commit d4f2f45
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 4 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,31 @@ To do so:
node --loader=./my-loader.mjs --no-warnings myscript.js # Note that --no-warnings is to disable a warning and is optional
```

## What if you want to change the matching behaviour?

You can also have a custom matcher, a function that customize the behaviour of a path matching an alias.

To do so:

* Create a custom file named as you want, for instance `my-loader.mjs`:
```js
import generateAliasesResolver from 'esm-module-alias';
const aliases = {
"@root": ".",
"@deep": "src/some/very/deep/directory/or/file",
"@my_module": "lib/some-file.js",
"something": "src/foo"
};
const matcher = (path, alias) => {
return (path.indexOf(alias) === 0); // Your customized code
};
export const resolve = generateAliasesResolver(aliases, { matcher }); // The custom matcher is passed to the options
```
* When you execute your script, **add that script as a loader** by adding `--loader ./my-loader.mjs`, for example:
```bash
node --loader=./my-loader.mjs --no-warnings myscript.js # Note that --no-warnings is to disable a warning and is optional
```
## Tests
Tests are run with **[Jest](https://jestjs.io/)** and work by executing `npm test` with **[shelljs](https://https://www.npmjs.com/package/shelljs)** on a bunch of sample projects,
Expand Down
3 changes: 3 additions & 0 deletions __tests__/sample-projects/custom-matcher/dist/src/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function printText(txt) {
console.log(txt);
}
8 changes: 8 additions & 0 deletions __tests__/sample-projects/custom-matcher/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import fs from 'node:fs';
import { printText } from '@/util.js';

async function main() {
const text = await fs.promises.readFile('./sample.txt', 'utf-8');
printText(text);
}
main();
13 changes: 13 additions & 0 deletions __tests__/sample-projects/custom-matcher/my-loader.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import generateAliasesResolver from '../../../index.js';
const aliases = {
"@/": "dist/src/"
};
const matcher = (path, alias) => {
return (
path.indexOf(alias) === 0 &&
(path.length === alias.length || path[alias.length] === "/" || path[alias.length - 1] === "/")
)
};
export const resolve = generateAliasesResolver(aliases, {
matcher
});
15 changes: 15 additions & 0 deletions __tests__/sample-projects/custom-matcher/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "esm-module-alias-test",
"version": "1.0.0",
"description": "A test project for the npm module esm-module-alias",
"type": "module",
"exports": "./index.js",
"scripts": {
"test": "node --loader ./my-loader.mjs index.js"
},
"engines": {
"node": ">=14.16"
},
"author": "Euber Developer <[email protected]>",
"license": "ISC"
}
2 changes: 2 additions & 0 deletions __tests__/sample-projects/custom-matcher/sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Sopra la panca la capra campa, sotto la panca la capra crepa.
Jeu tegnu tre ciucci zoppi, tie ci tre ciucci zoppi tei?
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path';

export default function generateAliasesResolver(aliasesToAdd) {
export default function generateAliasesResolver(aliasesToAdd, options) {
const getAliases = () => {

const base = process.cwd();
Expand All @@ -16,10 +16,10 @@ export default function generateAliasesResolver(aliasesToAdd) {

}

const isAliasInSpecifier = (path, alias) => {
const isAliasInSpecifier = options?.matcher ?? ((path, alias) => {
return path.indexOf(alias) === 0
&& (path.length === alias.length || path[alias.length] === '/');
};
});

const aliases = getAliases();

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": "esm-module-alias",
"version": "2.0.3",
"version": "2.1.0",
"description": "An alternative to module-alias, but for esm",
"exports": {
".": "./index.js",
Expand Down

0 comments on commit d4f2f45

Please sign in to comment.