Skip to content

Commit

Permalink
feat: add options.skipEncoding() to pug-load.read()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
brewingcode committed May 2, 2020
1 parent bb0731f commit 2062747
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/pug-load/index.js
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions packages/pug/lib/index.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 2062747

Please sign in to comment.