-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
282 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
|
||
var through = require('through2'); | ||
var normalizePath = require('normalize-path'); | ||
|
||
var generate = require('./lib/generate'); | ||
|
||
function identityMap() { | ||
|
||
function transform(file, _, cb) { | ||
if (!file.sourceMap || !file.isBuffer()) { | ||
return cb(null, file); | ||
} | ||
|
||
var sourcePath = normalizePath(file.relative); | ||
var contents = file.contents.toString(); | ||
|
||
switch (file.extname) { | ||
case '.js': { | ||
file.sourceMap = generate.js(sourcePath, contents); | ||
break; | ||
} | ||
case '.css': { | ||
file.sourceMap = generate.css(sourcePath, contents); | ||
break; | ||
} | ||
} | ||
|
||
cb(null, file); | ||
} | ||
|
||
return through.obj(transform); | ||
} | ||
|
||
module.exports = identityMap; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
'use strict'; | ||
|
||
var css = require('css'); | ||
var acorn = require('acorn'); | ||
var SourceMapGenerator = require('source-map').SourceMapGenerator; | ||
|
||
function generateJs(sourcePath, fileContent) { | ||
var generator = new SourceMapGenerator({ file: sourcePath }); | ||
var tokenizer = acorn.tokenizer(fileContent, { locations: true }); | ||
|
||
while (true) { | ||
var token = tokenizer.getToken(); | ||
|
||
if (token.type.label === 'eof') { | ||
break; | ||
} | ||
var mapping = { | ||
original: token.loc.start, | ||
generated: token.loc.start, | ||
source: sourcePath, | ||
}; | ||
if (token.type.label === 'name') { | ||
mapping.name = token.value; | ||
} | ||
generator.addMapping(mapping); | ||
} | ||
generator.setSourceContent(sourcePath, fileContent); | ||
|
||
return generator.toJSON(); | ||
} | ||
|
||
function generateCss(sourcePath, fileContent) { | ||
var generator = new SourceMapGenerator({ file: sourcePath }); | ||
var ast = css.parse(fileContent, { silent: true }); | ||
|
||
function registerTokens(ast) { | ||
if (ast.position) { | ||
generator.addMapping({ | ||
original: ast.position.start, | ||
generated: ast.position.start, | ||
source: sourcePath, | ||
}); | ||
} | ||
|
||
for (var key in ast) { | ||
if (key === 'position' || !ast[key]) { | ||
break; | ||
} | ||
if (Object.prototype.toString.call(ast[key]) === '[object Object]') { | ||
registerTokens(ast[key]); | ||
} else if (Array.isArray(ast[key])) { | ||
ast[key].forEach(registerTokens); | ||
} | ||
} | ||
} | ||
registerTokens(ast); | ||
generator.setSourceContent(sourcePath, fileContent); | ||
|
||
return generator.toJSON(); | ||
} | ||
|
||
module.exports = { | ||
js: generateJs, | ||
css: generateCss, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
body { | ||
background: #eee; | ||
color: #888; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
function helloWorld() { | ||
console.log('Hello world!'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
'use strict'; | ||
|
||
var fs = require('fs'); | ||
var path = require('path'); | ||
|
||
var expect = require('expect'); | ||
|
||
var miss = require('mississippi'); | ||
var File = require('vinyl'); | ||
|
||
var generate = require('../lib/generate'); | ||
|
||
var identityMap = require('../'); | ||
|
||
var pipe = miss.pipe; | ||
var from = miss.from; | ||
var concat = miss.concat; | ||
|
||
var jsContent = fs.readFileSync(path.join(__dirname, 'fixtures/helloworld.js')); | ||
var cssContent = fs.readFileSync(path.join(__dirname, 'fixtures/helloworld.css')); | ||
|
||
function makeFile() { | ||
var file = new File({ | ||
cwd: __dirname, | ||
base: __dirname + '/assets', | ||
path: __dirname + '/assets/helloworld.js', | ||
contents: jsContent, | ||
}); | ||
|
||
file.sourceMap = { | ||
version: 3, | ||
file: 'helloworld.js', | ||
names: [], | ||
mappings: '', | ||
sources: ['helloworld.js'], | ||
}; | ||
|
||
return file; | ||
} | ||
|
||
describe('identityMap', function() { | ||
|
||
it('ignores a file without sourceMap property', function(done) { | ||
var file = makeFile(); | ||
delete file.sourceMap; | ||
|
||
var spy = expect.spyOn(generate, 'js'); | ||
|
||
function assert(files) { | ||
expect.restoreSpies(); | ||
expect(files.length).toEqual(1); | ||
expect(spy).toNotHaveBeenCalled(); | ||
} | ||
|
||
pipe([ | ||
from.obj([file]), | ||
identityMap(), | ||
concat(assert), | ||
], done); | ||
}); | ||
|
||
it('only ignores a file without sourceMap property', function(done) { | ||
var file = makeFile(); | ||
delete file.sourceMap; | ||
var file2 = makeFile(); | ||
|
||
var spy = expect.spyOn(generate, 'js'); | ||
|
||
function assert(files) { | ||
expect.restoreSpies(); | ||
expect(files.length).toEqual(2); | ||
expect(spy.calls.length).toEqual(1); | ||
} | ||
|
||
pipe([ | ||
from.obj([file, file2]), | ||
identityMap(), | ||
concat(assert), | ||
], done); | ||
}); | ||
|
||
it('ignores a non-Buffer file', function(done) { | ||
var file = makeFile(); | ||
file.contents = null; | ||
|
||
var spy = expect.spyOn(generate, 'js'); | ||
|
||
function assert(files) { | ||
expect.restoreSpies(); | ||
expect(files.length).toEqual(1); | ||
expect(spy).toNotHaveBeenCalled(); | ||
} | ||
|
||
pipe([ | ||
from.obj([file]), | ||
identityMap(), | ||
concat(assert), | ||
], done); | ||
}); | ||
|
||
it('only ignores a non-Buffer file', function(done) { | ||
var file = makeFile(); | ||
file.contents = null; | ||
var file2 = makeFile(); | ||
|
||
var spy = expect.spyOn(generate, 'js'); | ||
|
||
function assert(files) { | ||
expect.restoreSpies(); | ||
expect(files.length).toEqual(2); | ||
expect(spy.calls.length).toEqual(1); | ||
} | ||
|
||
pipe([ | ||
from.obj([file, file2]), | ||
identityMap(), | ||
concat(assert), | ||
], done); | ||
}); | ||
|
||
it('adds a valid sourcemap for JS', function(done) { | ||
var file = makeFile(); | ||
|
||
function assert(files) { | ||
expect(files.length).toEqual(1); | ||
|
||
var sourcemap = files[0].sourceMap; | ||
expect(sourcemap).toExist(); | ||
expect(sourcemap.version).toEqual('3'); | ||
expect(sourcemap.sources[0]).toEqual('helloworld.js'); | ||
expect(sourcemap.sourcesContent[0]).toEqual(jsContent); | ||
expect(sourcemap.names).toEqual(['helloWorld', 'console','log']); | ||
expect(sourcemap.mappings).toEqual('AAAA,YAAY;;AAEZ,SAASA,UAAU,CAAC,EAAE;CACrBC,OAAO,CAACC,GAAG,CAAC,cAAc,CAAC;AAC5B'); | ||
} | ||
|
||
pipe([ | ||
from.obj([file]), | ||
identityMap(), | ||
concat(assert), | ||
], done); | ||
}); | ||
|
||
it('adds a valid source map for CSS', function(done) { | ||
var file = makeFile(); | ||
file.extname = '.css'; | ||
file.contents = cssContent; | ||
|
||
function assert(files) { | ||
expect(files.length).toEqual(1); | ||
|
||
var sourcemap = files[0].sourceMap; | ||
expect(sourcemap).toExist(); | ||
expect(sourcemap.version).toEqual('3'); | ||
expect(sourcemap.sources[0]).toBe('helloworld.css'); | ||
expect(sourcemap.sourcesContent[0]).toEqual(cssContent); | ||
expect(sourcemap.names).toEqual([]); | ||
expect(sourcemap.mappings).toBe('CAAC;EACC;EACA'); | ||
} | ||
|
||
pipe([ | ||
from.obj([file]), | ||
identityMap(), | ||
concat(assert), | ||
], done); | ||
}); | ||
}); |