From 91a52421d41b1f4223177163566eaa4559e89ea9 Mon Sep 17 00:00:00 2001 From: Mike Grabowski Date: Tue, 19 Apr 2016 18:08:59 -0700 Subject: [PATCH] Strip shebang when present in JS files Summary:Fixes #7034 Closes https://github.com/facebook/react-native/pull/7073 Differential Revision: D3199816 fb-gh-sync-id: 2099dd1f81b030933794be6a592a697cec3627d0 fbshipit-source-id: 2099dd1f81b030933794be6a592a697cec3627d0 --- .../JSTransformer/worker/__tests__/worker-test.js | 15 ++++++++++++++- .../src/JSTransformer/worker/index.js | 3 +++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/packager/react-packager/src/JSTransformer/worker/__tests__/worker-test.js b/packager/react-packager/src/JSTransformer/worker/__tests__/worker-test.js index b08035098..b66dabe2a 100644 --- a/packager/react-packager/src/JSTransformer/worker/__tests__/worker-test.js +++ b/packager/react-packager/src/JSTransformer/worker/__tests__/worker-test.js @@ -68,7 +68,20 @@ describe('code transformation worker:', () => { done(); }); }); - + + it('removes shebang when present', done => { + const shebang = '#!/usr/bin/env node'; + const result = { + code: `${shebang} \n arbitrary(code)`, + }; + transform.mockImplementation((_, callback) => callback(null, result)); + transformCode(transform, 'arbitrary/file.js', 'b', {}, (_, data) => { + expect(data.code).not.toContain(shebang); + expect(data.code.split('\n').length).toEqual(result.code.split('\n').length); + done(); + }); + }); + it('calls back with any error yielded by the transform', done => { const error = Error('arbitrary error'); transform.mockImplementation((_, callback) => callback(error)); diff --git a/packager/react-packager/src/JSTransformer/worker/index.js b/packager/react-packager/src/JSTransformer/worker/index.js index 1b68feaed..a767f9996 100644 --- a/packager/react-packager/src/JSTransformer/worker/index.js +++ b/packager/react-packager/src/JSTransformer/worker/index.js @@ -43,6 +43,9 @@ function transformCode(transform, filename, sourceCode, options, callback) { if (isJson) { code = code.replace(/^\w+\.exports=/, ''); + } else { + // Remove shebang + code = code.replace(/^#!.*/, ''); } const result = isJson || options.extern