Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions app/back-end/modules/render-html/helpers/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const UtilsHelper = require('./../../../helpers/utils');
const normalizePath = require('normalize-path');
const DiffCopy = require('./diffCopy.js');
const PluginsHelpers = require('./../../plugins/plugins-helpers');
const { minify } = require('terser');

class Files {
/**
Expand All @@ -17,7 +18,7 @@ class Files {
static copyRootFiles(inputDir, outputDir) {
let inputPath = path.join(inputDir, 'root-files');
let outputPath = path.join(outputDir);

fs.copySync(
path.join(inputPath),
path.join(outputPath),
Expand Down Expand Up @@ -54,7 +55,7 @@ class Files {
flatten: true
}).then(files => {
let dynamicAssetsPath = path.join(assetsPath, 'dynamic');

files.filter(item => {
let filename = path.parse(item.path).base;

Expand All @@ -64,7 +65,7 @@ class Files {

return themeConfig.files.ignoreAssets.indexOf(filename) === -1
}).forEach(item => {
if(item.mode.dir === false) {
if (item.mode.dir === false) {
let filePath = normalizePath(item.path);
let destinationPath = filePath.replace(
normalizePath(assetsPath),
Expand All @@ -75,6 +76,10 @@ class Files {
filePath,
destinationPath
);

if (path.extname(destinationPath) === '.js') {
this.#minifyJsFile(destinationPath);
}
}
});
});
Expand Down Expand Up @@ -113,7 +118,7 @@ class Files {
* @param outputDir
* @param themeConfig
*/
static copyDynamicAssetsFiles(themeDir, outputDir, themeConfig) {
static copyDynamicAssetsFiles(themeDir, outputDir, themeConfig) {
if (!themeConfig.files.useDynamicAssets) {
return;
}
Expand Down Expand Up @@ -164,7 +169,7 @@ class Files {
* @param outputDir
* @param postIDs
*/
static async copyMediaFiles (inputDir, outputDir, postIDs, pageIDs) {
static async copyMediaFiles(inputDir, outputDir, postIDs, pageIDs) {
let basePathInput = path.join(inputDir, 'media');
let basePathOutput = path.join(outputDir, 'media');
let dirs = ['website', 'files', 'tags', 'authors', 'posts/defaults'];
Expand Down Expand Up @@ -199,7 +204,7 @@ class Files {
);
} else {
await DiffCopy.copy(
path.join(basePathInput, dirs[i]),
path.join(basePathInput, dirs[i]),
path.join(basePathOutput, dirs[i])
);
}
Expand All @@ -222,11 +227,11 @@ class Files {
* @param inputDir
* @param outputDir
*/
static async copyPluginFiles (inputDir, outputDir, pluginsDir) {
static async copyPluginFiles(inputDir, outputDir, pluginsDir) {
let pluginsList = PluginsHelpers.getActivePluginsList(path.join(inputDir, 'config', 'site.plugins.json'));
let basePathInput = path.join(inputDir, 'media');
let basePathOutput = path.join(outputDir, 'media');

// create media dir if not exists
if (!UtilsHelper.dirExists(path.join(basePathOutput))) {
fs.mkdirSync(path.join(basePathOutput));
Expand Down Expand Up @@ -268,10 +273,31 @@ class Files {
}
}

static async removeEmptyDirectories (outputDir) {
static async removeEmptyDirectories(outputDir) {
let basePathOutput = path.join(outputDir, 'media');
deleteEmpty(basePathOutput);
}

/**
* @param {{ string }} filePath
* @returns {Promise<void>}
*/
static async #minifyJsFile(filePath) {
const filename = path.parse(filePath).base;

if (!filename.endsWith('.js')) {
throw new Error(`Expected .js file path, got: ${filePath}`);
}

const code = fs.readFileSync(filePath, 'utf8');
const result = await minify(code, { compress: true, mangle: true });
if (result.error) {
console.error(`Minify error for ${filePath}:`, result.error);
return;
}

fs.writeFileSync(filePath, result.code);
}
}

module.exports = Files;
86 changes: 86 additions & 0 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"ssh2-sftp-client": "10.0.3",
"striptags": "3.2.0",
"tar-fs": "3.0.4",
"terser": "^5.44.0",
"transliteration": "2.3.5",
"vue": "2.6.14",
"vue-color": "2.4.5",
Expand Down