From 7b3f6419d0101673ba7bf094d4ea6045e1a96449 Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Tue, 24 Mar 2026 03:24:37 +0900 Subject: [PATCH] feat(yaml)!: use hook filters BREAKING CHANGES: - drops support for rollup below v2.78.0 --- packages/yaml/README.md | 2 +- packages/yaml/package.json | 4 +-- packages/yaml/src/index.js | 51 ++++++++++++++++++++++---------------- packages/yaml/test/test.js | 10 ++++---- pnpm-lock.yaml | 21 ++++++++++++++-- 5 files changed, 56 insertions(+), 32 deletions(-) diff --git a/packages/yaml/README.md b/packages/yaml/README.md index d02d17b67..8d2302a97 100644 --- a/packages/yaml/README.md +++ b/packages/yaml/README.md @@ -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 diff --git a/packages/yaml/package.json b/packages/yaml/package.json index 7ee15eccb..382789c1e 100755 --- a/packages/yaml/package.json +++ b/packages/yaml/package.json @@ -50,7 +50,7 @@ "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": { @@ -58,7 +58,7 @@ } }, "dependencies": { - "@rollup/pluginutils": "^5.0.1", + "@rollup/pluginutils": "^5.3.0", "js-yaml": "^4.1.0", "tosource": "^2.0.0-alpha.3" }, diff --git a/packages/yaml/src/index.js b/packages/yaml/src/index.js index bec744f1a..e4bbcdc6a 100755 --- a/packages/yaml/src/index.js +++ b/packages/yaml/src/index.js @@ -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', @@ -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: '' } + }; + } } }; } diff --git a/packages/yaml/test/test.js b/packages/yaml/test/test.js index 4fc6a2891..a5797a8f0 100755 --- a/packages/yaml/test/test.js +++ b/packages/yaml/test/test.js @@ -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(); }); @@ -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(); }); @@ -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(); }); @@ -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(); }); @@ -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(); }); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 56792c42b..c4a36adee 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -805,8 +805,8 @@ importers: packages/yaml: dependencies: '@rollup/pluginutils': - specifier: ^5.0.1 - version: 5.0.1(rollup@4.0.0-24) + specifier: ^5.3.0 + version: 5.3.0(rollup@4.0.0-24) js-yaml: specifier: ^4.1.0 version: 4.1.0 @@ -1836,6 +1836,15 @@ packages: rollup: optional: true + '@rollup/pluginutils@5.3.0': + resolution: {integrity: sha512-5EdhGZtnu3V88ces7s53hhfK5KSASnJZv8Lulpc04cWO3REESroJXg73DFsOmgbU2BhwV0E20bu2IDZb3VKW4Q==} + engines: {node: '>=14.0.0'} + peerDependencies: + rollup: ^1.20.0||^2.0.0||^3.0.0||^4.0.0 + peerDependenciesMeta: + rollup: + optional: true + '@rollup/rollup-android-arm-eabi@4.0.0-24': resolution: {integrity: sha512-19cF3V1fHfzPzwu0cgZEdWLMdNkqSmKOhidqQv1CkUqAMcb7etA7WLx8YrX5ob31ruI0BYYrUDBunlIuMHHUrg==} cpu: [arm] @@ -6504,6 +6513,14 @@ snapshots: optionalDependencies: rollup: 4.0.0-24 + '@rollup/pluginutils@5.3.0(rollup@4.0.0-24)': + dependencies: + '@types/estree': 1.0.8 + estree-walker: 2.0.2 + picomatch: 4.0.3 + optionalDependencies: + rollup: 4.0.0-24 + '@rollup/rollup-android-arm-eabi@4.0.0-24': optional: true