Skip to content

Commit

Permalink
Close #74 PR: Don't add '.js' extension if the source file doesn't ha…
Browse files Browse the repository at this point in the history
…ve an extension.
  • Loading branch information
Christopher Crouzet authored and sindresorhus committed Jan 30, 2016
1 parent fe5908e commit 6c7a310
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
13 changes: 11 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
'use strict';
var path = require('path');
var gutil = require('gulp-util');
var through = require('through2');
var applySourceMap = require('vinyl-sourcemaps-apply');
var objectAssign = require('object-assign');
var replaceExt = require('replace-ext');
var babel = require('babel-core');

function replaceExtension(filepath) {
if (path.extname(filepath)) {
return replaceExt(filepath, '.js');
}

return filepath;
}

module.exports = function (opts) {
opts = opts || {};

Expand All @@ -32,13 +41,13 @@ module.exports = function (opts) {
var res = babel.transform(file.contents.toString(), fileOpts);

if (file.sourceMap && res.map) {
res.map.file = replaceExt(res.map.file, '.js');
res.map.file = replaceExtension(res.map.file);
applySourceMap(file, res.map);
}

if (!res.ignored) {
file.contents = new Buffer(res.code);
file.path = replaceExt(file.path, '.js');
file.path = replaceExtension(file.path);
}

file.babel = res.metadata;
Expand Down
20 changes: 20 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,23 @@ it('should not rename ignored files', function (cb) {
.on('end', cb)
.end(new gutil.File(inputFile));
});

it('should not rename files without an extension', function (cb) {
var stream = babel();

var inputFile = {
cwd: __dirname
};

inputFile.base = path.join(inputFile.cwd, 'bin');
inputFile.basename = 'app';
inputFile.path = path.join(inputFile.base, inputFile.basename);
inputFile.contents = new Buffer(';');

stream
.on('data', function (file) {
assert.equal(file.relative, inputFile.basename);
})
.on('end', cb)
.end(new gutil.File(inputFile));
});

0 comments on commit 6c7a310

Please sign in to comment.