From 2062747282afda73f92d41bd870db8ee1eaa541c Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 7 Feb 2020 14:39:26 -0800 Subject: [PATCH] feat: add options.skipEncoding() to pug-load.read() This allows Pug to correctly read non-utf8 files, if the user wants to do such a thing. The optional function takes the same arguments as load.read(), and should return a truthy/falsey value. If truthy, load.read() will NOT specify an encoding when it reads the file from disk. Otherwise, it will continue to specify "utf-8" as the encoding. --- packages/pug-load/index.js | 7 ++++++- packages/pug/lib/index.js | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/pug-load/index.js b/packages/pug-load/index.js index 1eb1ffcb1..2335d47b9 100644 --- a/packages/pug-load/index.js +++ b/packages/pug-load/index.js @@ -80,7 +80,12 @@ load.resolve = function resolve(filename, source, options) { return filename; }; load.read = function read(filename, options) { - return fs.readFileSync(filename, 'utf8'); + if (options && options.skipEncoding && options.skipEncoding(filename, options)) { + return fs.readFileSync(filename); + } + else { + return fs.readFileSync(filename, 'utf8'); + } }; load.validateOptions = function validateOptions(options) { diff --git a/packages/pug/lib/index.js b/packages/pug/lib/index.js index ec734f7bb..b20e88f9b 100644 --- a/packages/pug/lib/index.js +++ b/packages/pug/lib/index.js @@ -82,6 +82,7 @@ function compileBody(str, options) { var ast = load.string(str, { filename: options.filename, basedir: options.basedir, + skipEncoding: options.skipEncoding, lex: function(str, options) { var lexOptions = {}; Object.keys(options).forEach(function(key) { @@ -268,6 +269,7 @@ exports.compile = function(str, options) { compileDebug: options.compileDebug !== false, filename: options.filename, basedir: options.basedir, + skipEncoding: options.skipEncoding, pretty: options.pretty, doctype: options.doctype, inlineRuntimeFunctions: options.inlineRuntimeFunctions,