You'd lose out on compiler checks and IDE code hinting if you had to call require() directly in AS.
Wrapping the generated JS for a class in an IIFE, like I talked about in an earlier thread, could keep the requires confined to a single class. (function() { var fs = require("fs"); //the fs variable is accessible here })(); //but fs is not accessible here - Josh On Wed, Dec 2, 2015 at 11:59 AM, Alex Harui <aha...@adobe.com> wrote: > I read up a bit on Node's require system. It seems that because import > statements in AS are generally at the package level, it wouldn't be a good > equivalent for Node's require. I think Node's require system is an > explicit class/package loader like Java's and allows you to defer the > loading of code until just before it is needed. > > Do we have to find a way to replace the need to make require calls in your > AS code? > > -Alex > > On 12/2/15, 11:00 AM, "Josh Tynjala" <joshtynj...@gmail.com> wrote: > > >Node.js has its own require system. It's called CommonJS. > > > >I was thinking that it could also be based on imported classes. It can be > >a > >little tricky, though. A module is pretty flexible with what it exports. > >Most simply, a module could export a single class. In this case, the > >translation is pretty simple: > > > >//as > >import SomeClass; > > > >//js > >var SomeClass = require("SomeClass"); > > > >However, a module could export multiple classes or even functions too, > >more > >like an ActionScript package. It might work something like this: > > > >//as > >import fs.readFileSync; //function > >import fs.ReadStream; //class > > > >//js > >var readFileSync = require("fs").readFileSync; > >var ReadStream = require("fs").ReadStream; > > > >That's for the core modules. You can also require() modules using a > >relative path. If we were to allow the ability to emit classes as CommonJS > >modules, we might use the following syntax. In a class in the top-level > >package, it might look like this: > > > >//as > >import com.example.RelativeClass; > > > >//js > >var RelativeClass = require("./com/example/RelativeClass"); > > > >But for something in com/example, it would look like this: > > > >//as > >import com.example.RelativeClass; > > > >//js > >var RelativeClass = require("./RelativeClass"); > > > >- Josh > > > >On Wed, Dec 2, 2015 at 10:14 AM, Alex Harui <aha...@adobe.com> wrote: > > > >> > >> > >> On 12/2/15, 9:59 AM, "Josh Tynjala" <joshtynj...@gmail.com> wrote: > >> > >> >The require() call for a Node.js module should probably be generated in > >> >the > >> >PackageHeaderEmitter, where the goog.require() calls are generated. > >> > >> OK, but based on what kind of source code lines? The goog.requires are > >> generated from import statements, although we might change that to being > >> generated by explicit instantiation of a class someday. > >> > >> I think you are saying you want to replace the goog.require subsystem > >>with > >> some other require subsystem in order to get node.js to work. Is > >>node.js > >> using RequireJS or their own require system? The work item may be to > >> finally getting around to supporting alternative require subsystems. > >> > >> -Alex > >> > >> > >