Skip to content

Commit

Permalink
Minify js
Browse files Browse the repository at this point in the history
  • Loading branch information
NeeEoo committed Oct 1, 2024
1 parent abb490e commit d60e44a
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 11 deletions.
16 changes: 15 additions & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"devDependencies": {
"clean-css": "^5.3.3",
"sass": "^1.79.1"
"sass": "^1.79.1",
"uglify-js": "^3.19.3"
}
}
4 changes: 2 additions & 2 deletions src/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var tools = require('./pages/tools/tools.build.js');
var apiDocs = require('./pages/apiDocs.build.js');
var indexPage = require('./pages/index.build.js');

var { copyDir, fixHtmlRefs, parseTemplate, compileSass, setGlobals, getGlobals } = require('./utils.js');
var { copyDir, fixHtmlRefs, parseTemplate, compileSass, compileJs, setGlobals, getGlobals } = require('./utils.js');

var isFullBuild = process.argv.includes('--full');
process.argv = process.argv.filter(arg => arg != '--full');
Expand Down Expand Up @@ -44,7 +44,7 @@ compileSass("./src/pages/index.scss", exportPath + "/index.css");
compileSass("./src/pages/api-docs.scss", exportPath + "/api-docs.css");
compileSass("./src/giscus-theme.scss", exportPath + "/giscus-theme.css");

fs.copyFileSync("./src/pages/featuredMods.js", exportPath + "/featuredMods.js");
compileJs("./src/pages/featuredMods.js", exportPath + "/featuredMods.js");

indexPage.buildHtml(pageDir, exportPath); // builds into /
wiki.buildHtml(pageDir, exportPath); // builds into /wiki
Expand Down
1 change: 0 additions & 1 deletion src/pages/featuredMods.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ function getRelativeTimeString(

var lastUpdated = document.querySelectorAll(".last-updated");
if(lastUpdated.length > 0 && window.Intl) {
const rtf1 = new Intl.RelativeTimeFormat('en', { style: 'short' });
lastUpdated.forEach(lastUpdated => {
var time = lastUpdated.getAttribute("data-time");
if(time != "unknown" && time != null && time != "unreleased") {
Expand Down
8 changes: 3 additions & 5 deletions src/pages/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ pageTitle }} - Codename Engine Docs</title>
<link rel="stylesheet" href="root/style.css?index">
<link rel="stylesheet" href="root/index.css?index">
<link rel="stylesheet" href="root/style.css">
<link rel="stylesheet" href="root/index.css">

<meta name="title" content="{{ pageTitle }} - Codename Engine Docs" />
<meta name="description" content="Website dedicated to things for Codename Engine!" />
Expand All @@ -29,9 +29,7 @@
<link href='https://netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.css' rel='stylesheet' type='text/css' />
</head>
<body class="nojs">
<script>
document.body.classList.remove("nojs");
</script>
<script>document.body.classList.remove("nojs");</script>
<div class="header">
{{ header }}
</div>
Expand Down
43 changes: 42 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var hljs = require('highlight.js');
var fs = require('fs');
var sass = require('sass');
var CleanCSS = require('clean-css');
var UglifyJS = require("uglify-js");

var isFullBuild = false;
var isWatch = false;
Expand Down Expand Up @@ -138,9 +139,38 @@ function copyDir(src, dest) {
if (stats.isDirectory()) {
copyDir(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
if(isRelease) {
if(item.endsWith(".js")) {
compileJs(srcPath, destPath);
} else if(item.endsWith(".css")) {
compileCss(srcPath, destPath);
} else if(item.endsWith(".scss")) {
compileSass(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
}
} else {
fs.copyFileSync(srcPath, destPath);
}
}
}
}

function compileJs(file, dest) {
if(isRelease) {
var content = fs.readFileSync(file, 'utf8');
var result = UglifyJS.minify(content);
if(result.error) {
console.error(result.error);
console.error("Error minifying file: " + file);
console.error("Skipping...");
fs.copyFileSync(file, dest);
return;
}
fs.writeFileSync(dest, result.code);
return;
}
fs.copyFileSync(file, dest);
}

function parseTemplate(html, vars) {
Expand Down Expand Up @@ -191,6 +221,16 @@ function compileSass(file, dest) {
fs.writeFileSync(dest, result.css);
}

function compileCss(file, dest) {
var content = fs.readFileSync(file, 'utf8');
if(isRelease) {
content = new CleanCSS({
level: 2
}).minify(content).styles;
}
fs.writeFileSync(dest, content);
}

module.exports = {
setGlobals: setGlobals,
getGlobals: getGlobals,
Expand All @@ -199,4 +239,5 @@ module.exports = {
copyDir: copyDir,
parseTemplate: parseTemplate,
compileSass: compileSass,
compileJs: compileJs,
}

0 comments on commit d60e44a

Please sign in to comment.