0

My program is using RequireJS to manage dependencies in javascript, and it works fine - except that I have an issue where the only way to do the minification is to use the r.js optimization system to do minify and combine when I compile the application.

This does work; I have it functioning how I need it to - but it is very wasteful because it puts all of the scripts in a single document. This seems to defeat the entire purpose of the module loading - and it makes the site a great deal slower.

Is there a way - other than manually minifying each file, every time I change them, to have this optimizer keep files separated so that the module loading can still only pull the files it needs?

I am doing the minification/combination using nodejs with a build event in Visual Studio, similar to this;

build.bat

node minify.js -o build.json

build.json

{
    "baseUrl" : "../../home", 
    "name": "../lib/app/config", 
    "include": [
          // each file gets listed here
    ], 
    "exclude": [], 
    "optimize": "none", 
    "out": "program.js", 
    "insertRequire": [
        "../lib/app/config"
      ]
}

config.js

require.config({
    baseUrl: '/app_content/scripts',
});

Visual Studio Build Event

node "$(ProjectDir)scripts\lib\app\minify.js" -o "$(ProjectDir)scripts\lib\app\build.json"

So this makes a huge program.js file that has everything - with explicitly named modules. It runs and functions, but ... again, that kind of defeats the purpose, right?

1 Answer 1

2

it makes the site a great deal slower

That can't be true, concatenating into a single file reduces the amount of necessary HTTP requests for a page and should improve performance quite a bit. If you just want to minify your files, use a tool that does just that - e.g. UglifyJS.

Sign up to request clarification or add additional context in comments.

3 Comments

It makes the site slower because it loads the entire javascript system, which is considerably large in filesize. I thought one of the goals of requirejs is to only load the needed files on demand.
There is no on-demand loading in RequireJS (unless you wrap your require() calls in if clauses) and that's not one of its goals. Network connections are very slow compared to the JavaScript interpreter itself, it's almost always faster to serve a few bytes more up front than to do an additional HTTP request when something is missing.
Hey, I had honestly forgotten about this question. A lot has happened since I asked it and yes, you are correct, the site slowdown was not the fault of requirejs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.