'use strict';

var fs = require('fs');
var path = require('path');
var index_ts = require('../compiler/svelte.js');

var compileOptions = {
	extensions: ['.html']
};

function capitalise(name) {
	return name[0].toUpperCase() + name.slice(1);
}

function register(options) {
	if (options.extensions) {
		compileOptions.extensions.forEach(deregisterExtension);
		options.extensions.forEach(registerExtension);
	}

	compileOptions = options;
}

function deregisterExtension(extension) {
	delete require.extensions[extension];
}

function registerExtension(extension) {
	require.extensions[extension] = function(module, filename) {
		var name = path.basename(filename)
			.slice(0, -path.extname(filename).length)
			.replace(/^\d/, '_$&')
			.replace(/[^a-zA-Z0-9_$]/g, '');

		var options = Object.assign({}, compileOptions, {
			filename,
			name: capitalise(name),
			generate: 'ssr'
		});

		var ref = index_ts.compile(fs.readFileSync(filename, 'utf-8'), options);
		var js = ref.js;

		return module._compile(js.code, filename);
	};
}

registerExtension('.html');

module.exports = register;
//# sourceMappingURL=register.js.map
