From cb8a29a661593ca2d47a94c163c4a129055977d6 Mon Sep 17 00:00:00 2001 From: soulsands <407221377@qq.com> Date: Fri, 21 Apr 2023 21:46:09 +0800 Subject: [PATCH] perf: minify css --- .gitignore | 1 + Dockerfile | 4 +++- bin/copy-trilium.sh | 1 + package.json | 8 ++++++-- src/app.js | 9 ++++++--- 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 6c7f73ae13..d2a36cfe9d 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ node_modules/ dist/ src/public/app-dist/ +src/public/stylesheets-dist/ npm-debug.log yarn-error.log *.db diff --git a/Dockerfile b/Dockerfile index 84db0939b1..a7ff141c34 100644 --- a/Dockerfile +++ b/Dockerfile @@ -27,7 +27,9 @@ RUN set -x \ && npm prune --omit=dev \ && cp src/public/app/share.js src/public/app-dist/. \ && cp -r src/public/app/doc_notes src/public/app-dist/. \ - && rm -rf src/public/app + && rm -rf src/public/app \ + && rm -rf src/public/stylesheets + # Some setup tools need to be kept RUN apk add --no-cache su-exec shadow diff --git a/bin/copy-trilium.sh b/bin/copy-trilium.sh index ae460ff92a..fc9ef0c040 100755 --- a/bin/copy-trilium.sh +++ b/bin/copy-trilium.sh @@ -48,3 +48,4 @@ cp $DIR/src/public/app/share.js $DIR/src/public/app-dist/ cp -r $DIR/src/public/app/doc_notes $DIR/src/public/app-dist/ rm -rf $DIR/src/public/app +rm -rf $DIR/src/public/stylesheets diff --git a/package.json b/package.json index 3be963b574..1da9d45985 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,8 @@ "build-backend-docs": "rm -rf ./docs/backend_api && ./node_modules/.bin/jsdoc -c jsdoc-conf.json -d ./docs/backend_api src/becca/entities/*.js src/services/backend_script_api.js src/services/sql.js", "build-frontend-docs": "rm -rf ./docs/frontend_api && ./node_modules/.bin/jsdoc -c jsdoc-conf.json -d ./docs/frontend_api src/public/app/entities/*.js src/public/app/services/frontend_script_api.js src/public/app/widgets/right_panel_widget.js", "build-docs": "npm run build-backend-docs && npm run build-frontend-docs", - "webpack": "webpack -c webpack.config.js", + "webpack": "webpack -c webpack.config.js && npm run postcss", + "postcss": "postcss src/public/stylesheets --dir src/public/stylesheets-dist -u cssnano --no-map", "test-jasmine": "jasmine", "test-es6": "node -r esm spec-es6/attribute_parser.spec.js ", "test": "npm run test-jasmine && npm run test-es6", @@ -97,6 +98,7 @@ }, "devDependencies": { "cross-env": "7.0.3", + "cssnano": "^6.0.0", "electron": "16.2.8", "electron-builder": "23.6.0", "electron-packager": "17.1.1", @@ -116,6 +118,8 @@ "lorem-ipsum": "2.0.8", "prettier": "2.8.7", "nodemon": "^2.0.22", + "postcss": "^8.4.23", + "postcss-cli": "^10.1.0", "rcedit": "3.0.1", "webpack": "5.78.0", "webpack-cli": "5.0.1" @@ -126,4 +130,4 @@ "lint-staged": { "*.js": "eslint --cache --fix" } -} +} \ No newline at end of file diff --git a/src/app.js b/src/app.js index c876fceab9..ea363e221b 100644 --- a/src/app.js +++ b/src/app.js @@ -12,6 +12,7 @@ const dataDir = require('./services/data_dir'); const utils = require('./services/utils'); const assetPath = require('./services/asset_path'); const env = require('./services/env'); +const isDev = env.isDev(); require('./services/handlers'); require('./becca/becca_loader'); @@ -32,7 +33,7 @@ app.use(helmet({ })); const persistentCacheStatic = (root, options) => { - if (!env.isDev()) { + if (!isDev) { options = { maxAge: '1y', ...options @@ -51,8 +52,10 @@ app.use(`/${assetPath}/app`, persistentCacheStatic(path.join(__dirname, 'public/ app.use(`/${assetPath}/app-dist`, persistentCacheStatic(path.join(__dirname, 'public/app-dist'))); app.use(`/${assetPath}/fonts`, persistentCacheStatic(path.join(__dirname, 'public/fonts'))); app.use(`/assets/vX/fonts`, express.static(path.join(__dirname, 'public/fonts'))); -app.use(`/${assetPath}/stylesheets`, persistentCacheStatic(path.join(__dirname, 'public/stylesheets'))); -app.use(`/assets/vX/stylesheets`, express.static(path.join(__dirname, 'public/stylesheets'))); + +app.use(`/${assetPath}/stylesheets`, persistentCacheStatic(path.join(__dirname, `public/stylesheets${isDev ? '' : '-dist'}`))); +app.use(`/assets/vX/stylesheets`, express.static(path.join(__dirname, `public/stylesheets${isDev ? '' : '-dist'}`))); + app.use(`/${assetPath}/libraries`, persistentCacheStatic(path.join(__dirname, '..', 'libraries'))); app.use(`/assets/vX/libraries`, express.static(path.join(__dirname, '..', 'libraries'))); // excalidraw-view mode in shared notes