Compile multiple JS keeping the original names

Asked

Viewed 65 times

2

Guys, I have a folder inside the Resources called js where you have several JS files and subfolders with other Jss. How do I set up Laravel Mix to compile all the contents of the JS folder including the subfolders for the public/js folder keeping the original names and folders and if possible applying 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/teste2.js

Once compiled 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/teste2.js

Thank you

  • Have you ever tried something like this? mix.js('Resources/Assets/js/*. js', 'public/js');

  • @Diegovieira o mix.js does not accept jokers in his sentence.

  • Have you tried the copyDirectory?

  • @Valdeirpsr then copyDirectory it does not compile the JS, it apanas copies like this to the final directory. I need to compile to run on all browsers.

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);
});

Upshot:

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.