@pipepack/enhanced-resolver
Version:
yet, reimplement enhanced-resolve with the idea 'simple is best'.
95 lines (78 loc) • 2.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.parseModule = parseModule;
exports.parse = parse;
var _rxjs = require("rxjs");
var _operators = require("rxjs/operators");
var _url = require("url");
var _constant = require("../utils/constant");
/**
* @description - parse original reference path, split pathname, query, hash extra
* @author - huang.jian <hjj491229492@hotmail.com>
*/
// package
// internal
/**
* @description - only run within *Nix environment
*/
function parseChannel(referencePath) {
const head = referencePath.charAt(0);
switch (head) {
case '.':
return _constant.Channel.Relative;
case '/':
return _constant.Channel.Absolute;
case '#':
return _constant.Channel.Internal;
// already assume import reference pass lint
case '@':
return _constant.Channel.Node;
// leave it to developer to parse extra scenario
default:
return /[a-z]/.test(head) ? _constant.Channel.Node : _constant.Channel.Unknown;
}
}
/**
* @description - split extra hash, query part
*/
function parseIdentity(referencePath) {
// just convert file path into url path to extract search and fragment
const url = new _url.URL(referencePath, 'https://github.com');
const referencePathMeta = {
channel: parseChannel(referencePath),
referencePathName: referencePath.replace(url.search, '').replace(url.hash, ''),
referencePathQuery: url.search.replace(/^\?/, ''),
referencePathFragment: url.hash.replace(/^#/, '')
};
return referencePathMeta;
} // just avoid unnecessary type infer
// function parseModuleStatic(): ReferencePathNode {
// return {
// referenceModuleName: '',
// referenceModuleSubpath: '',
// };
// }
// parse node module name into parts
function parseModule(name) {
const regexp = {
normal: /^([^/@]+)(?:\/([^@]+))?/,
scoped: /^(@[^/]+\/[^/@]+)(?:\/([^@]+))?/
};
const [, referenceModuleName, referenceModuleSubpath] = name.startsWith('@') ? regexp.scoped.exec(name) : regexp.normal.exec(name);
const payload = {
referenceModuleName,
referenceModuleSubpath: referenceModuleSubpath || ''
};
return payload;
}
function parse() {
return (0, _rxjs.pipe)((0, _operators.map)(m => {
const meta = parseIdentity(m.referencePath);
return { ...meta,
...m
};
}));
}
//# sourceMappingURL=parse.js.map