Skip to content

Commit

Permalink
Fix: Allow hashbang in JS files (fixes #3) (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delagen authored and phated committed Apr 20, 2017
1 parent af3c4b0 commit f2287b2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var SourceMapGenerator = require('source-map').SourceMapGenerator;

function generateJs(sourcePath, fileContent) {
var generator = new SourceMapGenerator({ file: sourcePath });
var tokenizer = acorn.tokenizer(fileContent, { locations: true });
var tokenizer = acorn.tokenizer(fileContent, { allowHashBang: true, locations: true });

while (true) {
var token = tokenizer.getToken();
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/helloworld.bin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env node
'use strict';

function helloWorld() {
console.log('Hello world!');
}
24 changes: 24 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var from = miss.from;
var concat = miss.concat;

var jsContent = fs.readFileSync(path.join(__dirname, 'fixtures/helloworld.js'));
var jsHashBangContent = fs.readFileSync(path.join(__dirname, 'fixtures/helloworld.bin.js'));
var cssContent = fs.readFileSync(path.join(__dirname, 'fixtures/helloworld.css'));

function makeFile() {
Expand Down Expand Up @@ -140,6 +141,29 @@ describe('identityMap', function() {
], done);
});

it('adds a valid sourcemap for JS with hashBang', function(done) {
var file = makeFile();
file.contents = jsHashBangContent;

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(jsHashBangContent);
expect(sourcemap.names).toEqual(['helloWorld', 'console','log']);
expect(sourcemap.mappings).toEqual(';AACA,YAAY;;AAEZ,SAASA,UAAU,CAAC,EAAE;EACpBC,OAAO,CAACC,GAAG,CAAC,cAAc,CAAC;AAC7B');
}

pipe([
from.obj([file]),
identityMap(),
concat(assert),
], done);
});

it('adds a valid source map for CSS', function(done) {
var file = makeFile();
file.extname = '.css';
Expand Down

0 comments on commit f2287b2

Please sign in to comment.