4

I have a complex JS-project that is written in AMD form. It uses RequireJS.

My system should be able to include it's functionality to different external sites. I want to compile it to a single js-file and include it like this:

<script type="text/javascript" src="http://mysystem.com/core-build.js"></script>

I don't know if an external site has requirejs or not, that's why it seems that requirejs itself should be included into core-build.js. Compiled file should be completely independ on the environment.

From the other side, it should not redefine an external site environment. My system uses jquery, knockout, and theese libraries should be defined locally for my system purposes only. They should not affect the site's global namespace.

So my question is how to optimize scripts to one file, include requirejs itself in it and wrap everything into a local namespace?

1 Answer 1

3

You could use the Grunt concat task:

concat: {
    dist: {
        src: [
            "www/assets/js/libs/require.js",
            "dist/debug/app.js"
        ],

        dest: "www/app_build.js",

        separator: ";"
    }
}

You can see an example app setup on Backbone Boilerplate (even if you're not using Backbone).

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

2 Comments

Simon, thanks for Your reply. I use r.js, and it seems r.js and grunt have similar functionality. I'll take a look to r.js-concat option and write down config for it. Also r.js has namespace option, and that solves the thing about affections on the global environment.
Grunt wrap the r.js build into a task too, checkout: grunt-contrib-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.