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

feat: use cosmiconfig for finding configuration files #7

Merged
merged 4 commits into from
Dec 14, 2023
Merged
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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Or you can use the JavaScript api
import { register } from 'node:module';
register('node-esm-loader', import.meta.url, {

// Optional, will use the default .loaderrc.js config file if present
// Optional, will search for a configuration file automatically if not specified
data: {
loaderConfig: './path/to/config.js',
},
Expand All @@ -39,9 +39,10 @@ node --import=./register.js ./your/file.js
```
For more info about this, see https://nodejs.org/api/module.html#customization-hooks

`node-esm-loader` loader will subsequently look for a `.loaderrc.mjs` or `.loaderrc.js` file that exports the configuration.
This file can look something like this:
`node-esm-loader` loader will subsequently look for a configuration file in various places using [cosmiconfig](https://www.npmjs.com/package/cosmiconfig), where it also looks up the directory tree.
My advice is to either use `.loaderrc.js`, or use a `.config` folder with `.config/loaderrc.js`, but `loader.config.js` will also work.
```js
// .loaderrc.js, loader.config.js or .config/loaderrc.js
export default {
loaders: [
'vue-esm-loader',
Expand All @@ -57,8 +58,9 @@ export default {
],
};
```
For more info on what the loaders configuration should look like, have a look at [create-esm-loader](https://www.npmjs.com/package/create-esm-loader).

If you want to store the loader config in a different file, you can use
If you want to put the loader config in a different file, you can use
```
node --import=node-esm-loader/register ./your-file.js --loader-config=./path/to/config.js
```
Expand All @@ -70,8 +72,6 @@ module.exports = {
};
```

For more info on what the loaders configuration should look like, have a look at [create-esm-loader](https://www.npmjs.com/package/create-esm-loader).

## Compatible Loaders

You can find a [list of compatible loaders][loaderlist], here:
Expand Down
17 changes: 8 additions & 9 deletions lib/lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@
import { pathToFileURL } from 'node:url';
import path from 'node:path';
import findUp from 'find-up';

// The supported config files we look for, in that order.
const CONFIG_FILES = [
'.loaderrc.mjs',
'.loaderrc.js',
];
import { cosmiconfig } from 'cosmiconfig';

// Finds and loads the configuration file based on the parameters the process
// was run with.
Expand All @@ -21,7 +16,7 @@ export default async function lookup({ loaderConfig, cwd = process.cwd() }) {
if (loaderConfig) {
fullPath = path.resolve(process.cwd(), loaderConfig);
} else {
fullPath = findConfig(cwd);
fullPath = await findConfig(cwd);
}
return fullPath ? await loadConfig(fullPath) : {};
}
Expand All @@ -36,6 +31,10 @@ export async function loadConfig(file) {

// This function looks for a `.loaderrc` file in case no loader configuration
// was explicitly specified.
export function findConfig(cwd = process.cwd()) {
return findUp.sync(CONFIG_FILES, { cwd });
export async function findConfig(cwd = process.cwd()) {
const explorer = cosmiconfig('loader', {
searchStrategy: 'global',
});
let config = await explorer.search(cwd);
return config?.filepath;
}
Loading
Loading