Support for option `standalone`
It would be great to have support for browserifies option standalone, so we can create standalone modules.
Right now when I do:
gulp.src('./index.js')
.pipe(browserify({
standalone: 'mylib'
}))
.pipe(concat('mylib.js'))
.pipe(gulp.dest('./dist'));
I get an error:
stream.js:94
throw er; // Unhandled stream error in pipe.
^
standalone only works with a single entry point
Thanks for the info , will check it out.
Example using browserify api:
var fs = require('fs');
var browserify = require('browserify');
var b = browserify('./beep.js');
b.bundle({standalone: 'beep-boop'}).pipe(fs.createWriteStream(__dirname + '/bundle.js'));
Should be easy enough to just pass to ctrOpts.
@deepak1556 Can't you just deep clone opts, and then modify/add properties as your plugin is now doing? That way gulp-browserify is at least future compatible with browserify.
@Dashed could you patch up a PR for this, currently held up in some other stuff. Thanks!
Probably won't be able to. However, I think @josdejong would do a better job of the PR than I could.
I've had a quick look so far but couldn't solve it immediately. The standalone option is already neatly passed to browserify via ctrOpts, so that's ok.
The error "standalone only works with a single entry point" is thrown by browserify from this line because the number of entries is 0 instead of 1.
I will see if I can give it a closer look this week.
That's because streams are not added to self._entries but self.files.
https://github.com/substack/node-browserify/blob/master/index.js#L112
It seems that not all features of node-browserify work well with streams. require() and exclude() also do not work with streams. Currently, a workaround would be gulp.src('something.js', { read: false }) because it emits only file path but not stream.
@shuhei thanks for your workaround, this indeed works.
I have created an issue here: https://github.com/substack/node-browserify/issues/594
@shuhei thanks for the info!
@shuhei The workarround doesn't seem to work for me it fixed the "standalone only works with a single entry point", but it introduced a new one:
node_modules\gulp-browserify\node_modules\brows
erify\node_modules\umd\index.js:8
.replace(/\{\{name\}\}/g, moduleName.toLowerCase())
^
TypeError: Object #<error> has no method 'toLowerCase'
at template (c:\Users\gabriel\Desktop\website\node_modules\gulp-browserify\n
ode_modules\browserify\node_modules\umd\index.js:8:44)
at Function.exports.prelude (c:\Users\gabriel\Desktop\website\node_modules\g
ulp-browserify\node_modules\browserify\node_modules\umd\index.js:49:10)
at Stream.writePrelude (c:\Users\gabriel\Desktop\website\node_modules\gulp-b
rowserify\node_modules\browserify\index.js:469:35)
at Stream.write (c:\Users\gabriel\Desktop\website\node_modules\gulp-browseri
fy\node_modules\browserify\index.js:449:33)
at Stream.stream.write (c:\Users\gabriel\Desktop\website\node_modules\gulp-b
rowserify\node_modules\browserify\node_modules\through\index.js:26:11)
at Stream.ondata (stream.js:51:26)
at Stream.EventEmitter.emit (events.js:101:17)
at drain (c:\Users\gabriel\Desktop\website\node_modules\gulp-browserify\node
_modules\browserify\node_modules\through\index.js:36:16)
at Stream.stream.queue.stream.push (c:\Users\gabriel\Desktop\website\node_mo
dules\gulp-browserify\node_modules\browserify\node_modules\through\index.js:45:5
)
at Stream.write (c:\Users\gabriel\Desktop\website\node_modules\gulp-browseri
fy\node_modules\browserify\node_modules\browser-pack\index.js:38:27)
Code used:
gulp.task('scripts', function() {
gulp.src('public/js/app.js', {read:false})
.pipe(browserify({
insertGlobals: true,
debug: true,
standalone: true
}))
.pipe(concat('dest.js'))
.pipe(gulp.dest('build/js'));
});
@powerfear standalone option should be a module name string instead of true.
Now { read: false } workaround is no longer necessary for standalone since e0755c384eb4d722e7f0efc9c389dddfb96ef1eb. It's still necessary for non .js files, though.
Closing.
hm, I still get the same error here...
@josdejong Sorry for my wrong statement. I checked the code of node-browserify and made sure that it wouldn't work with stream input. Does { read: false } still work for you?
yes, { read: false } works just fine.
I guess to get standalone working, gulp-browserify needs to read the input stream into a string and feed that to browserify?
When node-browserify gets a string, it thinks it's a file name and it reads the file's content by itself. node-browserify primarily supports file names instead of streams and that's why { read: false } solves most of the issues on this plugin.
Now I think it would be better for this plugin to pass file names to node-browserify by default even if it gets file contents from gulp.src or other streams. This behavior solves most of the issues. I don't think it makes sense to pass only contents of entry points as streams to node-browserify and ask it to read other dependencies. If users want to transform entry points, they can use bowrserify's source transforms anyway. Just in case, we can add an option to pass files contents for unexpected usecases.
I'll make a PR when I get some free time.