vinyl-sourcemap
Advanced tools
Comparing version 1.1.0 to 2.0.0
37
index.js
'use strict'; | ||
var File = require('vinyl'); | ||
var vinylContents = require('vinyl-contents'); | ||
@@ -10,3 +11,2 @@ var helpers = require('./lib/helpers'); | ||
function add(file, callback) { | ||
// Bail early an error if the file argument is not a Vinyl file | ||
@@ -17,7 +17,2 @@ if (!File.isVinyl(file)) { | ||
// Bail early with an error if file has streaming contents | ||
if (file.isStream()) { | ||
return callback(new Error(PLUGIN_NAME + '-add: Streaming not supported')); | ||
} | ||
// Bail early successfully if file is null or already has a sourcemap | ||
@@ -28,15 +23,22 @@ if (file.isNull() || file.sourceMap) { | ||
var state = { | ||
path: '', // Root path for the sources in the map | ||
map: null, | ||
content: file.contents.toString(), | ||
// TODO: handle this? | ||
preExistingComment: null, | ||
}; | ||
vinylContents(file, onContents); | ||
helpers.addSourceMaps(file, state, callback); | ||
function onContents(err, contents) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
var state = { | ||
path: '', // Root path for the sources in the map | ||
map: null, | ||
content: contents.toString(), | ||
// TODO: handle this? | ||
preExistingComment: null, | ||
}; | ||
helpers.addSourceMaps(file, state, callback); | ||
} | ||
} | ||
function write(file, destPath, callback) { | ||
// Check if options or a callback are passed as second argument | ||
@@ -53,7 +55,2 @@ if (typeof destPath === 'function') { | ||
// Bail early with an error if file has streaming contents | ||
if (file.isStream()) { | ||
return callback(new Error(PLUGIN_NAME + '-write: Streaming not supported')); | ||
} | ||
// Bail early successfully if file is null or doesn't have sourcemap | ||
@@ -60,0 +57,0 @@ if (file.isNull() || !file.sourceMap) { |
'use strict'; | ||
var path = require('path'); | ||
var os = require('os'); | ||
var TextDecoder = require('util').TextDecoder; | ||
var fs = require('graceful-fs'); | ||
var Readable = require('streamx').Readable; | ||
var Transform = require('streamx').Transform; | ||
var nal = require('now-and-later'); | ||
var File = require('vinyl'); | ||
var convert = require('convert-source-map'); | ||
var removeBOM = require('remove-bom-buffer'); | ||
var appendBuffer = require('append-buffer'); | ||
var normalizePath = require('normalize-path'); | ||
var urlRegex = /^(https?|webpack(-[^:]+)?):\/\//; | ||
var bufCR = Buffer.from('\r'); | ||
var bufCRLF = Buffer.from('\r\n'); | ||
var bufLF = Buffer.from('\n'); | ||
var bufEOL = Buffer.from(os.EOL); | ||
var decoder = new TextDecoder('utf-8', { ignoreBOM: false }); | ||
function isRemoteSource(source) { | ||
@@ -21,3 +29,3 @@ return source.match(urlRegex); | ||
try { | ||
return JSON.parse(removeBOM(data)); | ||
return JSON.parse(decoder.decode(data)); | ||
} catch (err) { | ||
@@ -38,3 +46,7 @@ // TODO: should this log a debug? | ||
// Remove source map comment from source | ||
file.contents = new Buffer(state.content, 'utf8'); | ||
if (file.isStream()) { | ||
file.contents = Readable.from(state.content); | ||
} else { | ||
file.contents = Buffer.from(state.content, 'utf8'); | ||
} | ||
return callback(); | ||
@@ -51,3 +63,7 @@ } | ||
// Remove source map comment from source | ||
file.contents = new Buffer(state.content, 'utf8'); | ||
if (file.isStream()) { | ||
file.contents = Readable.from(state.content); | ||
} else { | ||
file.contents = Buffer.from(state.content, 'utf8'); | ||
} | ||
} else { | ||
@@ -107,3 +123,3 @@ // If no comment try map file with same name as source file | ||
var relPath = path.relative(basePath, absPath); | ||
var unixRelPath = normalizePath(relPath); | ||
var unixRelPath = normalizeRelpath(relPath); | ||
@@ -126,3 +142,3 @@ state.map.sources[idx] = unixRelPath; | ||
} | ||
assignSourcesContent(removeBOM(data).toString('utf8'), idx); | ||
assignSourcesContent(decoder.decode(data), idx); | ||
cb(); | ||
@@ -134,3 +150,2 @@ } | ||
function mapsLoaded(file, state, callback) { | ||
if (!state.map) { | ||
@@ -141,3 +156,3 @@ state.map = { | ||
mappings: '', | ||
sources: [normalizePath(file.relative)], | ||
sources: [normalizeRelpath(file.relative)], | ||
sourcesContent: [state.content], | ||
@@ -147,3 +162,3 @@ }; | ||
state.map.file = normalizePath(file.relative); | ||
state.map.file = normalizeRelpath(file.relative); | ||
file.sourceMap = state.map; | ||
@@ -155,7 +170,3 @@ | ||
function addSourceMaps(file, state, callback) { | ||
var tasks = [ | ||
loadSourceMap, | ||
fixImportedSourceMap, | ||
mapsLoaded, | ||
]; | ||
var tasks = [loadSourceMap, fixImportedSourceMap, mapsLoaded]; | ||
@@ -179,23 +190,23 @@ function apply(fn, key, cb) { | ||
path: opts.path, | ||
contents: new Buffer(JSON.stringify(opts.content)), | ||
contents: Buffer.from(JSON.stringify(opts.content)), | ||
stat: { | ||
isFile: function() { | ||
isFile: function () { | ||
return true; | ||
}, | ||
isDirectory: function() { | ||
isDirectory: function () { | ||
return false; | ||
}, | ||
isBlockDevice: function() { | ||
isBlockDevice: function () { | ||
return false; | ||
}, | ||
isCharacterDevice: function() { | ||
isCharacterDevice: function () { | ||
return false; | ||
}, | ||
isSymbolicLink: function() { | ||
isSymbolicLink: function () { | ||
return false; | ||
}, | ||
isFIFO: function() { | ||
isFIFO: function () { | ||
return false; | ||
}, | ||
isSocket: function() { | ||
isSocket: function () { | ||
return false; | ||
@@ -211,3 +222,3 @@ }, | ||
var opts = { | ||
multiline: (needsMultiline.indexOf(extname) !== -1), | ||
multiline: needsMultiline.indexOf(extname) !== -1, | ||
}; | ||
@@ -240,3 +251,3 @@ | ||
sourcemapLocation = normalizePath(sourcemapLocation); | ||
sourcemapLocation = normalizeRelpath(sourcemapLocation); | ||
@@ -247,3 +258,3 @@ comment = convert.generateMapFileComment(sourcemapLocation, commentOpts); | ||
// Append source map comment | ||
file.contents = appendBuffer(file.contents, comment); | ||
file = append(file, Buffer.from(comment)); | ||
@@ -253,2 +264,97 @@ callback(null, file, sourceMapFile); | ||
function append(file, comment) { | ||
if (file.isBuffer()) { | ||
file.contents = appendBuffer(file.contents, comment); | ||
} | ||
if (file.isStream()) { | ||
file.contents = file.contents.pipe(appendStream(comment)); | ||
} | ||
return file; | ||
} | ||
function appendBuffer(buf1, buf2) { | ||
var eol; | ||
if (buf1.slice(-2).equals(bufCRLF)) { | ||
eol = bufCRLF; | ||
} else if (buf1.slice(-1).equals(bufLF)) { | ||
eol = bufLF; | ||
} else if (buf1.slice(-1).equals(bufCR)) { | ||
eol = bufCR; | ||
} else { | ||
eol = bufEOL; | ||
} | ||
return Buffer.concat([buf1, buf2, eol]); | ||
} | ||
// Implementation heavily inspired by https://github.com/maxogden/eol-stream | ||
function appendStream(comment) { | ||
var crAtEnd = false; | ||
var eol = bufEOL; | ||
return new Transform({ | ||
transform: detect, | ||
flush: flush, | ||
}); | ||
function detect(chunk, callback) { | ||
var isString = typeof chunk === 'string'; | ||
var cr = isString ? bufCR.toString() : bufCR[0]; | ||
var lf = isString ? bufLF.toString() : bufLF[0]; | ||
if (crAtEnd) { | ||
if (chunk[0] === lf) { | ||
eol = bufCRLF; | ||
} else { | ||
eol = bufCR; | ||
} | ||
// Reset the flag because we search for the _last_ line ending | ||
crAtEnd = false; | ||
return callback(null, chunk); | ||
} | ||
var i = 0; | ||
while (i < chunk.length) { | ||
if (chunk[i] === cr) { | ||
if (i === chunk.length - 1) { | ||
// Need to read the next chunk to see if it is a CRLF | ||
crAtEnd = true; | ||
i += 1; | ||
} else if (chunk[i + 1] === lf) { | ||
eol = bufCRLF; | ||
// We skip two because we saw the CRLF | ||
i += 2; | ||
} else { | ||
eol = bufCR; | ||
i += 1; | ||
} | ||
continue; | ||
} | ||
if (chunk[i] === lf) { | ||
eol = bufLF; | ||
} | ||
i += 1; | ||
} | ||
callback(null, chunk); | ||
} | ||
function flush(cb) { | ||
this.push(comment); | ||
this.push(eol); | ||
cb(); | ||
} | ||
} | ||
function normalizeRelpath(fp) { | ||
// Since we only ever operate on relative paths, | ||
// this utility shouldn't need to handle path roots | ||
return path.posix.normalize(fp.replace(/\\/g, '/')); | ||
} | ||
module.exports = { | ||
@@ -255,0 +361,0 @@ addSourceMaps: addSourceMaps, |
{ | ||
"name": "vinyl-sourcemap", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"description": "Add/write sourcemaps to/from Vinyl files.", | ||
"author": "Gulp Team <team@gulpjs.com> (http://gulpjs.com/)", | ||
"author": "Gulp Team <team@gulpjs.com> (https://gulpjs.com/)", | ||
"contributors": [ | ||
@@ -13,3 +13,3 @@ "Robin Venneman", | ||
"engines": { | ||
"node": ">= 0.10" | ||
"node": ">=10.13.0" | ||
}, | ||
@@ -23,28 +23,35 @@ "main": "index.js", | ||
"scripts": { | ||
"lint": "eslint index.js lib/ test/add.js test/write.js && jscs index.js lib/ test/add.js test/write.js", | ||
"lint": "eslint .", | ||
"pretest": "npm run lint", | ||
"test": "mocha --async-only", | ||
"cover": "istanbul cover _mocha --report lcovonly", | ||
"coveralls": "npm run cover && istanbul-coveralls" | ||
"test": "nyc mocha --async-only" | ||
}, | ||
"dependencies": { | ||
"append-buffer": "^1.0.2", | ||
"convert-source-map": "^1.5.0", | ||
"graceful-fs": "^4.1.6", | ||
"normalize-path": "^2.1.1", | ||
"now-and-later": "^2.0.0", | ||
"remove-bom-buffer": "^3.0.0", | ||
"vinyl": "^2.0.0" | ||
"convert-source-map": "^2.0.0", | ||
"graceful-fs": "^4.2.10", | ||
"now-and-later": "^3.0.0", | ||
"streamx": "^2.12.5", | ||
"vinyl": "^3.0.0", | ||
"vinyl-contents": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^1.10.3", | ||
"eslint-config-gulp": "^2.0.0", | ||
"expect": "^1.20.2", | ||
"istanbul": "^0.4.3", | ||
"istanbul-coveralls": "^1.0.3", | ||
"jscs": "^2.4.0", | ||
"jscs-preset-gulp": "^1.0.0", | ||
"mississippi": "^1.3.0", | ||
"mocha": "^3.2.0" | ||
"eslint": "^7.32.0", | ||
"eslint-config-gulp": "^5.0.1", | ||
"eslint-plugin-node": "^11.1.0", | ||
"expect": "^27.5.1", | ||
"mocha": "^8.4.0", | ||
"nyc": "^15.1.0", | ||
"readable-stream": "^3.6.0" | ||
}, | ||
"nyc": { | ||
"extension": [ | ||
".js" | ||
], | ||
"reporter": [ | ||
"lcov", | ||
"text-summary" | ||
] | ||
}, | ||
"prettier": { | ||
"singleQuote": true | ||
}, | ||
"keywords": [ | ||
@@ -51,0 +58,0 @@ "vinyl", |
<p align="center"> | ||
<a href="http://gulpjs.com"> | ||
<a href="https://gulpjs.com"> | ||
<img height="257" width="114" src="https://raw.githubusercontent.com/gulpjs/artwork/master/gulp-2x.png"> | ||
@@ -9,3 +9,3 @@ </a> | ||
[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![AppVeyor Build Status][appveyor-image]][appveyor-url] [![Coveralls Status][coveralls-image]][coveralls-url] [![Gitter chat][gitter-image]][gitter-url] | ||
[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Coveralls Status][coveralls-image]][coveralls-url] | ||
@@ -17,3 +17,3 @@ Add/write sourcemaps to/from Vinyl files. | ||
```js | ||
sourcemap.add(file, function(err, updatedFile) { | ||
sourcemap.add(file, function (err, updatedFile) { | ||
// updatedFile will have a .sourceMap property | ||
@@ -23,3 +23,3 @@ }); | ||
// The 2nd argument can be given as a path string | ||
sourcemap.write(file, './maps', function(err, updatedFile, sourcemapFile) { | ||
sourcemap.write(file, './maps', function (err, updatedFile, sourcemapFile) { | ||
// sourcemapFile will be a Vinyl file to be written to some location | ||
@@ -30,3 +30,3 @@ // updatedFile will have the .contents property updated with a sourceMappingURL that resolves to sourcemapFile | ||
// If not defined, the sourcemap is inlined | ||
sourcemap.write(file, function(err, updatedFile, sourcemapFile) { | ||
sourcemap.write(file, function (err, updatedFile, sourcemapFile) { | ||
// sourcemapFile is undefined | ||
@@ -43,3 +43,3 @@ // updatedFile will have the .contents property updated with a sourceMappingURL that is an inlined sourcemap | ||
Once all resolution is complete, the `callback(err, updatedFile)` is called with the `updatedFile`. If an error occurs, it will be passed as `err` and `updatedFile` will be undefined. __Note:__ The original file is mutated but `updatedFile` is passed to the callback as a convenience. | ||
Once all resolution is complete, the `callback(err, updatedFile)` is called with the `updatedFile`. If an error occurs, it will be passed as `err` and `updatedFile` will be undefined. **Note:** The original file is mutated but `updatedFile` is passed to the callback as a convenience. | ||
@@ -56,5 +56,5 @@ If the `file` is not a Vinyl object or the contents are streaming, an Error will be passed to the `callback`. | ||
If `outputPath` is not passed, an inline sourcemap will be generated from the `file.sourceMap` property and appended to the `file.contents`. Once the inline sourcemap is appended, the `callback(err, updatedFile)` is called with the `updatedFile`. If an error occurs, it will be passed as `err` and `updatedFile` will be undefined. __Note:__ The original file is mutated but `updatedFile` is passed to the callback as a convenience. | ||
If `outputPath` is not passed, an inline sourcemap will be generated from the `file.sourceMap` property and appended to the `file.contents`. Once the inline sourcemap is appended, the `callback(err, updatedFile)` is called with the `updatedFile`. If an error occurs, it will be passed as `err` and `updatedFile` will be undefined. **Note:** The original file is mutated but `updatedFile` is passed to the callback as a convenience. | ||
If `outputPath` is passed, a new Vinyl file will be generated using `file.cwd` and `file.base` from the original file, the path to the external sourcemap, and the `file.sourceMap` (as contents). The external location will be appended to the `file.contents` of the original file. Once the new file is created and location appended, the `callback(err, updatedFile, sourcemapFile)` is called with the `updatedFile` and the `sourcemapFile`. If an error occurs, it will be passed as `err` and `updatedFile`/`sourcemapFile` will be undefined. __Note:__ The original file is mutated but `updatedFile` is passed to the callback as a convenience. | ||
If `outputPath` is passed, a new Vinyl file will be generated using `file.cwd` and `file.base` from the original file, the path to the external sourcemap, and the `file.sourceMap` (as contents). The external location will be appended to the `file.contents` of the original file. Once the new file is created and location appended, the `callback(err, updatedFile, sourcemapFile)` is called with the `updatedFile` and the `sourcemapFile`. If an error occurs, it will be passed as `err` and `updatedFile`/`sourcemapFile` will be undefined. **Note:** The original file is mutated but `updatedFile` is passed to the callback as a convenience. | ||
@@ -69,18 +69,16 @@ If the `file` is not a Vinyl object or the contents are streaming, an Error will be passed to the `callback`. | ||
[vinyl]: https://github.com/gulpjs/vinyl | ||
[downloads-image]: http://img.shields.io/npm/dm/vinyl-sourcemap.svg | ||
<!-- prettier-ignore-start --> | ||
[downloads-image]: https://img.shields.io/npm/dm/vinyl-sourcemap.svg?style=flat-square | ||
[npm-url]: https://npmjs.com/package/vinyl-sourcemap | ||
[npm-image]: http://img.shields.io/npm/v/vinyl-sourcemap.svg | ||
[npm-image]: https://img.shields.io/npm/v/vinyl-sourcemap.svg?style=flat-square | ||
[travis-url]: https://travis-ci.org/gulpjs/vinyl-sourcemap | ||
[travis-image]: http://img.shields.io/travis/gulpjs/vinyl-sourcemap.svg?label=travis-ci | ||
[ci-url]: https://github.com/gulpjs/vinyl-sourcemap/actions?query=workflow:dev | ||
[ci-image]: https://img.shields.io/github/workflow/status/gulpjs/vinyl-sourcemap/dev?style=flat-square | ||
[appveyor-url]: https://ci.appveyor.com/project/gulpjs/vinyl-sourcemap | ||
[appveyor-image]: https://img.shields.io/appveyor/ci/gulpjs/vinyl-sourcemap.svg?label=appveyor | ||
[coveralls-url]: https://coveralls.io/r/gulpjs/vinyl-sourcemap | ||
[coveralls-image]: http://img.shields.io/coveralls/gulpjs/vinyl-sourcemap/master.svg | ||
[coveralls-image]: https://img.shields.io/coveralls/gulpjs/vinyl-sourcemap/master.svg?style=flat-square | ||
<!-- prettier-ignore-end --> | ||
[gitter-url]: https://gitter.im/gulpjs/gulp | ||
[gitter-image]: https://badges.gitter.im/gulpjs/gulp.png | ||
<!-- prettier-ignore-start --> | ||
[vinyl]: https://github.com/gulpjs/vinyl | ||
<!-- prettier-ignore-end --> |
Sorry, the diff of this file is not supported yet
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
16800
6
7
335
78
+ Addedstreamx@^2.12.5
+ Addedvinyl-contents@^2.0.0
+ Addedbare-events@2.5.0(transitive)
+ Addedbase64-js@1.5.1(transitive)
+ Addedbl@5.1.0(transitive)
+ Addedbuffer@6.0.3(transitive)
+ Addedconvert-source-map@2.0.0(transitive)
+ Addedfast-fifo@1.3.2(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addednow-and-later@3.0.0(transitive)
+ Addedqueue-tick@1.0.1(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedreplace-ext@2.0.0(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedstreamx@2.20.2(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedteex@1.0.1(transitive)
+ Addedtext-decoder@1.2.1(transitive)
+ Addedvinyl@3.0.0(transitive)
+ Addedvinyl-contents@2.0.0(transitive)
- Removedappend-buffer@^1.0.2
- Removednormalize-path@^2.1.1
- Removedremove-bom-buffer@^3.0.0
- Removedappend-buffer@1.0.2(transitive)
- Removedbuffer-equal@1.0.1(transitive)
- Removedclone-buffer@1.0.0(transitive)
- Removedcloneable-readable@1.1.3(transitive)
- Removedconvert-source-map@1.9.0(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removedis-buffer@1.1.6(transitive)
- Removedis-utf8@0.2.1(transitive)
- Removedisarray@1.0.0(transitive)
- Removednormalize-path@2.1.1(transitive)
- Removednow-and-later@2.0.1(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedremove-bom-buffer@3.0.0(transitive)
- Removedreplace-ext@1.0.1(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedstring_decoder@1.1.1(transitive)
- Removedvinyl@2.2.1(transitive)
Updatedconvert-source-map@^2.0.0
Updatedgraceful-fs@^4.2.10
Updatednow-and-later@^3.0.0
Updatedvinyl@^3.0.0