-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for node-webkit #481
Comments
+1 on this issue It would be great if browserify has an option for renaming BTW, this is my workaround using grunt-browserify (which looks hacky): browserify: {
dist: {
options: {
postBundleCB: function (err, src, next) {
// HACK: save Node's `require` before it gets overrided by browserify
next(err, 'nodeRequire = require; ' + src)
}
}
}
} Then when referring to Node.js modules, you use sth like |
+1 I can't seem to get it to work even using your suggested workaround and a few others. Any other hints on how you guys got it working correctly? These are the two errors I continue to get regardless of what I try. :(
|
This may be or sound like a noob question, but why browserify for node-webkit? Is it part of an obfuscation step? |
@mephux: Here is the basic structure of my document (using Jade):
Here's the essence of
Owing to the fact that is is being require'd instead of being included directly by a script tag, As for |
Darn! I closed the issue by mistake - trying to re-open now... |
@drkibitz: No, it's not for obfuscation, I simply wanted to be able to use CommonJS rather than AMD. I'm not yet 100% sure that was a good idea within node-webkit :-). |
Can't you just do:
and then we don't need to bake this into browserify itself? |
I did exactly what @substack is suggesting in the ace editor version that gets auto refactored to be browserifyable and inline webworkers. So you could implement a transform that works similarly. |
@substack: that is one way to solve that part of the problem, though it requires an extra scan or parsing pass. It does not address the other part though (the mocked-up process object). Maybe there is a misunderstanding on my part somewhere. I see browserify not only as a tool to make CommonJS require possible in the browser, but even more importantly as a way to automaticaly collect all js files that my code needs, and only those it needs. In fact, I'm mildly suprised that no equivalent tool seems to exist for node itself. |
You can use the new
to run my tests with code coverage in node. This works in libraries that use core node APIs because browserify's shims are all turned off. I can run the test suite for node-mkdirp for example. |
@substack: thanks a ton! This is what I was hoping for. |
Just for info, to use this in context of grunt-browserify,
|
@substack, @mkramb Ok, that didn't work for me with gulp-browserify; gulp.task('scripts', function(){
gulp.src(paths.appEntryScript, {read:false})
.pipe(browserify({
transform: ['coffeeify'],
extensions: ['.coffee'],
insertGlobals: false,
detectGlobals: false
}))
.pipe(rename('app.js'))
.pipe(gulp.dest(paths.publicJS));
}); The only way I could get this all working is to use gulp.task('scripts', function(){
gulp.src(paths.appEntryScript, {read:false})
.pipe(browserify({
transform: ['coffeeify'],
extensions: ['.coffee']
}))
.pipe(replace('require', 'requireClient'))
.pipe(replace('nequire', 'require'))
.pipe(rename('app.js'))
.pipe(gulp.dest(paths.publicJS));
}); Example usage: $ = nequire 'jquery'
gui = require 'nw.gui'
gui.Window.get().on 'minimize', () ->
$('body').css('background', 'red'); |
@JPGygax68 Then to use node's |
@trevordixon Actually, my approach was to avoid mixing the two forms of |
@JPGygax68 Can you provide a snippet to demonstrate your approach? I can't quite follow the solution and also want to make node-webkit and browserify working together. |
It should be as simple as that (using Jade for brevity): doctype html
html
head
title My Project
script.
// This script executes in Node context.
var nodeRequire = require; // just in case you still need to node-require later
// Anything require'd here will also execute in Node context.
var aNodeModule = require('thenodemodule');
link(rel="stylesheet", href="style.css")
// The following is a Browserify'd bundle and will run in Webkit context
script(src="main.js")
body
h1 Hello World! Apparently under Node-Webkit, embedded scripts execute in Node context, while external ones execute in Webkit context. |
@JPGygax68 Thanks, it helps a lot. |
@JPGygax68 Thanks for sharing 👍 |
I'm still seeing this issue, especially when a dependency requires a node module. |
The following are suggestions.
The node-webkit project fuses NodeJS and webkit into a single runtime environment.
This makes it easy to require() modules intended for Node, which will (mostly) work. Sometimes though, you want to load modules into the browser "aspect" of node-webkit, and browserify is great for that. However, there are two problems:
browserify's require() hides the one provided by the NodeJS aspect of node-webkit.
(This can be worked around by simply putting all the node-dependent modules before the browserify-generated bundle)
browserify generates a "process" object that hides the one provided by the NodeJS aspect of node-webkit.
(This can be worked around as well, by assigning the original process object to a global variable.)
Even though work-arounds exist for both issues, I think it would be better if browserify could be told to a) provide its require() method under a different name and b) refrain from generating its own process object.
Other than that, thanks for providing a great tool!
The text was updated successfully, but these errors were encountered: