From 47666a7559c627dc91c8d3d7d508b419518a0721 Mon Sep 17 00:00:00 2001 From: Cristopher Date: Fri, 9 May 2025 21:42:59 +0700 Subject: [PATCH] [WIP] refactor: add hook filter --- packages/yaml/package.json | 4 +-- packages/yaml/src/index.js | 55 ++++++++++++++++++++------------------ packages/yaml/test/test.js | 45 ------------------------------- 3 files changed, 31 insertions(+), 73 deletions(-) diff --git a/packages/yaml/package.json b/packages/yaml/package.json index cd7c102ab..a0a7ba028 100755 --- a/packages/yaml/package.json +++ b/packages/yaml/package.json @@ -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": { @@ -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", diff --git a/packages/yaml/src/index.js b/packages/yaml/src/index.js index bec744f1a..775b52e8a 100755 --- a/packages/yaml/src/index.js +++ b/packages/yaml/src/index.js @@ -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') { @@ -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: '' } + }; + } } }; } diff --git a/packages/yaml/test/test.js b/packages/yaml/test/test.js index e0e730c17..9b2f9e39a 100755 --- a/packages/yaml/test/test.js +++ b/packages/yaml/test/test.js @@ -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'); -});