website.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. //@ts-check
  6. /** @typedef {import('../build/utils').IFile} IFile */
  7. const path = require('path');
  8. const fs = require('fs');
  9. const cp = require('child_process');
  10. const CleanCSS = require('clean-css');
  11. const { REPO_ROOT, removeDir, readFiles, writeFiles } = require('./utils');
  12. /** @type {string} */
  13. const MONACO_EDITOR_VERSION = (() => {
  14. const packageJsonPath = path.join(REPO_ROOT, 'package.json');
  15. const packageJson = JSON.parse(fs.readFileSync(packageJsonPath).toString());
  16. const version = packageJson.version;
  17. if (!/\d+\.\d+\.\d+/.test(version)) {
  18. console.log('unrecognized package.json version: ' + version);
  19. process.exit(1);
  20. }
  21. return version;
  22. })();
  23. removeDir(`../monaco-editor-website`);
  24. generateWebsite();
  25. /**
  26. * @param {string} dataPath
  27. * @param {string} contents
  28. * @param {RegExp} regex
  29. * @param {(match:string, fileContents:Buffer)=>string} callback
  30. * @returns {string}
  31. */
  32. function replaceWithRelativeResource(dataPath, contents, regex, callback) {
  33. return contents.replace(regex, function (_, m0) {
  34. const filePath = path.join(REPO_ROOT, 'monaco-editor/website', path.dirname(dataPath), m0);
  35. return callback(m0, fs.readFileSync(filePath));
  36. });
  37. }
  38. function generateWebsite() {
  39. const files = readFiles('monaco-editor/website/**/*', {
  40. base: 'monaco-editor/website',
  41. dot: true
  42. });
  43. for (const file of files) {
  44. if (!file.contents || !/\.(html)$/.test(file.path) || /new-samples/.test(file.path)) {
  45. continue;
  46. }
  47. let contents = file.contents.toString();
  48. contents = contents.replace(/\.\.\/\.\.\/release\/dev/g, 'node_modules/monaco-editor/min');
  49. // contents = contents.replace(/\.\.\/\.\.\/release\/dev/g, '../monaco-editor/release/dev');
  50. contents = contents.replace(/{{version}}/g, MONACO_EDITOR_VERSION);
  51. contents = contents.replace(/{{year}}/g, String(new Date().getFullYear()));
  52. // Preload xhr contents
  53. contents = replaceWithRelativeResource(
  54. file.path,
  55. contents,
  56. /<pre data-preload="([^"]+)".*/g,
  57. function (m0, fileContents) {
  58. return (
  59. '<pre data-preload="' +
  60. m0 +
  61. '" style="display:none">' +
  62. fileContents
  63. .toString('utf8')
  64. .replace(/&/g, '&amp;')
  65. .replace(/</g, '&lt;')
  66. .replace(/>/g, '&gt;') +
  67. '</pre>'
  68. );
  69. }
  70. );
  71. // Inline fork.png
  72. contents = replaceWithRelativeResource(
  73. file.path,
  74. contents,
  75. /src="(\.\/fork.png)"/g,
  76. function (m0, fileContents) {
  77. return 'src="data:image/png;base64,' + fileContents.toString('base64') + '"';
  78. }
  79. );
  80. // let allCSS = '';
  81. contents = replaceWithRelativeResource(
  82. file.path,
  83. contents,
  84. /<link data-inline="yes-please" href="([^"]+)".*/g,
  85. function (m0, fileContents) {
  86. const minifiedCSS = new CleanCSS().minify(fileContents.toString('utf8')).styles;
  87. return `<style>${minifiedCSS}</style>`;
  88. }
  89. );
  90. // Inline javascript
  91. contents = replaceWithRelativeResource(
  92. file.path,
  93. contents,
  94. /<script data-inline="yes-please" src="([^"]+)".*/g,
  95. function (m0, fileContents) {
  96. return '<script>' + fileContents.toString('utf8') + '</script>';
  97. }
  98. );
  99. file.contents = Buffer.from(contents.split(/\r\n|\r|\n/).join('\n'));
  100. }
  101. writeFiles(files, `../monaco-editor-website`);
  102. // temporarily create package.json so that npm install doesn't bark
  103. fs.writeFileSync(path.join(REPO_ROOT, '../monaco-editor-website/package.json'), '{}');
  104. fs.writeFileSync(path.join(REPO_ROOT, '../monaco-editor-website/.nojekyll'), '');
  105. cp.execSync('npm install monaco-editor', {
  106. cwd: path.join(REPO_ROOT, '../monaco-editor-website')
  107. });
  108. fs.unlinkSync(path.join(REPO_ROOT, '../monaco-editor-website/package.json'));
  109. }