From 42124852a9b2134a445c1daf98424f7a8fc7fb9d Mon Sep 17 00:00:00 2001 From: Janosh Riebesell Date: Wed, 21 Oct 2020 12:23:45 +0200 Subject: [PATCH 1/5] yaml plugin: provide file id to transform function reason for this change: i need to apply different content transformations depending on which file the data came from without knowing the file, the transformer function has to rely on brittle heuristics to identify the file based on the content loaded from it (which can obviously change over time) non-breaking change since people don't have to use the extra transformer argument --- packages/yaml/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/yaml/src/index.js b/packages/yaml/src/index.js index d1d5f2e56..d71a3fbb8 100755 --- a/packages/yaml/src/index.js +++ b/packages/yaml/src/index.js @@ -35,7 +35,7 @@ export default function yamll(opts = {}) { let data = loadMethod(content); if (typeof options.transform === 'function') { - const result = options.transform(data); + const result = options.transform(data, id); // eslint-disable-next-line no-undefined if (result !== undefined) { data = result; From d8b52ab9e9af0d65b18b89c93029f3c68965d926 Mon Sep 17 00:00:00 2001 From: Janosh Riebesell Date: Wed, 21 Oct 2020 17:01:05 +0200 Subject: [PATCH 2/5] update transform function signature --- packages/yaml/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/yaml/README.md b/packages/yaml/README.md index 7a41ae8ea..c436bdb87 100644 --- a/packages/yaml/README.md +++ b/packages/yaml/README.md @@ -90,8 +90,8 @@ A function which can optionally mutate parsed YAML. The function should return t ```js yaml({ - transform(data) { - if (Array.isArray(data)) { + transform(data, filePath) { + if (Array.isArray(data) && filePath === './my-file.yml') { return data.filter(character => !character.batman); } } From 33297a99ef68e3439845a77141893af0b4f7566d Mon Sep 17 00:00:00 2001 From: Janosh Riebesell Date: Wed, 21 Oct 2020 17:05:04 +0200 Subject: [PATCH 3/5] fix typo --- packages/yaml/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/yaml/src/index.js b/packages/yaml/src/index.js index d71a3fbb8..7bbe08976 100755 --- a/packages/yaml/src/index.js +++ b/packages/yaml/src/index.js @@ -9,7 +9,7 @@ const defaults = { }; const ext = /\.ya?ml$/; -export default function yamll(opts = {}) { +export default function yaml(opts = {}) { const options = Object.assign({}, defaults, opts); const { documentMode, safe } = options; const filter = createFilter(options.include, options.exclude); From 610ac221e88451c7ef3fe024c699a3622f95297e Mon Sep 17 00:00:00 2001 From: Janosh Riebesell Date: Wed, 21 Oct 2020 19:44:34 +0200 Subject: [PATCH 4/5] add test case for yaml transformer filepath arg --- packages/yaml/test/test.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/yaml/test/test.js b/packages/yaml/test/test.js index 3f4ce26bc..4f85121dc 100755 --- a/packages/yaml/test/test.js +++ b/packages/yaml/test/test.js @@ -59,8 +59,11 @@ test('resolves extensionless imports in conjunction with nodeResolve plugin', as }); test('applies the optional transform method to parsed YAML', async (t) => { - const transform = (data) => { + const transform = (data, filePath) => { + // check that transformer is passed a correct file path + t.truthy(typeof filePath === 'string' && filePath.endsWith('.yaml')); if (Array.isArray(data)) { + t.truthy(filePath.endsWith('array.yaml')); return data.filter((datum) => !datum.private); } Object.keys(data).forEach((key) => { From 7e68f27d5e6e919d6c04c949c8667e2547a6dd33 Mon Sep 17 00:00:00 2001 From: Janosh Riebesell Date: Thu, 22 Oct 2020 18:15:24 +0200 Subject: [PATCH 5/5] implement @NotWoods suggestion see https://github.com/rollup/plugins/pull/615/files#r510283452 --- packages/yaml/test/test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/yaml/test/test.js b/packages/yaml/test/test.js index 4f85121dc..d8d5b937f 100755 --- a/packages/yaml/test/test.js +++ b/packages/yaml/test/test.js @@ -61,9 +61,9 @@ test('resolves extensionless imports in conjunction with nodeResolve plugin', as test('applies the optional transform method to parsed YAML', async (t) => { const transform = (data, filePath) => { // check that transformer is passed a correct file path - t.truthy(typeof filePath === 'string' && filePath.endsWith('.yaml')); + t.true(typeof filePath === 'string' && filePath.endsWith('.yaml'), filePath); if (Array.isArray(data)) { - t.truthy(filePath.endsWith('array.yaml')); + t.true(filePath.endsWith('array.yaml'), filePath); return data.filter((datum) => !datum.private); } Object.keys(data).forEach((key) => {