{"size": 4388, "ext": "js", "max_stars_repo_path": "index.js", "max_stars_repo_name": "Pure-UI/pure-loader", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "index.js", "max_issues_repo_name": "Pure-UI/pure-loader", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "index.js", "max_forks_repo_name": "Pure-UI/pure-loader", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.1282051282, "max_line_length": 178, "alphanum_fraction": 0.646308113}
var loaderUtils = require( 'loader-utils' );
var parse = require( './parser' );
var assign = require('object-assign');
var hash = require('hash-sum');
var es6Promise = require('es6-promise');
var path = require('path')

es6Promise.polyfill();

module.exports = function( content ) {
	this.cacheable();

	var loaderContext = this;
	var options = this.options.pure || {};
	var filePath = this.resourcePath;
	var fileName = path.basename( filePath );
	var moduleId = '_r-' + hash( filePath );
	var rewriterInjectRE = /\b(css(-loader)?(\?[^!]+)?)(?:!|$)/
	var selectorPath = require.resolve( './selector' );
	var precompileLoaderPath = require.resolve( './precompile' );
	// use modified html-loader
	var htmlLoaderPath = require.resolve( './html-loader' );
	var defaultLoaders = {
		html: precompileLoaderPath + '!' + htmlLoaderPath,
		css: 'style-loader!css-loader',
		js: 'babel-loader?babelrc=false&presets[]=' + require.resolve( 'babel-preset-es2015' ) + '&plugins[]=' + require.resolve( 'babel-plugin-transform-runtime' ) + '&comments=false'
	};
	var defaultLang = {
		template: 'html',
		style: 'css',
		script: 'js'
	};
	var rewriters = {
		style: require.resolve( './style-rewriter' ),
		template: require.resolve( './template-rewriter.js' )
	};

	if( this.sourceMap && !this.minimize ) {
		defaultLoaders.css = 'style-loader!css-loader?sourceMap';
	}

	var loaders = assign( {}, defaultLoaders, options.loaders );

	function getSelectorString( type, index ) {
		return selectorPath +
			'?type=' + type +
			'&index=' + index + '!';
	}

	function ensureBang( loader ) {
		if (loader.charAt( loader.length - 1 ) !== '!') {
			return loader + '!'
		} else {
			return loader
		}
	}

	function getRewriter( type, scoped ) {
		var meta = '?id=' + moduleId;
		switch( type ) {
			case 'template':
				return rewriters.template + ( scoped ? meta + '&scoped=true!' : '!');
			case 'style':
				return rewriters.style + ( scoped ? meta + '&scoped=true!' : '!');
			default:
				return '';
		}
	}

	function getLoaderString( type, part, scoped ) {
		var lang = part.lang || defaultLang[ type ]
		var loader = loaders[lang]
		var rewriter = getRewriter( type, scoped )
		if (loader !== undefined) {
			if( type === 'style' && rewriterInjectRE.test( loader ) ) {
				// ensure rewriter is executed before css-loader
				loader = loader.replace(rewriterInjectRE, function (m, $1) {
					return ensureBang($1) + rewriter
				})
			} else if( type === 'template' ) {
				// can not change loaders for template
				loader = rewriter + ensureBang( defaultLoaders.html )
			} else {
				loader = ensureBang( loader ) + rewriter
			}

			return ensureBang( loader )
		} else {
			// unknown lang, infer the loader to be used
			switch (type) {
				case 'template':
					return rewriter + defaultLoaders.html + '!'
				case 'style':
					return defaultLoaders.css + '!' + rewriter + lang + '!'
				case 'script':
					return lang + '!'
			}
		}
	}

	function getRequireString( type, part, index, scoped ) {
		return loaderUtils.stringifyRequest( loaderContext,
			// disable all configuration loaders
			'!!' +
			// get loader string for pre-processors
			getLoaderString( type, part, scoped ) +
			// select the corresponding part from the rgl file
			getSelectorString( type, index || 0 ) +
			// the url to the actual rgl file
			filePath
		)
	}

	function getRequire( type, part, index, scoped ) {
		return 'require(' +
			getRequireString( type, part, index, scoped ) +
			')\n'
	}

	var parts = parse( content, fileName, this.sourceMap );

	var output = 'var __regular_script__, __regular_template__;\n';

	var hasScopedStyle = false;

	// require style
	parts.style.forEach(function( style, i ) {
		if( style.scoped ) {
			hasScopedStyle = true;
		}
		output += getRequire( 'style', style, i, style.scoped );
	});

	// require script
	var script;
	if (parts.script.length) {
		script = parts.script[0];
		output += '__regular_script__ = ' + getRequire( 'script', script, 0 );
	}

	// require template
	var template;
	if (parts.template.length) {
		template = parts.template[0]
		output += '__regular_template__ = ' + getRequire( 'template', template, 0, hasScopedStyle )
	}

	output += 'var __rs__ = __regular_script__ || {};\n' +
		'if (__rs__.__esModule) __rs__ = __rs__.default;\n' +
		'__rs__.template = __regular_template__;\n' +
		'module.exports = __rs__;';

	// done
	return output;
}
