Compile multiple JS retaining the original names

2

Personal I have a folder inside the resources called js where it has several JS files and subfolders with other JSs. How do I set up Laravel Mix for it to compile all the contents of the JS folder including the subfolders into the public / js folder while retaining the original names and folders and if possible applying the versioning to the files. Ex.

  

resources / js / teste1.js
  resources / js / teste2.js
  resources / js / teste3.js
  resources / js / temp / teste1.js
  resources / js / temp / test2.js

After compiling it has to look like this:

  

public / js / teste1.js
  public / js / teste2.js
  public / js / teste3.js
  public / js / temp / teste1.js
  public / js / temp / test2.js

Thank you

    
asked by anonymous 27.03.2018 / 03:58

1 answer

2
var fs = require('fs');

// retorna todos os arquivos dentro do diretório dir
let getFiles = function (dir) {
    return fs.readdirSync(dir).reduce((prev, file) => {
        var fullPath = '${dir}/${file}';
        if (fs.statSync(fullPath).isFile()) {
            // apenas terminados em .js
            if (file.indexOf('.js') === file.length - 3) {
                prev.push(file);
            }
        }   
        else {
            // folder, recurse e retorna subpath junto com arquivo
            prev = prev.concat(getFiles(fullPath).map((f) => file + '/' + f))
        }
        return prev;
    }, []);
};

getFiles('resources/assets/js/').forEach(function(file) {
    mix.js('resources/assets/js/' + file, 'public/js/' + file);
});

Result:

DONE  Compiled successfully in 725ms                                                                                           
13:55:47

              Asset     Size  Chunks                    Chunk Names
   /js/bootstrap.js   275 kB       0  [emitted]  [big]  /js/bootstrap
         /js/app.js  3.06 kB       1  [emitted]         /js/app
/js/image-resize.min.js  29.9 kB   2  [emitted]     /js/image-resize.min
   /js/temp/test.js  2.95 kB       3  [emitted]     /js/temp/test
   /js/page-html.js  5.79 kB       4  [emitted]     /js/page-html
 /js/calculadora.js  5.85 kB       5  [emitted]         
/js/calculadora
       /css/app.css  7.92 kB       1  [emitted]     /js/ap


$ ls public/js/
app.js  bootstrap.js  calculadora.js  image-resize.min.js  page-html.js  temp
$ ls public/js/temp/
test.js

If your js are not in resources/assets/js adapt the calls to the desired location.

    
27.03.2018 / 18:59