How to optimize all javascript files in a directory with requirejs

I have a folder structure like this:

. β”œβ”€β”€ autocomplete β”‚  β”œβ”€β”€ core.js β”‚  β”œβ”€β”€ search.js β”‚  └── user.js β”œβ”€β”€ build.js β”œβ”€β”€ collapsible_lists.js β”œβ”€β”€ griffgrabber β”‚  β”œβ”€β”€ canvasobject.js β”‚  β”œβ”€β”€ cargame.js β”‚  β”œβ”€β”€ car.js β”‚  β”œβ”€β”€ griffDrawer.js β”‚  β”œβ”€β”€ keylistener.js β”‚  β”œβ”€β”€ run.js β”‚  └── victim.js β”œβ”€β”€ main.js β”œβ”€β”€ newsfeed.js β”œβ”€β”€ require.js β”œβ”€β”€ shortcut.js └── sidebar.js 3 directories, 20 files 

main.js is a boot file. This file requires several files, but not all of them. The remaining files are included in

 <script> require(['shortcut'], function(shortcut){ // ... }) </script> 

in some html files.

This is my build.js file:

 { baseUrl: ".", name: "main", out: "main-built.js", } 

But it only includes files that are required by main.js. Is it possible to optimize all javascript files in one go?

+4
source share
3 answers

(to expand @ Ryan Lynch's offer):

Use the include parameter, as per the documentation:

You can always explicitly add modules that are not found through static optimizer analysis using the include parameter.

( http://requirejs.org/docs/optimization.html )

 { baseUrl: ".", // ? appDir: ".", // ? dir: "output/", modules: [ { name: "main", include: [ "shortcut" ] } ] } 

A more detailed example in a great example.build.js (I actually find it more useful than the documentation page)


(Sorry, I did not manage to replicate and check correctly to make sure that the path values ​​are correct, I will try later and update my answer)

+1
source

Try to include an array of modules in your modules, therefore:

 { baseUrl: ".", out: "main-built.js", modules: [ { name: "main" } ] } 

According to the documentation:

In the module array, specify the names of the modules you want to optimize in the "main" example. "main" will map to appdirectory / scripts / main.js in your project. The build system will then track the dependencies for main.js and inject them into appdirectory-build / scripts / main.js.

0
source

Another less orthodox way of achieving this would be to add this "shortcut" module depending on any of the β€œvisible” modules detected by the r.js scan (ie, β€œmain.js”). Thus, the entire dependency branch starting with the "shortcut" will be included in the output.

0
source

Source: https://habr.com/ru/post/1479967/


All Articles