2

I'm very interested in module loaders for node.js and also the new harmony modules specification.

In general, I like the spec, but there is a problem that I'm not sure about how it is solved.

If one wants to build a cross-platform JavaScript library, it's sometimes necessary to use different libraries on different platforms for the same job. For example, the Buffer module in node.js, does not exist in the Browser, so one would have to write an abstraction module which loads, depending on the platform, different code.

Since harmony resolves the dependencies before the execution of any code, how can it know, that it only has to load the code for one platform, and not for all?

In the browser, I don't want to load code that only runs in node.js, so how is this problem solved?

Van Coding
  • 24,244
  • 24
  • 88
  • 132

1 Answers1

1

There will be two ways to load a module in ES6: (1) statically, through the module X at "url" declaration (or whatever concrete syntax it will end up with), or (2) dynamically, through the load method of a loader. For your scenario, you probably want to use option 2 for those modules that depend on the context.

In the browser, you also have ways of staging your program using multiple <script> tags, but since that doesn't apply to node.js, it probably doesn't help your use case.

Andreas Rossberg
  • 34,518
  • 3
  • 61
  • 72
  • I know of the module loader API, but is there really no way to do this statically? Later, I want that other programmers can embed my library through the static method. – Van Coding Nov 06 '12 at 12:24