Skip to content

Commit

Permalink
yaml transform now handles requires
Browse files Browse the repository at this point in the history
  • Loading branch information
stolksdorf committed Apr 17, 2019
1 parent 4453bd1 commit 08e5c87
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
34 changes: 32 additions & 2 deletions lib/transforms/yaml.js
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))}`
}
};
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@
"@babel/plugin-proposal-object-rest-spread": "^7.4.0",
"@babel/preset-react": "^7.0.0",
"react": "^16.4.2",
"react-dom": "^16.4.2",
"create-react-class": "^15.6.3"
"react-dom": "^16.4.2"
},
"keywords": [
"browserify",
Expand Down

0 comments on commit 08e5c87

Please sign in to comment.