瀏覽代碼

Ship ESM variant

Alex Dima 7 年之前
父節點
當前提交
0ab1a24b8d
共有 4 個文件被更改,包括 278 次插入80 次删除
  1. 206 27
      gulpfile.js
  2. 60 49
      metadata.js
  3. 9 3
      package-lock.json
  4. 3 1
      package.json

+ 206 - 27
gulpfile.js

@@ -1,21 +1,23 @@
 
-var gulp = require('gulp');
-var metadata = require('./metadata');
-var es = require('event-stream');
-var path = require('path');
-var fs = require('fs');
-var rimraf = require('rimraf');
-var cp = require('child_process');
-var httpServer = require('http-server');
-var typedoc = require("gulp-typedoc");
-var CleanCSS = require('clean-css');
-var uncss = require('uncss');
-
-var WEBSITE_GENERATED_PATH = path.join(__dirname, 'website/playground/new-samples');
-var MONACO_EDITOR_VERSION = (function() {
-	var packageJsonPath = path.join(__dirname, 'package.json');
-	var packageJson = JSON.parse(fs.readFileSync(packageJsonPath).toString());
-	var version = packageJson.version;
+const gulp = require('gulp');
+const metadata = require('./metadata');
+const es = require('event-stream');
+const path = require('path');
+const fs = require('fs');
+const rimraf = require('rimraf');
+const cp = require('child_process');
+const httpServer = require('http-server');
+const typedoc = require("gulp-typedoc");
+const CleanCSS = require('clean-css');
+const uncss = require('uncss');
+const File = require('vinyl');
+const ts = require('typescript');
+
+const WEBSITE_GENERATED_PATH = path.join(__dirname, 'website/playground/new-samples');
+const MONACO_EDITOR_VERSION = (function() {
+	const packageJsonPath = path.join(__dirname, 'package.json');
+	const packageJson = JSON.parse(fs.readFileSync(packageJsonPath).toString());
+	const version = packageJson.version;
 	if (!/\d+\.\d+\.\d+/.test(version)) {
 		console.log('unrecognized package.json version: ' + version);
 		process.exit(0);
@@ -33,8 +35,8 @@ gulp.task('release', ['clean-release'], function() {
 		// min folder
 		releaseOne('min'),
 
-		// // esm folder
-		// releaseESM(),
+		// esm folder
+		ESM_release(),
 
 		// package.json
 		gulp.src('package.json')
@@ -65,6 +67,9 @@ gulp.task('release', ['clean-release'], function() {
 	)
 });
 
+/**
+ * Release to `dev` or `min`.
+ */
 function releaseOne(type) {
 	return es.merge(
 		gulp.src('node_modules/monaco-editor-core/' + type + '/**/*')
@@ -74,12 +79,9 @@ function releaseOne(type) {
 	)
 }
 
-// function releaseESM() {
-// 	// return es.merge(
-// 	// 	gulp.src('node_modules/monaco-editor-core/esm/**/*')
-// 	// )
-// }
-
+/**
+ * Release plugins to `dev` or `min`.
+ */
 function pluginStreams(type, destinationPath) {
 	return es.merge(
 		metadata.METADATA.PLUGINS.map(function(plugin) {
@@ -88,14 +90,16 @@ function pluginStreams(type, destinationPath) {
 	);
 }
 
+/**
+ * Release a plugin to `dev` or `min`.
+ */
 function pluginStream(plugin, type, destinationPath) {
 	var pluginPath = plugin.paths[`npm/${type}`]; // npm/dev or npm/min
 	var contribPath = path.join(pluginPath, plugin.contrib.substr(plugin.modulePrefix.length)) + '.js';
 	return (
 		gulp.src([
 			pluginPath + '/**/*',
-			'!' + contribPath,
-			'!' + pluginPath + '/**/monaco.d.ts'
+			'!' + contribPath
 		])
 		.pipe(gulp.dest(destinationPath + plugin.modulePrefix))
 	);
@@ -178,6 +182,181 @@ function addPluginContribs(type) {
 	});
 }
 
+function ESM_release() {
+	return es.merge(
+		gulp.src('node_modules/monaco-editor-core/esm/**/*')
+			.pipe(ESM_addImportSuffix())
+			.pipe(ESM_addPluginContribs('release/esm'))
+			.pipe(gulp.dest('release/esm')),
+		ESM_pluginStreams('release/esm/')
+	)
+}
+
+/**
+ * Release plugins to `esm`.
+ */
+function ESM_pluginStreams(destinationPath) {
+	return es.merge(
+		metadata.METADATA.PLUGINS.map(function(plugin) {
+			return ESM_pluginStream(plugin, destinationPath);
+		})
+	);
+}
+
+/**
+ * Release a plugin to `esm`.
+ * Adds a dependency to 'vs/editor/editor.api' in contrib files in order for `monaco` to be defined.
+ * Rewrites imports for 'monaco-editor-core/**'
+ */
+function ESM_pluginStream(plugin, destinationPath) {
+	const DESTINATION = path.join(__dirname, destinationPath);
+	let pluginPath = plugin.paths[`esm`];
+	return (
+		gulp.src([
+			pluginPath + '/**/*'
+		])
+		.pipe(es.through(function(data) {
+			if (!/\.js$/.test(data.path)) {
+				this.emit('data', data);
+				return;
+			}
+
+			let contents = data.contents.toString();
+
+			const info = ts.preProcessFile(contents);
+			for (let i = info.importedFiles.length - 1; i >= 0; i--) {
+				const 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}`);
+						process.exit(0);
+					}
+
+					const myFileDestPath = path.join(DESTINATION, plugin.modulePrefix, data.relative);
+					const importFilePath = path.join(DESTINATION, importText.substr('monaco-editor-core/esm/'.length));
+					let relativePath = path.relative(path.dirname(myFileDestPath), importFilePath);
+					if (!/(^\.\/)|(^\.\.\/)/.test(relativePath)) {
+						relativePath = './' + relativePath;
+					}
+
+					contents = (
+						contents.substring(0, pos + 1)
+						+ relativePath
+						+ contents.substring(end + 1)
+					);
+				}
+			}
+
+			data.contents = new Buffer(contents);
+			this.emit('data', data);
+		}))
+		.pipe(es.through(function(data) {
+			if (!/monaco\.contribution\.js$/.test(data.path)) {
+				this.emit('data', data);
+				return;
+			}
+
+			const myFileDestPath = path.join(DESTINATION, plugin.modulePrefix, data.relative);
+			const apiFilePath = path.join(DESTINATION, 'vs/editor/editor.api');
+			let relativePath = path.relative(path.dirname(myFileDestPath), apiFilePath);
+			if (!/(^\.\/)|(^\.\.\/)/.test(relativePath)) {
+				relativePath = './' + relativePath;
+			}
+
+			let contents = data.contents.toString();
+			contents = (
+				`import '${relativePath}';\n` +
+				contents
+			);
+
+			data.contents = new Buffer(contents);
+
+			this.emit('data', data);
+		}))
+		.pipe(ESM_addImportSuffix())
+		.pipe(gulp.dest(destinationPath + plugin.modulePrefix))
+	);
+}
+
+function ESM_addImportSuffix() {
+	return es.through(function(data) {
+		if (!/\.js$/.test(data.path)) {
+			this.emit('data', data);
+			return;
+		}
+
+		let contents = data.contents.toString();
+
+		const info = ts.preProcessFile(contents);
+		for (let i = info.importedFiles.length - 1; i >= 0; i--) {
+			const importText = info.importedFiles[i].fileName;
+			const pos = info.importedFiles[i].pos;
+			const end = info.importedFiles[i].end;
+
+			if (/\.css$/.test(importText)) {
+				continue;
+			}
+
+			contents = (
+				contents.substring(0, pos + 1)
+				+ importText + '.js'
+				+ contents.substring(end + 1)
+			);
+		}
+
+		data.contents = new Buffer(contents);
+		this.emit('data', data);
+	});
+}
+
+/**
+ * - Rename esm/vs/editor/editor.main.js to esm/vs/editor/edcore.main.js
+ * - Create esm/vs/editor/editor.main.js that that stiches things together
+ */
+function ESM_addPluginContribs(dest) {
+	const DESTINATION = path.join(__dirname, dest);
+	return es.through(function(data) {
+		if (!/editor\.main\.js$/.test(data.path)) {
+			this.emit('data', data);
+			return;
+		}
+
+		this.emit('data', new File({
+			path: data.path.replace(/editor\.main/, 'edcore.main'),
+			base: data.base,
+			contents: data.contents
+		}));
+
+		const mainFileDestPath = path.join(DESTINATION, 'vs/editor/editor.main.js');
+		let mainFileImports = [];
+		metadata.METADATA.PLUGINS.forEach(function(plugin) {
+			const contribDestPath = path.join(DESTINATION, plugin.contrib);
+
+			let relativePath = path.relative(path.dirname(mainFileDestPath), contribDestPath);
+			if (!/(^\.\/)|(^\.\.\/)/.test(relativePath)) {
+				relativePath = './' + relativePath;
+			}
+
+			mainFileImports.push(relativePath);
+		});
+
+		let mainFileContents = (
+			mainFileImports.map((name) => `import '${name}';`).join('\n')
+			+ `\n\nexport * from './edcore.main';`
+		);
+
+		this.emit('data', new File({
+			path: data.path,
+			base: data.base,
+			contents: new Buffer(mainFileContents)
+		}));
+	});
+}
+
 /**
  * Edit monaco.d.ts:
  * - append monaco.d.ts from plugins

+ 60 - 49
metadata.js

@@ -1,4 +1,4 @@
-(function() {
+(function () {
 
 	var METADATA = {
 		CORE: {
@@ -11,55 +11,66 @@
 				releaseMin: 'release/min/vs',
 			}
 		},
-		PLUGINS: [{
-			name: 'monaco-typescript',
-			contrib: 'vs/language/typescript/monaco.contribution',
-			modulePrefix: 'vs/language/typescript',
-			thirdPartyNotices: 'node_modules/monaco-typescript/ThirdPartyNotices.txt',
-			paths: {
-				src: '/monaco-typescript/release/dev',
-				'npm/dev': 'node_modules/monaco-typescript/release/dev',
-				'npm/min': 'node_modules/monaco-typescript/release/min',
-			}
-		},{
-			name: 'monaco-css',
-			contrib: 'vs/language/css/monaco.contribution',
-			modulePrefix: 'vs/language/css',
-			paths: {
-				src: '/monaco-css/release/dev',
-				'npm/dev': 'node_modules/monaco-css/release/dev',
-				'npm/min': 'node_modules/monaco-css/release/min',
-			}
-		},{
-			name: 'monaco-json',
-			contrib: 'vs/language/json/monaco.contribution',
-			modulePrefix: 'vs/language/json',
-			paths: {
-				src: '/monaco-json/release/dev',
-				'npm/dev': 'node_modules/monaco-json/release/dev',
-				'npm/min': 'node_modules/monaco-json/release/min',
-			}
-		},{
-			name: 'monaco-html',
-			contrib: 'vs/language/html/monaco.contribution',
-			modulePrefix: 'vs/language/html',
-			thirdPartyNotices: 'node_modules/monaco-html/ThirdPartyNotices.txt',
-			paths: {
-				src: '/monaco-html/release/dev',
-				'npm/dev': 'node_modules/monaco-html/release/dev',
-				'npm/min': 'node_modules/monaco-html/release/min',
-			}
-		},{
-			name: 'monaco-languages',
-			contrib: 'vs/basic-languages/monaco.contribution',
-			modulePrefix: 'vs/basic-languages',
-			thirdPartyNotices: 'node_modules/monaco-languages/ThirdPartyNotices.txt',
-			paths: {
-				src: '/monaco-languages/release/dev',
-				'npm/dev': 'node_modules/monaco-languages/release/dev',
-				'npm/min': 'node_modules/monaco-languages/release/min',
+		PLUGINS: [
+			{
+				name: 'monaco-typescript',
+				contrib: 'vs/language/typescript/monaco.contribution',
+				modulePrefix: 'vs/language/typescript',
+				thirdPartyNotices: 'node_modules/monaco-typescript/ThirdPartyNotices.txt',
+				paths: {
+					src: '/monaco-typescript/release/dev',
+					'npm/dev': 'node_modules/monaco-typescript/release/dev',
+					'npm/min': 'node_modules/monaco-typescript/release/min',
+					esm: 'node_modules/monaco-typescript/release/esm',
+				}
+			},
+			{
+				name: 'monaco-css',
+				contrib: 'vs/language/css/monaco.contribution',
+				modulePrefix: 'vs/language/css',
+				paths: {
+					src: '/monaco-css/release/dev',
+					'npm/dev': 'node_modules/monaco-css/release/dev',
+					'npm/min': 'node_modules/monaco-css/release/min',
+					esm: 'node_modules/monaco-css/release/esm',
+				}
+			},
+			{
+				name: 'monaco-json',
+				contrib: 'vs/language/json/monaco.contribution',
+				modulePrefix: 'vs/language/json',
+				paths: {
+					src: '/monaco-json/release/dev',
+					'npm/dev': 'node_modules/monaco-json/release/dev',
+					'npm/min': 'node_modules/monaco-json/release/min',
+					esm: 'node_modules/monaco-json/release/esm',
+				}
+			},
+			{
+				name: 'monaco-html',
+				contrib: 'vs/language/html/monaco.contribution',
+				modulePrefix: 'vs/language/html',
+				thirdPartyNotices: 'node_modules/monaco-html/ThirdPartyNotices.txt',
+				paths: {
+					src: '/monaco-html/release/dev',
+					'npm/dev': 'node_modules/monaco-html/release/dev',
+					'npm/min': 'node_modules/monaco-html/release/min',
+					esm: 'node_modules/monaco-html/release/esm',
+				}
+			},
+			{
+				name: 'monaco-languages',
+				contrib: 'vs/basic-languages/monaco.contribution',
+				modulePrefix: 'vs/basic-languages',
+				thirdPartyNotices: 'node_modules/monaco-languages/ThirdPartyNotices.txt',
+				paths: {
+					src: '/monaco-languages/release/dev',
+					'npm/dev': 'node_modules/monaco-languages/release/dev',
+					'npm/min': 'node_modules/monaco-languages/release/min',
+					esm: 'node_modules/monaco-languages/release/esm',
+				}
 			}
-		}]
+		]
 	}
 
 	if (typeof exports !== 'undefined') {

+ 9 - 3
package-lock.json

@@ -3891,6 +3891,12 @@
           "requires": {
             "brace-expansion": "1.1.11"
           }
+        },
+        "typescript": {
+          "version": "2.4.1",
+          "resolved": "https://registry.npmjs.org/typescript/-/typescript-2.4.1.tgz",
+          "integrity": "sha1-w8yxbdqgsjFN4DHn5v7onlujRrw=",
+          "dev": true
         }
       }
     },
@@ -3901,9 +3907,9 @@
       "dev": true
     },
     "typescript": {
-      "version": "2.4.1",
-      "resolved": "https://registry.npmjs.org/typescript/-/typescript-2.4.1.tgz",
-      "integrity": "sha1-w8yxbdqgsjFN4DHn5v7onlujRrw=",
+      "version": "2.7.2",
+      "resolved": "https://registry.npmjs.org/typescript/-/typescript-2.7.2.tgz",
+      "integrity": "sha512-p5TCYZDAO0m4G344hD+wx/LATebLWZNkkh2asWUFqSsD2OrDNhbAHuSjobrmsUmdzjJjEeZVU9g1h3O6vpstnw==",
       "dev": true
     },
     "uglify-js": {

+ 3 - 1
package.json

@@ -29,6 +29,8 @@
     "monaco-typescript": "3.0.0",
     "rimraf": "^2.5.2",
     "typedoc": "^0.8.0",
-    "uncss": "^0.14.1"
+    "typescript": "^2.7.2",
+    "uncss": "^0.14.1",
+    "vinyl": "^0.5.3"
   }
 }