Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Source Maps #311

Merged
merged 42 commits into from
@josh

No description provided.

@vjpr vjpr referenced this pull request in nodeca/mincer
Closed

Feature: CoffeeScript Source Map Support #39

@josh

@SamSaffron yeah, I'll be back hacking on this stuff soon.

@michaelficarra

@SamSaffron: Yep, source maps are fully supported, but those instructions are a little out of date. Follow the installation instructions in the README, then bin/coffee --help should help you figure it out yourself.

@guilleiguaran guilleiguaran referenced this pull request in igrigorik/closure-sprockets
Open

Source maps support #11

@loopj

Uglify now has sourcemaps support

@elia

:+1: (http://opalrb.org is going to have them implemented too)

@JAStanton

Waiting for CoffeeScript source maps <---- Wait no longer: https://twitter.com/jashkenas/status/308659094930214912

@josh

Yippy!

@paulmillr paulmillr referenced this pull request in brunch/brunch
Closed

Add source maps support #288

@ndbroadbent

awesome, can't wait for this!

@josh

Waiting on 1.6.1 for the required ruby bridge changes.

https://twitter.com/jashkenas/status/308676522502410240

@josh

Beautiful.

@PavelDemyanenko PavelDemyanenko referenced this pull request in rails/coffee-rails
Closed

Support for source maps? #40

@josh josh was assigned
@hijonathan

Looks like Coffeescript 1.6.2 now contains source maps in the browser build. Any other blockers?

@schmurfy

I am also really interested in this, how is the work going ? need any help/testers ?

@josh josh closed this
@josh josh reopened this
@bhollis bhollis referenced this pull request in middleman/middleman-sprockets
Open

Support source maps #44

0 of 5 tasks complete
This was referenced
@josh josh added this to the 4.0 milestone
@josh

The API for amending map data to the processing pipeline has mostly been figured out as of #555.

A big chunk of work remains around the unification of asset variants which will actually allow you to get the map data out of the pipeline, served up or statically compiled. If you want to help out, feedback should be focused on #569.

@josh josh locked and limited conversation to collaborators
@josh josh merged commit efe1f91 into 4.x
@josh josh deleted the source-maps branch
@josh josh removed their assignment
@josh josh removed their assignment
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Commits on Mar 22, 2012
  1. @josh
  2. @josh

    Add X-SourceMap header to server

    josh authored
  3. @josh

    Basic bundle source mappings

    josh authored
Commits on Apr 28, 2012
  1. @josh
Commits on Apr 29, 2012
  1. @josh

    Add VLQ encoding

    josh authored
Commits on Apr 30, 2012
  1. @josh

    Initial source map concatenation

    josh authored
  2. @josh

    Fix 1.8

    josh authored
Commits on May 6, 2012
  1. @josh
  2. @josh

    Add mapping binary search

    josh authored
Commits on Jul 27, 2013
  1. @josh
Commits on Aug 20, 2013
  1. @josh
  2. @josh

    Depend on sourcemap gem

    josh authored
  3. @josh
  4. @josh

    Axe vlq test

    josh authored
  5. @josh

    Axe source map test

    josh authored
  6. @josh

    Drop builtin source map class

    josh authored
  7. @josh

    Remove sprockets vlq module

    josh authored
  8. @josh
Commits on Nov 17, 2013
  1. @josh
  2. @josh

    Get oughta here DS_Store

    josh authored
  3. @josh

    Adjust new source map offset

    josh authored
  4. @josh
  5. @josh
  6. @josh

    Map object, not mappings

    josh authored
  7. @josh
Commits on Jul 9, 2014
  1. @josh

    Add sourcemap gem

    josh authored
  2. @josh
Commits on Jul 10, 2014
  1. @dgraham

    Ignore bundler config.

    dgraham authored
  2. @dgraham

    Pass source file name to coffee compiler.

    dgraham authored
    The compiler populates the source map’s `sources` array with this name
    so we can map the compiled js back to its original coffee file.
Commits on Jul 15, 2014
  1. @dgraham

    Add source map test fixtures.

    dgraham authored
  2. @dgraham
  3. @dgraham
  4. @dgraham
  5. @dgraham

    Test source maps.

    dgraham authored
  6. @dgraham
  7. @dgraham

    Add metadata to test hash.

    dgraham authored
  8. @dgraham
  9. @josh

    Merge pull request #612 from dgraham/coffee-script-sourcemap

    josh authored
    Pass source file name to coffee compiler.
  10. @josh

    Merge branch 'coffee-script-sourcemap' into source-maps

    josh authored
    Conflicts:
    	lib/sprockets/asset.rb
    	lib/sprockets/base.rb
    	lib/sprockets/bundled_asset.rb
    	lib/sprockets/context.rb
    	lib/sprockets/server.rb
    	sprockets.gemspec
    	test/test_asset.rb
    	test/test_server.rb
  11. @josh

    Merge branch 'master' into source-maps

    josh authored
    Conflicts:
    	.gitignore
    	lib/sprockets/coffee_script_template.rb
  12. @josh

    Resort

    josh authored
Commits on Aug 17, 2014
  1. @josh

    Merge branch 'master' into source-maps

    josh authored
    Conflicts:
    	lib/sprockets/bundle.rb
This page is out of date. Refresh to see the latest.
View
15 lib/sprockets.rb
@@ -114,6 +114,21 @@ module Sprockets
register_bundle_reducer 'application/javascript', :data, Utils.method(:concat_javascript_sources)
register_bundle_reducer '*/*', :dependency_paths, :+
+ register_postprocessor 'application/javascript', proc { |input|
+ # Use an identity map if no mapping is defined
+ if !input[:metadata][:map]
+ map = SourceMap::Map.new([
+ SourceMap::Mapping.new(
+ input[:name],
+ SourceMap::Offset.new(0, 0),
+ SourceMap::Offset.new(0, 0)
+ )
+ ])
+ { data: input[:data], map: map }
+ end
+ }
+ register_bundle_reducer 'application/javascript', :map, :+
+
register_compressor 'text/css', :sass, LazyProcessor.new { SassCompressor }
register_compressor 'text/css', :scss, LazyProcessor.new { SassCompressor }
register_compressor 'text/css', :yui, LazyProcessor.new { YUICompressor }
View
16 lib/sprockets/coffee_script_template.rb
@@ -1,4 +1,5 @@
require 'coffee_script'
+require 'source_map'
module Sprockets
# Template engine class for the CoffeeScript compiler.
@@ -9,14 +10,23 @@ module Sprockets
# https://github.com/josh/ruby-coffee-script
#
module CoffeeScriptTemplate
- VERSION = '1'
+ VERSION = '2'
SOURCE_VERSION = ::CoffeeScript::Source.version
def self.call(input)
data = input[:data]
key = ['CoffeeScriptTemplate', SOURCE_VERSION, VERSION, data]
- input[:cache].fetch(key) do
- ::CoffeeScript.compile(data)
+
+ result = input[:cache].fetch(key) do
+ ::CoffeeScript.compile(data, sourceMap: true, sourceFiles: [input[:name]])
+ end
+
+ if input[:map]
+ map = input[:map] | SourceMap::Map.from_json(result['v3SourceMap'])
+ { data: result['js'],
+ map: map }
+ else
+ result['js']
end
end
end
View
4 lib/sprockets/processing.rb
@@ -1,3 +1,4 @@
+require 'source_map'
require 'sprockets/engines'
require 'sprockets/lazy_processor'
require 'sprockets/legacy_proc_processor'
@@ -170,7 +171,8 @@ def process(processors, filename, load_path, name, content_type, data)
name: name,
content_type: content_type,
data: data,
- metadata: metadata
+ metadata: metadata,
+ map: SourceMap::Map.new
}
processors.each do |processor|
View
17 lib/sprockets/uglifier_compressor.rb
@@ -14,7 +14,7 @@ module Sprockets
# Sprockets::UglifierCompressor.new(comments: :copyright)
#
class UglifierCompressor
- VERSION = '1'
+ VERSION = '2'
def self.call(*args)
new.call(*args)
@@ -42,8 +42,19 @@ def initialize(options = {})
def call(input)
data = input[:data]
- input[:cache].fetch(@cache_key + [data]) do
- @uglifier.compile(data)
+
+ js, map = input[:cache].fetch(@cache_key + [data]) do
+ @uglifier.compile_with_map(data)
+ end
+
+ if input[:metadata][:map]
+ minified = SourceMap::Map.from_json(map)
+ original = input[:metadata][:map]
+ combined = original | minified
+ { data: js,
+ map: combined }
+ else
+ js
end
end
end
View
1  sprockets.gemspec
@@ -12,6 +12,7 @@ Gem::Specification.new do |s|
s.executables = ["sprockets"]
s.add_dependency "rack", "~> 1.0"
+ s.add_dependency "sourcemap"
s.add_development_dependency "closure-compiler", "~> 1.1"
s.add_development_dependency "coffee-script-source", "~> 1.6"
View
5 test/fixtures/source-maps/application.coffee
@@ -0,0 +1,5 @@
+#= require project
+#= require users
+
+document.on 'dom:loaded', ->
+ $('search').focus()
View
3  test/fixtures/source-maps/child.js
@@ -0,0 +1,3 @@
+var Child = {
+ find: function(id) { }
+};
View
6 test/fixtures/source-maps/parent.js
@@ -0,0 +1,6 @@
+//= require child
+//= require users
+
+document.on('dom:loaded', function() {
+ $('parent').focus();
+});
View
2  test/fixtures/source-maps/project.coffee
@@ -0,0 +1,2 @@
+Project =
+ find: (id) ->
View
2  test/fixtures/source-maps/users.coffee
@@ -0,0 +1,2 @@
+Users =
+ find: (id) ->
View
14 test/test_coffee_script_template.rb
@@ -10,4 +10,18 @@ class TestCoffeeScriptTemplate < Sprockets::TestCase
}
assert Sprockets::CoffeeScriptTemplate.call(input).match(/var square/)
end
+
+ test "compile coffee-script template with source map" do
+ input = {
+ content_type: 'application/javascript',
+ data: "square = (n) -> n * n",
+ name: 'squared',
+ cache: Sprockets::Cache.new,
+ map: SourceMap::Map.new
+ }
+ result = Sprockets::CoffeeScriptTemplate.call(input)
+ assert result[:data].match(/var square/)
+ assert_equal 19, result[:map].size
+ assert_equal ['squared'], result[:map].sources
+ end
end
View
1  test/test_path_utils.rb
@@ -30,6 +30,7 @@ class TestPathUtils < Sprockets::TestCase
"resolve",
"sass",
"server",
+ "source-maps",
"symlink"
], entries(FIXTURE_ROOT)
end
View
30 test/test_source_maps.rb
@@ -0,0 +1,30 @@
+require 'sprockets_test'
+require 'sprockets/bundle'
+
+class TestSourceMaps < Sprockets::TestCase
+ def setup
+ @environment = Sprockets::Environment.new
+ @environment.append_path fixture_path('source-maps')
+ end
+
+ test "builds a source map for js files" do
+ asset = @environment['child.js']
+ map = asset.metadata[:map]
+ assert_equal ['child'], map.sources
+ end
+
+ test "builds a minified source map" do
+ @environment.js_compressor = :uglifier
+
+ asset = @environment['application.js']
+ map = asset.metadata[:map]
+ assert map.all? {|mapping| mapping.generated.line == 1 }
+ assert_equal %w[project users application], map.sources
+ end
+
+ test "builds a source map with js dependency" do
+ asset = @environment['parent.js']
+ map = asset.metadata[:map]
+ assert_equal %w[child users parent], map.sources
+ end
+end
View
3  test/test_uglifier_compressor.rb
@@ -6,7 +6,8 @@ class TestUglifierCompressor < Sprockets::TestCase
input = {
content_type: 'application/javascript',
data: "function foo() {\n return true;\n}",
- cache: Sprockets::Cache.new
+ cache: Sprockets::Cache.new,
+ metadata: {}
}
output = "function foo(){return!0}"
assert_equal output, Sprockets::UglifierCompressor.call(input)
Something went wrong with that request. Please try again.