(function ()()

in build/importTypescript.js [21:161]


(function () {
	try {
		fs.statSync(TYPESCRIPT_LIB_DESTINATION);
	} catch (err) {
		fs.mkdirSync(TYPESCRIPT_LIB_DESTINATION);
	}
	importLibs();

	const npmLsOutput = JSON.parse(
		child_process.execSync('npm ls typescript --depth=0 --json=true', { cwd: REPO_ROOT }).toString()
	);
	const typeScriptDependencyVersion = npmLsOutput.dependencies.typescript.version;

	fs.writeFileSync(
		path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServicesMetadata.ts'),
		`${generatedNote}
export const typescriptVersion = "${typeScriptDependencyVersion}";\n`
	);

	let tsServices = fs
		.readFileSync(path.join(TYPESCRIPT_LIB_SOURCE, 'typescriptServices.js'))
		.toString();

	// Ensure we never run into the node system...
	// (this also removes require calls that trick webpack into shimming those modules...)
	tsServices = tsServices.replace(
		/\n    ts\.sys =([^]*)\n    \}\)\(\);/m,
		`\n    // MONACOCHANGE\n    ts.sys = undefined;\n    // END MONACOCHANGE`
	);

	// Eliminate more require() calls...
	tsServices = tsServices.replace(
		/^( +)etwModule = require\(.*$/m,
		'$1// MONACOCHANGE\n$1etwModule = undefined;\n$1// END MONACOCHANGE'
	);
	tsServices = tsServices.replace(
		/^( +)var result = ts\.sys\.require\(.*$/m,
		'$1// MONACOCHANGE\n$1var result = undefined;\n$1// END MONACOCHANGE'
	);
	tsServices = tsServices.replace(
		/^( +)fs = require\("fs"\);$/m,
		'$1// MONACOCHANGE\n$1fs = undefined;\n$1// END MONACOCHANGE'
	);
	tsServices = tsServices.replace(
		/^( +)debugger;$/m,
		'$1// MONACOCHANGE\n$1// debugger;\n$1// END MONACOCHANGE'
	);
	tsServices = tsServices.replace(
		/= require\("perf_hooks"\)/m,
		'/* MONACOCHANGE */= {}/* END MONACOCHANGE */'
	);
	tsServices = tsServices.replace(
		/typeof require === "function"/m,
		'/* MONACOCHANGE */false/* END MONACOCHANGE */'
	);

	// Flag any new require calls (outside comments) so they can be corrected preemptively.
	// To avoid missing cases (or using an even more complex regex), temporarily remove comments
	// about require() and then check for lines actually calling require().
	// \/[*/] matches the start of a comment (single or multi-line).
	// ^\s+\*[^/] matches (presumably) a later line of a multi-line comment.
	const tsServicesNoCommentedRequire = tsServices.replace(
		/(\/[*/]|^\s+\*[^/]).*\brequire\(.*/gm,
		''
	);
	const linesWithRequire = tsServicesNoCommentedRequire.match(/^.*?\brequire\(.*$/gm);

	// Allow error messages to include references to require() in their strings
	const runtimeRequires =
		linesWithRequire &&
		linesWithRequire.filter((l) => !l.includes(': diag(') && !l.includes('ts.DiagnosticCategory'));

	if (runtimeRequires && runtimeRequires.length && linesWithRequire) {
		console.error(
			'Found new require() calls on the following lines. These should be removed to avoid breaking webpack builds.\n'
		);
		console.error(
			runtimeRequires.map((r) => `${r} (${tsServicesNoCommentedRequire.indexOf(r)})`).join('\n')
		);
		process.exit(1);
	}

	const tsServices_amd =
		generatedNote +
		tsServices +
		`
// MONACOCHANGE
// Defining the entire module name because r.js has an issue and cannot bundle this file
// correctly with an anonymous define call
define("vs/language/typescript/lib/typescriptServices", [], function() { return ts; });
// END MONACOCHANGE
`;
	fs.writeFileSync(
		path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices-amd.js'),
		stripSourceMaps(tsServices_amd)
	);

	// Remove pattern that creates warnings with esbuild
	// e.g.
	// > /src/typescript/lib/typescriptServices.js:20:21: warning: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
	// 20 │ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
	//    ╵                      ~~~~
	//

	tsServices = tsServices.replace(/\nvar ([^ ]+) = \(this && this\.([^)]+)\) \|\|/gm, '\nvar $1 =');

	const tsServices_esm =
		generatedNote +
		tsServices +
		`
// MONACOCHANGE
export var createClassifier = ts.createClassifier;
export var createLanguageService = ts.createLanguageService;
export var displayPartsToString = ts.displayPartsToString;
export var EndOfLineState = ts.EndOfLineState;
export var flattenDiagnosticMessageText = ts.flattenDiagnosticMessageText;
export var IndentStyle = ts.IndentStyle;
export var ScriptKind = ts.ScriptKind;
export var ScriptTarget = ts.ScriptTarget;
export var TokenClass = ts.TokenClass;
export var typescript = ts;
// END MONACOCHANGE
`;
	fs.writeFileSync(
		path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices.js'),
		stripSourceMaps(tsServices_esm)
	);

	let dtsServices = fs
		.readFileSync(path.join(TYPESCRIPT_LIB_SOURCE, 'typescriptServices.d.ts'))
		.toString();
	dtsServices += `
// MONACOCHANGE
export = ts;
// END MONACOCHANGE
`;
	fs.writeFileSync(
		path.join(TYPESCRIPT_LIB_DESTINATION, 'typescriptServices.d.ts'),
		generatedNote + dtsServices
	);
})();