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(yaml): introduce #36

Open
wants to merge 2 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
131 changes: 95 additions & 36 deletions coverage.lcov
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,12 @@ BRDA:1,0,0,2
BRDA:1,1,0,41
BRDA:4,2,0,5
BRDA:4,3,0,36
BRDA:6,4,0,3
BRDA:8,5,0,2
BRDA:6,4,0,2
BRDA:6,5,0,31
BRDA:8,6,0,6
BRDA:8,7,0,28
BRDA:6,8,0,2
BRDA:6,9,0,31
BRDA:6,8,0,3
BRDA:8,9,0,2
BRF:10
BRH:10
DA:1,2
Expand All @@ -297,7 +297,7 @@ DA:3,41
DA:4,41
DA:5,36
DA:6,41
DA:7,31
DA:7,3
DA:8,41
DA:9,28
DA:10,28
Expand Down Expand Up @@ -351,13 +351,13 @@ DA:19,1
DA:20,1
DA:21,1
DA:22,1
DA:23,1
DA:23,0
DA:24,1
DA:25,1
DA:26,1
DA:27,1
DA:25,0
DA:26,0
DA:27,0
DA:28,0
LH:20
LH:16
LF:28
end_of_record
SF:packages/mismatched-format/mismatched-format.mjs
Expand Down Expand Up @@ -438,39 +438,39 @@ SF:packages/parse-filename/parse-filename.mjs
FN:10,getFilenameExt
FN:14,stripExtras
FN:21,getFilenameParts
FNDA:93,getFilenameExt
FNDA:99,stripExtras
FNDA:96,getFilenameExt
FNDA:102,stripExtras
FNDA:3,getFilenameParts
FNF:3
FNH:3
BRDA:1,0,0,7
BRDA:10,1,0,93
BRDA:14,2,0,99
BRDA:1,0,0,8
BRDA:10,1,0,96
BRDA:14,2,0,102
BRDA:21,3,0,3
BRDA:24,4,0,0
BRF:5
BRH:4
DA:1,7
DA:2,7
DA:3,7
DA:4,7
DA:5,7
DA:6,7
DA:7,7
DA:8,7
DA:9,7
DA:10,7
DA:11,93
DA:12,93
DA:13,7
DA:14,7
DA:15,99
DA:16,99
DA:17,7
DA:18,7
DA:19,7
DA:20,7
DA:21,7
DA:1,8
DA:2,8
DA:3,8
DA:4,8
DA:5,8
DA:6,8
DA:7,8
DA:8,8
DA:9,8
DA:10,8
DA:11,96
DA:12,96
DA:13,8
DA:14,8
DA:15,102
DA:16,102
DA:17,8
DA:18,8
DA:19,8
DA:20,8
DA:21,8
DA:22,3
DA:23,3
DA:24,3
Expand Down Expand Up @@ -760,3 +760,62 @@ DA:70,1
LH:70
LF:70
end_of_record
SF:packages/yaml/yaml.mjs
FN:7,resolveYaml
FN:26,loadYaml
FNDA:3,resolveYaml
FNDA:3,loadYaml
FNF:2
FNH:2
BRDA:1,0,0,1
BRDA:7,1,0,3
BRDA:13,2,0,2
BRDA:14,3,0,2
BRDA:17,4,0,1
BRDA:26,5,0,3
BRDA:27,6,0,1
BRDA:27,7,0,2
BRF:8
BRH:8
DA:1,1
DA:2,1
DA:3,1
DA:4,1
DA:5,1
DA:6,1
DA:7,3
DA:8,3
DA:9,3
DA:10,3
DA:11,3
DA:12,3
DA:13,3
DA:14,3
DA:15,2
DA:16,2
DA:17,2
DA:18,1
DA:19,1
DA:20,3
DA:21,1
DA:22,1
DA:23,1
DA:24,1
DA:25,1
DA:26,3
DA:27,3
DA:28,2
DA:29,2
DA:30,2
DA:31,2
DA:32,2
DA:33,2
DA:34,2
DA:35,2
DA:36,2
DA:37,2
DA:38,3
DA:39,1
LH:39
LF:39
end_of_record
30 changes: 29 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions packages/yaml/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Nodejs Loaders: YAML

<img src="https://raw.githubusercontent.com/JakobJingleheimer/nodejs-loaders/refs/heads/main/logo.svg" height="100" width="100" alt="@node.js loaders logo" />

[![npm version](https://img.shields.io/npm/v/nodejs-loaders/yaml.svg)](https://www.npmjs.com/package/nodejs-loaders/yaml)
![size](https://img.shields.io/github/languages/code-size/JakobJingleheimer/nodejs-loaders/yaml)

**Environment**: test, development, maybe production (but carfully)

This loader allow you to import [YAML](https://yaml.org) files as plain objects. It uses the `js-yaml` package to parse the YAML files. This is useful for configuration files, or any other data that is more easily represented in YAML than JSON.

```yaml
# config.yaml
foo: bar
baz:
- qux
- zed
```

```js
import config from './config.yaml';

config.foo; // 'bar'
config.baz; // ['qux', 'zed']
```

<details>
<summary>Supported file extensions</summary>

* `.yaml`
* `.yml`
</details>

**Alternatives**

* [`esm-loader-yaml`](https://www.npmjs.com/package/esm-loader-yaml)
24 changes: 24 additions & 0 deletions packages/yaml/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@nodejs-loaders/yaml",
"description": "YAML loader for Node.js",
"version": "1.0.0",
"type": "module",
"author": "Augustin Mauroy",
"maintainers": [
"Jacob Smith"
],
"license": "ISC",
"keywords": [
"customisation hook",
"loader",
"node.js",
"yaml"
],
"engines": {
"node": ">20"
},
"dependencies": {
"@nodejs-loaders/parse-filename": "1.0.0",
"yaml": "^2.6.0"
}
}
39 changes: 39 additions & 0 deletions packages/yaml/yaml.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { getFilenameExt } from '@nodejs-loaders/parse-filename';
import { parse } from 'yaml';

/**
* @type {import('node:module').ResolveHook}
*/
async function resolveYaml(specifier, ctx, nextResolve) {
const nextResult = await nextResolve(specifier);
// Check against the fully resolved URL, not just the specifier, in case another loader has
// something to contribute to the resolution.
const ext = getFilenameExt(nextResult.url);

if (ext === '.yaml' || ext === '.yml')
return {
...nextResult,
format: 'yaml',
};

return nextResult;
}
export { resolveYaml as resolve }

/**
* @type {import('node:module').LoadHook}
*/
async function loadYaml(url, ctx, nextLoad) {
if (ctx.format !== 'yaml') return nextLoad(url);

const nextResult = await nextLoad(url, { format: 'module' });
const rawSource = '' + nextResult.source; // byte array → string

const source = parse(rawSource);

return {
format: 'module',
source,
};
}
export { loadYaml as load }
Loading
Loading