2

I am looking for a "module definition" framework to run on the server when deploying my JavaScript App.

I am aware of require.js and other AMD frameworks. However, the necessity to implement define() and require() in my production code is non-satisfying for my purposes and since the App will be deployed in one file, I do not need the ability to inject scripts asynchronously.

Is there a build tool that can merge scripts without adding infrastructure code?

For clarification: The HTML page embedding the scripts is not relevant to my problem. The merging process should be done on script level.

main.js:

function a() {
    import("b");
    b();
}

b.js:

var b = function() {
    alert("b!");
};

Should simply become something like:

function a() {
    var b = function() {
        alert("b!");
    };
    b();
}
9
  • 4
    cat Commented Aug 6, 2013 at 13:40
  • Then why would you use require.js? Just load that 1 file full of JS and you're done, why would you need synchronous module definition? It makes no sense, synchronous is easy to understand and it's implemented by default. On the other hand, require.js has something called r.js, you should read about it. Commented Aug 6, 2013 at 13:42
  • 1
    @N.B. "why would you … why would you … it makes no sense" followed by "you should read about it". So which is it: does the question not make sense, or are you proposing an answer? Make your mind up ;) Commented Aug 6, 2013 at 13:45
  • 1
    @n-b I dont Like having one file with 10.000+ lines of JavaScript code. That is why I want to logically seperate my code in different files. Commented Aug 6, 2013 at 13:46
  • possible duplicate of require.js synchronous Commented Aug 6, 2013 at 13:47

1 Answer 1

0

So you want a tool to find and replace

<script src="SOURCE_URI" type="text/javascript"></script>

in your HTML file With

<script type="text/javascript">
CONTENT_OF_SOURCE_URI
</script>

Or maybe you want to collect all of the SOURCE_URI's and then combine them into one concatenated file and then run that file into a JavaScript compiler/optimizer/obfuscater like Closure and then output the inline script?

Most of the tools that exist will probably use require() and take a JS file as the input, so I suggest you write your own. A simple regex to parse the SRC= out of the script tags in your HTML will probably suffice.

edit https://developers.google.com/closure/compiler/docs/api-tutorial3

With the default compilation level of SIMPLE_OPTIMIZATIONS, the Closure Compiler makes JavaScript smaller by renaming local variables. There are symbols other than local variables that can be shortened, however, and there are ways to shrink code other than renaming symbols. Compilation with ADVANCED_OPTIMIZATIONS exploits the full range of code-shrinking possibilities.

Compare the outputs for SIMPLE_OPTIMIZATIONS and ADVANCED_OPTIMIZATIONS for the following code:

function unusedFunction(note) {
  alert(note['text']);
}

function displayNoteTitle(note) {
  alert(note['title']);
}

var flowerNote = {};
flowerNote['title'] = "Flowers";
displayNoteTitle(flowerNote);

Compilation with SIMPLE_OPTIMIZATIONS shortens the code to this:

function unusedFunction(a){alert(a.text)}function displayNoteTitle(a){alert(a.title)}var flowerNote={};flowerNote.title="Flowers";displayNoteTitle(flowerNote);

Compilation with ADVANCED_OPTIMIZATIONS shortens the code even further to this:

var a={};a.title="Flowers";alert(a.title);

Both of these scripts produce an alert reading "Flowers", but the second script is much smaller.

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

2 Comments

Thats not really what I want. Unfortunately it is not as simple as concatenating scripts. I'm going to update my question...
@mritz_p - as I said, if you concatenate all of the script file and run it through a JavaScript compiler/optimizer it will "inline" code when possible for you.

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.