warriorjs icon indicating copy to clipboard operation
warriorjs copied to clipboard

Allow import/require in Player.js for modularity of user code

Open hexpunk opened this issue 6 years ago • 9 comments

Originally mentioned in https://github.com/olistic/warriorjs/issues/39, it would be a great improvement to code organization if users like myself could split our code into modules (*.js and *.mjs) that could be loaded into the Player.js file (via import and/or require).

It doesn't appear that this is supported at the moment. That means that a lot of code ends up in Player.js. At various times while playing, I've been in the neighborhood of 250 lines.

hexpunk avatar May 14 '18 19:05 hexpunk

Hello @JayAndCatchFire and thanks for creating this issue. I'm open to receive a PR that implements this.

olistic avatar May 14 '18 19:05 olistic

I'll be happy to take a crack at it, but I can't guarantee anything forthcoming immediately. So much to do and so little time. So if anyone else sees this ticket and wants to try their hand, please do!

hexpunk avatar May 14 '18 19:05 hexpunk

Please comment here if you grab this so I mark it as claimed.

olistic avatar May 14 '18 20:05 olistic

looks like this can be achieved using vm.Module, but this is an experimental feature. I think this would also have to be feature flagged.

--experimental-vm-modules is the required nodejs flag

this requires running bin/warrior.js with node --experimental-vm-modules path/to/node_modules/warrior-cli/bin/warrior.js

gordon-to avatar May 15 '18 02:05 gordon-to

Pleasee i need some of this!!!! :)))))

netzulo avatar May 17 '18 11:05 netzulo

@netzulo you can work around it by importing your code as a string and call eval(code)

gordon-to avatar May 18 '18 00:05 gordon-to

I published a tutorial on how this can be achieved using a module bundler here.

olistic avatar May 18 '18 00:05 olistic

@olistic in addition to your module bundler solution, in order to automatically remove the two last lines, you can run: rollup src/main.js --f cjs | head -n -2 > Player.js ;)

AFaugeras avatar May 26 '18 09:05 AFaugeras

News on this? If a contributor can point me on the right direction I'm available to implement it this weekend

alexmachina avatar Feb 06 '19 23:02 alexmachina