|
|
@@ -1,13 +1,18 @@
|
|
|
const axios = require('axios');
|
|
|
+const path = require('path');
|
|
|
const fs = require('graceful-fs');
|
|
|
|
|
|
const helpers = require('@commons/util/helpers');
|
|
|
const cdnLocalScriptRoot = 'public/js/cdn';
|
|
|
+const cdnLocalScriptWebRoot = '/js/cdn';
|
|
|
const cdnLocalStyleRoot = 'public/styles/cdn';
|
|
|
+const cdnLocalStyleWebRoot = '/styles/cdn';
|
|
|
|
|
|
class CdnResourcesService {
|
|
|
constructor() {
|
|
|
- this.logger = require('@alias/logger')('growi:service:CdnResourcesService');
|
|
|
+ this.logger = require('@alias/logger')('growi:service:CdnResourcesResolver');
|
|
|
+
|
|
|
+ this.noCdn = !!process.env.NO_CDN;
|
|
|
this.loadMetaData();
|
|
|
}
|
|
|
|
|
|
@@ -33,6 +38,69 @@ class CdnResourcesService {
|
|
|
|
|
|
return Promise.all([promisesForScript, promisesForStyle]);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Generate script tag string
|
|
|
+ *
|
|
|
+ * @param {Object} resource
|
|
|
+ * @param {boolean} noCdn
|
|
|
+ */
|
|
|
+ generateScriptTag(resource, noCdn) {
|
|
|
+ const attrs = [];
|
|
|
+ const args = resource.args || {};
|
|
|
+
|
|
|
+ if (args.async) {
|
|
|
+ attrs.push('async');
|
|
|
+ }
|
|
|
+ if (args.defer) {
|
|
|
+ attrs.push('defer');
|
|
|
+ }
|
|
|
+
|
|
|
+ // TODO process integrity
|
|
|
+
|
|
|
+ const url = noCdn
|
|
|
+ ? path.join(cdnLocalScriptWebRoot, resource.name) + '.js'
|
|
|
+ : resource.url;
|
|
|
+ return `<script src="${url}" ${attrs.join(' ')}></script>`;
|
|
|
+ }
|
|
|
+
|
|
|
+ getAllScriptTags() {
|
|
|
+ return this.cdnResources.js.map(resource => {
|
|
|
+ return this.generateScriptTag(resource, this.noCdn);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Generate style tag string
|
|
|
+ *
|
|
|
+ * @param {Object} resource
|
|
|
+ * @param {boolean} noCdn
|
|
|
+ */
|
|
|
+ generateStyleTag(resource, noCdn) {
|
|
|
+ const attrs = [];
|
|
|
+ const args = resource.args || {};
|
|
|
+
|
|
|
+ if (args.async) {
|
|
|
+ attrs.push('async');
|
|
|
+ }
|
|
|
+ if (args.defer) {
|
|
|
+ attrs.push('defer');
|
|
|
+ }
|
|
|
+
|
|
|
+ // TODO process integrity
|
|
|
+
|
|
|
+ const url = noCdn
|
|
|
+ ? path.join(cdnLocalStyleWebRoot, resource.name) + '.css'
|
|
|
+ : resource.url;
|
|
|
+
|
|
|
+ return `<link href="${url}" ${attrs.join(' ')}>`;
|
|
|
+ }
|
|
|
+
|
|
|
+ getAllStyleTags() {
|
|
|
+ return this.cdnResources.style.map(resource => {
|
|
|
+ return this.generateStyleTag(resource, this.noCdn);
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
module.exports = CdnResourcesService;
|