Skip to content

Commit

Permalink
Merge pull request #25 from devatherock/skip-minified-files
Browse files Browse the repository at this point in the history
feat: Skipped `*.min.*` files from minification
  • Loading branch information
devatherock authored Apr 28, 2024
2 parents df98ccd + 023b525 commit c4f40af
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

### Changed
- Replaced `ubuntu-2004:2022.04.1` image with `ubuntu-2204:2023.04.2`
- [#21](https://github.com/devatherock/minify-js/pull/21): Skipped `*.min.*` files from minification

## [3.0.0] - 2023-11-26
### Added
Expand Down
16 changes: 9 additions & 7 deletions lib/minify-js.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ export function minifyFiles (inputPath, addSuffix, outputPath, inclusions, exclu
} else {
getLogger().debug('Input path ', inputPath, ' is a file')

if (supportedExtensions.includes(path.extname(inputPath))) {
if ((inclusions.length === 0 || inclusions.some((regex) => regex.test(inputPath)))
&& (exclusions.length === 0 || !exclusions.some((regex) => regex.test(inputPath)))) {
minifyFile(inputPath, addSuffix, outputPath)
} else {
getLogger().debug('Skipping file ', inputPath, " that doesn't match any inclusions")
}
if (inputPath.includes('.min.')) {
getLogger().debug('Skipping file ', inputPath, ' is already minified.')
} else if (supportedExtensions.includes(path.extname(inputPath))) {
if ((inclusions.length === 0 || inclusions.some((regex) => regex.test(inputPath)))
&& (exclusions.length === 0 || !exclusions.some((regex) => regex.test(inputPath)))) {
minifyFile(inputPath, addSuffix, outputPath)
} else {
getLogger().debug('Skipping file ', inputPath, " that doesn't match any inclusions")
}
} else {
getLogger().debug('Skipping file ', inputPath, ' with unsupported extension')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import spock.lang.Specification
import spock.lang.Unroll

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths

/**
Expand Down Expand Up @@ -71,6 +72,54 @@ class MinifyJsDockerSpec extends Specification {
Files.deleteIfExists(Paths.get(outputCssFile))
}

void 'test minify - *.min.* files should be skipped'() {
given:
String baseDir = System.properties['user.dir']
String outputHtmlFile = "${baseDir}/src/test/resources/static/index.min.html"
String outputJsFile = "${baseDir}/src/test/resources/scripts/scripts.min.js"
String outputCssFile = "${baseDir}/src/test/resources/static/main.min.css"

and:
Path recursiveOutputHtmlFile = Paths.get("${baseDir}/src/test/resources/static/index.min.min.html")
Path recursiveOutputJsFile = Paths.get("${baseDir}/src/test/resources/scripts/scripts.min.min.js")
Path recursiveOutputCssFile = Paths.get("${baseDir}/src/test/resources/static/main.min.min.css")

and:
def command = ['docker', 'run', '--rm',
'-v', "${baseDir}:/work",
'-w=/work',
imageName]

when:
def output = ProcessUtil.executeCommand(command)
ProcessUtil.executeCommand(command)

then:
output[0] == 0
output[1].contains('Minified /work/src/test/resources/static/index.html > /work/src/test/resources/static/index.min.html')
output[1].contains('Minified /work/src/test/resources/static/main.css > /work/src/test/resources/static/main.min.css')
output[1].contains('Minified /work/src/test/resources/scripts/scripts.js > /work/src/test/resources/scripts/scripts.min.js')
new File(outputHtmlFile).text ==
'<!doctype html><title>Test title</title><div id=layout><div id=main><div class=header><h1>Test body</h1></div></div></div>\n'
new File(outputJsFile).text ==
'$((function(){$("#templateAndModelForm *:input[type!=hidden]:first").focus()}));\n'
new File(outputCssFile).text ==
'.content{width:50%;display:block;margin:2% auto}.header{text-align:center;color:#444;border-bottom:1px solid #eee}\n'

and:
Files.notExists(recursiveOutputHtmlFile)
Files.notExists(recursiveOutputJsFile)
Files.notExists(recursiveOutputCssFile)

cleanup:
Files.deleteIfExists(Paths.get(outputHtmlFile))
Files.deleteIfExists(Paths.get(outputJsFile))
Files.deleteIfExists(Paths.get(outputCssFile))
Files.deleteIfExists(recursiveOutputHtmlFile)
Files.deleteIfExists(recursiveOutputJsFile)
Files.deleteIfExists(recursiveOutputCssFile)
}

@Unroll
void 'test minify - input path specified. ci: #ci'() {
given:
Expand Down

0 comments on commit c4f40af

Please sign in to comment.