Skip to content

Commit

Permalink
Allow transformers to return an empty string (#2861)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed May 16, 2019
1 parent 53947a1 commit 33eba89
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/utils/transform.ts
Expand Up @@ -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') {
Expand Down
21 changes: 21 additions & 0 deletions 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 '';
}
}
}
}
};
3 changes: 3 additions & 0 deletions test/function/samples/transform-empty-string/main.js
@@ -0,0 +1,3 @@
import './transformed.js';

sideEffects.push('this happens');
@@ -0,0 +1 @@
sideEffects.push('should not happen');

0 comments on commit 33eba89

Please sign in to comment.