diff --git a/lib/applyLoaders.js b/lib/applyLoaders.js index 2c1b168..353bfa7 100644 --- a/lib/applyLoaders.js +++ b/lib/applyLoaders.js @@ -1,5 +1,6 @@ var HappyFakeLoaderContext = require('./HappyFakeLoaderContext'); var fnOncePedantic = require('./fnOncePedantic'); +var convertArgs = require('./convertArgs'); var assert = require('assert'); /** @@ -274,7 +275,11 @@ function NormalLoaderApplier(loaders, dataMap, done) { loader.module ; - applySyncOrAsync(fn, context, [ sourceCode, sourceMap ], + var raw = loader.module.raw; + + var args = [ sourceCode, sourceMap ]; + + applySyncOrAsync(fn, context, convertArgs(args, raw), function(err, nextSource, nextSourceMap) { if (err) { return done(err); diff --git a/lib/convertArgs.js b/lib/convertArgs.js new file mode 100644 index 0000000..bda9032 --- /dev/null +++ b/lib/convertArgs.js @@ -0,0 +1,24 @@ +module.exports = function convertArgs(args, raw) { + /* Not sure why buffer would convert to JSON */ + if (args[0].type === 'Buffer') { + args[0] = Buffer.from(args[0].data); + } + + if(!raw && Buffer.isBuffer(args[0])) { + args[0] = utf8BufferToString(args[0]); + } + else if(raw && typeof args[0] === "string") { + args[0] = new Buffer(args[0], "utf-8"); + } + + return args; +} + +function utf8BufferToString(buf) { + var str = buf.toString("utf-8"); + if(str.charCodeAt(0) === 0xFEFF) { + return str.substr(1); + } else { + return str; + } +}