From 7453ed5575d2c7f0e6aa8639a832020e5e8dee71 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 9b12d3472..1fb206cd9 100644 --- a/packages/pug-load/index.js +++ b/packages/pug-load/index.js @@ -66,7 +66,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 e76487d2c..878b4b610 100644 --- a/packages/pug/lib/index.js +++ b/packages/pug/lib/index.js @@ -86,6 +86,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) { @@ -243,6 +244,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,