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

Configurable file transform encodings #40

Merged
merged 1 commit into from
Mar 28, 2015
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions lib/builtins/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,26 @@ function map(inputdir, outputdir, options) {

// Otherwise, we queue up a transformation
return queue.add(function (fulfil, reject) {
return sander.readFile(src).then(String).then(function (data) {
// Create context object - this will be passed to transformers
var context = {
log: _this.log,
env: config['default'].env,
src: src, dest: dest, filename: filename
};

var transformOptions = assign['default']({}, options.fn.defaults, options.userOptions);

delete transformOptions.accept;
delete transformOptions.ext;

return sander.readFile(src).then(function (buffer) {
return buffer.toString(transformOptions.sourceEncoding);
}).then(function (data) {
var result, code, map, mappath;

if (_this.aborted) return;

try {
// Create context object - this will be passed to transformers
var context = {
log: _this.log,
env: config['default'].env,
src: src, dest: dest, filename: filename
};

var transformOptions = assign['default']({}, options.fn.defaults, options.userOptions);

delete transformOptions.accept;
delete transformOptions.ext;
result = options.fn.call(context, data, transformOptions);
} catch (e) {
var err = createTransformError(e, src, filename, _this.node);
Expand Down
25 changes: 13 additions & 12 deletions src/builtins/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,24 @@ export default function map ( inputdir, outputdir, options ) {

// Otherwise, we queue up a transformation
return queue.add( ( fulfil, reject ) => {
return readFile( src ).then( String ).then( data => {
// Create context object - this will be passed to transformers
let context = {
log: this.log,
env: config.env,
src, dest, filename
};

let transformOptions = assign( {}, options.fn.defaults, options.userOptions );

delete transformOptions.accept;
delete transformOptions.ext;

return readFile( src ).then( buffer => buffer.toString( transformOptions.sourceEncoding ) ).then( data => {
var result, code, map, mappath;

if ( this.aborted ) return;

try {
// Create context object - this will be passed to transformers
let context = {
log: this.log,
env: config.env,
src, dest, filename
};

let transformOptions = assign( {}, options.fn.defaults, options.userOptions );

delete transformOptions.accept;
delete transformOptions.ext;
result = options.fn.call( context, data, transformOptions );
} catch ( e ) {
let err = createTransformError( e, src, filename, this.node );
Expand Down
29 changes: 29 additions & 0 deletions test/scenarios.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,35 @@ module.exports = function () {
});
});
});

it( 'should use the specified encoding when reading files', function () {
var source = gobble( 'tmp/foo' ), count = 0, foundBar = false;

function plugin ( input, options ) {
count++;

if(this.filename === 'bar.md') {
foundBar = true;

assert.equal(
new Buffer( input, 'base64' ).toString('utf8').trim(),
'bar: this is some text'
);
}

return input.toString( 'base64' );
}
plugin.defaults = {sourceEncoding: 'base64'};

task = source.transform( plugin ).build({
dest: 'tmp/output'
});

return task.then( function () {
assert.equal( count, 3 );
assert( foundBar );
});
});
});


Expand Down