2

Currently I am working on a JavaScript application and I am trying to build it as modular as possible. What I'm planning to do is to get a folder structure like

js
|__ controllers
|__ services
|__ directives
index.html

I am using angularjs and i want to split all the controllers and services into seperate files.

I am a Java-Developper and I want to use maven to build this project and deploying it on a tomcat.

In production mode i want to a compressor/obfuscator to pack all files into one single application.js. Normally when I build my projects i only got one file for all controllers and one for all services and so on. So what I am doing is have one profile for dev and one for live and with filtering I append .min.js in my index.html to include all the files ( minimized on live, non-minimized on dev )

Finally my question:

What is the best way to do that with multiple files?

In development mode i want

<script type="text/javascript" src="MyFirstController.js">
<script type="text/javascript" src="MySecondController.js">
<script type="text/javascript" src="MyThirdController.js">
<script type="text/javascript" src="MyFirstService.js">
/* ... */

And in production mode i only want one file included in my index.html

<script type="text/javascript" src="myapp.js">

The first solution but clearly a not very pretty solution would be to have a multiline property in my maven file where i got all the script tags which i then replace with filtering.

I hope someone can tell me a good solution for my problem. Thanks in advance!

2
  • 1
    FYI, combining them like that may actually do more harm than good, especially when taking caching into account and when using HTTP/2 or SPDY where you can send all the files multiplexed over a single connection. Commented Dec 24, 2015 at 12:39
  • I thought about that too. Nur its possible that there are ~70 controllers in this project. And including 70 js files... I dont think that brings a great performance with it. Commented Dec 25, 2015 at 3:27

1 Answer 1

2

Take a look at the frontend-maven-plugin. This is useful if you want to bundle scripts with a maven artefact.

I haven't needed to use this because my javascript code has been an entirely separate deployable (decoupling and all that). So this is a good use case for gulp. Take a look at the gulpfile.js from hottowel.

First take a look at the build task and its dependencies:

/**
 * Build everything
 * This is separate so we can run tests on
 * optimize before handling image or fonts
 */
gulp.task('build', ['optimize', 'images', 'fonts', 'test-server'], function() {
    log('Building everything');

    var msg = {
        title: 'gulp build',
        subtitle: 'Deployed to the build folder',
        message: 'Running `gulp serve-build`'
    };
    del(config.temp);
    log(msg);
    notify(msg);
});

These should do most of the things you want. There are also environment specific gulp tasks:

gulp serve-dev
gulp build

There are a lot more in there so take a read through.

I've also worked on java + angular projects and it was handy for deployment and consistent with the java/maven approach to create artefacts for the javascript code that can be stored on nexus and fetched for deployment when needed.

The artefact for the javascript code is just a zip file which I create with the following gulp task (ignoring directories and files with ! in front of the directory name):

/**
 * Create a versioned artefact
 */

    gulp.task('generate-artifact', function() {
        var filename = getFilename();
        return gulp
            .src([
                './**/*.*',
                '!artifacts/',
                '!artifacts/**',
                '!build/',
                '!build/**',
                '!node_modules/',
                '!node_modules/**',
                '!bower_components/',
                '!bower_components/**'
                ])
            .pipe($.tar(filename + '.tar'))
            .pipe($.gzip())
            .pipe(gulp.dest('./artifacts/'));
    });

Then to deploy to nexus using the nexus-deployer plugin:

/**
 * Deploy release to nexus
 */ 
gulp.task('deploy-release', ['clean-pomdir'], function(cb) {
    var deployer = require('nexus-deployer');
    var filename = getFilename();
    var pkg = require('./package.json');

    log('Retrieving artifact ' + filename + '.tar.gz' + 'from artifacts dir');
    log('Deploying version ' + pkg.version + ' of ' + pkg.name);

    var release = {
        groupId: 'my.group.id',
        artifactId: 'my-artefact',
        version: pkg.version,
        packaging: 'tar.gz',
        auth: {
            username: args.username,
            password: args.password
        },
        pomDir: config.pomDirReleases,
        url: 'http://path-to-nexus.repo',
        artifact: './artifacts/' + filename + '.tar.gz',
        noproxy: '',
        cwd: ''
    };

    deployer.deploy(release, cb);
});

There are a few custom methods and config things in there too: the great yargs module to pass in username and password as command line arguments; convenience method in gulpfile.js to get the filename:

function getFilename() {
    var pkg = require('./package.json');
    var filename = pkg.name + '_' + pkg.version;
    return filename;
}
Sign up to request clarification or add additional context in comments.

Comments

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.