Skip to content

Commit bfc8ae6

Browse files
committed
feat: Added ability to target only specific files within the input path
1 parent 3d1fcca commit bfc8ae6

5 files changed

Lines changed: 87 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Changelog
22

33
## [Unreleased]
4+
### Added
5+
- [#5](https://github.com/devatherock/minify-js/issues/5): Ability to minify only specific files within the input directory
6+
7+
### Changed
8+
- Rewrote the shell script in Node JS
49

510
## [2.0.0] - 2023-06-04
611
### Changed

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,22 @@ The following parameters can be set to configure the action.
1919

2020
* **add_suffix** - Indicates if the output files should have the suffix `.min` added after the name. Default is true
2121

22+
* **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
23+
2224
```yaml
2325
jobs:
2426
build:
2527
runs-on: ubuntu-latest # Docker-based github actions have to run on a linux environment
2628
steps:
2729
- name: HTML/CSS/JS Minifier
28-
uses: docker://devatherock/minify-js:2.0.0
30+
uses: docker://devatherock/minify-js:3.0.0
2931
with:
3032
directory: 'src' # Optional
3133
output: 'minify/src' # Optional
3234
add_suffix: false # Optional
35+
inclusions: |- # Optional
36+
.*assets.*
37+
.*static/index.html
3338
```
3439
3540
### Docker
@@ -41,7 +46,8 @@ docker run --rm \
4146
-e PARAMETER_INPUT_PATH=/work/src \
4247
-e PARAMETER_OUTPUT_PATH=/work/minify/src \
4348
-e PARAMETER_ADD_SUFFIX=false \
44-
devatherock/minify-js:2.0.0
49+
-e PARAMETER_INCLUSIONS=".*assets.*\n.*static/index.html" \
50+
devatherock/minify-js:3.0.0
4551
```
4652

4753
### vela
@@ -55,17 +61,22 @@ The following parameters can be set to configure the plugin.
5561

5662
* **add_suffix** - Indicates if the output files should have the suffix `.min` added after the name. Default is true
5763

64+
* **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
65+
5866
```yaml
5967
steps:
6068
- name: minify_js
6169
ruleset:
6270
branch: master
6371
event: push
64-
image: devatherock/minify-js:2.0.0
72+
image: devatherock/minify-js:3.0.0
6573
parameters:
6674
input_path: src
6775
output_path: minify/src
6876
add_suffix: false
77+
inclusions: |-
78+
.*assets.*
79+
.*static/index.html
6980
```
7081
7182
### CircleCI
@@ -75,12 +86,13 @@ version: 2.1
7586
jobs:
7687
minify_js:
7788
docker:
78-
- image: devatherock/minify-js:2.0.0
89+
- image: devatherock/minify-js:3.0.0
7990
working_directory: ~/my-repo
8091
environment:
8192
PARAMETER_INPUT_PATH: src
8293
PARAMETER_OUTPUT_PATH: minify/src
8394
PARAMETER_ADD_SUFFIX: false
95+
PARAMETER_INCLUSIONS: '.*assets.*\n.*static/index.html'
8496
steps:
8597
- checkout
8698
- run: sh /entrypoint.sh

bin/cli.mjs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { stringToBoolean, valueFromEnvVariables, getLogger } from '../lib/utils.
88
const inputPathVariables = ['PLUGIN_INPUT_PATH', 'PARAMETER_INPUT_PATH', 'INPUT_DIRECTORY']
99
const outputPathVariables = ['PLUGIN_OUTPUT_PATH', 'PARAMETER_OUTPUT_PATH', 'INPUT_OUTPUT']
1010
const addSuffixVariables = ['PLUGIN_ADD_SUFFIX', 'PARAMETER_ADD_SUFFIX', 'INPUT_ADD_SUFFIX']
11+
const inclusionsVariables = ['PLUGIN_INCLUSIONS', 'PARAMETER_INCLUSIONS', 'INPUT_INCLUSIONS']
1112

1213
const options = yargs(hideBin(process.argv))
1314
.option('input-path', {
@@ -28,14 +29,29 @@ const options = yargs(hideBin(process.argv))
2829
description: 'Indicates if the output files should have the suffix `.min` added after the name',
2930
default: valueFromEnvVariables(addSuffixVariables) ? stringToBoolean(valueFromEnvVariables(addSuffixVariables)) : true
3031
})
32+
.option('inclusions', {
33+
type: 'string',
34+
description: 'Multi-line string, each line of which contains a regex representing files to include/minify',
35+
default: valueFromEnvVariables(inclusionsVariables)
36+
})
3137
.parse()
3238

3339
const inputPath = options.i
3440
const outputPath = options.o
3541
const addSuffix = options.a
42+
var inclusions = []
43+
44+
if (options.inclusions) {
45+
const inclusionParts = options.inclusions.replace('\\n', '\n').split(/[\r\n]+/)
46+
getLogger().debug('Inclusions: ', inclusionParts)
47+
48+
for (var index = 0; index < inclusionParts.length; index++) {
49+
inclusions.push(RegExp(inclusionParts[index]))
50+
}
51+
}
3652

3753
if (fs.existsSync(inputPath)) {
38-
minifyFiles(inputPath, addSuffix, outputPath)
54+
minifyFiles(inputPath, addSuffix, outputPath, inclusions)
3955
} else {
4056
getLogger().error('Input path ', inputPath, " doesn't exist")
4157
}

lib/minify-js.mjs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,25 @@ export async function minifyFile (inputFile, addSuffix, outputPath) {
4242
getLogger().info('Minified ', inputFile, ' > ', outputFileName)
4343
}
4444

45-
export function minifyFiles (inputPath, addSuffix, outputPath) {
45+
export function minifyFiles (inputPath, addSuffix, outputPath, inclusions) {
4646
if (isDirectory(inputPath)) {
4747
getLogger().debug('Input path ', inputPath, ' is a directory')
4848

4949
// Loop through all the files in the input path
5050
fs.readdir(inputPath, function (_, files) {
5151
files.forEach(function (file, index) {
52-
minifyFiles(pathWithTrailingSeparator(inputPath) + file, addSuffix, outputPath)
52+
minifyFiles(pathWithTrailingSeparator(inputPath) + file, addSuffix, outputPath, inclusions)
5353
})
5454
})
5555
} else {
5656
getLogger().debug('Input path ', inputPath, ' is a file')
5757

5858
if (supportedExtensions.includes(path.extname(inputPath))) {
59-
minifyFile(inputPath, addSuffix, outputPath)
59+
if (inclusions.length === 0 || inclusions.some((regex) => regex.test(inputPath))) {
60+
minifyFile(inputPath, addSuffix, outputPath)
61+
} else {
62+
getLogger().debug('Skipping file ', inputPath, " that doesn't match any inclusions")
63+
}
6064
} else {
6165
getLogger().debug('Skipping file ', inputPath, ' with unsupported extension')
6266
}

src/test/groovy/io/github/devatherock/minify/MinifyJsDockerSpec.groovy

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,46 @@ class MinifyJsDockerSpec extends Specification {
201201
'github'
202202
]
203203
}
204+
205+
@Unroll
206+
void 'test minify - inclusions specified. ci: #ci'() {
207+
given:
208+
String outputHtmlFile = "${System.properties['user.dir']}/src/test/resources/output/index.min.html"
209+
String outputJsFile = "${System.properties['user.dir']}/src/test/resources/output/scripts.min.js"
210+
String outputCssFile = "${System.properties['user.dir']}/src/test/resources/output/main.min.css"
211+
212+
when:
213+
def output = ProcessUtil.executeCommand(['docker', 'run', '--rm',
214+
'-v', "${System.properties['user.dir']}:/work",
215+
'-w=/work',
216+
'-e', "${config[ci].inputPathParam}=/work/src/test/resources",
217+
'-e', "${config[ci].outputPathParam}=/work/src/test/resources/output",
218+
'-e', "${config[ci].envPrefix}INCLUSIONS=.*scripts/scripts.js.*\n.*static/index.*",
219+
imageName])
220+
221+
then:
222+
output[0] == 0
223+
output[1].contains('Minified /work/src/test/resources/static/index.html > /work/src/test/resources/output/index.min.html')
224+
output[1].contains('Minified /work/src/test/resources/scripts/scripts.js > /work/src/test/resources/output/scripts.min.js')
225+
new File(outputHtmlFile).text ==
226+
'<!doctype html><title>Test title</title><div id=layout><div id=main><div class=header><h1>Test body</h1></div></div></div>\n'
227+
new File(outputJsFile).text ==
228+
'$((function(){$("#templateAndModelForm *:input[type!=hidden]:first").focus()}));\n'
229+
230+
then: "css output file should not exist as it wasn't specified in the inclusions"
231+
Files.notExists(Paths.get(outputCssFile))
232+
!output[1].contains('Minified /work/src/test/resources/static/main.css > /work/src/test/resources/output/main.min.css')
233+
234+
cleanup:
235+
Files.deleteIfExists(Paths.get(outputHtmlFile))
236+
Files.deleteIfExists(Paths.get(outputJsFile))
237+
Files.deleteIfExists(Paths.get(outputCssFile))
238+
239+
where:
240+
ci << [
241+
'drone',
242+
'vela',
243+
'github'
244+
]
245+
}
204246
}

0 commit comments

Comments
 (0)