From f2287b2f29f0881441c79fb71c737f04ad96b9ab Mon Sep 17 00:00:00 2001 From: Vohmyanin Sergey Vasilevich Date: Thu, 20 Apr 2017 19:37:11 +0300 Subject: [PATCH] Fix: Allow hashbang in JS files (fixes #3) (#4) --- lib/generate.js | 2 +- test/fixtures/helloworld.bin.js | 6 ++++++ test/index.js | 24 ++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/helloworld.bin.js diff --git a/lib/generate.js b/lib/generate.js index 10631b0..433e855 100644 --- a/lib/generate.js +++ b/lib/generate.js @@ -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(); diff --git a/test/fixtures/helloworld.bin.js b/test/fixtures/helloworld.bin.js new file mode 100644 index 0000000..1811bd2 --- /dev/null +++ b/test/fixtures/helloworld.bin.js @@ -0,0 +1,6 @@ +#!/usr/bin/env node +'use strict'; + +function helloWorld() { + console.log('Hello world!'); +} diff --git a/test/index.js b/test/index.js index 5e50ff5..014f0e6 100644 --- a/test/index.js +++ b/test/index.js @@ -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() { @@ -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';