Skip to content

Commit

Permalink
feat: Added ability to exclude specific files from minification
Browse files Browse the repository at this point in the history
Addresses #22
  • Loading branch information
devatherock committed Apr 26, 2024
1 parent cb44392 commit 245b2ba
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog

## [Unreleased]
### Added
- [#22](https://github.com/devatherock/minify-js/issues/22): Ability to exclude specific files/folders within the input directory from minification

### Changed
- Replaced `ubuntu-2004:2022.04.1` image with `ubuntu-2204:2023.04.2`

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ The following parameters can be set to configure the action.

* **inclusions** - Multi-line string, each line of which contains a regex representing files/paths within the input directory to include/minify. By default, all files in the input directory will be minified

* **exclusions** - Multi-line string, each line of which contains a regex representing files to exclude from minification. By default, no file in the input directory will be excluded

```yaml
jobs:
build:
Expand All @@ -35,6 +37,8 @@ jobs:
inclusions: |- # Optional
.*assets.*
.*static/index.html
exclusions: |- # Optional
.*assets/generated/.*
```
### Docker
Expand All @@ -47,6 +51,7 @@ docker run --rm \
-e PARAMETER_OUTPUT_PATH=/work/minify/src \
-e PARAMETER_ADD_SUFFIX=false \
-e PARAMETER_INCLUSIONS=".*assets.*\n.*static/index.html" \
-e PARAMETER_EXCLUSIONS=".*assets/generated/.*" \
devatherock/minify-js:3.0.0
```

Expand All @@ -63,6 +68,8 @@ The following parameters can be set to configure the plugin.

* **inclusions** - Multi-line string, each line of which contains a regex representing files to include/minify. By default, all files in the input directory will be minified

* **exclusions** - Multi-line string, each line of which contains a regex representing files to exclude from minification. By default, no file in the input directory will be excluded

```yaml
steps:
- name: minify_js
Expand All @@ -77,6 +84,8 @@ steps:
inclusions: |-
.*assets.*
.*static/index.html
exclusions: |-
.*assets/generated/.*
```
### CircleCI
Expand All @@ -93,6 +102,7 @@ jobs:
PARAMETER_OUTPUT_PATH: minify/src
PARAMETER_ADD_SUFFIX: false
PARAMETER_INCLUSIONS: '.*assets.*\n.*static/index.html'
PARAMETER_EXCLUSIONS: '.*assets/generated/.*'
steps:
- checkout
- run: node /app/bin/cli.mjs
Expand Down
37 changes: 25 additions & 12 deletions bin/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,26 @@ import { hideBin } from 'yargs/helpers'
import { minifyFiles } from '../lib/minify-js.mjs'
import { stringToBoolean, valueFromEnvVariables, getLogger } from '../lib/utils.mjs'

function readRegexArray(array) {
var regexes = []

if (array) {
const arrayParts = array.replace('\\n', '\n').split(/[\r\n]+/)
getLogger().debug('Regexes: ', arrayParts)

for (var index = 0; index < arrayParts.length; index++) {
regexes.push(RegExp(arrayParts[index]))
}
}

return regexes
}

const inputPathVariables = ['PLUGIN_INPUT_PATH', 'PARAMETER_INPUT_PATH', 'INPUT_DIRECTORY']
const outputPathVariables = ['PLUGIN_OUTPUT_PATH', 'PARAMETER_OUTPUT_PATH', 'INPUT_OUTPUT']
const addSuffixVariables = ['PLUGIN_ADD_SUFFIX', 'PARAMETER_ADD_SUFFIX', 'INPUT_ADD_SUFFIX']
const inclusionsVariables = ['PLUGIN_INCLUSIONS', 'PARAMETER_INCLUSIONS', 'INPUT_INCLUSIONS']
const exclusionsVariables = ['PLUGIN_EXCLUSIONS', 'PARAMETER_EXCLUSIONS', 'INPUT_EXCLUSIONS']

const options = yargs(hideBin(process.argv))
.option('input-path', {
Expand All @@ -34,24 +50,21 @@ const options = yargs(hideBin(process.argv))
description: 'Multi-line string, each line of which contains a regex representing files to include/minify',
default: valueFromEnvVariables(inclusionsVariables)
})
.option('exclusions', {
type: 'string',
description: 'Multi-line string, each line of which contains a regex representing files to exclude from minification',
default: valueFromEnvVariables(exclusionsVariables)
})
.parse()

const inputPath = options.i
const outputPath = options.o
const addSuffix = options.a
var inclusions = []

if (options.inclusions) {
const inclusionParts = options.inclusions.replace('\\n', '\n').split(/[\r\n]+/)
getLogger().debug('Inclusions: ', inclusionParts)

for (var index = 0; index < inclusionParts.length; index++) {
inclusions.push(RegExp(inclusionParts[index]))
}
}
var inclusions = readRegexArray(options.inclusions)
var exclusions = readRegexArray(options.exclusions)

if (fs.existsSync(inputPath)) {
minifyFiles(inputPath, addSuffix, outputPath, inclusions)
minifyFiles(inputPath, addSuffix, outputPath, inclusions, exclusions)
} else {
getLogger().error('Input path ', inputPath, " doesn't exist")
}
}
7 changes: 4 additions & 3 deletions lib/minify-js.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,22 @@ export async function minifyFile (inputFile, addSuffix, outputPath) {
getLogger().info('Minified ', inputFile, ' > ', outputFileName)
}

export function minifyFiles (inputPath, addSuffix, outputPath, inclusions) {
export function minifyFiles (inputPath, addSuffix, outputPath, inclusions, exclusions) {
if (isDirectory(inputPath)) {
getLogger().debug('Input path ', inputPath, ' is a directory')

// Loop through all the files in the input path
fs.readdir(inputPath, function (_, files) {
files.forEach(function (file, index) {
minifyFiles(pathWithTrailingSeparator(inputPath) + file, addSuffix, outputPath, inclusions)
minifyFiles(pathWithTrailingSeparator(inputPath) + file, addSuffix, outputPath, inclusions, exclusions)
})
})
} 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))) {
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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,45 @@ class MinifyJsDockerSpec extends Specification {
'github'
]
}

@Unroll
void 'test minify - exclusions specified. ci: #ci'() {
given:
String outputHtmlFile = "${System.properties['user.dir']}/src/test/resources/output/index.min.html"
String outputJsFile = "${System.properties['user.dir']}/src/test/resources/output/scripts.min.js"
String outputCssFile = "${System.properties['user.dir']}/src/test/resources/output/main.min.css"

when:
def output = ProcessUtil.executeCommand(['docker', 'run', '--rm',
'-v', "${System.properties['user.dir']}:/work",
'-w=/work',
'-e', "${config[ci].inputPathParam}=/work/src/test/resources",
'-e', "${config[ci].outputPathParam}=/work/src/test/resources/output",
'-e', "${config[ci].envPrefix}EXCLUSIONS=.*static/.*",
imageName])

then:
output[0] == 0
output[1].contains('Minified /work/src/test/resources/scripts/scripts.js > /work/src/test/resources/output/scripts.min.js')
new File(outputJsFile).text ==
'$((function(){$("#templateAndModelForm *:input[type!=hidden]:first").focus()}));\n'

then: "html and css output files should not exist as their parent folder was specified in exclusions"
Files.notExists(Paths.get(outputHtmlFile))
Files.notExists(Paths.get(outputCssFile))
!output[1].contains('Minified /work/src/test/resources/static/index.html > /work/src/test/resources/output/index.min.html')
!output[1].contains('Minified /work/src/test/resources/static/main.css > /work/src/test/resources/output/main.min.css')

cleanup:
Files.deleteIfExists(Paths.get(outputHtmlFile))
Files.deleteIfExists(Paths.get(outputJsFile))
Files.deleteIfExists(Paths.get(outputCssFile))

where:
ci << [
'drone',
'vela',
'github'
]
}
}

0 comments on commit 245b2ba

Please sign in to comment.