gulp-sourcemaps
Advanced tools
Comparing version 0.4.6 to 1.0.0
93
index.js
@@ -13,3 +13,3 @@ 'use strict'; | ||
*/ | ||
module.exports.init = function init() { | ||
module.exports.init = function init(options) { | ||
function sourceMapInit(file, encoding, callback) { | ||
@@ -24,21 +24,82 @@ /*jshint validthis:true */ | ||
if (file.isStream()) { | ||
return callback(new Error(PLUGIN_NAME + ': Streaming not supported')); | ||
return callback(new Error(PLUGIN_NAME + '-init: Streaming not supported')); | ||
} | ||
var map = { | ||
version : 3, | ||
file: file.relative, | ||
names: [], | ||
mappings: '', | ||
sources: [file.relative], | ||
sourcesContent: [file.contents.toString()] | ||
}; | ||
var fileContent = file.contents.toString(); | ||
var sourceMap; | ||
var embeddedMap = convert.fromSource(file.contents.toString()); | ||
if (options && options.loadMaps) { | ||
var sourcePath = ''; //root path for the sources in the map | ||
file.sourceMap = embeddedMap ? embeddedMap.toObject() : map; | ||
// Try to read inline source map | ||
sourceMap = convert.fromSource(fileContent); | ||
if (sourceMap) { | ||
sourceMap = sourceMap.toObject(); | ||
// sources in map are relative to the source file | ||
sourcePath = path.dirname(file.path); | ||
} else { | ||
// look for source map comment referencing a source map file | ||
var mapComment = convert.mapFileCommentRegex.exec(fileContent); | ||
var str = convert.removeComments(file.contents.toString()); | ||
file.contents = new Buffer(str, 'utf8'); | ||
var mapFile; | ||
if (mapComment) | ||
mapFile = path.resolve(path.dirname(file.path), mapComment[1]); | ||
// if no comment try map file with same name as source file | ||
else | ||
mapFile = file.path + '.map'; | ||
// sources in external map are relative to map file | ||
sourcePath = path.dirname(mapFile); | ||
try { | ||
sourceMap = JSON.parse(fs.readFileSync(mapFile).toString()); | ||
} catch(e) {} | ||
} | ||
// fix source paths and sourceContent for imported source map | ||
if (sourceMap) { | ||
sourceMap.sourcesContent = sourceMap.sourcesContent || []; | ||
sourceMap.sources.forEach(function(source, i) { | ||
var absPath = path.resolve(sourcePath, source); | ||
sourceMap.sources[i] = path.relative(file.base, absPath); | ||
if (!sourceMap.sourcesContent[i]) { | ||
var sourceContent = null; | ||
// if current file: use content | ||
if (absPath === file.path) { | ||
sourceContent = fileContent; | ||
// else load content from file | ||
} else { | ||
try { | ||
sourceContent = fs.readFileSync(absPath).toString(); | ||
} catch (e) { | ||
console.warn(PLUGIN_NAME + '-init: source file not found:' + absPath); | ||
} | ||
} | ||
sourceMap.sourcesContent[i] = sourceContent; | ||
} | ||
}); | ||
// remove source map comment from source | ||
file.contents = new Buffer(convert.removeComments(fileContent), 'utf8'); | ||
} | ||
} | ||
if (!sourceMap) { | ||
// Make an empty source map | ||
sourceMap = { | ||
version : 3, | ||
file: file.relative, | ||
names: [], | ||
mappings: '', | ||
sources: [file.relative], | ||
sourcesContent: [fileContent] | ||
}; | ||
} | ||
file.sourceMap = sourceMap; | ||
this.push(file); | ||
@@ -79,3 +140,3 @@ callback(); | ||
if (file.isStream()) { | ||
return callback(new Error(PLUGIN_NAME + ': Streaming not supported')); | ||
return callback(new Error(PLUGIN_NAME + '-write: Streaming not supported')); | ||
} | ||
@@ -100,3 +161,3 @@ | ||
} catch (e) { | ||
console.warn(PLUGIN_NAME + ': source file not found:' + sourcePath); | ||
console.warn(PLUGIN_NAME + '-write: source file not found:' + sourcePath); | ||
} | ||
@@ -103,0 +164,0 @@ } |
{ | ||
"name": "gulp-sourcemaps", | ||
"version": "0.4.6", | ||
"version": "1.0.0", | ||
"description": "Source map support for Gulp.js", | ||
@@ -15,4 +15,6 @@ "homepage": "http://github.com/floridoo/gulp-sourcemaps", | ||
"keywords": [ | ||
"gulpplugin", | ||
"gulp", | ||
"sourcemap" | ||
"source maps", | ||
"sourcemaps" | ||
], | ||
@@ -27,9 +29,9 @@ "author": "Florian Reiterer <me@florianreiterer.com>", | ||
"devDependencies": { | ||
"jshint": "^2.5.0", | ||
"tape": "^2.12.3", | ||
"jshint": "^2.5.2", | ||
"tape": "^2.13.3", | ||
"argg": "0.0.1", | ||
"istanbul": "^0.2.8", | ||
"istanbul": "^0.3.0", | ||
"faucet": "0.0.1", | ||
"coveralls": "^2.10.0" | ||
"coveralls": "^2.11.1" | ||
} | ||
} |
@@ -8,2 +8,3 @@ ## gulp-sourcemaps [![NPM version][npm-image]][npm-url] [![build status][travis-image]][travis-url] [![Test coverage][coveralls-image]][coveralls-url] | ||
Example: | ||
```javascript | ||
@@ -49,5 +50,34 @@ var gulp = require('gulp'); | ||
#### Load existing source maps | ||
### Options | ||
To load existing source maps, pass the option `loadMaps: true` to `sourcemaps.init()`. | ||
Example: | ||
```javascript | ||
var gulp = require('gulp'); | ||
var concat = require('gulp-concat'); | ||
var uglify = require('gulp-uglify'); | ||
var sourcemaps = require('gulp-sourcemaps'); | ||
gulp.task('javascript', function() { | ||
gulp.src('src/**/*.js') | ||
.pipe(sourcemaps.init({loadMaps: true})) | ||
.pipe(concat('all.js')) | ||
.pipe(uglify()) | ||
.pipe(sourcemaps.write()) | ||
.pipe(gulp.dest('dist')); | ||
}); | ||
``` | ||
### Init Options | ||
- `loadMaps` | ||
Set to true to load existing maps for source files. Supports the following: | ||
- inline source maps | ||
- source map files referenced by a `sourceMappingURL=` comment | ||
- source map files with the same name (plus .map) in the same directory | ||
### Write Options | ||
- `addComment` | ||
@@ -54,0 +84,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
12284
171
0
164