Browse Source

dynamic import manifest

Yuki Takei 3 năm trước cách đây
mục cha
commit
6a7bcbf61f

+ 0 - 1
packages/app/.gitignore

@@ -13,7 +13,6 @@ test/cypress/videos
 /transpiled/
 /public/static/js
 /public/static/styles
-/public/static/preset-themes
 /public/uploads
 /tmp/
 

+ 1 - 1
packages/app/package.json

@@ -46,7 +46,7 @@
     "openapi:v3": "yarn cross-env API_VERSION=3 yarn swagger-jsdoc -- \"src/server/routes/apiv3/**/*.js\" \"src/server/models/**/*.js\"",
     "openapi:v1": "yarn cross-env API_VERSION=1 yarn swagger-jsdoc -- \"src/server/*/*.js\" \"src/server/models/**/*.js\"",
     "resources:hackmd": "yarn lerna run build --scope=@growi/hackmd",
-    "resources:preset-themes": "yarn lerna run build --scope=@growi/preset-themes && npx shx mkdir -p public/static/preset-themes && npx -y shx cp -r ../preset-themes/dist/* public/static/preset-themes/",
+    "resources:preset-themes": "yarn lerna run build --scope=@growi/preset-themes",
     "// resources:dl-resources": "yarn ts-node bin/download-cdn-resources.ts",
     "ts-node": "node -r ts-node/register -r tsconfig-paths/register -r dotenv-flow/config"
   },

+ 15 - 8
packages/app/src/pages/_document.page.tsx

@@ -2,7 +2,7 @@
 import React from 'react';
 
 import type { PresetThemesManifest } from '@growi/preset-themes';
-import presetThemesManifest from '@growi/preset-themes/dist/manifest.json';
+import { getManifestKeyFromTheme } from '@growi/preset-themes';
 import mongoose from 'mongoose';
 import Document, {
   DocumentContext, DocumentInitialProps,
@@ -14,11 +14,14 @@ import { CrowiRequest } from '~/interfaces/crowi-request';
 import { GrowiPlugin, GrowiPluginResourceType } from '~/interfaces/plugin';
 
 
-const HeadersForPresetThemes = (): JSX.Element => {
-  const manifest: PresetThemesManifest = presetThemesManifest;
+type HeadersForPresetThemesProps = {
+  manifest: PresetThemesManifest,
+}
+const HeadersForPresetThemes = (props: HeadersForPresetThemesProps): JSX.Element => {
+  const { manifest } = props;
 
-  const themeName = 'halloween';
-  const manifestResourceKey = `src/${themeName}.css`;
+  const themeName = 'default';
+  const manifestResourceKey = getManifestKeyFromTheme(themeName);
   const href = `/static/preset-themes/${manifest[manifestResourceKey].file}`; // configured by express.static
 
   const elements: JSX.Element[] = [];
@@ -67,6 +70,7 @@ const HeadersForGrowiPlugin = (props: HeadersForGrowiPluginProps): JSX.Element =
 
 interface GrowiDocumentProps {
   customCss: string;
+  presetThemesManifest: PresetThemesManifest,
   pluginManifestEntries: GrowiPluginManifestEntries;
 }
 declare type GrowiDocumentInitialProps = DocumentInitialProps & GrowiDocumentProps;
@@ -79,18 +83,21 @@ class GrowiDocument extends Document<GrowiDocumentInitialProps> {
     const { customizeService } = crowi;
     const customCss: string = customizeService.getCustomCss();
 
+    // import preset-themes manifest
+    const presetThemesManifest = await import('@growi/preset-themes/dist/themes/manifest.json').then(imported => imported.default);
+
     // retrieve plugin manifests
     const GrowiPlugin = mongoose.model<GrowiPlugin>('GrowiPlugin');
     const growiPlugins = await GrowiPlugin.find({ isEnabled: true });
     const pluginManifestEntries: GrowiPluginManifestEntries = await ActivatePluginService.retrievePluginManifests(growiPlugins);
 
     return {
-      ...initialProps, customCss, pluginManifestEntries,
+      ...initialProps, customCss, presetThemesManifest, pluginManifestEntries,
     };
   }
 
   override render(): JSX.Element {
-    const { customCss, pluginManifestEntries } = this.props;
+    const { customCss, presetThemesManifest, pluginManifestEntries } = this.props;
 
     return (
       <Html>
@@ -108,7 +115,7 @@ class GrowiDocument extends Document<GrowiDocumentInitialProps> {
           <link rel='preload' href="/static/fonts/Lato-Regular-latin-ext.woff2" as="font" type="font/woff2" />
           <link rel='preload' href="/static/fonts/Lato-Bold-latin.woff2" as="font" type="font/woff2" />
           <link rel='preload' href="/static/fonts/Lato-Bold-latin-ext.woff2" as="font" type="font/woff2" />
-          <HeadersForPresetThemes />
+          <HeadersForPresetThemes manifest={presetThemesManifest} />
           <HeadersForGrowiPlugin pluginManifestEntries={pluginManifestEntries} />
         </Head>
         <body>

+ 4 - 1
packages/app/src/server/crowi/express-init.js

@@ -1,3 +1,4 @@
+import { manifestPath as presetThemesManifestPath } from '@growi/preset-themes';
 import csrf from 'csurf';
 import mongoose from 'mongoose';
 
@@ -116,7 +117,9 @@ module.exports = function(crowi, app) {
 
   const staticOption = (crowi.node_env === 'production') ? { maxAge: '30d' } : {};
   app.use(express.static(crowi.publicDir, staticOption));
-  app.use('/static/preset-themes', express.static(resolveFromRoot('../../node_modules/@growi/preset-themes/dist')));
+  app.use('/static/preset-themes', express.static(
+    resolveFromRoot(`../../node_modules/@growi/preset-themes/${path.dirname(presetThemesManifestPath)}`),
+  ));
   app.use('/plugins', express.static(path.resolve(__dirname, '../../../tmp/plugins')));
 
   app.engine('html', swig.renderFile);