I am new to RequireJS and maybe this is an already discussed issue but I could not find a clear answer or opinion about this.
I have an application working with RequireJS. It has a lot of JavaScript files loaded by Require as they are needed. Working as expected.
Looking to Fiddler inspector I could see that all the files were loaded when the application started. I believe that Require has made a deep traversal of all the references to JavaScript files and loaded them all in the beginning.
If this is the way it works I believe that it would be better to generate a single file, minify it, and load it using a <script>
html tag.
If I generate one single file with all scripts, and pre-load them with <script>
will RequireJS load them again?
Am I doing something wrong?
In a production environment what could be the better solution?
Thanks in advance.
To minify JavaScript, try UglifyJS. The Closure Compiler is also very effective. You can create a build process that uses these tools to minify and rename the development files and save them to a production directory.
Minifying strips out all comments, superfluous white space and shortens variable names. It thus reduces download time for your JavaScript files as they are (usually) a lot smaller in filesize. So, yes it does improve performance. The obfuscation shouldn't adversely affect performance.
Minification is the process of minimizing code and markup in your web pages and script files. It's one of the main methods used to reduce load times and bandwidth usage on websites. Minification dramatically improves site speed and accessibility, directly translating into a better user experience.
These characters include whitespaces, line breaks, comments, and block delimiters which are useful for us humans but unnecessary for machines. We minify the files of a website containing CSS, HTML, and Javascript code so your web browser can read them faster.
What you want is the RequireJS Optimizer.
The optimizer does the following (from the docs):
Here's one article on the topic for further reference.
Have a look also at r.js
, the command line tool which includes the optimizer. Using r.js
, you can generate an optimized build from an application file main.js
with just:
node r.js -o build.js
where build.js
is a requirejs config file containing your build profile (baseUrl
, paths
, etc.).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With