I’m beginning to evaluate javascript module tools like RequireJS for javascript modularization. This seems useful, especially during development, so I don’t need to recompile all of the js files into mylib-<version>.js whenever I change one of the dependent files.
My app is distributed with both html and javascript files, and in production, I would like to use the compiled version of the javascript file.
So in development, my html file might look something like
<html>
<head>
<script data-main="scripts/main" src="scripts/require.js"></script>
</head>
</html>
But in production, I would expect it to look more like
<html>
<head>
<script src="mylib-1.0.js"></script>
</head>
</html>
I wouldn’t think it production that there should be any need to reference requirejs if I am distributing a compiled file.
Is there a way to do this without having to manually change my html files before I distribute the app?
RequireJs has an optimization tool, which can help you to minify and concatenate your modules. It has a lot of options, and can be difficult to use, but it gets easier with a build tool like GruntJs or (especially) Yeoman, which uses GruntJs to build.
In both you can use the
rjstask (which optimizes modules), but again Yeoman is a bit easier since it has generators which will configure it already for you:In the
index.htmlyou just use a comment line to specify which js files should be minified/concatenated to which output file:In the example above, the modules will be concatenated to ONE file, named
amd-app.js.Edit:
This will be done by executing
gruntfrom the command line. This will start a lot of useful tasks, which will build the project in adistfolder, but again this is highly adaptable.The resulting
index.htmlfile (indist) has only (if you want) one javascript file:My advice: use Yeoman to make life easier (at least for handling minification/concatenation).