32

After grunt building my AngularJS app to my dist directory, I would like to test it out with grunt server. Problem is that grunt server just serves up all the code in my app/ directory. Additionally, keep in mind that I created my app with yo angular.

Here is the server task code in my Gruntfile.js

grunt.registerTask('server', [
    'clean:server',
    'coffee:dist',
    'compass:server',
    'livereload-start',
    'connect:livereload',
    'open',
    'watch'
  ]);

Is there a way to make grunt server only serve up the built code in my dist/ directory?

4 Answers4

116

The very short answer is

grunt serve:dist

That works with my yeoman-generated Gruntfile.js which contains:

  grunt.registerTask('serve', function (target) {
    if (target === 'dist') {
      return grunt.task.run(['build', 'connect:dist:keepalive']);
    }

    grunt.task.run([
      'clean:server',
      'bower-install',
      'concurrent:server',
      'autoprefixer',
      'connect:livereload',
      'watch'
    ]);
  });
Jarl
  • 2,831
  • 4
  • 24
  • 31
30

Whether or not you could do it isn't important. What is important is that you should not use grunt server for this. The server task in grunt as you can see does many things that you don't need in a dist folder, as well as the fact that they will all be hard coded for your app folder.

If you just want to try out the result of the dist folder, what you should do is just cd into the dist directory and then start a simple HTTP server like the one that comes with python.

python -m SimpleHTTPServer

That should start a small server for you to try the dist directory out.

Danger14
  • 760
  • 2
  • 12
  • 33
DerekR
  • 3,886
  • 2
  • 23
  • 24
  • 1
    Thats what I do. I also sometimes have the dist directory symlinked (or actually pointed at) an apache controlled directory so i can serve it up as a vhost locally thats close to my actual production server. Another thing you could/should do related to this is run your dist folder through your testing to make sure the built files truly are the same (only minified) as the source files. – Eddie Monge Jr Aug 20 '13 at 22:57
  • @DerekR Thanks for that tip. I will try that out. Thanks yes, it works :) –  Aug 21 '13 at 01:49
  • @Eddie Monge Jr How do I run my dist folder through my testing? Not sure what 'my testing' means. Curious to learn though. –  Aug 21 '13 at 01:50
  • 1
    Python3's command is more memorable: `python3 -m http.server` – Bengt Jul 18 '14 at 18:41
3

Although other have not advised doing this, I believe this is what you are looking for. I do something like this in my server.js file when using express.

if (process.env.NODE_ENV == 'production') {
    app.use(express.static('/dist'));
}else{
    app.use(express.static('/app'));       
}

Hope this helps!

Mackenzie Browne
  • 443
  • 3
  • 10
3

The grunt serve:dist answer is the correct one, but the process is quite annoying because you need to compile your whole project into production mode every time you call grunt serve:dist.

What I did is I modified the Gruntfile.js in the serve task, removing the build task from the parameter sent to run. You need to manually build first with grunt or grunt build and then call grunt serve:dist and manually open the URL.

Ramiro Araujo
  • 465
  • 3
  • 11