Brak opisu

url-rebase.js 886B

1234567891011121314151617181920212223242526272829303132
  1. var path = require('path');
  2. var UrlRewriter = require('./url-rewriter');
  3. module.exports = function UrlRebase(options, context) {
  4. var process = function(data) {
  5. var rebaseOpts = {
  6. absolute: !!options.root,
  7. relative: !options.root && !!options.target,
  8. fromBase: options.relativeTo
  9. };
  10. if (!rebaseOpts.absolute && !rebaseOpts.relative)
  11. return data;
  12. if (rebaseOpts.absolute && !!options.target)
  13. context.warnings.push('Both \'root\' and output file given so rebasing URLs as absolute paths');
  14. if (rebaseOpts.absolute)
  15. rebaseOpts.toBase = path.resolve(options.root);
  16. if (rebaseOpts.relative)
  17. rebaseOpts.toBase = path.resolve(path.dirname(options.target));
  18. if (!rebaseOpts.fromBase || !rebaseOpts.toBase)
  19. return data;
  20. return UrlRewriter.process(data, rebaseOpts);
  21. };
  22. return { process: process };
  23. };