Skip to content
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
2 changes: 1 addition & 1 deletion packages/yaml/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

## Requirements

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

## Install

Expand Down
4 changes: 2 additions & 2 deletions packages/yaml/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@
"rollup-plugin"
],
"peerDependencies": {
"rollup": "^1.20.0||^2.0.0||^3.0.0||^4.0.0"
"rollup": "^2.78.0||^3.0.0||^4.0.0"
},
"peerDependenciesMeta": {
"rollup": {
"optional": true
}
},
"dependencies": {
"@rollup/pluginutils": "^5.0.1",
"@rollup/pluginutils": "^5.3.0",
"js-yaml": "^4.1.0",
"tosource": "^2.0.0-alpha.3"
},
Expand Down
51 changes: 29 additions & 22 deletions packages/yaml/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import YAML from 'js-yaml';
import toSource from 'tosource';
import { createFilter, makeLegalIdentifier } from '@rollup/pluginutils';
import { createFilter, makeLegalIdentifier, suffixRegex } from '@rollup/pluginutils';

const defaults = {
documentMode: 'single',
Expand All @@ -24,33 +24,40 @@ export default function yaml(opts = {}) {
);
}

const extensionsFilter = suffixRegex(extensions, 'i');

return {
name: 'yaml',

transform(content, id) {
if (!extensions.some((ext) => id.toLowerCase().endsWith(ext))) return null;
if (!filter(id)) return null;

let data = loadMethod(content);

if (typeof options.transform === 'function') {
const result = options.transform(data, id);
// eslint-disable-next-line no-undefined
if (result !== undefined) {
data = result;
transform: {
filter: {
id: extensionsFilter
},
handler(content, id) {
if (!extensionsFilter.test(id)) return null;
if (!filter(id)) return null;

let data = loadMethod(content);

if (typeof options.transform === 'function') {
const result = options.transform(data, id);
// 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');
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: '' }
};
return {
code: code + exports,
map: { mappings: '' }
};
}
}
};
}
10 changes: 5 additions & 5 deletions packages/yaml/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ test('file extension not in the list', async () => {
const content = 'some content';
const id = 'testfile.unknown';
const plugin = yaml();
const output = plugin.transform(content, id);
const output = plugin.transform.handler(content, id);

expect(output).toBeNull();
});
Expand All @@ -111,7 +111,7 @@ test('file passes the filter', async () => {
const content = 'some content';
const id = 'testfile.yaml';
const plugin = yaml({ include: '**/*.yaml' });
const output = plugin.transform(content, id);
const output = plugin.transform.handler(content, id);

expect(output).not.toBeNull();
});
Expand All @@ -120,7 +120,7 @@ test('file does not pass the filter', async () => {
const content = 'some content';
const id = 'testfile.yaml';
const plugin = yaml({ exclude: '**/*.yaml' });
const output = plugin.transform(content, id);
const output = plugin.transform.handler(content, id);

expect(output).toBeNull();
});
Expand All @@ -129,7 +129,7 @@ test('uses custom extensions', async () => {
const content = 'some content';
const id = 'testfile.custom';
const plugin = yaml({ extensions: ['.custom'] });
const output = plugin.transform(content, id);
const output = plugin.transform.handler(content, id);

expect(output).not.toBeNull();
});
Expand All @@ -138,7 +138,7 @@ test('does not process non-custom extensions', async () => {
const content = 'some content';
const id = 'testfile.yaml';
const plugin = yaml({ extensions: ['.custom'] });
const output = plugin.transform(content, id);
const output = plugin.transform.handler(content, id);

expect(output).toBeNull();
});
21 changes: 19 additions & 2 deletions pnpm-lock.yaml

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

Loading