Skip to content

[WIP] refactor: add hook filter #1870

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions packages/yaml/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"yaml"
],
"peerDependencies": {
"rollup": "^1.20.0||^2.0.0||^3.0.0||^4.0.0"
"rollup": "^4.38.0"
},
"peerDependenciesMeta": {
"rollup": {
Expand All @@ -64,7 +64,7 @@
"devDependencies": {
"@rollup/plugin-node-resolve": "^15.0.0",
"del-cli": "^5.0.0",
"rollup": "^4.0.0-24",
"rollup": "^4.38.0",
"source-map-support": "^0.5.21"
},
"types": "./types/index.d.ts",
Expand Down
55 changes: 29 additions & 26 deletions packages/yaml/src/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import YAML from 'js-yaml';
import toSource from 'tosource';
import { createFilter, makeLegalIdentifier } from '@rollup/pluginutils';
import { makeLegalIdentifier } from '@rollup/pluginutils';

const defaults = {
documentMode: 'single',
transform: null,
extensions: ['.yaml', '.yml']
include: ['*.yaml', '.yml']
};

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

if (documentMode === 'single') {
Expand All @@ -26,31 +25,35 @@ export default function yaml(opts = {}) {

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: {
include,
exclude
}
},
handler(content, id) {
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: '' }
};
}
}
};
}
45 changes: 0 additions & 45 deletions packages/yaml/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,48 +98,3 @@ 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');
});