function ESM_releasePlugin()

in build/release.js [191:255]


function ESM_releasePlugin(plugin, destinationPath) {
	const pluginPath = path.join(plugin.rootPath, plugin.paths['esm']);

	const files = readFiles(`${pluginPath}/**/*`, { base: pluginPath });

	for (const file of files) {
		if (!/(\.js$)|(\.ts$)/.test(file.path)) {
			continue;
		}

		let contents = file.contents.toString();

		const info = ts.preProcessFile(contents);
		for (let i = info.importedFiles.length - 1; i >= 0; i--) {
			let importText = info.importedFiles[i].fileName;
			const pos = info.importedFiles[i].pos;
			const end = info.importedFiles[i].end;

			if (!/(^\.\/)|(^\.\.\/)/.test(importText)) {
				// non-relative import
				if (!/^monaco-editor-core/.test(importText)) {
					console.error(`Non-relative import for unknown module: ${importText} in ${file.path}`);
					process.exit(1);
				}

				if (importText === 'monaco-editor-core') {
					importText = 'monaco-editor-core/esm/vs/editor/editor.api';
				}

				const myFileDestPath = path.join(plugin.modulePrefix, file.path);
				const importFilePath = importText.substring('monaco-editor-core/esm/'.length);
				let relativePath = path
					.relative(path.dirname(myFileDestPath), importFilePath)
					.replace(/\\/g, '/');
				if (!/(^\.\/)|(^\.\.\/)/.test(relativePath)) {
					relativePath = './' + relativePath;
				}

				contents = contents.substring(0, pos + 1) + relativePath + contents.substring(end + 1);
			}
		}

		file.contents = Buffer.from(contents);
	}

	for (const file of files) {
		if (!/monaco\.contribution\.js$/.test(file.path)) {
			continue;
		}

		const myFileDestPath = path.join(plugin.modulePrefix, file.path);
		const apiFilePath = 'vs/editor/editor.api';
		let relativePath = path.relative(path.dirname(myFileDestPath), apiFilePath).replace(/\\/g, '/');
		if (!/(^\.\/)|(^\.\.\/)/.test(relativePath)) {
			relativePath = './' + relativePath;
		}

		let contents = file.contents.toString();
		contents = `import '${relativePath}';\n` + contents;
		file.contents = Buffer.from(contents);
	}

	ESM_addImportSuffix(files);
	writeFiles(files, path.join(destinationPath, plugin.modulePrefix));
}