Skip to content

Commit 7b3f641

Browse files
committed
feat(yaml)!: use hook filters
BREAKING CHANGES: - drops support for rollup below v2.78.0
1 parent 5cc7fc5 commit 7b3f641

5 files changed

Lines changed: 56 additions & 32 deletions

File tree

packages/yaml/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
## Requirements
1515

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

1818
## Install
1919

packages/yaml/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@
5050
"rollup-plugin"
5151
],
5252
"peerDependencies": {
53-
"rollup": "^1.20.0||^2.0.0||^3.0.0||^4.0.0"
53+
"rollup": "^2.78.0||^3.0.0||^4.0.0"
5454
},
5555
"peerDependenciesMeta": {
5656
"rollup": {
5757
"optional": true
5858
}
5959
},
6060
"dependencies": {
61-
"@rollup/pluginutils": "^5.0.1",
61+
"@rollup/pluginutils": "^5.3.0",
6262
"js-yaml": "^4.1.0",
6363
"tosource": "^2.0.0-alpha.3"
6464
},

packages/yaml/src/index.js

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import YAML from 'js-yaml';
22
import toSource from 'tosource';
3-
import { createFilter, makeLegalIdentifier } from '@rollup/pluginutils';
3+
import { createFilter, makeLegalIdentifier, suffixRegex } from '@rollup/pluginutils';
44

55
const defaults = {
66
documentMode: 'single',
@@ -24,33 +24,40 @@ export default function yaml(opts = {}) {
2424
);
2525
}
2626

27+
const extensionsFilter = suffixRegex(extensions, 'i');
28+
2729
return {
2830
name: 'yaml',
2931

30-
transform(content, id) {
31-
if (!extensions.some((ext) => id.toLowerCase().endsWith(ext))) return null;
32-
if (!filter(id)) return null;
33-
34-
let data = loadMethod(content);
35-
36-
if (typeof options.transform === 'function') {
37-
const result = options.transform(data, id);
38-
// eslint-disable-next-line no-undefined
39-
if (result !== undefined) {
40-
data = result;
32+
transform: {
33+
filter: {
34+
id: extensionsFilter
35+
},
36+
handler(content, id) {
37+
if (!extensionsFilter.test(id)) return null;
38+
if (!filter(id)) return null;
39+
40+
let data = loadMethod(content);
41+
42+
if (typeof options.transform === 'function') {
43+
const result = options.transform(data, id);
44+
// eslint-disable-next-line no-undefined
45+
if (result !== undefined) {
46+
data = result;
47+
}
4148
}
42-
}
4349

44-
const keys = Object.keys(data).filter((key) => key === makeLegalIdentifier(key));
45-
const code = `var data = ${toSource(data)};\n\n`;
46-
const exports = ['export default data;']
47-
.concat(keys.map((key) => `export var ${key} = data.${key};`))
48-
.join('\n');
50+
const keys = Object.keys(data).filter((key) => key === makeLegalIdentifier(key));
51+
const code = `var data = ${toSource(data)};\n\n`;
52+
const exports = ['export default data;']
53+
.concat(keys.map((key) => `export var ${key} = data.${key};`))
54+
.join('\n');
4955

50-
return {
51-
code: code + exports,
52-
map: { mappings: '' }
53-
};
56+
return {
57+
code: code + exports,
58+
map: { mappings: '' }
59+
};
60+
}
5461
}
5562
};
5663
}

packages/yaml/test/test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ test('file extension not in the list', async () => {
102102
const content = 'some content';
103103
const id = 'testfile.unknown';
104104
const plugin = yaml();
105-
const output = plugin.transform(content, id);
105+
const output = plugin.transform.handler(content, id);
106106

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

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

125125
expect(output).toBeNull();
126126
});
@@ -129,7 +129,7 @@ test('uses custom extensions', async () => {
129129
const content = 'some content';
130130
const id = 'testfile.custom';
131131
const plugin = yaml({ extensions: ['.custom'] });
132-
const output = plugin.transform(content, id);
132+
const output = plugin.transform.handler(content, id);
133133

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

143143
expect(output).toBeNull();
144144
});

pnpm-lock.yaml

Lines changed: 19 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)