How to use the enhanced-resolve.parse function in enhanced-resolve

To help you get started, we’ve selected a few enhanced-resolve examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github botify-labs / webpack-dependency-tree / index.js View on Github external
stats.toJson({modules: true}).modules.forEach(function(mod) {
    // Ignore origin modules
    if (mod.name.indexOf('~') === -1) {
      return;
    }

    // Resolve webpack query string and loaders to retrieve the resource path.
    // webpack dependencies are prepended with (webpack)
    // Module directories are represented by a ~
    var modulePath = resolve.parse(mod.name).resource.path.replace('(webpack)', './~/webpack');
    var moduleFullPath = resolve.parse(mod.identifier).resource.path;

    // The following code maps every `~/module_name` in `modulePath` to its corresponding
    // `module_folder/module_name` in `moduleFullPath`

    // Split at each `~/module_name` and escape for use in a regexp
    var splitParts = modulePath.split(/~\/[^\/]+\//).map(escapeRegExp);

    // Find and traverse every `module_folder/module_name` construct in `moduleFullPath`
    var parentTree = tree;
    for (var i = 1; i < splitParts.length; i++) {
      // Remove everything before the module occurrence so that match.index points
      // to the beginning of the module_folder/module_name construct
      var regexpParts = splitParts.slice(i);
      var regexpHead = '([^\/]+\/([^\/]+)\/)'; // match `module_folder/module_name`
      var regexpTail = regexpParts.join('[^\/]+\/[^\/]+\/'); // match the rest of the path
github botify-labs / webpack-dependency-tree / index.js View on Github external
stats.toJson({modules: true}).modules.forEach(function(mod) {
    // Ignore origin modules
    if (mod.name.indexOf('~') === -1) {
      return;
    }

    // Resolve webpack query string and loaders to retrieve the resource path.
    // webpack dependencies are prepended with (webpack)
    // Module directories are represented by a ~
    var modulePath = resolve.parse(mod.name).resource.path.replace('(webpack)', './~/webpack');
    var moduleFullPath = resolve.parse(mod.identifier).resource.path;

    // The following code maps every `~/module_name` in `modulePath` to its corresponding
    // `module_folder/module_name` in `moduleFullPath`

    // Split at each `~/module_name` and escape for use in a regexp
    var splitParts = modulePath.split(/~\/[^\/]+\//).map(escapeRegExp);

    // Find and traverse every `module_folder/module_name` construct in `moduleFullPath`
    var parentTree = tree;
    for (var i = 1; i < splitParts.length; i++) {
      // Remove everything before the module occurrence so that match.index points
      // to the beginning of the module_folder/module_name construct
      var regexpParts = splitParts.slice(i);
      var regexpHead = '([^\/]+\/([^\/]+)\/)'; // match `module_folder/module_name`
      var regexpTail = regexpParts.join('[^\/]+\/[^\/]+\/'); // match the rest of the path
      var regexp = new RegExp(regexpHead + regexpTail + '$');
github webpack / enhanced-require / lib / RequireContext.js View on Github external
cache[request] = substitutionModule;
			return substitutionModule.exports;
		}
		if(hasOwnProperty.call(this.root.options.substitutionFactories, request)) {
			var substitutionModule = {
				id: request,
				substitution: true,
				exports: {}
			};
			cache[request] = substitutionModule;
			substitutionModule.exports = this.root.options.substitutionFactories[request](this.root.require);
			return substitutionModule.exports;
		}

		// split loaders from resource
		var requestObj = resolve.parse(request);
		var filename = requestObj.resource && requestObj.resource.path;
		var loaders = requestObj.loaders || [];

		// check for resource cache
		if(filename) {
			var content = this.root.contentCache[filename];
			if(!content) {
				content = this.root.contentCache[filename] = fs.readFileSync(filename);
			}
		}

		// execute the loaders
		var source = this.root.sourceCache[request];
		if(!source) {
			var dependencies = filename ? [filename] : [];
			source =
github webpack / enhanced-require / lib / RequireContext.js View on Github external
mapAsync(resolvedModules, function(request, callback) {
			if(typeof request != "string") return callback();
			if(existsNativeModule(request)) return callback();
			if(cache[request]) return callback();
			if(hasOwnProperty.call(options.substitutions, request)) return callback();
			if(hasOwnProperty.call(options.substitutionFactories, request)) return callback();
			if(sourceCache[request]) return callback();

			// split loaders from resource
			var requestObj = resolve.parse(request);
			var filename = requestObj.resource && requestObj.resource.path;
			var loaders = requestObj.loaders || [];

			if(!filename || contentCache[filename]) return makeSource(null, contentCache[filename]);
			return loadContent();

			function loadContent() {
				if(!loadingContent[filename]) {
					loadingContent[filename] = [makeSource];
					fs.readFile(filename, applyToAll(loadingContent[filename], function(content) {
						if(!contentCache[filename])
							contentCache[filename] = content;
						delete loadingContent[filename];
						return contentCache[filename];
					}));
				} else
github webpack / enhanced-require / lib / RequireContext.js View on Github external
contextFn.keys = function() {
		var request = resolve.context.sync(this.context, contextName, this.root.options.resolve);
		var requestObj = resolve.parse(request);
		var files = fs.readdirSync(requestObj.resource.path);
		return files.map(function(file) { return "./" + file; });
	}.bind(this);
	return contextFn;
github webpack / enhanced-require / lib / RequireContext.js View on Github external
function RequireContext(parent, root) {
	this.root = root;
	if(typeof parent == "string") {
		this.context = parent;
	} else {
		this.module = parent;
		var parsedRequest = parent && resolve.parse(parent.id);
		var mod = parsedRequest && (parsedRequest.resource && parsedRequest.resource.path ? parent : root.main);
		if(typeof mod == "string") {
			this.context = mod;
		} else {
			parsedRequest = mod && resolve.parse(mod.id);
			this.context = (parsedRequest) ? path.dirname(mod.id === "." ? process.argv[1] : parsedRequest.resource.path) : "";
		}
	}
}
github webpack / enhanced-require / lib / RequireContext.js View on Github external
function RequireContext(parent, root) {
	this.root = root;
	if(typeof parent == "string") {
		this.context = parent;
	} else {
		this.module = parent;
		var parsedRequest = parent && resolve.parse(parent.id);
		var mod = parsedRequest && (parsedRequest.resource && parsedRequest.resource.path ? parent : root.main);
		if(typeof mod == "string") {
			this.context = mod;
		} else {
			parsedRequest = mod && resolve.parse(mod.id);
			this.context = (parsedRequest) ? path.dirname(mod.id === "." ? process.argv[1] : parsedRequest.resource.path) : "";
		}
	}
}