Skip to content

Commit

Permalink
feat: add source file metadata to output when applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
webketje committed Feb 8, 2022
1 parent 42c13c3 commit 395dc34
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ function initSass(options) {
let compiled

if (isInSrcDir) {
// if the sass file is inside src, it could contain front-matter or templating
// therefore it is unsafe/error-prone to (re-)read it from disk
compiled = sassLib.compileString(files[srcRelPath].contents.toString(), options)
} else {
compiled = sassLib.compile(metalsmith.path(src), options)
Expand All @@ -67,20 +69,24 @@ function initSass(options) {
css += `${EOL}/*# sourceMappingURL=${sourceMapRelPath} */`
}

files[dest] = {
contents: Buffer.from(css)
}

// if the sass entry is inside Metalsmith.source, delete it
if (files[srcRelPath]) {
// if the sass entry is inside Metalsmith.source...
if (isInSrcDir) {
// ...reuse its file metadata
files[dest] = files[srcRelPath]
// ...then delete the source
delete files[srcRelPath]
// log a useful warning to avoid RAM overhead
debug(
'Removed %s from the build. For better performance, move your SASS source files out of Metalsmith.source',
srcRelPath
)
} else {
files[dest] = {}
}

// finally (over)write its contents
files[dest].contents = Buffer.from(css)

debug('Finished compiling %s to %s', src, dest)
} catch (err) {
// we don't call done(err) so we can log multiple sass errors at once for multiple entrypoints
Expand Down

0 comments on commit 395dc34

Please sign in to comment.