Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: CoffeeScript Source Map Support #39

Closed
vjpr opened this issue Sep 28, 2012 · 6 comments
Closed

Feature: CoffeeScript Source Map Support #39

vjpr opened this issue Sep 28, 2012 · 6 comments

Comments

@vjpr
Copy link
Contributor

vjpr commented Sep 28, 2012

http://ryanflorence.com/2012/coffeescript-source-maps/

Only a few simple changes needed:

  1. Add a new PostProcessor for application/javascript
# stdlib
path = require "path"

# internal
Template = require "mincer/lib/mincer/template"

#//////////////////////////////////////////////////////////////////////////////

# Class constructor
SourceMapComments = module.exports = SourceMapComments = ->
  Template.apply this, arguments

require("util").inherits SourceMapComments, Template

# Process data
SourceMapComments::evaluate = (context, locals, callback) ->
  pathname = path.join(context.rootPath, context.logicalPath)
  @data = "\n\n//@ sourceMappingURL=#{pathname}.map\n\n" + @data
  callback null, @data
@env.registerPostProcessor 'application/javascript', require('./SourceMapComments')
  1. Serve .map files compiled with CoffeeScriptRedux. I'm digging into this now to see how it integrates with Mincer.

Generating source maps involves the following code:

csAst = coffee.parse input
jsAst = coffee.compile csAst
sourceMap = coffee.sourceMap jsAst

Should be as easy as using an engine to handle the .map extension.

@vjpr vjpr closed this as completed Sep 28, 2012
@vjpr vjpr reopened this Sep 28, 2012
@vjpr
Copy link
Contributor Author

vjpr commented Sep 28, 2012

Not as straightforward as I first thought.

Below is a very rough, proof-of-concept. Manages to show CoffeeScript source in browser - but is unusably slow, incorrect line number mapping due to PostProcessors, and very badly integrated.

CoffeeScriptRedux has a ton of bugs too.

# mincer/server.js:310

  var path = require('path')

  // If asset is a `.coffee` file we return the coffee file.
  if (path.extname(pathname) === '.coffee') {

    //var pathname = pathname.slice(0, -7)
    var asset = this.environment.findAsset(pathname)
    var source = require('fs').readFileSync(asset.pathname, 'utf8')
    return res.end(new Buffer(source))

  // If asset is a `.map` file we compile the source map.
  } else if (path.extname(pathname) === '.map') {

    var pathname = pathname.slice(0, -4)
    var asset = this.environment.findAsset(pathname)
    console.log (asset.pathname)
    if (path.extname(asset.pathname) === '.coffee') {
      var input = require('fs').readFileSync(asset.pathname, 'utf8')
      var coffee = require("/Users/Vaughan/dev/CoffeeScriptRedux")
      var csAst = coffee.parse(input)
      var jsAst = coffee.compile(csAst)
      var sourceMap = coffee.sourceMap(jsAst, path.basename(asset.logicalPath) + '.coffee')
      return res.end(new Buffer(sourceMap))
    }

  }

@vjpr
Copy link
Contributor Author

vjpr commented Sep 28, 2012

Just found the branch being worked on for Sprockets: sstephenson/sprockets#311. Seems to be aimed at source maps for the concatenated Javascript use case, not the Javascript to CoffeeScript use case.

Using the X-SourceMap header instead of comments is probably a better approach most of the time.

Also, using a query string like ?source=true looks like a good approach to get .coffee files.

@puzrin
Copy link
Contributor

puzrin commented Sep 28, 2012

Anyway, we should wait until UglifyJS v2 released https://github.com/mishoo/UglifyJS2

@rubemz
Copy link

rubemz commented Oct 8, 2012

I believe it is already released https://github.com/mishoo/UglifyJS2/tags

@jpillora
Copy link

jpillora commented Nov 4, 2012

+1

vjpr added a commit to vjpr/mincer that referenced this issue Apr 19, 2013
If you have a file `foo.coffee` or `foo.js.coffee`, when you load `foo.js`, the `X-SourceMap` header is added to the response with value `foo.js.map`.
Inside this source map is a reference to `foo.coffee`. When the browser makes a request to `foo.coffee` it will return the uncompiled CoffeeScript source.
This allows debugging CoffeeScript source in a browser and also works with IntelliJ Remote Javascript debugger too.

Includes tests.

See Issue js-kyle#39.
@puzrin
Copy link
Contributor

puzrin commented Dec 22, 2013

This ticket is closed as not enougth generic, in flavour of #116 .

@puzrin puzrin closed this as completed Dec 22, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants