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

Added support for saving the generated source maps in separate files. #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -33,7 +33,8 @@ Install globally via npm:
-h, --help output usage information
-V, --version output the version number
-b, --bare compile without a top-level function wrapper
-M, --map create source maps
-M, --map create source maps, appended to the JS file
-x, --extmap create source maps, shipped in a separate file
-m, --minify minify output files
-o, --output <path> output path
-s, --silent suppress console output
Expand Down Expand Up @@ -69,7 +70,8 @@ Compiles all .coffee files found in `inputPaths`, which can be a single string o
* `minify` - Minify output files. Defaults to false.
* `output` - The path to the output file. If the path has a file extension, all files will be joined at that location. Otherwise, the path is assumed to be a directory.
* `silent` - Suppress all console output. Defaults to true.
* `sourceMap` - Generate source maps for output files. Not currentl compatible with minification. Defaults to false.
* `sourceMap` - Generate source maps for output files, and append them to the JS. Not currentl compatible with minification. Defaults to false.
* `extSourceMap` - Generate source maps for output files, and save them in saparate files. Not currentl compatible with minification. Defaults to false.
* `watch` - (boolean) Watch all files and directories for changes and recompile automatically. Defaults to false.

##### Example:
Expand Down
16 changes: 13 additions & 3 deletions src/coffeebar.coffee
Expand Up @@ -40,8 +40,11 @@ class Coffeebar
@options.watch ?= false
@options.silent ?= true
@options.minify ?= false
@options.extSourceMap ?= false
@options.extSourceMap = false if @options.minify
@options.sourceMap ?= false
@options.sourceMap = false if @options.minify
@options.sourceMap = @options.sourceMap or @options.extSourceMap
@options.join = true if @options.output and path.extname(@options.output)

@options.bare ?= false
Expand All @@ -59,6 +62,7 @@ class Coffeebar
unless Array.isArray(@inputPaths) then @inputPaths = [@inputPaths]

for inputPath, i in @inputPaths
@inputPaths[i] = inputPath = path.normalize inputPath
unless path.extname(inputPath)
@inputPaths[i] = "#{inputPath}/**/*.{#{exts}}"

Expand Down Expand Up @@ -115,15 +119,21 @@ class Coffeebar
# src file that we sent to the compiler.
mapSources: ->
unless @options.join
source.writeMapComment() for source in @sources
unless @options.extSourceMap
source.writeMapComment() for source in @sources
else
source.writeMapCommentExt() for source in @sources
return

return unless @outputs[0].sourceMap
smOld = new sourcemap.SourceMapConsumer @outputs[0].sourceMap
smNew = new sourcemap.SourceMapGenerator {file: smOld.file, sourceRoot: "#{path.basename(@options.output, '.js')}_mapsrc"}
smNew = new sourcemap.SourceMapGenerator {file: (path.basename smOld.file), sourceRoot: "#{path.basename(@options.output, '.js')}_mapsrc"}
smOld.eachMapping (map) => smNew.addMapping(@offsetMapping map)

@outputs[0].writeMapComment smNew.toString()
unless @options.extSourceMap
@outputs[0].writeMapComment smNew.toString()
else
@outputs[0].writeMapCommentExt smNew.toString()

# After compilation, report each error that was logged. In the event
# that this is a joined output file, use the line number offset to
Expand Down
4 changes: 3 additions & 1 deletion src/command.coffee
Expand Up @@ -15,7 +15,8 @@ module.exports.run = ->
.usage('[options] [path ...]')
.option('-b, --bare', 'compile without a top-level function wrapper')
.option('-m, --minify', 'minify output files')
.option('-M, --map', 'create source maps')
.option('-M, --map', 'create source maps, appended to the JS file')
.option('-x, --extmap', 'create source maps, shipped in a separate file')
.option('-o, --output <path>', 'output path')
.option('-s, --silent', 'suppress console output')
.option('-w, --watch', 'watch files for changes')
Expand All @@ -28,6 +29,7 @@ module.exports.run = ->
silent : program.silent || false
minify : program.minify
sourceMap : program.map
extSourceMap : program.extmap
bare : program.bare

console.log '' unless options.silent
Expand Down
10 changes: 10 additions & 0 deletions src/source.coffee
Expand Up @@ -96,6 +96,16 @@ class Source
commentMap = "//@ sourceMappingURL=data:application/json;base64,#{commentMap}"
@compiled = "#{@compiled}\n#{commentMap}"

# Save the source map to an external file.
writeMapCommentExt: (map) ->
mkdirp.sync path.dirname @outputPath
mapOutput = @outputPath.replace '.js', '.map'
map or= JSON.stringify @sourceMap
fs.writeFileSync mapOutput, map, 'utf8'
mapName = path.basename mapOutput
commentMap = "//@ sourceMappingURL=#{mapName}"
@compiled = "#{@compiled}\n#{commentMap}"

# Utilities
# ---------

Expand Down