Split minify options for CSS and JS

This commit is contained in:
Christoph Hagen 2022-12-10 22:28:39 +01:00
parent 3991211e37
commit 31923974a6
5 changed files with 24 additions and 13 deletions

View File

@ -21,12 +21,20 @@ struct Configuration: Codable {
let fullScreenImageWidth: Int let fullScreenImageWidth: Int
/** /**
Automatically minify all `.css` and `.js` resources which are copied Automatically minify all `.css` resources which are copied
to the output folder. to the output folder.
- Note: This option requires the `uglifyjs` and `clean-css` tools, - Note: This option requires the `clean-css` tool,
which can be installed using the `install.sh` script in the root folder of the generator. which can be installed using the `install.sh` script in the root folder of the generator.
*/ */
let minifyCSSandJS: Bool let minifyCSS: Bool
/**
Automatically minify all `.js` resources which are copied
to the output folder.
- Note: This option requires the `uglifyjs` tool,
which can be installed using the `install.sh` script in the root folder of the generator.
*/
let minifyJavaScript: Bool
/** /**
The path to the directory where the root element metadata is located. The path to the directory where the root element metadata is located.
@ -76,8 +84,8 @@ struct Configuration: Codable {
print(" Output folder: \(outputDirectory.path)") print(" Output folder: \(outputDirectory.path)")
print(" Page width: \(pageImageWidth)") print(" Page width: \(pageImageWidth)")
print(" Full-screen width: \(fullScreenImageWidth)") print(" Full-screen width: \(fullScreenImageWidth)")
print(" Minify JavaScript: \(minifyCSSandJS)") print(" Minify JavaScript: \(minifyJavaScript)")
print(" Minify CSS: \(minifyCSSandJS)") print(" Minify CSS: \(minifyCSS)")
print(" Create markdown files: \(createMdFilesIfMissing)") print(" Create markdown files: \(createMdFilesIfMissing)")
} }
} }

View File

@ -155,11 +155,11 @@ final class GenerationResultsHandler {
private func markForCopyOrMinification(file: String, source: String) { private func markForCopyOrMinification(file: String, source: String) {
let ext = file.lastComponentAfter(".") let ext = file.lastComponentAfter(".")
if configuration.minifyCSSandJS, ext == "js" { if configuration.minifyJavaScript, ext == "js" {
files.toMinify[file] = (source, .js) files.toMinify[file] = (source, .js)
return return
} }
if configuration.minifyCSSandJS, ext == "css" { if configuration.minifyCSS, ext == "css" {
files.toMinify[file] = (source, .css) files.toMinify[file] = (source, .css)
return return
} }

View File

@ -18,6 +18,10 @@ private func loadConfiguration(at configPath: String) -> Configuration? {
print(" ") print(" ")
print(" Configuration file: \(configPath)") print(" Configuration file: \(configPath)")
let configUrl = URL(fileURLWithPath: configPath) let configUrl = URL(fileURLWithPath: configPath)
guard configUrl.exists else {
print(" Error: Configuration file not found")
return nil
}
var config: Configuration var config: Configuration
do { do {
let data = try Data(contentsOf: configUrl) let data = try Data(contentsOf: configUrl)

View File

@ -1,6 +1,8 @@
{ {
"pageImageWidth" : 748, "pageImageWidth" : 748,
"minifyCSSandJS" : true, "fullScreenImageWidth" : 4000,
"minifyJavaScript" : false,
"minifyCSS" : false,
"createMdFilesIfMissing" : false, "createMdFilesIfMissing" : false,
"contentPath" : "/path/to/content/folder", "contentPath" : "/path/to/content/folder",
"outputPath" : "/path/to/output/folder") "outputPath" : "/path/to/output/folder")

View File

@ -1,14 +1,11 @@
#!/bin/bash #!/bin/bash
# Install the required dependencies for CHGenerator # Install the required dependencies for CHGenerator
# Note: The following is only required if `minifyCSSandJS=True` # Install the Javascript minifier (required if `minifyJavaScript = True`)
# is set in the generator configuration
# Install the Javascript minifier
# https://github.com/mishoo/UglifyJS # https://github.com/mishoo/UglifyJS
npm install uglify-js -g npm install uglify-js -g
# Install the clean-css minifier # Install the clean-css minifier (required if `minifyCSS = True`)
# https://github.com/clean-css/clean-css-cli # https://github.com/clean-css/clean-css-cli
npm install clean-css-cli -g npm install clean-css-cli -g