Skip to content

Commit

Permalink
chore: migrate rollup-plugin-yaml (#36)
Browse files Browse the repository at this point in the history
* chore: migrate rollup-plugin-yaml

* chore: misc metadata fixes

* chore: resolve review comments:
  • Loading branch information
shellscape authored Nov 17, 2019
1 parent 757948a commit e3e20e9
Show file tree
Hide file tree
Showing 25 changed files with 1,479 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
link-workspace-packages = true
link-workspace-packages = false
shamefully-hoist = true
shared-workspace-shrinkwrap = true
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ This repository houses plugins that Rollup considers critical to every day use o
| [replace](packages/replace) | Replace strings in files while bundling |
| [strip](packages/strip) | Remove debugger statements and functions like assert.equal and console.log from your code |
| [wasm](packages/wasm) | Import WebAssembly code with Rollup |
| [yaml](packages/yaml) | Convert YAML files to ES6 modules |
| | |

## Contributing
Expand Down
2 changes: 1 addition & 1 deletion packages/json/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# @rollup/plugin-json

🍣 A Rollup which Converts .json files to ES6 modules.
🍣 A Rollup plugin which Converts .json files to ES6 modules.

## Requirements

Expand Down
5 changes: 4 additions & 1 deletion packages/strip/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ import pkg from './package.json';

export default {
input: 'src/index.js',
output: [{ format: 'cjs', file: pkg.main }, { format: 'esm', file: pkg.module }]
output: [
{ format: 'cjs', file: pkg.main },
{ format: 'esm', file: pkg.module }
]
};
15 changes: 15 additions & 0 deletions packages/yaml/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# @rollup/plugin-yaml Change Log

## 2.0.0
*2019-10-18*

* Add `transform` option [#6](https://github.com/rollup/rollup-plugin-yaml/pull/6) (by @CharlesHolbrow)
* Update dependencies and build, require Node 6 [#7](https://github.com/rollup/rollup-plugin-yaml/pull/7) (by @lukastaegert)

## 1.1.0

* Switch to `js-yaml` for parsing [#2](https://github.com/rollup/rollup-plugin-yaml/pull/2)

## 1.0.0

* First release
91 changes: 91 additions & 0 deletions packages/yaml/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
[npm]: https://img.shields.io/npm/v/@rollup/plugin-yaml
[npm-url]: https://www.npmjs.com/package/@rollup/plugin-yaml
[size]: https://packagephobia.now.sh/badge?p=@rollup/plugin-yaml
[size-url]: https://packagephobia.now.sh/result?p=@rollup/plugin-yaml

[![npm][npm]][npm-url]
[![size][size]][size-url]
[![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com)

# @rollup/plugin-yaml

🍣 A Rollup plugin which Converts YAML files to ES6 modules.

## Requirements

This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v8.0.0+) and Rollup v1.20.0+.

## Install

Using npm:

```console
npm install @rollup/plugin-yaml --save-dev
```

## Usage

Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:

```js
import yaml from '@rollup/plugin-yaml';

export default {
input: 'src/index.js',
output: {
dir: 'output',
format: 'cjs'
},
plugins: [yaml()]
};
```

Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#command-line-reference) or the [API](https://www.rollupjs.org/guide/en/#javascript-api).

With an accompanying file `src/index.js`, the local `heroes.yaml` file would now be importable as seen below:

```js
// src/index.js
import { batman } from './heroes.yaml';

console.log(`na na na na ${batman}`);
```

## Options

### `exclude`

Type: `String` | `Array[...String]`<br>
Default: `null`

A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should _ignore_. By default no files are ignored.

### `include`

Type: `String` | `Array(String)`<br>
Default: `null`

A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should operate on. By default all files are targeted.

### `transform`

Type: `Function`<br>
Default: `undefined`

A function which can optionally mutate parsed YAML. The function should return the mutated `object`, or `undefined` which will make no changes to the parsed YAML.

```js
yaml({
transform(data) {
if (Array.isArray(data)) {
return data.filter(character => !character.batman);
}
}
});
```

## Meta

[CONTRIBUTING](/.github/CONTRIBUTING.md)

[LICENSE (MIT)](/LICENSE)
68 changes: 68 additions & 0 deletions packages/yaml/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"name": "@rollup/plugin-yaml",
"version": "2.0.0",
"publishConfig": {
"access": "public"
},
"description": "Convert YAML files to ES6 modules",
"license": "MIT",
"repository": "rollup/plugins",
"author": "rollup",
"homepage": "https://github.com/rollup/plugins/packages/yaml/#readme",
"bugs": "https://github.com/rollup/plugins/issues",
"main": "dist/index.js",
"scripts": {
"build": "rollup -c",
"ci:coverage": "nyc pnpm run test && nyc report --reporter=text-lcov > coverage.lcov",
"ci:lint": "pnpm run build && pnpm run lint && pnpm run security",
"ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}",
"ci:test": "pnpm run test -- --verbose",
"lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package",
"lint:docs": "prettier --single-quote --write README.md",
"lint:js": "eslint --fix --cache src test",
"lint:package": "prettier --write package.json --plugin=prettier-plugin-package",
"prebuild": "del-cli dist",
"prepare": "pnpm run build",
"prepublishOnly": "pnpm run lint",
"pretest": "pnpm run build",
"security": "echo 'pnpm needs `npm audit` support'",
"test": "ava"
},
"files": [
"dist",
"README.md",
"LICENSE"
],
"keywords": [
"rollup",
"plugin",
"yaml"
],
"peerDependencies": {
"rollup": "^1.20.0"
},
"dependencies": {
"js-yaml": "^3.13.1",
"rollup-pluginutils": "^2.8.2",
"tosource": "^1.0.0"
},
"devDependencies": {
"@babel/core": "^7.7.2",
"@babel/preset-env": "^7.7.1",
"del-cli": "^3.0.0",
"rollup": "^1.27.0",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-node-resolve": "^5.2.0",
"source-map-support": "^0.5.16"
},
"ava": {
"files": [
"!**/fixtures/**",
"!**/helpers/**",
"!**/recipes/**",
"!**/types.ts"
]
},
"jsnext:main": "dist/index.es.js",
"module": "dist/index.es.js"
}
26 changes: 26 additions & 0 deletions packages/yaml/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import babel from 'rollup-plugin-babel';

import pkg from './package.json';

export default {
input: 'src/index.js',
plugins: [
babel({
presets: [
[
'@babel/preset-env',
{
targets: {
node: 6
}
}
]
]
})
],
external: Object.keys(pkg.dependencies),
output: [
{ file: pkg.main, format: 'cjs', sourcemap: true },
{ file: pkg.module, format: 'es', sourcemap: true }
]
};
41 changes: 41 additions & 0 deletions packages/yaml/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import YAML from 'js-yaml';
import toSource from 'tosource';
import { createFilter, makeLegalIdentifier } from 'rollup-pluginutils';

const ext = /\.ya?ml$/;

export default function yamll(options = {}) {
const filter = createFilter(options.include, options.exclude);

return {
name: 'yaml',

transform(content, id) {
if (!ext.test(id)) return null;
if (!filter(id)) return null;

let data = YAML.load(content);

if (typeof options.transform === 'function') {
const result = options.transform(data);
// eslint-disable-next-line no-undefined
if (result !== undefined) {
data = result;
}
}

const keys = Object.keys(data).filter((key) => key === makeLegalIdentifier(key));

const code = `var data = ${toSource(data)};\n\n`;

const exports = ['export default data;']
.concat(keys.map((key) => `export var ${key} = data.${key};`))
.join('\n');

return {
code: code + exports,
map: { mappings: '' }
};
}
};
}
5 changes: 5 additions & 0 deletions packages/yaml/test/fixtures/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"globals": {
"t": "readonly"
}
}
2 changes: 2 additions & 0 deletions packages/yaml/test/fixtures/basic/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
answer:
42
3 changes: 3 additions & 0 deletions packages/yaml/test/fixtures/basic/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import config from './config.yaml';

t.is(config.answer, 42);
2 changes: 2 additions & 0 deletions packages/yaml/test/fixtures/extensionless/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
answer:
42
2 changes: 2 additions & 0 deletions packages/yaml/test/fixtures/extensionless/dir/index.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Are extensionless imports and /index resolutions a good idea?:
No.
7 changes: 7 additions & 0 deletions packages/yaml/test/fixtures/extensionless/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* eslint-disable import/no-unresolved, import/extensions */

import config from './config';
import questions from './dir';

t.is(config.answer, 42);
t.is(questions['Are extensionless imports and /index resolutions a good idea?'], 'No.');
2 changes: 2 additions & 0 deletions packages/yaml/test/fixtures/named/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
answer:
42
3 changes: 3 additions & 0 deletions packages/yaml/test/fixtures/named/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { answer } from './config.yaml';

t.is(answer, 42);
5 changes: 5 additions & 0 deletions packages/yaml/test/fixtures/transform/array.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- name: alice
private: true
- name: bob
private: false
- name: carl
13 changes: 13 additions & 0 deletions packages/yaml/test/fixtures/transform/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* eslint-disable no-prototype-builtins */

import array from './array.yaml';
import object from './object.yaml';

t.is(array.length, 2);
t.is(array[0].name, 'bob');
t.is(array[1].name, 'carl');

t.is(Object.keys(object).length, 2);
t.falsy(object.hasOwnProperty('alice'));
t.truthy(object.hasOwnProperty('bob'));
t.truthy(object.hasOwnProperty('carl'));
3 changes: 3 additions & 0 deletions packages/yaml/test/fixtures/transform/object.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
alice: { private: true }
bob: { private: false }
carl: {}
2 changes: 2 additions & 0 deletions packages/yaml/test/fixtures/yml/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
answer:
42
3 changes: 3 additions & 0 deletions packages/yaml/test/fixtures/yml/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import config from './config.yml';

t.is(config.answer, 42);
Loading

0 comments on commit e3e20e9

Please sign in to comment.