From a013c890588f77e4ad960e36890c983eb284e3ee Mon Sep 17 00:00:00 2001 From: Jeff Escalante Date: Tue, 11 Nov 2014 12:50:52 -0500 Subject: [PATCH] add actual sourcemap support and tests for any compiler that supports them --- docs/coco.md | 4 ++++ docs/coffeescript.md | 4 ++++ docs/csso.md | 4 ++++ docs/dogescript.md | 4 ++++ docs/handlebars.md | 4 ++++ docs/jade.md | 4 ++++ docs/less.md | 4 ++++ docs/livescript.md | 4 ++++ docs/minify-css.md | 4 ++++ docs/minify-js.md | 4 ++++ docs/myth.md | 4 ++++ docs/scss.md | 4 ++++ docs/stylus.md | 4 ++++ lib/adapters/coffee-script.coffee | 5 ++++- lib/adapters/less.coffee | 4 +++- lib/adapters/minify-js.coffee | 10 +++++++--- lib/adapters/myth.coffee | 4 +++- test/fixtures/myth/expected/import.css | 10 ++++++++++ test/fixtures/myth/import.myth | 5 +++++ test/fixtures/myth/imported.myth | 3 +++ test/test.coffee | 25 +++++++++++++++++++++++++ 21 files changed, 112 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/myth/expected/import.css create mode 100644 test/fixtures/myth/import.myth create mode 100644 test/fixtures/myth/imported.myth diff --git a/docs/coco.md b/docs/coco.md index 0e61d55..b117974 100644 --- a/docs/coco.md +++ b/docs/coco.md @@ -4,6 +4,10 @@ Coco is a fork of CoffeeScript that tries to eliminate some pain points. It has ## Supported Methods - render +## Source Maps + +Coco does not support source maps, unfortunately. If they do add support we would be happy to add it to accord. + ## Additional Options ### Bare - key: `bare` diff --git a/docs/coffeescript.md b/docs/coffeescript.md index f984a38..3097f60 100644 --- a/docs/coffeescript.md +++ b/docs/coffeescript.md @@ -4,6 +4,10 @@ The CoffeeScript adapter API is almost exactly the same as the CoffeeScript JS A ## Supported Methods - render +## Source Maps + +Source maps are supported by the coffeescript adapter. Just pass in `sourceMap: true` as an option and you will get back both `sourcemap` and `v3sourcemap` (this is what coffeescript passes back by default), on the response object. + ## Additional Options ### Bare - key: `bare` diff --git a/docs/csso.md b/docs/csso.md index 4f3888b..6511b7a 100644 --- a/docs/csso.md +++ b/docs/csso.md @@ -4,6 +4,10 @@ CSSO is a great tool for optimizing CSS. It's public API is very simple, sometim ## Supported Methods - render +## Source Maps + +CSSO does not have support for source maps, unfortunately. There is an open issue for it [here](https://github.com/css/csso/issues/173). If they do add support we'd be happy to integrate it with accord. + ## Additional Options ### No Restructure - key: `no_restructure` diff --git a/docs/dogescript.md b/docs/dogescript.md index 86f0ea6..23c5aa5 100644 --- a/docs/dogescript.md +++ b/docs/dogescript.md @@ -10,3 +10,7 @@ dogescript.render('wow', { beauty: true, trueDoge: true }) ## Supported Methods - render + +## Source Maps + +Dogescript does not support sourcemaps. Honestly, I doubt it ever will. If you are writing dogescript, you probably don't need sourcemaps anyway because you are too boss for that kind of stuff. diff --git a/docs/handlebars.md b/docs/handlebars.md index ff25102..080d460 100644 --- a/docs/handlebars.md +++ b/docs/handlebars.md @@ -18,3 +18,7 @@ hbs.render("hello there {{ name }}", { - compile - compileClient - clientHelpers + +## Source Maps + +Handlebars [does support source maps](https://github.com/wycats/handlebars.js/pull/902), but the functionality is brand new (at the time of writing), and is still undocumented. We will add this feature to accord once it's documented in handlebars if there's any demand for it. diff --git a/docs/jade.md b/docs/jade.md index b713503..4e2fe7b 100644 --- a/docs/jade.md +++ b/docs/jade.md @@ -6,3 +6,7 @@ The jade adapter is used in just about the exact same as the jade public api, ma - compile - compileClient - clientHelpers + +## Source Maps + +Jade does not support source maps and has no plans of doing so in the near future. See [this issue](https://github.com/jadejs/jade/issues/941) for discussion. diff --git a/docs/less.md b/docs/less.md index acc1252..41c677b 100644 --- a/docs/less.md +++ b/docs/less.md @@ -11,3 +11,7 @@ less.render('some less code', { ## Supported Methods - render + +## Source Maps + +The less adapter does have support for sourcemaps. You can pass in `sourceMap: true` as an option and receive a map back on the response object as `sourcemap`. diff --git a/docs/livescript.md b/docs/livescript.md index 68c3dfd..4e976d8 100644 --- a/docs/livescript.md +++ b/docs/livescript.md @@ -4,6 +4,10 @@ LiveScript is a fork of Coco, which is a fork of CoffeeScript. More info on chan ## Supported Methods - render +## Source Maps + +Livescript does not yet have support for source maps. You can see an open issue for this feature [here](https://github.com/gkz/LiveScript/issues/452). + ## Additional Options Its compiler is unsurprisingly very similar to both CoffeeScript and Coco, and has only 2 unique options, explained below. diff --git a/docs/minify-css.md b/docs/minify-css.md index da4523d..53bb6d6 100644 --- a/docs/minify-css.md +++ b/docs/minify-css.md @@ -3,3 +3,7 @@ CSS is minified with the fantastic [clean-css](https://github.com/GoalSmashers/c ## Supported Methods - render + +## Source Maps + +Clean CSS does not yet have support for source maps, but it is [in the works](https://github.com/jakubpawlowicz/clean-css/issues/125) at the time of writing. We will re-examine if/when this feature is fully implemented. diff --git a/docs/minify-js.md b/docs/minify-js.md index fdcf20f..bd7066b 100644 --- a/docs/minify-js.md +++ b/docs/minify-js.md @@ -3,3 +3,7 @@ Uses [uglifyjs2](https://github.com/mishoo/UglifyJS2) to minify javascript code. ## Supported Methods - render + +## Source Maps + +Although UglifyJS has sourcemap support, unfortunately it forces you to write them to a specific file rather than simply returning the map so you can do with it what you want. Since accord does not write files and simply executes transforms then returns the text, we do not support this in any special way. You can pass `sourceMap` as an option with the path to a file and presumably it will be written. This is not tested or officially supported and will not be until UglifyJS allows us to return the sourcemap as a string or object. diff --git a/docs/myth.md b/docs/myth.md index a3d7ec8..c0b29bb 100644 --- a/docs/myth.md +++ b/docs/myth.md @@ -3,3 +3,7 @@ Essentially a polyfill for the css future spec. Check the [features](https://git ## Supported Methods - render + +## Source Maps + +Myth does support sourcemaps, but automatically inlines them. If call the adapter with `sourcemap: true` as an option, it will return a compiled result with an inline sourcemap. diff --git a/docs/scss.md b/docs/scss.md index 91ca49f..d4b5bc6 100644 --- a/docs/scss.md +++ b/docs/scss.md @@ -4,6 +4,10 @@ This adapter uses [node-sass](https://github.com/andrew/node-sass), an incredibl ## Supported Methods - render +## Source Maps + +Libsass does not yet support source maps, although at the time of writing there is [an open pull request](https://github.com/sass/libsass/pull/591) to work on this. + ## Additional Options It has a pretty standard API, and uses the [options documented here](https://github.com/andrew/node-sass#options). Do not pass through `data` or `file`, as this will be overridden by accord's wrapper - everything else is fair game. diff --git a/docs/stylus.md b/docs/stylus.md index 6a56a6e..57e5fba 100644 --- a/docs/stylus.md +++ b/docs/stylus.md @@ -4,6 +4,10 @@ The Stylus compiler interface is one of the most abnormal and has gone through h ## Supported Methods - render +## Source Maps + +Source maps are supported by the stylus adapter. Just pass in a `sourcemap` option [as described in the docs](http://learnboost.github.io/stylus/docs/sourcemaps.html), and it will come back as `sourcemap` on the response object. + ## Additional Options ### [Define](http://learnboost.github.io/stylus/docs/js.html#definename-node) diff --git a/lib/adapters/coffee-script.coffee b/lib/adapters/coffee-script.coffee index 3211d83..b986082 100644 --- a/lib/adapters/coffee-script.coffee +++ b/lib/adapters/coffee-script.coffee @@ -15,6 +15,9 @@ class CoffeeScript extends Adapter compile = (fn) -> try res = fn() catch err then return W.reject(err) - W.resolve(compiled: res) + if res.sourceMap + W.resolve(compiled: res.js, sourcemap: res.sourceMap, v3sourcemap: res.v3SourceMap) + else + W.resolve(compiled: res) module.exports = CoffeeScript diff --git a/lib/adapters/less.coffee b/lib/adapters/less.coffee index 1d34a2a..5ab5096 100644 --- a/lib/adapters/less.coffee +++ b/lib/adapters/less.coffee @@ -16,7 +16,9 @@ class Less extends Adapter @engine.render str, options, (err, res) -> if err then return deferred.reject(err) - deferred.resolve(compiled: res.css) + obj = { compiled: res.css } + if options.sourceMap then obj.sourcemap = res.map + deferred.resolve(obj) return deferred.promise diff --git a/lib/adapters/minify-js.coffee b/lib/adapters/minify-js.coffee index 8f236e4..4ea21fb 100644 --- a/lib/adapters/minify-js.coffee +++ b/lib/adapters/minify-js.coffee @@ -10,13 +10,17 @@ class MinifyJS extends Adapter isolated: true _render: (str, options) -> - compile => @engine.minify(str, _.extend(options, fromString: true)).code + compile => + res = @engine.minify(str, _.extend(options, fromString: true)) + obj = { compiled: res.code } + if options.sourceMap then obj.sourcemap = res.map + obj # private - compile = (fn) -> + compile = (fn, map) -> try res = fn() catch err then return W.reject(err) - W.resolve(compiled: res) + W.resolve(res) module.exports = MinifyJS diff --git a/lib/adapters/myth.coffee b/lib/adapters/myth.coffee index 1c714e0..9e6a529 100644 --- a/lib/adapters/myth.coffee +++ b/lib/adapters/myth.coffee @@ -7,7 +7,9 @@ class Myth extends Adapter output: 'css' _render: (str, options) -> - compile => @engine(str) + options.source = options.filename + delete options.filename + compile => @engine(str, options) # private diff --git a/test/fixtures/myth/expected/import.css b/test/fixtures/myth/expected/import.css new file mode 100644 index 0000000..bbc9e85 --- /dev/null +++ b/test/fixtures/myth/expected/import.css @@ -0,0 +1,10 @@ +html { + background: blue; +} + +body { + color: red; +} + + +/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi9Vc2Vycy9qZWZmL1NpdGVzL2FjY29yZC90ZXN0L2ZpeHR1cmVzL215dGgvaW1wb3J0ZWQubXl0aCIsIi9Vc2Vycy9qZWZmL1NpdGVzL2FjY29yZC90ZXN0L2ZpeHR1cmVzL215dGgvaW1wb3J0Lm15dGgiLCJzb3VyY2UuY3NzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBO0VBQ0U7OztBQ0NGO0VBQ0U7OztBQ0lGIiwic291cmNlc0NvbnRlbnQiOlsiaHRtbCB7XG4gIGJhY2tncm91bmQ6IGJsdWU7XG59XG4iLCJAaW1wb3J0IFwiaW1wb3J0ZWQubXl0aFwiO1xuXG5ib2R5IHtcbiAgY29sb3I6IHJlZFxufVxuIiwiaHRtbCB7XG4gIGJhY2tncm91bmQ6IGJsdWU7XG59XG5cbmJvZHkge1xuICBjb2xvcjogcmVkO1xufVxuLyojIHNvdXJjZU1hcHBpbmdVUkw9ZGF0YTphcHBsaWNhdGlvbi9qc29uO2Jhc2U2NCxleUoyWlhKemFXOXVJam96TENKemIzVnlZMlZ6SWpwYklpOVZjMlZ5Y3k5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1WTNsSk5rbHJSa0pSVlVVM1VsVkdSRkpVY3pkUE1FWkVVVEJaTjFKVlJrUlNVMGx6U1c1T2RtUllTbXBhV0U1RVlqSTFNRnBYTlRCSmFuQmlTVzFvTUdKWGQyZGxNWGgxU1VOQ2FWbFhUbkphTTBwMlpGYzFhMDlwUW1saVNGWnNUekY0ZFdaV2VIVkphWGRwVVVkc2RHTkhPWGxrUTBKalNXMXNkR05IT1hsa1IxWnJURzB4TldSSGFHTkphblJqWW14NGRWbHRPV3RsVTBJM1dFYzBaMGxIVG5aaVJ6bDVUMmxDZVZwWFVtTmliakZqWW1sS1pHWlJQVDBnS2k4aUxDSkFhVzF3YjNKMElGd2lhVzF3YjNKMFpXUXViWGwwYUZ3aU8xeHVYRzVpYjJSNUlIdGNiaUFnWTI5c2IzSTZJSEpsWkZ4dWZWeHVJbDE5ICovIl19 */ diff --git a/test/fixtures/myth/import.myth b/test/fixtures/myth/import.myth new file mode 100644 index 0000000..8972bb3 --- /dev/null +++ b/test/fixtures/myth/import.myth @@ -0,0 +1,5 @@ +@import "imported.myth"; + +body { + color: red +} diff --git a/test/fixtures/myth/imported.myth b/test/fixtures/myth/imported.myth new file mode 100644 index 0000000..c913323 --- /dev/null +++ b/test/fixtures/myth/imported.myth @@ -0,0 +1,3 @@ +html { + background: blue; +} diff --git a/test/test.coffee b/test/test.coffee index 6dc6d37..322c820 100644 --- a/test/test.coffee +++ b/test/test.coffee @@ -174,6 +174,13 @@ describe 'coffeescript', -> @coffee.render("! ---@#$$@%#$") .done(should.not.exist, (-> done())) + it 'should generate sourcemaps', (done) -> + lpath = path.join(@path, 'basic.coffee') + @coffee.renderFile(lpath, sourceMap: true ).done (res) => + res.sourcemap.should.exist + res.v3sourcemap.should.exist + should.match_expected(@coffee, res.compiled, lpath, done) + describe 'stylus', -> before -> @@ -426,6 +433,13 @@ describe 'minify-js', -> @minifyjs.render("@#$%#I$$N%NI#$%I$PQ") .done(should.not.exist, (-> done())) + it.skip 'should generate sourcemaps', (done) -> + lpath = path.join(@path, 'basic.js') + @minifyjs.renderFile(lpath, sourceMap: true).done (res) => + res.sourcemap.should.exist + res.sourcemap.should.not.equal('null') + should.match_expected(@minifyjs, res.compiled, lpath, done) + describe 'minify-css', -> before -> @@ -736,6 +750,12 @@ describe 'less', -> ''') .done(should.not.exist, (-> done())) + it 'should generate sourcemaps', (done) -> + lpath = path.join(@path, 'basic.less') + @less.renderFile(lpath, sourceMap: true).done (res) => + res.sourcemap.should.exist + should.match_expected(@less, res.compiled, lpath, done) + describe 'coco', -> before -> @@ -823,6 +843,11 @@ describe 'myth', -> @myth.render("!! --- )I%$_(I(YRTO") .done(should.not.exist, (-> done())) + it 'should generate sourcemaps', (done) -> + lpath = path.join(@path, 'import.myth') + @myth.renderFile(lpath, sourcemap: true).done (res) => + should.match_expected(@myth, res.compiled, lpath, done) + describe 'haml', -> before ->