From 33eba896621d65ced6b24eb8722658f8d830f023 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Thu, 16 May 2019 07:50:15 +0200 Subject: [PATCH] Allow transformers to return an empty string (#2861) --- src/utils/transform.ts | 6 +++--- .../samples/transform-empty-string/_config.js | 21 +++++++++++++++++++ .../samples/transform-empty-string/main.js | 3 +++ .../transform-empty-string/transformed.js | 1 + 4 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 test/function/samples/transform-empty-string/_config.js create mode 100644 test/function/samples/transform-empty-string/main.js create mode 100644 test/function/samples/transform-empty-string/transformed.js diff --git a/src/utils/transform.ts b/src/utils/transform.ts index 56b1f321d10..84531218a80 100644 --- a/src/utils/transform.ts +++ b/src/utils/transform.ts @@ -76,21 +76,21 @@ export default function transform( } } - if (!result) return code; - if (typeof result === 'string') { result = { ast: undefined, code: result, map: undefined }; - } else { + } else if (result && typeof result === 'object') { if (typeof result.map === 'string') { result.map = JSON.parse(result.map); } if (typeof result.moduleSideEffects === 'boolean') { moduleSideEffects = result.moduleSideEffects; } + } else { + return code; } if (result.map && typeof (result.map as ExistingRawSourceMap).mappings === 'string') { diff --git a/test/function/samples/transform-empty-string/_config.js b/test/function/samples/transform-empty-string/_config.js new file mode 100644 index 00000000000..e6a15ba155f --- /dev/null +++ b/test/function/samples/transform-empty-string/_config.js @@ -0,0 +1,21 @@ +const assert = require('assert'); + +const sideEffects = []; + +module.exports = { + description: 'allows transformers to transform code to an empty string', + context: { sideEffects }, + exports() { + assert.deepStrictEqual(sideEffects, ['this happens']); + }, + options: { + plugins: { + name: 'test-plugin', + transform(code, id) { + if (id.endsWith('transformed.js')) { + return ''; + } + } + } + } +}; diff --git a/test/function/samples/transform-empty-string/main.js b/test/function/samples/transform-empty-string/main.js new file mode 100644 index 00000000000..2a3a72d53c6 --- /dev/null +++ b/test/function/samples/transform-empty-string/main.js @@ -0,0 +1,3 @@ +import './transformed.js'; + +sideEffects.push('this happens'); diff --git a/test/function/samples/transform-empty-string/transformed.js b/test/function/samples/transform-empty-string/transformed.js new file mode 100644 index 00000000000..b302749e118 --- /dev/null +++ b/test/function/samples/transform-empty-string/transformed.js @@ -0,0 +1 @@ +sideEffects.push('should not happen');