ericclemmons / grunt-express-server

Grunt task for running an Express Server that works great with LiveReload + Watch/Regarde
MIT License
248 stars 64 forks source link

using traceur as preprocessor #100

Closed mohamnag closed 8 years ago

mohamnag commented 8 years ago

I have been using this grunt plugin and just tried to use https://www.npmjs.com/package/traceur as a preprocessor to enable await/async style but no success until now.

I tried using traceur like coffee:

        express: {
            options: {
                opts: ['node_modules/traceur/bin/traceur']
            },
            dev: {
                options: {
                    script: 'bin/www',
                    debug: true
                }
            }
        },

but interestingly the server just starts and stops:

Running "express:dev" (express) task
Starting background Express server
Debugger listening on port 5858
Stopping Express server

I also tried a shim:

        express: {
            options: {
            },
            dev: {
                options: {
                    script: 'bin/shim',
                    debug: true
                }
            }
        },

where shim is:

#!/usr/bin/env node

require('traceur');
require('./www');

but this seems not to work at all, as I get syntax errors:

Running "express:dev" (express) task
Starting background Express server
Debugger listening on port 5858
/Users/mohamnag/Projects/pardpay/routes/users/sign-up.js:75
        let info = await db.createDatabase('mydb');
                         ^^

SyntaxError: Unexpected identifier
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:404:25)
    at Object.Module._extensions..js (module.js:432:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at Object.<anonymous> (/Users/mohamnag/Projects/pardpay/app.js:14:14)
    at Module._compile (module.js:425:26)
    at Object.Module._extensions..js (module.js:432:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at Object.<anonymous> (/Users/mohamnag/Projects/pardpay/bin/www:7:11)
    at Module._compile (module.js:425:26)
Stopping Express server

is it possible to use traceur here?

mohamnag commented 8 years ago

I finally succeed using ES7 and wanted to let others know too, maybe this can even go to docs here. First step, installing dependencies (I gave up on traceur and used babel):

npm i babel-cli
npm i babel-preset-es2015
npm i babel-preset-stage-0
npm i babel-register

then creating a shim bin/es7-shim script (sets up the necessary babel translator and then calls express 4's www script created by express generator):

#!/usr/bin/env node

require('babel-polyfill');
require("babel-register")({
    presets: [
        "es2015",
        "stage-0"
    ]
});
require('./www');

and finally tuning express-server grunt config:

        express: {
            options: {
            },
            dev: {
                options: {
                    script: 'bin/es7-shim',
                    debug: true
                }
            }
        },

and thats it!

ericclemmons commented 8 years ago

Babel is definitely the way to go. Traceur has major performance penalties & little community behind it by comparison.

You should be able to use babel-node path/to/my/script.js alongside a .babelrc file with your presets & settings without creating a shim script.

mohamnag commented 8 years ago

should I set babel-node path/to/my/script.js as cmd in options?

ericclemmons commented 8 years ago

Correct!

mohamnag commented 8 years ago

after switching to following settings

        express: {
            options: {
                // Override defaults here
                cmd: "node_modules/babel-cli/bin/babel-node.js"
            },
            dev: {
                options: {
                    script: 'bin/www',
                    debug: true
                }
            }
        }

now the watch task does not work as expected. after detecting changes the last thing is logs is:

Stopping Express server
Starting background Express server
Error: listen EADDRINUSE :::5858
    at Object.exports._errnoException (util.js:856:11)
    at exports._exceptionWithHostPort (util.js:879:20)
    at Agent.Server._listen2 (net.js:1234:14)
    at listen (net.js:1270:10)
    at Agent.Server.listen (net.js:1366:5)
    at Object.start (_debug_agent.js:21:9)
    at startup (node.js:72:9)
    at node.js:974:3

I suppose stopping server does not work.

mohamnag commented 8 years ago

now looking more closely, my server runs on 3000 and not 5858, not sure what the reason for message is.