
let del = require('del');
let gulp = require('gulp');
let babel = require('gulp-babel');
let plumber = require('gulp-plumber');
let sourcemaps = require('gulp-sourcemaps');
let tap = require('gulp-tap');

gulp.task('compile', () => {
	return (
		gulp
			.src([
				__dirname + '/main.js',
				__dirname + '/namespace.js',
				__dirname + '/Bootstrap.js',
				__dirname + '/ObjectContainer.js',
				__dirname + '/!(node_modules|polyfill)/**/!(*Spec).js'
			])
			.pipe(plumber())
			.pipe(sourcemaps.init())
			.pipe(babel({
				moduleIds: true,
				presets: ['es2015'],
				plugins: ['external-helpers-2']
			}))
			.pipe(plumber.stop())
			.pipe(tap((file) => {
				let moduleName = 'ima' + file.path.slice(__dirname.length, -3);
				let fileContents = file.contents.toString();

				let dependencyMatcher = /require\(['"]([^'"]+)['"]\)/g;
				let dependencies = [];
				let match = dependencyMatcher.exec(fileContents);
				while (match) {
					dependencies.push(match[1]);
					match = dependencyMatcher.exec(fileContents);
				}

				let exportMatcher = /\nexports\.([a-zA-Z_][a-zA-Z_1-9]*)\s*=\s*([^;]+);/g;
				let moduleExports = [];
				match = exportMatcher.exec(fileContents);
				while (match) {
					moduleExports.push({ symbol: match[1], value: match[2] });
					match = exportMatcher.exec(fileContents);
				}

				// Note: there is no point in specifying the dependencies in
				// the the IMA loader's register() method, as these will
				// already be resolved by browserify's require() function used
				// in the code before with the IMA loader.
				file.contents = new Buffer(
					fileContents + '\n\n' +
					`$IMA.Loader.register('${moduleName}', [], function (_export, _context) {\n` +
					`	'use strict';\n` +
					`	return {\n` +
					`		setters: [],\n` +
					`		execute: function () {\n` +
					moduleExports.map(({ symbol, value }) => `			_export('${symbol}', exports.${symbol});\n`).join('') +
					`		}\n` +
					`	};\n` +
					`});\n`
				);
			}))
			.pipe(sourcemaps.write())
			.pipe(gulp.dest(__dirname + '/dist'))
	);
});

gulp.task('copy-metadata', () => {
	return gulp
		.src([
			__dirname + '/LICENSE',
			__dirname + '/package.json',
			__dirname + '/README.md'
		])
		.pipe(gulp.dest(__dirname + '/dist'));
});

gulp.task('clean', () => {
	return del([
		__dirname + '/dist'
	]);
});

@@ -384,24 +384,60 @@
 tainer.js',%0A
+%09%09%09%09__dirname + '/vendorLinker.js',%0A
 %09%09%09%09__dirnam
@@ -2148,16 +2148,44 @@
 %09.src(%5B%0A
+%09%09%09__dirname + '/build.js',%0A
 %09%09%09__dir
@@ -2262,16 +2262,43 @@
 ADME.md'
+,%0A%09%09%09__dirname + '/test.js'
 %0A%09%09%5D)%0A%09%09

build process fixes