Skip to content

Commit

Permalink
feat(yaml): support specifying custom file extensions (#1445)
Browse files Browse the repository at this point in the history
* make yaml plugin extensions configurable

* prettier

* add tests for extensions option
  • Loading branch information
janosh committed May 12, 2023
1 parent 7dfc876 commit 59a2dc2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/yaml/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { createFilter, makeLegalIdentifier } from '@rollup/pluginutils';

const defaults = {
documentMode: 'single',
transform: null
transform: null,
extensions: ['.yaml', '.yml']
};
const ext = /\.ya?ml$/;

export default function yaml(opts = {}) {
const options = Object.assign({}, defaults, opts);
const { documentMode } = options;
const { documentMode, extensions } = options;
const filter = createFilter(options.include, options.exclude);
let loadMethod = null;

Expand All @@ -28,7 +28,7 @@ export default function yaml(opts = {}) {
name: 'yaml',

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

let data = loadMethod(content);
Expand Down
45 changes: 45 additions & 0 deletions packages/yaml/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,48 @@ test('bad documentMode', async (t) => {

t.throws(exec);
});

test('file extension not in the list', async (t) => {
const content = 'some content';
const id = 'testfile.unknown';
const plugin = yaml();
const output = plugin.transform(content, id);

t.is(output, null, 'Should return null for unlisted extensions');
});

test('file passes the filter', async (t) => {
const content = 'some content';
const id = 'testfile.yaml';
const plugin = yaml({ include: '**/*.yaml' });
const output = plugin.transform(content, id);

t.not(output, null, 'Should not return null for files passing the filter');
});

test('file does not pass the filter', async (t) => {
const content = 'some content';
const id = 'testfile.yaml';
const plugin = yaml({ exclude: '**/*.yaml' });
const output = plugin.transform(content, id);

t.is(output, null, 'Should return null for files not passing the filter');
});

test('uses custom extensions', async (t) => {
const content = 'some content';
const id = 'testfile.custom';
const plugin = yaml({ extensions: ['.custom'] });
const output = plugin.transform(content, id);

t.not(output, null, 'Should not return null for files with custom extensions');
});

test('does not process non-custom extensions', async (t) => {
const content = 'some content';
const id = 'testfile.yaml';
const plugin = yaml({ extensions: ['.custom'] });
const output = plugin.transform(content, id);

t.is(output, null, 'Should return null for files without custom extensions');
});

0 comments on commit 59a2dc2

Please sign in to comment.