diff --git a/.gitignore b/.gitignore index b6a211def2..a03f8035e1 100644 --- a/.gitignore +++ b/.gitignore @@ -122,6 +122,7 @@ test/functional/*.png # Built asset files /ghost/core/core/built /ghost/core/core/frontend/public/ghost.min.css +/ghost/core/core/frontend/public/comment-counts.min.js # Caddyfile - for local development with ssl + caddy Caddyfile diff --git a/ghost/core/core/bridge.js b/ghost/core/core/bridge.js index 0ace590f5c..6549a15f65 100644 --- a/ghost/core/core/bridge.js +++ b/ghost/core/core/bridge.js @@ -16,7 +16,7 @@ const logging = require('@tryghost/logging'); const tpl = require('@tryghost/tpl'); const themeEngine = require('./frontend/services/theme-engine'); const appService = require('./frontend/services/apps'); -const {adminAuthAssets, cardAssets,commentCountsAssets, memberAttributionAssets} = require('./frontend/services/assets-minification'); +const {adminAuthAssets, cardAssets, memberAttributionAssets} = require('./frontend/services/assets-minification'); const routerManager = require('./frontend/services/routing').routerManager; const settingsCache = require('./shared/settings-cache'); const urlService = require('./server/services/url'); @@ -72,7 +72,6 @@ class Bridge { // TODO: is this in the right place? // rebuild asset files - commentCountsAssets.invalidate(); adminAuthAssets.invalidate(); memberAttributionAssets.invalidate(); } catch (err) { diff --git a/ghost/core/core/frontend/src/comment-counts/js/comment-counts.js b/ghost/core/core/frontend/public/comment-counts.js similarity index 100% rename from ghost/core/core/frontend/src/comment-counts/js/comment-counts.js rename to ghost/core/core/frontend/public/comment-counts.js diff --git a/ghost/core/core/frontend/services/assets-minification/CommentCountsAssets.js b/ghost/core/core/frontend/services/assets-minification/CommentCountsAssets.js deleted file mode 100644 index 422b0b305f..0000000000 --- a/ghost/core/core/frontend/services/assets-minification/CommentCountsAssets.js +++ /dev/null @@ -1,33 +0,0 @@ -const Minifier = require('@tryghost/minifier'); -const path = require('path'); -const config = require('../../../shared/config'); -const AssetsMinificationBase = require('./AssetsMinificationBase'); - -module.exports = class CommentCountsAssets extends AssetsMinificationBase { - constructor(options = {}) { - super(options); - - this.src = options.src || path.join(config.get('paths').assetSrc, 'comment-counts'); - this.dest = options.dest || config.getContentPath('public'); - this.minifier = new Minifier({src: this.src, dest: this.dest}); - - this.files = []; - } - - /** - * @override - */ - generateGlobs() { - return { - 'comment-counts.min.js': 'js/*.js' - }; - } - - /** - * @override - */ - async load() { - const globs = this.generateGlobs(); - this.files = await this.minify(globs); - } -}; diff --git a/ghost/core/core/frontend/services/assets-minification/index.js b/ghost/core/core/frontend/services/assets-minification/index.js index f69e5352fe..611c9d4845 100644 --- a/ghost/core/core/frontend/services/assets-minification/index.js +++ b/ghost/core/core/frontend/services/assets-minification/index.js @@ -1,16 +1,13 @@ const AdminAuthAssets = require('./AdminAuthAssets'); const CardAssets = require('./CardAssets'); -const CommentCountsAssets = require('./CommentCountsAssets'); const MemberAttributionAssets = require('./MemberAttributionAssets'); const adminAuthAssets = new AdminAuthAssets(); const cardAssets = new CardAssets(); -const commentCountsAssets = new CommentCountsAssets(); const memberAttributionAssets = new MemberAttributionAssets(); module.exports = { adminAuthAssets, cardAssets, - commentCountsAssets, memberAttributionAssets -}; \ No newline at end of file +}; diff --git a/ghost/core/core/frontend/web/site.js b/ghost/core/core/frontend/web/site.js index 637e3a8f83..934994ae2e 100644 --- a/ghost/core/core/frontend/web/site.js +++ b/ghost/core/core/frontend/web/site.js @@ -16,7 +16,7 @@ const membersService = require('../../server/services/members'); const offersService = require('../../server/services/offers'); const customRedirects = require('../../server/services/custom-redirects'); const linkRedirects = require('../../server/services/link-redirection'); -const {cardAssets, commentCountsAssets, memberAttributionAssets} = require('../services/assets-minification'); +const {cardAssets, memberAttributionAssets} = require('../services/assets-minification'); const siteRoutes = require('./routes'); const shared = require('../../server/web/shared'); const errorHandler = require('@tryghost/mw-error-handler'); @@ -77,7 +77,7 @@ module.exports = function setupSiteApp(routerConfig) { siteApp.use(cardAssets.serveMiddleware(), mw.servePublicFile('built', 'public/cards.min.js', 'application/javascript', config.get('caching:publicAssets:maxAge'))); // Comment counts - siteApp.use(commentCountsAssets.serveMiddleware(), mw.servePublicFile('built', 'public/comment-counts.min.js', 'application/javascript', config.get('caching:publicAssets:maxAge'))); + siteApp.use(mw.servePublicFile('static', 'public/comment-counts.min.js', 'application/javascript', config.get('caching:publicAssets:maxAge'))); // Member attribution siteApp.use(memberAttributionAssets.serveMiddleware(), mw.servePublicFile('built', 'public/member-attribution.min.js', 'application/javascript', config.get('caching:publicAssets:maxAge'))); diff --git a/ghost/core/package.json b/ghost/core/package.json index f43406c864..283400c2e9 100644 --- a/ghost/core/package.json +++ b/ghost/core/package.json @@ -22,7 +22,9 @@ "scripts": { "archive": "npm pack", "dev": "node --watch index.js", - "build:assets": "postcss core/frontend/public/ghost.css --no-map --use cssnano -o core/frontend/public/ghost.min.css", + "build:assets:css": "postcss core/frontend/public/ghost.css --no-map --use cssnano -o core/frontend/public/ghost.min.css", + "build:assets:js": "../minifier/bin/minify core/frontend/public/comment-counts.js core/frontend/public/comment-counts.min.js", + "build:assets": "yarn build:assets:css && yarn build:assets:js", "test": "yarn test:unit", "test:base": "mocha --reporter dot --require=./test/utils/overrides.js --exit --trace-warnings --recursive --extension=test.js", "test:single": "yarn test:base --timeout=60000", @@ -282,6 +284,7 @@ }, "dev": { "dependsOn": [ + "build:assets", "^build:ts" ] }, diff --git a/ghost/minifier/bin/minify b/ghost/minifier/bin/minify new file mode 100755 index 0000000000..2ab3340bcf --- /dev/null +++ b/ghost/minifier/bin/minify @@ -0,0 +1,27 @@ +#!/usr/bin/env node + +/** + * Script to minify a given file + * + * Usage: bin/minify + */ + +const path = require('path'); +const Minifier = require('../index'); + +const src = path.parse(process.argv[2]); +const dest = path.parse(process.argv[3]); + +const minifier = new Minifier({src: src.dir, dest: dest.dir}); + +(async () => { + try { + await minifier.minify({ + [dest.base]: src.base + }); + } catch (error) { + console.error(error); + + process.exit(1); + } +})();