npm-module.js 997 B

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict';
  2. var path = require('path'),
  3. fs = require('fs');
  4. var loaderUtils = require('loader-utils');
  5. var getContextDirectory = require('./utility/get-context-directory');
  6. /**
  7. * Codec for relative paths with respect to the context directory.
  8. * @type {{name:string, decode: function}}
  9. */
  10. module.exports = {
  11. name : 'npmModule',
  12. decode: decode
  13. };
  14. /**
  15. * Decode the given uri.
  16. * Include only module paths containing `~`.
  17. * @this {{options: object}} A loader or compilation
  18. * @param {string} uri A source uri to decode
  19. * @returns {boolean|string} False where unmatched else the decoded path
  20. */
  21. function decode(uri) {
  22. /* jshint validthis:true */
  23. if (/~/.test(uri)) {
  24. var relative = loaderUtils.urlToRequest(uri),
  25. base = getContextDirectory.call(this),
  26. absFile = path.normalize(path.join(base, 'node_modules', relative)),
  27. isValid = !!absFile && fs.existsSync(absFile) && fs.statSync(absFile).isFile();
  28. return isValid && absFile;
  29. }
  30. }