nizarmah / auto-minify

Minifies JS and CSS files in GitHub workflows.
GNU General Public License v3.0
48 stars 18 forks source link

Refactor: Updated the Code to Handle Multiple Directories inside Each… #2

Closed nizarmah closed 4 years ago

nizarmah commented 4 years ago

… Other with Correct Output Forwarding

The needed files to be minified are now found using find in bash. Output is grepped to avoid minified files. Then we get the output path, name, file extension of the output file, and run the npm script on the files.

The new approach fixes the bug represented in issue #1. In addition, there is a commit referenced in that issue to a failed workflow for that commit

This refactor fixes issue #1, makes the code cleaner, and more modular

This was tested in auto-minify-test/issues#1

Thanks to @ekerstein for the issue ( :

repl-tusar-rath commented 4 years ago

Hi @nizarmah

Its actually creating separate css/js files for each css/js files inside dirrectory.

if I have

CSS/abc.css CSS/cde.css CSS/xyz.css CSS/newfolder/ghi.css CSS/newfolder2/mno.css

I want to merge and minify them all to CSS/abc.min.css

Is this possible to do for both CSS and JS.

Please suggest and help me.

nizarmah commented 4 years ago

Hello @repl-tusar-rath

That's how it is supposed to work. It works in a way that minifies the file filename.css and outputs it to filename.min.css.

So, currently it does not support multiple files minified into a single one.

You can follow two solutions:

  1. After auto-minify, use a bash function that collects all those .min.css files into a single file.
  2. I'll have to implement a specific solution for you, which I need to look into more closely and let you know if it will be possible or not.

Would it be alright if I let you know by Monday?

repl-tusar-rath commented 4 years ago

@nizarmah i will try with the bash function tomorrow.

Yes you can implement the solution on monday if possible.

Thanks for your reply.

nizarmah commented 4 years ago

So, after thinking about it, I decided that I won't be implementing the Minify multiple files into a single file. This isn't very important; in addition, it can raise a lot of issues when done in Javascript or in CSS files. Therefore, it would be a better solution if you could use a bash function in order to cat all the minified files into a single one. Then delete the minified files.

So, it would look like this.

cat *.min.css > somefile.css
rm *.min.css
repl-tusar-rath commented 4 years ago

@nizarmah Thanks a lot. It worked. Now my action looks like this.

# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
    - uses: actions/checkout@v2

    - name: Auto Minify
      uses: nizarmah/auto-minify@v1.4
      with:
        directory: 'assets/css/*'

    - name: Merge All min css to one min css file
      run:  cat assets/css/*.min.css > assets/cssminified.min.css

    - name: Delete all min css files
      run:  rm assets/css/*.min.css

    # Auto commits minified files to the repository
    # Ignore it if you don't want to commit the files to the repository 
    - name: Auto committing minified files
      uses: stefanzweifel/git-auto-commit-action@v3.0.0
      with:

        commit_message: "Github Action: Auto Minified CSS files"
        branch: ${{ github.ref }}

But I want the same for the JS files also.

with:
        directory: 'assets/css/*'
        directory: assets/js/*

If I am adding this, it's not working. Please suggest me the correct way to minify both css and js at the same time.

Thanks

nizarmah commented 4 years ago

@repl-tusar-rath to minify them at the same time, they need to be in the same directory. Otherwise you will need to rewrite the commands and actions for CSS with JS.

So just copy paste what you have and change it from .css to .js