Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow transformers to return an empty string #2861

Merged
merged 1 commit into from May 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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');