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
4 changes: 2 additions & 2 deletions packages/yaml/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"rollup-plugin"
],
"peerDependencies": {
"rollup": "^2.78.0||^3.0.0||^4.0.0"
"rollup": "^2.78.0||^3.0.0||^4.0.0||^5.0.0"
},
"peerDependenciesMeta": {
"rollup": {
Expand All @@ -65,7 +65,7 @@
"devDependencies": {
"@rollup/plugin-node-resolve": "^15.0.0",
"del-cli": "^5.0.0",
"rollup": "^4.0.0-24",
"rollup": "catalog:rollup-next",
"source-map-support": "^0.5.21"
},
"types": "./types/index.d.ts"
Expand Down
16 changes: 11 additions & 5 deletions packages/yaml/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,28 @@ export default function yaml(opts = {}) {
}

const extensionsFilter = suffixRegex(extensions, 'i');
// Rollup 5 appends encoded import attributes to the id as a query
const extensionsWithQueryFilter = new RegExp(
extensionsFilter.source.replace(/\$$/, '(?:$|\\?)'),
extensionsFilter.flags
);

return {
name: 'yaml',

transform: {
filter: {
id: extensionsFilter
id: extensionsWithQueryFilter
},
handler(content, id) {
if (extensions.length === 0 || !extensionsFilter.test(id)) return null;
if (!filter(id)) return null;
handler(content, id, transformOptions) {
const rawId = (transformOptions && transformOptions.rawId) || id;
if (extensions.length === 0 || !extensionsFilter.test(rawId)) return null;
if (!filter(rawId)) return null;

let data = loadMethod(content);

if (typeof options.transform === 'function') {
const result = options.transform(data, id);
const result = options.transform(data, rawId);
// eslint-disable-next-line no-undefined
if (result !== undefined) {
data = result;
Expand Down
3 changes: 3 additions & 0 deletions packages/yaml/test/fixtures/basic/main-with-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import config from './config.yaml' with { type: 'yaml' };

expect(config.answer).toBe(42);
19 changes: 19 additions & 0 deletions packages/yaml/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,22 @@ test('does not process non-custom extensions', async () => {

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

test('handles yaml files imported with import attributes', async () => {
let transformedFilePath;
const bundle = await rollup.rollup({
input: 'fixtures/basic/main-with-type.js',
plugins: [
yaml({
include: '**/*.yaml',
exclude: /attributes/,
transform(data, filePath) {
transformedFilePath = filePath;
}
})
]
});
const { error } = await testBundle(null, bundle);
expect(error).toBeUndefined();
expect(transformedFilePath).toMatch(/config\.yaml$/);
});
Loading
Loading