From 368eb4279ff6a1f26d134a2349e2ebd8d9c35644 Mon Sep 17 00:00:00 2001 From: Ben Gubler Date: Tue, 5 May 2020 00:25:26 -0600 Subject: [PATCH] refactor(v2): replace EJS with Eta for SSR generation (#2684) * Replace EJS with Eta for SSR generation * Format with Prettier * Remove ejs alias * Don't autotrim, Terser will minify already * Don't autoTrim, files are HTML-minified later * Equivalent of rmWhitespace option for Eta * Update Eta version and add rmWhitespace * Format with Prettier --- packages/docusaurus/package.json | 2 +- packages/docusaurus/src/client/serverEntry.js | 5 ++-- .../src/client/templates/ssr.html.template.js | 30 +++++++++---------- packages/docusaurus/src/webpack/base.ts | 2 -- yarn.lock | 10 +++---- 5 files changed, 24 insertions(+), 25 deletions(-) diff --git a/packages/docusaurus/package.json b/packages/docusaurus/package.json index 8f74f6948fa8..3d0bfca9a29f 100644 --- a/packages/docusaurus/package.json +++ b/packages/docusaurus/package.json @@ -53,7 +53,7 @@ "core-js": "^2.6.5", "css-loader": "^3.4.2", "del": "^5.1.0", - "ejs": "^3.0.2", + "eta": "^1.1.1", "express": "^4.17.1", "fs-extra": "^8.1.0", "globby": "^10.0.1", diff --git a/packages/docusaurus/src/client/serverEntry.js b/packages/docusaurus/src/client/serverEntry.js index b700e41aa6a8..e31c94b432c8 100644 --- a/packages/docusaurus/src/client/serverEntry.js +++ b/packages/docusaurus/src/client/serverEntry.js @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import ejs from 'ejs'; +import * as eta from 'eta'; import React from 'react'; import {StaticRouter} from 'react-router-dom'; import ReactDOMServer from 'react-dom/server'; @@ -61,7 +61,7 @@ export default async function render(locals) { const scripts = (bundles.js || []).map((b) => b.file); const {baseUrl} = locals; - const renderedHtml = ejs.render( + const renderedHtml = eta.render( ssrTemplate.trim(), { appHtml, @@ -77,6 +77,7 @@ export default async function render(locals) { version: packageJson.version, }, { + name: 'ssr-template', rmWhitespace: true, }, ); diff --git a/packages/docusaurus/src/client/templates/ssr.html.template.js b/packages/docusaurus/src/client/templates/ssr.html.template.js index f3c9db7a13b7..8fbd99e6489f 100644 --- a/packages/docusaurus/src/client/templates/ssr.html.template.js +++ b/packages/docusaurus/src/client/templates/ssr.html.template.js @@ -7,31 +7,31 @@ module.exports = ` -> +> - - <%- headTags %> - <% metaAttributes.forEach((metaAttribute) => { %> - <%- metaAttribute %> + + <%~ it.headTags %> + <% it.metaAttributes.forEach((metaAttribute) => { %> + <%~ metaAttribute %> <% }); %> - <% stylesheets.forEach((stylesheet) => { %> - + <% it.stylesheets.forEach((stylesheet) => { %> + <% }); %> - <% scripts.forEach((script) => { %> - + <% it.scripts.forEach((script) => { %> + <% }); %> - > - <%- preBodyTags %> + > + <%~ it.preBodyTags %>
- <%- appHtml %> + <%~ it.appHtml %>
- <% scripts.forEach((script) => { %> - + <% it.scripts.forEach((script) => { %> + <% }); %> - <%- postBodyTags %> + <%~ it.postBodyTags %> `; diff --git a/packages/docusaurus/src/webpack/base.ts b/packages/docusaurus/src/webpack/base.ts index 775c3676ff38..f2c6a349f65d 100644 --- a/packages/docusaurus/src/webpack/base.ts +++ b/packages/docusaurus/src/webpack/base.ts @@ -61,8 +61,6 @@ export function createBaseConfig( extensions: ['.wasm', '.mjs', '.js', '.jsx', '.ts', '.tsx', '.json'], symlinks: true, alias: { - // https://stackoverflow.com/a/55433680/6072730 - ejs: 'ejs/ejs.min.js', '@site': siteDir, '@generated': generatedFilesDir, '@docusaurus': path.resolve(__dirname, '../client/exports'), diff --git a/yarn.lock b/yarn.lock index f7e3ebbf1d97..68e30243afc8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6194,11 +6194,6 @@ ejs@^2.6.1: resolved "https://registry.yarnpkg.com/ejs/-/ejs-2.7.4.tgz#48661287573dcc53e366c7a1ae52c3a120eec9ba" integrity sha512-7vmuyh5+kuUyJKePhQfRQBhXV5Ce+RnaeeQArKu1EAMpL3WbgMt5WG6uQZpEVvYSSsxMXRKOewtDk9RaTKXRlA== -ejs@^3.0.2: - version "3.0.2" - resolved "https://registry.yarnpkg.com/ejs/-/ejs-3.0.2.tgz#745b01cdcfe38c1c6a2da3bbb2d9957060a31226" - integrity sha512-IncmUpn1yN84hy2shb0POJ80FWrfGNY0cxO9f4v+/sG7qcBvAtVWUA1IdzY/8EYUmOVhoKJVdJjNd3AZcnxOjA== - electron-to-chromium@^1.3.247, electron-to-chromium@^1.3.322: version "1.3.322" resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.322.tgz#a6f7e1c79025c2b05838e8e344f6e89eb83213a8" @@ -6700,6 +6695,11 @@ esutils@^2.0.2: resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.3.tgz#74d2eb4de0b8da1293711910d50775b9b710ef64" integrity sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g== +eta@^1.1.1: + version "1.1.1" + resolved "https://registry.yarnpkg.com/eta/-/eta-1.1.1.tgz#acd575025233488a66870b02223001d4cd467441" + integrity sha512-YRchTAXZZrrJVmlAwmLHuKmlHNJ2hO0uen8LsDqvH5kknRjh0ef+Y9kInENygCgvu7FQNJBpbyDChfMo8f5Qgw== + etag@~1.8.1: version "1.8.1" resolved "https://registry.yarnpkg.com/etag/-/etag-1.8.1.tgz#41ae2eeb65efa62268aebfea83ac7d79299b0887"