forked from stolksdorf/vitreum
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4453bd1
commit 08e5c87
Showing
2 changed files
with
33 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,38 @@ | ||
const path = require('path'); | ||
const yaml = require('js-yaml'); | ||
const utils = require('../utils.js'); | ||
|
||
const assetTransform = require('./assets.js'); | ||
|
||
|
||
const processRequires = async (filepath, contents, opts, func)=>{ | ||
const requires = utils.regex(/require\(['"`]([\s\S]*?)['"`]\)/g, contents); | ||
if(requires.length){ | ||
const paths = opts.shared.concat(path.dirname(filepath)); | ||
return await requires.reduce((flow, match)=>{ | ||
let [matchString, reqPath] = match; | ||
const requirePath = require.resolve(reqPath, { paths }); | ||
return flow.then(async (content)=>{ | ||
const replace = await func(requirePath); | ||
return content.replace(matchString, replace); | ||
}) | ||
}, Promise.resolve(contents)); | ||
} | ||
return contents; | ||
}; | ||
|
||
module.exports = { | ||
name : 'yaml', | ||
test : (filepath)=>['.yaml', '.yml'].includes(path.extname(filepath)), | ||
apply : (filepath, contents, opts)=>`module.exports=${JSON.stringify(yaml.safeLoad(contents))}` | ||
}; | ||
apply : async (filepath, contents, opts)=>{ | ||
|
||
contents = await processRequires(filepath, contents, opts, async (requirePath)=>{ | ||
if(assetTransform.test(requirePath)){ | ||
return await assetTransform.apply(requirePath, null, {pathOnly:true, ...opts}); | ||
} | ||
return requirePath; | ||
}); | ||
|
||
return `module.exports=${JSON.stringify(yaml.safeLoad(contents))}` | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters