gulpfile.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. const gulp = require('gulp');
  2. /**
  3. * @typedef { { src:string; 'npm/dev':string; 'npm/min':string; built:string; releaseDev:string; releaseMin:string; } } ICorePaths
  4. * @typedef { { src:string; dev:string; min:string; esm: string; } } IPluginPaths
  5. * @typedef { { name:string; contrib:string; modulePrefix:string; rootPath:string; paths:IPluginPaths } } IPlugin
  6. * @typedef { { METADATA: {CORE:{paths:ICorePaths}; PLUGINS:IPlugin[];} } } IMetadata
  7. * @type { IMetadata }
  8. */
  9. const metadata = require('./monaco-editor/metadata');
  10. const es = require('event-stream');
  11. const path = require('path');
  12. const fs = require('fs');
  13. const rimraf = require('rimraf');
  14. const cp = require('child_process');
  15. const yaserver = require('yaserver');
  16. const http = require('http');
  17. const CleanCSS = require('clean-css');
  18. const uncss = require('uncss');
  19. const File = require('vinyl');
  20. const ts = require('typescript');
  21. const WEBSITE_GENERATED_PATH = path.join(__dirname, 'monaco-editor/website/playground/new-samples');
  22. /** @type {string} */
  23. const MONACO_EDITOR_VERSION = (function () {
  24. const packageJsonPath = path.join(__dirname, 'package.json');
  25. const packageJson = JSON.parse(fs.readFileSync(packageJsonPath).toString());
  26. const version = packageJson.version;
  27. if (!/\d+\.\d+\.\d+/.test(version)) {
  28. console.log('unrecognized package.json version: ' + version);
  29. process.exit(0);
  30. }
  31. return version;
  32. })();
  33. async function _execute(task) {
  34. // Always invoke as if it were a callback task
  35. return new Promise((resolve, reject) => {
  36. if (task.length === 1) {
  37. // this is a calback task
  38. task((err) => {
  39. if (err) {
  40. return reject(err);
  41. }
  42. resolve();
  43. });
  44. return;
  45. }
  46. const taskResult = task();
  47. if (typeof taskResult === 'undefined') {
  48. // this is a sync task
  49. resolve();
  50. return;
  51. }
  52. if (typeof taskResult.then === 'function') {
  53. // this is a promise returning task
  54. taskResult.then(resolve, reject);
  55. return;
  56. }
  57. // this is a stream returning task
  58. taskResult.on('end', (_) => resolve());
  59. taskResult.on('error', (err) => reject(err));
  60. });
  61. }
  62. function taskSeries(...tasks) {
  63. return async () => {
  64. for (let i = 0; i < tasks.length; i++) {
  65. await _execute(tasks[i]);
  66. }
  67. };
  68. }
  69. const cleanReleaseTask = function (cb) {
  70. rimraf('release', { maxBusyTries: 1 }, cb);
  71. };
  72. gulp.task(
  73. 'release',
  74. taskSeries(cleanReleaseTask, function () {
  75. return es.merge(
  76. // dev folder
  77. releaseOne('dev'),
  78. // min folder
  79. releaseOne('min'),
  80. // esm folder
  81. ESM_release(),
  82. // package.json
  83. gulp
  84. .src('package.json')
  85. .pipe(
  86. es.through(function (data) {
  87. var json = JSON.parse(data.contents.toString());
  88. json.private = false;
  89. data.contents = Buffer.from(JSON.stringify(json, null, ' '));
  90. delete json.scripts['postinstall'];
  91. this.emit('data', data);
  92. })
  93. )
  94. .pipe(gulp.dest('release')),
  95. gulp.src('CHANGELOG.md').pipe(gulp.dest('release')),
  96. // min-maps folder
  97. gulp.src('node_modules/monaco-editor-core/min-maps/**/*').pipe(gulp.dest('release/min-maps')),
  98. // other files
  99. gulp
  100. .src([
  101. 'node_modules/monaco-editor-core/LICENSE',
  102. 'node_modules/monaco-editor-core/monaco.d.ts',
  103. 'node_modules/monaco-editor-core/ThirdPartyNotices.txt',
  104. 'README.md'
  105. ])
  106. .pipe(addPluginDTS())
  107. .pipe(addPluginThirdPartyNotices())
  108. .pipe(gulp.dest('release'))
  109. );
  110. })
  111. );
  112. /**
  113. * Release to `dev` or `min`.
  114. * @param {'dev'|'min'} type
  115. * @returns {NodeJS.ReadWriteStream}
  116. */
  117. function releaseOne(type) {
  118. return es.merge(
  119. gulp
  120. .src('node_modules/monaco-editor-core/' + type + '/**/*')
  121. .pipe(addPluginContribs(type))
  122. .pipe(gulp.dest('release/' + type)),
  123. pluginStreams(type, 'release/' + type + '/')
  124. );
  125. }
  126. /**
  127. * Release plugins to `dev` or `min`.
  128. * @param {'dev'|'min'} type
  129. * @param {string} destinationPath
  130. * @returns {NodeJS.ReadWriteStream}
  131. */
  132. function pluginStreams(type, destinationPath) {
  133. return es.merge(
  134. metadata.METADATA.PLUGINS.map(function (plugin) {
  135. return pluginStream(plugin, type, destinationPath);
  136. })
  137. );
  138. }
  139. /**
  140. * Release a plugin to `dev` or `min`.
  141. * @param {IPlugin} plugin
  142. * @param {'dev'|'min'} type
  143. * @param {string} destinationPath
  144. * @returns {NodeJS.ReadWriteStream}
  145. */
  146. function pluginStream(plugin, type, destinationPath) {
  147. const pluginPath = path.join(plugin.rootPath, plugin.paths[type]); // dev or min
  148. const contribPath =
  149. path.join(pluginPath, plugin.contrib.substr(plugin.modulePrefix.length)) + '.js';
  150. return gulp
  151. .src([pluginPath + '/**/*', '!' + contribPath])
  152. .pipe(
  153. es.through(
  154. /**
  155. * @param {File} data
  156. */
  157. function (data) {
  158. if (!/_\.contribution/.test(data.path)) {
  159. this.emit('data', data);
  160. return;
  161. }
  162. let contents = data.contents.toString();
  163. contents = contents.replace(
  164. 'define(["require", "exports"],',
  165. 'define(["require", "exports", "vs/editor/editor.api"],'
  166. );
  167. data.contents = Buffer.from(contents);
  168. this.emit('data', data);
  169. }
  170. )
  171. )
  172. .pipe(gulp.dest(destinationPath + plugin.modulePrefix));
  173. }
  174. /**
  175. * Edit editor.main.js:
  176. * - rename the AMD module 'vs/editor/editor.main' to 'vs/editor/edcore.main'
  177. * - append monaco.contribution modules from plugins
  178. * - append new AMD module 'vs/editor/editor.main' that stiches things together
  179. *
  180. * @param {'dev'|'min'} type
  181. * @returns {NodeJS.ReadWriteStream}
  182. */
  183. function addPluginContribs(type) {
  184. return es.through(
  185. /**
  186. * @param {File} data
  187. */
  188. function (data) {
  189. if (!/editor\.main\.js$/.test(data.path)) {
  190. this.emit('data', data);
  191. return;
  192. }
  193. let contents = data.contents.toString();
  194. // Rename the AMD module 'vs/editor/editor.main' to 'vs/editor/edcore.main'
  195. contents = contents.replace(/"vs\/editor\/editor\.main\"/, '"vs/editor/edcore.main"');
  196. /** @type {string[]} */
  197. let extraContent = [];
  198. /** @type {string[]} */
  199. let allPluginsModuleIds = [];
  200. metadata.METADATA.PLUGINS.forEach(function (plugin) {
  201. allPluginsModuleIds.push(plugin.contrib);
  202. const pluginPath = path.join(plugin.rootPath, plugin.paths[type]); // dev or min
  203. const contribPath =
  204. path.join(__dirname, pluginPath, plugin.contrib.substr(plugin.modulePrefix.length)) +
  205. '.js';
  206. let contribContents = fs.readFileSync(contribPath).toString();
  207. contribContents = contribContents.replace(
  208. /define\((['"][a-z\/\-]+\/fillers\/monaco-editor-core['"]),\[\],/,
  209. "define($1,['vs/editor/editor.api'],"
  210. );
  211. extraContent.push(contribContents);
  212. });
  213. extraContent.push(
  214. `define("vs/editor/editor.main", ["vs/editor/edcore.main","${allPluginsModuleIds.join(
  215. '","'
  216. )}"], function(api) { return api; });`
  217. );
  218. let insertIndex = contents.lastIndexOf('//# sourceMappingURL=');
  219. if (insertIndex === -1) {
  220. insertIndex = contents.length;
  221. }
  222. contents =
  223. contents.substring(0, insertIndex) +
  224. '\n' +
  225. extraContent.join('\n') +
  226. '\n' +
  227. contents.substring(insertIndex);
  228. data.contents = Buffer.from(contents);
  229. this.emit('data', data);
  230. }
  231. );
  232. }
  233. /**
  234. * @returns {NodeJS.ReadWriteStream}
  235. */
  236. function ESM_release() {
  237. return es.merge(
  238. gulp
  239. .src([
  240. 'node_modules/monaco-editor-core/esm/**/*',
  241. // we will create our own editor.api.d.ts which also contains the plugins API
  242. '!node_modules/monaco-editor-core/esm/vs/editor/editor.api.d.ts'
  243. ])
  244. .pipe(ESM_addImportSuffix())
  245. .pipe(ESM_addPluginContribs('release/esm'))
  246. .pipe(gulp.dest('release/esm')),
  247. ESM_pluginStreams('release/esm/')
  248. );
  249. }
  250. /**
  251. * Release plugins to `esm`.
  252. * @param {string} destinationPath
  253. * @returns {NodeJS.ReadWriteStream}
  254. */
  255. function ESM_pluginStreams(destinationPath) {
  256. return es.merge(
  257. metadata.METADATA.PLUGINS.map(function (plugin) {
  258. return ESM_pluginStream(plugin, destinationPath);
  259. })
  260. );
  261. }
  262. /**
  263. * Release a plugin to `esm`.
  264. * Adds a dependency to 'vs/editor/editor.api' in contrib files in order for `monaco` to be defined.
  265. * Rewrites imports for 'monaco-editor-core/**'
  266. * @param {IPlugin} plugin
  267. * @param {string} destinationPath
  268. * @returns {NodeJS.ReadWriteStream}
  269. */
  270. function ESM_pluginStream(plugin, destinationPath) {
  271. const DESTINATION = path.join(__dirname, destinationPath);
  272. const pluginPath = path.join(plugin.rootPath, plugin.paths['esm']);
  273. return gulp
  274. .src([pluginPath + '/**/*'])
  275. .pipe(
  276. es.through(
  277. /**
  278. * @param {File} data
  279. */
  280. function (data) {
  281. if (!/(\.js$)|(\.ts$)/.test(data.path)) {
  282. this.emit('data', data);
  283. return;
  284. }
  285. let contents = data.contents.toString();
  286. const info = ts.preProcessFile(contents);
  287. for (let i = info.importedFiles.length - 1; i >= 0; i--) {
  288. let importText = info.importedFiles[i].fileName;
  289. const pos = info.importedFiles[i].pos;
  290. const end = info.importedFiles[i].end;
  291. if (!/(^\.\/)|(^\.\.\/)/.test(importText)) {
  292. // non-relative import
  293. if (!/^monaco-editor-core/.test(importText)) {
  294. console.error(
  295. `Non-relative import for unknown module: ${importText} in ${data.path}`
  296. );
  297. process.exit(0);
  298. }
  299. if (importText === 'monaco-editor-core') {
  300. importText = 'monaco-editor-core/esm/vs/editor/editor.api';
  301. }
  302. const myFileDestPath = path.join(DESTINATION, plugin.modulePrefix, data.relative);
  303. const importFilePath = path.join(
  304. DESTINATION,
  305. importText.substr('monaco-editor-core/esm/'.length)
  306. );
  307. let relativePath = path
  308. .relative(path.dirname(myFileDestPath), importFilePath)
  309. .replace(/\\/g, '/');
  310. if (!/(^\.\/)|(^\.\.\/)/.test(relativePath)) {
  311. relativePath = './' + relativePath;
  312. }
  313. contents =
  314. contents.substring(0, pos + 1) + relativePath + contents.substring(end + 1);
  315. }
  316. }
  317. data.contents = Buffer.from(contents);
  318. this.emit('data', data);
  319. }
  320. )
  321. )
  322. .pipe(
  323. es.through(
  324. /**
  325. * @param {File} data
  326. */
  327. function (data) {
  328. if (!/monaco\.contribution\.js$/.test(data.path)) {
  329. this.emit('data', data);
  330. return;
  331. }
  332. const myFileDestPath = path.join(DESTINATION, plugin.modulePrefix, data.relative);
  333. const apiFilePath = path.join(DESTINATION, 'vs/editor/editor.api');
  334. let relativePath = path
  335. .relative(path.dirname(myFileDestPath), apiFilePath)
  336. .replace(/\\/g, '/');
  337. if (!/(^\.\/)|(^\.\.\/)/.test(relativePath)) {
  338. relativePath = './' + relativePath;
  339. }
  340. let contents = data.contents.toString();
  341. contents = `import '${relativePath}';\n` + contents;
  342. data.contents = Buffer.from(contents);
  343. this.emit('data', data);
  344. }
  345. )
  346. )
  347. .pipe(ESM_addImportSuffix())
  348. .pipe(gulp.dest(destinationPath + plugin.modulePrefix));
  349. }
  350. /**
  351. * Adds `.js` to all import statements.
  352. * @returns {NodeJS.ReadWriteStream}
  353. */
  354. function ESM_addImportSuffix() {
  355. return es.through(
  356. /**
  357. * @param {File} data
  358. */
  359. function (data) {
  360. if (!/\.js$/.test(data.path)) {
  361. this.emit('data', data);
  362. return;
  363. }
  364. let contents = data.contents.toString();
  365. const info = ts.preProcessFile(contents);
  366. for (let i = info.importedFiles.length - 1; i >= 0; i--) {
  367. const importText = info.importedFiles[i].fileName;
  368. const pos = info.importedFiles[i].pos;
  369. const end = info.importedFiles[i].end;
  370. if (/\.css$/.test(importText)) {
  371. continue;
  372. }
  373. contents =
  374. contents.substring(0, pos + 1) + importText + '.js' + contents.substring(end + 1);
  375. }
  376. data.contents = Buffer.from(contents);
  377. this.emit('data', data);
  378. }
  379. );
  380. }
  381. /**
  382. * - Rename esm/vs/editor/editor.main.js to esm/vs/editor/edcore.main.js
  383. * - Create esm/vs/editor/editor.main.js that that stiches things together
  384. * @param {string} dest
  385. * @returns {NodeJS.ReadWriteStream}
  386. */
  387. function ESM_addPluginContribs(dest) {
  388. const DESTINATION = path.join(__dirname, dest);
  389. return es.through(
  390. /**
  391. * @param {File} data
  392. */
  393. function (data) {
  394. if (!/editor\.main\.js$/.test(data.path)) {
  395. this.emit('data', data);
  396. return;
  397. }
  398. this.emit(
  399. 'data',
  400. new File({
  401. path: data.path.replace(/editor\.main/, 'edcore.main'),
  402. base: data.base,
  403. contents: data.contents
  404. })
  405. );
  406. const mainFileDestPath = path.join(DESTINATION, 'vs/editor/editor.main.js');
  407. /** @type {string[]} */
  408. let mainFileImports = [];
  409. metadata.METADATA.PLUGINS.forEach(function (plugin) {
  410. const contribDestPath = path.join(DESTINATION, plugin.contrib);
  411. let relativePath = path
  412. .relative(path.dirname(mainFileDestPath), contribDestPath)
  413. .replace(/\\/g, '/');
  414. if (!/(^\.\/)|(^\.\.\/)/.test(relativePath)) {
  415. relativePath = './' + relativePath;
  416. }
  417. mainFileImports.push(relativePath);
  418. });
  419. const mainFileContents =
  420. mainFileImports.map((name) => `import '${name}';`).join('\n') +
  421. `\n\nexport * from './edcore.main';`;
  422. this.emit(
  423. 'data',
  424. new File({
  425. path: data.path,
  426. base: data.base,
  427. contents: Buffer.from(mainFileContents)
  428. })
  429. );
  430. }
  431. );
  432. }
  433. /**
  434. * Edit monaco.d.ts:
  435. * - append monaco.d.ts from plugins
  436. * @returns {NodeJS.ReadWriteStream}
  437. */
  438. function addPluginDTS() {
  439. return es.through(
  440. /**
  441. * @param {File} data
  442. */
  443. function (data) {
  444. if (!/monaco\.d\.ts$/.test(data.path)) {
  445. this.emit('data', data);
  446. return;
  447. }
  448. let contents = data.contents.toString();
  449. /** @type {string[]} */
  450. const extraContent = [];
  451. metadata.METADATA.PLUGINS.forEach(function (plugin) {
  452. const dtsPath = path.join(plugin.rootPath, './monaco.d.ts');
  453. try {
  454. let plugindts = fs.readFileSync(dtsPath).toString();
  455. plugindts = plugindts.replace(/\/\/\/ <reference.*\n/m, '');
  456. extraContent.push(plugindts);
  457. } catch (err) {
  458. return;
  459. }
  460. });
  461. contents =
  462. [
  463. '/*!-----------------------------------------------------------',
  464. ' * Copyright (c) Microsoft Corporation. All rights reserved.',
  465. ' * Type definitions for monaco-editor',
  466. ' * Released under the MIT license',
  467. '*-----------------------------------------------------------*/'
  468. ].join('\n') +
  469. '\n' +
  470. contents +
  471. '\n' +
  472. extraContent.join('\n');
  473. // Ensure consistent indentation and line endings
  474. contents = cleanFile(contents);
  475. data.contents = Buffer.from(contents);
  476. this.emit(
  477. 'data',
  478. new File({
  479. path: path.join(path.dirname(data.path), 'esm/vs/editor/editor.api.d.ts'),
  480. base: data.base,
  481. contents: Buffer.from(toExternalDTS(contents))
  482. })
  483. );
  484. fs.writeFileSync('monaco-editor/website/playground/monaco.d.ts.txt', contents);
  485. fs.writeFileSync('monaco-editor/typedoc/monaco.d.ts', contents);
  486. this.emit('data', data);
  487. }
  488. );
  489. }
  490. /**
  491. * Transforms a .d.ts which uses internal modules (namespaces) to one which is usable with external modules
  492. * This function is duplicated in the `vscode` repo.
  493. * @param {string} contents
  494. * @returns string
  495. */
  496. function toExternalDTS(contents) {
  497. let lines = contents.split(/\r\n|\r|\n/);
  498. let killNextCloseCurlyBrace = false;
  499. for (let i = 0; i < lines.length; i++) {
  500. let line = lines[i];
  501. if (killNextCloseCurlyBrace) {
  502. if ('}' === line) {
  503. lines[i] = '';
  504. killNextCloseCurlyBrace = false;
  505. continue;
  506. }
  507. if (line.indexOf(' ') === 0) {
  508. lines[i] = line.substr(4);
  509. } else if (line.charAt(0) === '\t') {
  510. lines[i] = line.substr(1);
  511. }
  512. continue;
  513. }
  514. if ('declare namespace monaco {' === line) {
  515. lines[i] = '';
  516. killNextCloseCurlyBrace = true;
  517. continue;
  518. }
  519. if (line.indexOf('declare namespace monaco.') === 0) {
  520. lines[i] = line.replace('declare namespace monaco.', 'export namespace ');
  521. }
  522. if (line.indexOf('declare let MonacoEnvironment') === 0) {
  523. lines[i] = `declare global {\n let MonacoEnvironment: Environment | undefined;\n}`;
  524. }
  525. if (line.indexOf(' MonacoEnvironment?') === 0) {
  526. lines[i] = ` MonacoEnvironment?: Environment | undefined;`;
  527. }
  528. }
  529. return lines.join('\n').replace(/\n\n\n+/g, '\n\n');
  530. }
  531. /**
  532. * Normalize line endings and ensure consistent 4 spaces indentation
  533. * @param {string} contents
  534. * @returns {string}
  535. */
  536. function cleanFile(contents) {
  537. return contents
  538. .split(/\r\n|\r|\n/)
  539. .map(function (line) {
  540. const m = line.match(/^(\t+)/);
  541. if (!m) {
  542. return line;
  543. }
  544. const tabsCount = m[1].length;
  545. let newIndent = '';
  546. for (let i = 0; i < 4 * tabsCount; i++) {
  547. newIndent += ' ';
  548. }
  549. return newIndent + line.substring(tabsCount);
  550. })
  551. .join('\n');
  552. }
  553. /**
  554. * Edit ThirdPartyNotices.txt:
  555. * - append ThirdPartyNotices.txt from plugins
  556. * @returns {NodeJS.ReadWriteStream}
  557. */
  558. function addPluginThirdPartyNotices() {
  559. return es.through(
  560. /**
  561. * @param {File} data
  562. */
  563. function (data) {
  564. if (!/ThirdPartyNotices\.txt$/.test(data.path)) {
  565. this.emit('data', data);
  566. return;
  567. }
  568. let contents = data.contents.toString();
  569. console.log('ADDING ThirdPartyNotices from ./ThirdPartyNotices.txt');
  570. let thirdPartyNoticeContent = fs
  571. .readFileSync(path.join(__dirname, 'ThirdPartyNotices.txt'))
  572. .toString();
  573. thirdPartyNoticeContent = thirdPartyNoticeContent.split('\n').slice(8).join('\n');
  574. contents += '\n' + thirdPartyNoticeContent;
  575. data.contents = Buffer.from(contents);
  576. this.emit('data', data);
  577. }
  578. );
  579. }
  580. // --- website
  581. const cleanWebsiteTask = function (cb) {
  582. rimraf('../monaco-editor-website', { maxBusyTries: 1 }, cb);
  583. };
  584. const buildWebsiteTask = taskSeries(cleanWebsiteTask, function () {
  585. /**
  586. * @param {string} dataPath
  587. * @param {string} contents
  588. * @param {RegExp} regex
  589. * @param {(match:string, fileContents:Buffer)=>string} callback
  590. * @returns {string}
  591. */
  592. function replaceWithRelativeResource(dataPath, contents, regex, callback) {
  593. return contents.replace(regex, function (_, m0) {
  594. var filePath = path.join(path.dirname(dataPath), m0);
  595. return callback(m0, fs.readFileSync(filePath));
  596. });
  597. }
  598. var waiting = 0;
  599. var done = false;
  600. return es
  601. .merge(
  602. gulp
  603. .src(['monaco-editor/website/**/*'], { dot: true })
  604. .pipe(
  605. es.through(
  606. /**
  607. * @param {File} data
  608. */
  609. function (data) {
  610. if (!data.contents || !/\.(html)$/.test(data.path) || /new-samples/.test(data.path)) {
  611. return this.emit('data', data);
  612. }
  613. let contents = data.contents.toString();
  614. contents = contents.replace(/\.\.\/release\/dev/g, 'node_modules/monaco-editor/min');
  615. contents = contents.replace(/{{version}}/g, MONACO_EDITOR_VERSION);
  616. contents = contents.replace(/{{year}}/g, new Date().getFullYear());
  617. // Preload xhr contents
  618. contents = replaceWithRelativeResource(
  619. data.path,
  620. contents,
  621. /<pre data-preload="([^"]+)".*/g,
  622. function (m0, fileContents) {
  623. return (
  624. '<pre data-preload="' +
  625. m0 +
  626. '" style="display:none">' +
  627. fileContents
  628. .toString('utf8')
  629. .replace(/&/g, '&amp;')
  630. .replace(/</g, '&lt;')
  631. .replace(/>/g, '&gt;') +
  632. '</pre>'
  633. );
  634. }
  635. );
  636. // Inline fork.png
  637. contents = replaceWithRelativeResource(
  638. data.path,
  639. contents,
  640. /src="(\.\/fork.png)"/g,
  641. function (m0, fileContents) {
  642. return 'src="data:image/png;base64,' + fileContents.toString('base64') + '"';
  643. }
  644. );
  645. let allCSS = '';
  646. let tmpcontents = replaceWithRelativeResource(
  647. data.path,
  648. contents,
  649. /<link data-inline="yes-please" href="([^"]+)".*/g,
  650. function (m0, fileContents) {
  651. allCSS += fileContents.toString('utf8');
  652. return '';
  653. }
  654. );
  655. tmpcontents = tmpcontents.replace(/<script.*/g, '');
  656. tmpcontents = tmpcontents.replace(/<link.*/g, '');
  657. waiting++;
  658. uncss(
  659. tmpcontents,
  660. {
  661. raw: allCSS,
  662. ignore: [/\.alert\b/, /\.alert-error\b/, /\.playground-page\b/]
  663. },
  664. function (err, output) {
  665. waiting--;
  666. if (!err) {
  667. output = new CleanCSS().minify(output).styles;
  668. let isFirst = true;
  669. contents = contents.replace(
  670. /<link data-inline="yes-please" href="([^"]+)".*/g,
  671. function (_, m0) {
  672. if (isFirst) {
  673. isFirst = false;
  674. return '<style>' + output + '</style>';
  675. }
  676. return '';
  677. }
  678. );
  679. }
  680. // Inline javascript
  681. contents = replaceWithRelativeResource(
  682. data.path,
  683. contents,
  684. /<script data-inline="yes-please" src="([^"]+)".*/g,
  685. function (m0, fileContents) {
  686. return '<script>' + fileContents.toString('utf8') + '</script>';
  687. }
  688. );
  689. data.contents = Buffer.from(contents.split(/\r\n|\r|\n/).join('\n'));
  690. this.emit('data', data);
  691. if (done && waiting === 0) {
  692. this.emit('end');
  693. }
  694. }.bind(this)
  695. );
  696. },
  697. function () {
  698. done = true;
  699. if (waiting === 0) {
  700. this.emit('end');
  701. }
  702. }
  703. )
  704. )
  705. .pipe(gulp.dest('../monaco-editor-website'))
  706. )
  707. .pipe(
  708. es.through(
  709. /**
  710. * @param {File} data
  711. */
  712. function (data) {
  713. this.emit('data', data);
  714. },
  715. function () {
  716. // temporarily create package.json so that npm install doesn't bark
  717. fs.writeFileSync('../monaco-editor-website/package.json', '{}');
  718. fs.writeFileSync('../monaco-editor-website/.nojekyll', '');
  719. cp.execSync('npm install monaco-editor', {
  720. cwd: path.join(__dirname, '../monaco-editor-website')
  721. });
  722. fs.unlinkSync('../monaco-editor-website/package.json');
  723. this.emit('end');
  724. }
  725. )
  726. );
  727. });
  728. gulp.task('build-website', buildWebsiteTask);
  729. gulp.task('prepare-website-branch', async function () {
  730. cp.execSync('git init', {
  731. cwd: path.join(__dirname, '../monaco-editor-website')
  732. });
  733. let remoteUrl = cp.execSync('git remote get-url origin');
  734. let committerUserName = cp.execSync("git log --format='%an' -1");
  735. let committerEmail = cp.execSync("git log --format='%ae' -1");
  736. cp.execSync(`git config user.name ${committerUserName}`, {
  737. cwd: path.join(__dirname, '../monaco-editor-website')
  738. });
  739. cp.execSync(`git config user.email ${committerEmail}`, {
  740. cwd: path.join(__dirname, '../monaco-editor-website')
  741. });
  742. cp.execSync(`git remote add origin ${remoteUrl}`, {
  743. cwd: path.join(__dirname, '../monaco-editor-website')
  744. });
  745. cp.execSync('git checkout -b gh-pages', {
  746. cwd: path.join(__dirname, '../monaco-editor-website')
  747. });
  748. cp.execSync('git add .', {
  749. cwd: path.join(__dirname, '../monaco-editor-website')
  750. });
  751. cp.execSync('git commit -m "Publish website"', {
  752. cwd: path.join(__dirname, '../monaco-editor-website')
  753. });
  754. console.log('RUN monaco-editor-website>git push origin gh-pages --force');
  755. });
  756. const generateTestSamplesTask = function () {
  757. var sampleNames = fs.readdirSync(path.join(__dirname, 'monaco-editor/test/samples'));
  758. var samples = sampleNames.map(function (sampleName) {
  759. var samplePath = path.join(__dirname, 'monaco-editor/test/samples', sampleName);
  760. var sampleContent = fs.readFileSync(samplePath).toString();
  761. return {
  762. name: sampleName,
  763. content: sampleContent
  764. };
  765. });
  766. var prefix =
  767. '//This is a generated file via gulp generate-test-samples\ndefine([], function() { return';
  768. var suffix = '; });';
  769. fs.writeFileSync(
  770. path.join(__dirname, 'monaco-editor/test/samples-all.generated.js'),
  771. prefix + JSON.stringify(samples, null, '\t') + suffix
  772. );
  773. var PLAY_SAMPLES = require(path.join(WEBSITE_GENERATED_PATH, 'all.js')).PLAY_SAMPLES;
  774. var locations = [];
  775. for (var i = 0; i < PLAY_SAMPLES.length; i++) {
  776. var sample = PLAY_SAMPLES[i];
  777. var sampleId = sample.id;
  778. var samplePath = path.join(WEBSITE_GENERATED_PATH, sample.path);
  779. var html = fs.readFileSync(path.join(samplePath, 'sample.html'));
  780. var js = fs.readFileSync(path.join(samplePath, 'sample.js'));
  781. var css = fs.readFileSync(path.join(samplePath, 'sample.css'));
  782. var result = [
  783. '<!DOCTYPE html>',
  784. '<!-- THIS IS A GENERATED FILE VIA gulp generate-test-samples -->',
  785. '<html>',
  786. '<head>',
  787. ' <base href="..">',
  788. ' <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />',
  789. '</head>',
  790. '<body>',
  791. '<style>',
  792. '/*----------------------------------------SAMPLE CSS START*/',
  793. '',
  794. css,
  795. '',
  796. '/*----------------------------------------SAMPLE CSS END*/',
  797. '</style>',
  798. '<a class="loading-opts" href="playground.generated/index.html">[&lt;&lt; BACK]</a> <br/>',
  799. 'THIS IS A GENERATED FILE VIA gulp generate-test-samples',
  800. '',
  801. '<div id="bar" style="margin-bottom: 6px;"></div>',
  802. '',
  803. '<div style="clear:both"></div>',
  804. '<div id="outer-container" style="width:800px;height:450px;border: 1px solid grey">',
  805. '<!-- ----------------------------------------SAMPLE HTML START-->',
  806. '',
  807. html,
  808. '',
  809. '<!-- ----------------------------------------SAMPLE HTML END-->',
  810. '</div>',
  811. '<div style="clear:both"></div>',
  812. '',
  813. '<script src="../metadata.js"></script>',
  814. '<script src="dev-setup.js"></script>',
  815. '<script>',
  816. 'loadEditor(function() {',
  817. '/*----------------------------------------SAMPLE JS START*/',
  818. '',
  819. js,
  820. '',
  821. '/*----------------------------------------SAMPLE JS END*/',
  822. '});',
  823. '</script>',
  824. '</body>',
  825. '</html>'
  826. ];
  827. fs.writeFileSync(
  828. path.join(__dirname, 'monaco-editor/test/playground.generated/' + sampleId + '.html'),
  829. result.join('\n')
  830. );
  831. locations.push({
  832. path: sampleId + '.html',
  833. name: sample.chapter + ' &gt; ' + sample.name
  834. });
  835. }
  836. var index = [
  837. '<!DOCTYPE html>',
  838. '<!-- THIS IS A GENERATED FILE VIA gulp generate-test-samples -->',
  839. '<html>',
  840. '<head>',
  841. ' <base href="..">',
  842. '</head>',
  843. '<body>',
  844. '<a class="loading-opts" href="index.html">[&lt;&lt; BACK]</a><br/>',
  845. 'THIS IS A GENERATED FILE VIA gulp generate-test-samples<br/><br/>',
  846. locations
  847. .map(function (location) {
  848. return (
  849. '<a class="loading-opts" href="playground.generated/' +
  850. location.path +
  851. '">' +
  852. location.name +
  853. '</a>'
  854. );
  855. })
  856. .join('<br/>\n'),
  857. '<script src="../metadata.js"></script>',
  858. '<script src="dev-setup.js"></script>',
  859. '</body>',
  860. '</html>'
  861. ];
  862. fs.writeFileSync(
  863. path.join(__dirname, 'monaco-editor/test/playground.generated/index.html'),
  864. index.join('\n')
  865. );
  866. };
  867. function createSimpleServer(rootDir, port) {
  868. yaserver
  869. .createServer({
  870. rootDir: rootDir
  871. })
  872. .then((staticServer) => {
  873. const server = http.createServer((request, response) => {
  874. return staticServer.handle(request, response);
  875. });
  876. server.listen(port, '127.0.0.1', () => {
  877. console.log(`Running at http://127.0.0.1:${port}`);
  878. });
  879. });
  880. }
  881. gulp.task('generate-test-samples', taskSeries(generateTestSamplesTask));
  882. gulp.task(
  883. 'simpleserver',
  884. taskSeries(generateTestSamplesTask, function () {
  885. const SERVER_ROOT = path.normalize(path.join(__dirname, '../'));
  886. createSimpleServer(SERVER_ROOT, 8080);
  887. createSimpleServer(SERVER_ROOT, 8088);
  888. })
  889. );