juliangruber / browser-run

Run code inside a browser from the command line
447 stars 62 forks source link

Ability to add additional script tags to html #23

Closed btakita closed 5 years ago

btakita commented 9 years ago

I'm testing a set of interdependent libraries using browserify. I'd like to load multiple <script> tags into <head>.

Right now, static/index.html is loaded. I'd like the ability to use a custom html file and/or have a custom handler for GET /.

btakita commented 9 years ago

I forced pushed https://github.com/btakita/browser-run/commit/38b3f786a92f8eb76bc521cf7c81659e0933823a

juliangruber commented 9 years ago

if you set .input or -input to "html", browser-run run accepts a html file and that can point to any javascript file you want. does that work for you?

neojski commented 9 years ago

It would be useful for me as well. I wanted to run tape tests using browser-run. But I need some custom html to run it. -input html does not work because the server does not serve my dist/js/*.js files.

juliangruber commented 9 years ago

Would something like browser-run --script dist/js/foo.js --script dist/js/bar.js work for you guys? Maybe help me understand your usecase better as I don't see how that can't be achieved with a custom html file.

neojski commented 9 years ago

Try this:

a.js:
console.log('test')
test.html:
<script src="a.js"></script>

Then browser-run --browser chrome --input html < test.html. This won't work because the file a.js won't be accessible from the browser-run server.

juliangruber commented 9 years ago

one thing you need to keep in mind is that a custom html file will always need to have <script src="/reporter.js"></script>, i'm adding this to the docs now.

yeah so as i see it we need a static file server at least... I'm thinking

Since I don't have this use case I'm depending on your input what would work best. My gut says the 2nd option should be the most convenient.

neojski commented 9 years ago

For me I'd prefer to serve the current directory at /. But then what would happen if there is already a reporter.js file in that directory?

My use case is running https://github.com/substack/tape but I need to include two files: dependencies.js and index.js.

juliangruber commented 9 years ago

may i ask what's the reason dependencies.js and index.js haven't been compiled into the same file? like browserify index.js dependencies.js | browser-run

neojski commented 9 years ago

It''s because dependencies.js contains all of the external dependencies but I have index.js and something.js and yet_another_something.js.

juliangruber commented 9 years ago

take this simple example:

// dependencies.js
window.foo = 'bar';
// something.js
console.log(window.foo);
$ browserify dependencies.js something.js | browser-run
bar
neojski commented 9 years ago

I realize that I can bundle it but there is something preventing me from doing that. But maybe I just also realized that I can make that obstacle disappear with small amount of work.

Feel free to close this issue with, possibly, having a comment about multiple js files (or about how they will be injected into html together with fake console.log). On Sep 15, 2015 3:39 AM, "Julian Gruber" notifications@github.com wrote:

take this simple example:

// dependencies.jswindow.foo = 'bar';

// something.jsconsole.log(window.foo);

$ browserify dependencies.js something.js | browser-run bar

— Reply to this email directly or view it on GitHub https://github.com/juliangruber/browser-run/issues/23#issuecomment-140307953 .

juliangruber commented 9 years ago

awesome! @btakita same for you?

btakita commented 9 years ago

I'd rather allow multiple js files to test external requires & my build script.

I want the code that I test to be the same as, or close to, production code.

On Tue, Sep 15, 2015 at 7:20 PM, Julian Gruber notifications@github.com wrote:

awesome! @btakita https://github.com/btakita same for you?

— Reply to this email directly or view it on GitHub https://github.com/juliangruber/browser-run/issues/23#issuecomment-140578913 .

Brian Takita briantakita.com about.me/brian_takita

neojski commented 9 years ago

In my case I just ended up getting rid of one strange dependency and running: cat dist/js/dependencies.js dist/js/tests/test.js | browser-run -b chrome

btakita commented 9 years ago

Ok, does that create one concatenated javascript file?

I want to test multiple javascript files, just like how things will be in production.

On Wed, Sep 16, 2015 at 8:57 PM, Tomasz Kołodziejski < notifications@github.com> wrote:

In my case I just ended up getting rid of one strange dependency and running: cat dist/js/dependencies.js dist/js/tests/test.js | browser-run -b chrome

— Reply to this email directly or view it on GitHub https://github.com/juliangruber/browser-run/issues/23#issuecomment-140935661 .

Brian Takita briantakita.com about.me/brian_takita

juliangruber commented 9 years ago

it would be nice to use a format that concatenates many js files into one but tells browser-run to serve as multiple. something like

pack src/*.js | browser-run -b chrome

i'll try to come up with a poc

juliangruber commented 9 years ago

ah let's rather keep it simple and have an api like

browser-run -b chrome src/*.js
btakita commented 9 years ago

The concatenation is nice, as long as I can specify multiple individual files:

browser-run -b chrome src/foo.js src/bar.js

btakita commented 9 years ago

-input html may work. I'll give it a shot & report back. Are there any docs on using -input html?

btakita commented 9 years ago

I'm attempting to pipe html into a node process, with input: html by calling the tape-run module in javascript. The process hangs on GET / :-(

I added logging into browser-run/index.js to debug.

$ echo '<html></html>' | test/tape-run.js
server
server|html
server|html|/
var tapeRun = require('tape-run');
var _ = require("lodash");
function run(opts) {
  return tapeRun(_.extend({input: "html"}, opts))
    .on('results', console.log)
    .pipe(process.stdout);
}
module.exports = run;
if (process.argv[1] == __filename) {
  var argv = require("yargs").argv;
  run(argv);
}
lastmjs commented 8 years ago

I agree @btakita , I'm wondering how -input html works?

juliangruber commented 8 years ago

does this help? https://github.com/juliangruber/browser-run#custom-html-file

lastmjs commented 8 years ago

@juliangruber I'm confused with the command. Where do I specify the filename?

ashnur commented 8 years ago

@lastmjs maybe this will help more? https://github.com/juliangruber/browser-run/blob/master/index.js#L23

juliangruber commented 8 years ago

@lastmjs

$ echo myCustom.html | browser-run -input html

That's what the -input option means, that what you feed into browser-run is html instead of javascript. Just make sure your html has <script src="/reporter.js"></script> above all other script tags.

lastmjs commented 8 years ago

Okay, got it. Thank you.

juliangruber commented 8 years ago

I'm attempting to pipe html into a node process, with input: html by calling the tape-run module in javascript. The process hangs on GET / :-(

your html is missing the <script src="/reporter.js"></script>

PriyeshGohil commented 7 years ago

Hey guys, I'm also trying to pipe a html file, however I keep getting a SyntaxError: Unexpected token < error. I know for sure the html is valid. Here is the code:

var stream = fs.createReadStream(__dirname + '/test.html');
stream
    .pipe(run([{'browser': 'chrome'}, {'input':'html'}]))
    .pipe(tapSpec())
    .pipe(process.stdout);

Any ideas why I'm getting this error ? thanks.

juliangruber commented 7 years ago

try run({browser: 'chrome', input:'html'})) instead

PriyeshGohil commented 7 years ago

It works! thanks dude.

fregante commented 5 years ago

Sounds resolved