You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
997 B

4 years ago
  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 : 'npm-module',
  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. }