Skip to content

Commit

Permalink
feat: allow filters to read non-text include files (#3213)
Browse files Browse the repository at this point in the history
  • Loading branch information
brewingcode authored May 19, 2020
1 parent bb0731f commit 9e96bb7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 9 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"testEnvironment": "node",
"snapshotSerializers": [
"./scripts/filename-serializer.js",
"./scripts/prettier-javascript-serializer.js"
"./scripts/prettier-javascript-serializer.js",
"./scripts/buffer-serializer.js"
]
},
"license": "MIT",
Expand Down
16 changes: 12 additions & 4 deletions packages/pug-filters/lib/handle-filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ function handleFilters(ast, filters, options, filterAliases) {
var firstFilter = node.filters.pop();
var attrs = getAttributes(firstFilter, options);
var filename = (attrs.filename = node.file.fullPath);
var str = node.file.str;
node.type = 'Text';
node.val = filterFileWithFallback(firstFilter, filename, str, attrs);
node.val = filterFileWithFallback(
firstFilter,
filename,
node.file,
attrs
);
node.filters
.slice()
.reverse()
Expand Down Expand Up @@ -55,10 +59,14 @@ function handleFilters(ast, filters, options, filterAliases) {
}
}

function filterFileWithFallback(filter, filename, text, attrs) {
function filterFileWithFallback(filter, filename, file, attrs) {
var filterName = getFilterName(filter);
if (filters && filters[filterName]) {
return filters[filterName](text, attrs);
if (filters[filterName].renderBuffer) {
return filters[filterName].renderBuffer(file.raw, attrs);
} else {
return filters[filterName](file.str, attrs);
}
} else {
return filterWithFallback(filter, filename, attrs, 'renderFile');
}
Expand Down
10 changes: 6 additions & 4 deletions packages/pug-load/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ function load(ast, options) {
if (file.type !== 'FileReference') {
throw new Error('Expected file.type to be "FileReference"');
}
var path, str;
var path, str, raw;
try {
path = options.resolve(file.path, file.filename, options);
file.fullPath = path;
str = options.read(path, options);
raw = options.read(path, options);
str = raw.toString('utf8');
} catch (ex) {
ex.message += '\n at ' + node.filename + ' line ' + node.line;
throw ex;
}
file.str = str;
file.raw = raw;
if (node.type === 'Extends' || node.type === 'Include') {
file.ast = load.string(
str,
Expand All @@ -56,7 +58,7 @@ load.file = function loadFile(filename, options) {
options = assign(getOptions(options), {
filename: filename,
});
var str = options.read(filename);
var str = options.read(filename).toString('utf8');
return load.string(str, options);
};

Expand All @@ -80,7 +82,7 @@ load.resolve = function resolve(filename, source, options) {
return filename;
};
load.read = function read(filename, options) {
return fs.readFileSync(filename, 'utf8');
return fs.readFileSync(filename);
};

load.validateOptions = function validateOptions(options) {
Expand Down
15 changes: 15 additions & 0 deletions packages/pug-load/test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ Object {
"fullPath": "<dirname>/bar.pug",
"line": 1,
"path": "bar.pug",
"raw": Object {
"hash": "538bf7d4b81ef364b1f2e9d42c11f156",
"size": 11,
"type": "Buffer",
},
"str": "block bing
",
"type": "FileReference",
Expand Down Expand Up @@ -99,6 +104,11 @@ Object {
"fullPath": "<dirname>/bing.pug",
"line": 4,
"path": "bing.pug",
"raw": Object {
"hash": "58ecbe086e7a045084cbddac849a2563",
"size": 11,
"type": "Buffer",
},
"str": ".bing bong
",
"type": "FileReference",
Expand All @@ -122,6 +132,11 @@ Object {
"fullPath": "<dirname>/script.js",
"line": 6,
"path": "script.js",
"raw": Object {
"hash": "86d4f8e34165faeb09f10255121078f8",
"size": 32,
"type": "Buffer",
},
"str": "document.write('hello world!');
",
"type": "FileReference",
Expand Down
19 changes: 19 additions & 0 deletions scripts/buffer-serializer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const crypto = require('crypto');

// Buffer serializer to reduce snapshot gore for Node Buffer type
module.exports = {
test: function(val) {
return val && Buffer.isBuffer(val);
},
print: function(val, serialize, indent) {
const output = {
type: 'Buffer',
size: val.length,
hash: crypto
.createHash('md5')
.update(val)
.digest('hex'),
};
return serialize(output);
},
};

0 comments on commit 9e96bb7

Please sign in to comment.