boardgameio / boardgame.io

State Management and Multiplayer Networking for Turn-Based Games
https://boardgame.io
MIT License
10.04k stars 709 forks source link

Server export difference in v0.34.x #518

Closed nearwood closed 4 years ago

nearwood commented 4 years ago

It looks like the exports were changed and maybe Server is not longer exported. Using this small example:

const Server = require('boardgame.io/server').Server;

const server = Server({
  games: [],
});

server.run(8000);

With:

Results in:

$ node -r esm server.js 
/home/nick/dev/tmp-boardgame.io/server.js:1
Error: Cannot find module 'boardgame.io/server'
Require stack:
- /home/nick/dev/tmp-boardgame.io/server.js
    at Object.<anonymous> (/home/nick/dev/tmp-boardgame.io/server.js:1)
    at Generator.next (<anonymous>) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/home/nick/dev/tmp-boardgame.io/server.js' ]
}

It looks like it may not be exported anymore:

$ console.dir(require('boardgame.io));
{
  AI: [Function: AI],
  Client: [Function: Client],
  MCTSBot: [Function: MCTSBot],
  RandomBot: [Function: RandomBot],
  ReactClient: [Function: Client$1],
  ReactNativeClient: [Function: Client$2],
  TurnOrder: {
    DEFAULT: { first: [Function: first], next: [Function: next] },
    RESET: { first: [Function: first], next: [Function: next] },
    ONCE: { first: [Function: first], next: [Function: next] },
    CUSTOM: [Function: CUSTOM],
    CUSTOM_FROM: [Function: CUSTOM_FROM],
    SKIP: { first: [Function: first], next: [Function: next] }
  }
}

But I was able to find it at :

$ console.dir(require('boardgame.io/dist/server'));
{
  Firebase: [Function: Firebase],
  FlatFile: [Function: FlatFile],
  Mongo: [Function: Mongo],
  Server: [Function: Server]
}

So you can use const Server = require('boardgame.io/dist/server').Server; but that is weird. Advise on proper way to import it now?

nicolodavis commented 4 years ago

Ah, this is a bug because I forgot to consider server.js in https://github.com/nicolodavis/boardgame.io/blob/master/scripts/proxy-dirs.js

nearwood commented 4 years ago

Cool. Looks like I can just add it to subpackages.js? Easy first PR!

EDIT: Not so fast. Looks like the server code was missed in a conversion from typescript. I also cannot get whatever native library that is compiled on install to compile (but it doesn't look needed yet). I'll try a bit more comparing with the other modules.

nicolodavis commented 4 years ago

Sure, feel free to send a PR.

It's not as simple as adding it to subpackages.js though. The other subpackages are generated in dist/esm and dist/cjs. What we do to wire them both is to generate a package.json file with a module and main field pointing to each.

So, we'd generate something like: client/package.json core/package.json ai/package.json ...

at the top level.

The server package does not have an esm version (we just generate a cjs version at ./dist/server.js like you've discovered already). So we'd need to generate a different package.json for it. Simpler yet, we don't need to even generate a package.json for it but just a simple server.js at the top level that imports ./dist/server.js.

We'd also need to add a shell.rm here to clean up the server related files at the top-level. The way this whole thing works is that proxy-dirs.js is called right before the package is published (so we end up with these files at the top-level that can be imported as boardgame.io/client for example). clean.js is run right after the package is published so these temporary files aren't kept around.

nearwood commented 4 years ago

Ok, I understand the submodule generation at at least a high level. But, it looks like Rollup is having issues with the typescript in the server module. I see there's a typescript plugin to convert it but something or some config appears to be missing.

nicolodavis commented 4 years ago

But you are able to see dist/server.js and even import it in your client code, so why do you think the typescript compilation isn't working?

On Thu, Nov 21, 2019, 11:00 AM Nick Earwood notifications@github.com wrote:

Ok, I understand the submodule generation at at least a high level. But, it looks like Rollup is having issues with the typescript in the server module. I see there's a typescript plugin to convert it but something appears to be missing.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/nicolodavis/boardgame.io/issues/518?email_source=notifications&email_token=AABZULW3OLRKKXBVBK2KHOTQUX2VDA5CNFSM4JP2INE2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEEYWVGY#issuecomment-556886683, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABZULU4MS3WGIPNS4NJSTLQUX2VDANCNFSM4JP2INEQ .

nearwood commented 4 years ago

Yes, I was able to get it working on a packaged version from npm. Here, I am trying to build it locally to create a PR. I think I just figured it out though.

nearwood commented 4 years ago

Ok, created #519, let me know if it needs any changes.

nicolodavis commented 4 years ago

Thanks for the PR @nearwood!