Skip to content
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

Correct way to use Steal on Node? #800

Closed
stucox opened this issue Jul 21, 2016 · 9 comments

Comments

Projects
None yet
3 participants
@stucox
Copy link

commented Jul 21, 2016

I have some code I wrote to use client-side, but would like to reuse it on the server. It currently depends on my Steal config — specifically a YAML translate() plugin. In general it’d be nice if I could have my Steal config apply both on client & server.

My naive attempt to run my code on Node with Steal in control was:

// start-server.js
import steal from 'steal';
steal.startup({main: 'src/server'});
babel-node start-server.js

But my server-side code also includes modules which use Node builtins, which aren’t found.

I tried adding steal-builtins — but I realised that’s more designed to provide would-be-node modules in the browser — e.g. require('http') loads http-browserify, which realises it’s not running in a browser so throws a no window object present’ error.

I saw that #749 brings these into core — but looks like it’s still using http-browserify, so I’d guess the same issue would arise?

@matthewp

This comment has been minimized.

Copy link
Member

commented Jul 21, 2016

The canonical way would be:

var steal = require("steal");

steal.import("my-main").then( ...

steal.import will take care of making sure startup has been called.

As for your builtins issue, you might try using node-locate which should resolve to the Node's own builtins. This is an experimental module (intended for 1.1) so your results may vary, but it's worth a shot. Would be something like:

var steal = require("steal");
var nodeLocate = require("@stealjs/node-locate");

// This configures steal to use the extension
nodeLocate(steal.System);

steal.import("my/main").then( ...
@stucox

This comment has been minimized.

Copy link
Author

commented Jul 22, 2016

Thanks for the response.

Results:

  1. I had main: "src/client" in my package.json — with this, calling steal.import("src/server") in my server-side code still loads src/client.js, which isn’t what I want.
  2. Removing main from package.json and explicitly calling steal.import("src/client") in my client-side code causes Steal to try and fetch an index.js file from the root of my repo — which didn’t exist. Added an empty index.js and now client-side just loads src/client.js and server-side just loads src/server.js, which is what I’m after (but feels dirty!)
  3. Some paths are not being loaded correctly — e.g. I’m importing snabbdom-to-html, for which Steal correctly loads its src/index.js entry point, but the require('./init') call within that file rather than loading node_modules/snabbdom-to-html/src/init.js tries to load /init.js from the root of my project. Is this likely to be a bug in node-locate? If so I can raise an issue in that repo and discuss further / do my best to contribute.
@matthewp

This comment has been minimized.

Copy link
Member

commented Jul 22, 2016

Thanks, 1&2 are bugs, with steal.import we shouldn't force the main to load first. Could you file a new issue for that here? If so I can probably fix it soon.

3 I'm not sure about, can you recreate it in the browser?

@stucox

This comment has been minimized.

Copy link
Author

commented Jul 25, 2016

Re. 1&2: Done: #802

3: I’ll try and create a minimal case to reproduce it.

@pYr0x pYr0x added the question label Jul 26, 2016

@stucox

This comment has been minimized.

Copy link
Author

commented Aug 3, 2016

Ok, here’s a test case — not with the same lib, but it looks like it might be the same cause:

https://gist.github.com/stucox/889767b4677818fbcefe35c1138a781e

(reproduced in the browser without node-locate, so looks like this isn’t Node-specific, or a bug in node-locate, so feel free to rename this issue accordingly!)

@matthewp matthewp added bug and removed question labels Aug 3, 2016

@matthewp

This comment has been minimized.

Copy link
Member

commented Aug 3, 2016

Thanks, will take a look.

@matthewp matthewp added this to the Bug fixes milestone Oct 6, 2016

@stucox

This comment has been minimized.

Copy link
Author

commented Oct 6, 2016

Sorry about the silence — after a bit of absence, I tried the test case above with 0.16.38 and still the same result: Steal tries to request localhost:8080/es5-ext/array/%23/clear.js, when it should be localhost:8080/node_modules/es5-ext/array/%23/clear.js.

Have got as far as finding out it’s down to the path normalization — maybe it doesn’t deal with the /%23/ (i.e. /#/) too well? (which is a really weird thing to be in a path anyway). Although I don’t think the other case I found these sorts of issues with had any such weird paths.

Will report back when I have more.

@matthewp

This comment has been minimized.

Copy link
Member

commented Oct 6, 2016

I was going to possibly take a look today.

@matthewp

This comment has been minimized.

Copy link
Member

commented Oct 25, 2016

Moved this over to stealjs/steal-npm#192

@matthewp matthewp closed this Oct 25, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.