|
| 1 | +# Compiler Directives |
| 2 | + |
| 3 | +The Opal compiler supports some special directives that can optimize or |
| 4 | +enhance the output of compiled ruby code to suit the ruby environment. |
| 5 | + |
| 6 | +## Require Directive |
| 7 | + |
| 8 | +All calls to `require` are captured so that the compiler and build tools |
| 9 | +can determine which dependencies a file has. In the case of `Builder`, |
| 10 | +these are collated and then added to a list of files to be processed. |
| 11 | + |
| 12 | +### `require` |
| 13 | + |
| 14 | +Assuming we have a file `foo.rb`: |
| 15 | + |
| 16 | + # foo.rb |
| 17 | + require 'bar' |
| 18 | + require 'baz' |
| 19 | + |
| 20 | +The compiler will collect these two dependencies, and then `Builder` |
| 21 | +will attempt to discover them within the opal load path to also compile |
| 22 | +them into the target output. If these dependencies cannot be resolved, |
| 23 | +then a compile time error will be thrown. |
| 24 | + |
| 25 | +#### Dynamic Requires |
| 26 | + |
| 27 | +Opal only supports hard-coded requires. This means that any dynamically |
| 28 | +generated require statemnts cannot be discoeverd. Opal may raise an |
| 29 | +error or just produce a warning if a dynamic require is used. A dynamic |
| 30 | +require is any require that cannot be resolved using static analysis. A |
| 31 | +common use case of dynamic requires is to include a directory of ruby |
| 32 | +files. In this case, see `require_tree` below. |
| 33 | + |
| 34 | +### `require_relative` |
| 35 | + |
| 36 | +`require_relative` is also supported by opals compiler for ahead-of-time |
| 37 | +inclusion. |
| 38 | + |
| 39 | + # foo.rb |
| 40 | + require_relative 'bar' |
| 41 | + |
| 42 | +This example will try to resolve `bar.rb` in the same directory. |
| 43 | + |
| 44 | +### `autoload` |
| 45 | + |
| 46 | +`autoload` is used to load a modules and classes within a modules |
| 47 | +namespace. Aslong as the string argument given to `autoload` can be |
| 48 | +resolved in Opals load paths, in the same way as `require`, then these |
| 49 | +referenced dependencies will also be compiled. |
| 50 | + |
| 51 | + # foo.rb |
| 52 | + module Foo |
| 53 | + autoload :Bar, 'bar' |
| 54 | + end |
| 55 | + |
| 56 | +In this example, `bar.rb` will also be required. |
| 57 | + |
| 58 | +### `require_tree` |
| 59 | + |
| 60 | +`require_tree` can be used as an Opal friendly alternative to globbing |
| 61 | +over a directory to require a list of dependencies. |
| 62 | + |
| 63 | + # foo.rb |
| 64 | + require_tree './models' |
| 65 | + |
| 66 | +This will, at compile time, resolve all files inside the `models/` |
| 67 | +directory and also compile them to the output. At runtime this method |
| 68 | +will then loop over all modules defined, and require them if they match |
| 69 | +that given module path. |
| 70 | + |
| 71 | +Note: The given directory **must** be inside Opals load path, otherwise |
| 72 | +no files will be compiled. |
| 73 | + |
| 74 | +### Handling non-ruby requirements |
| 75 | + |
| 76 | +Opal's `require` method is also special as it allows non-ruby source |
| 77 | +files to be required and generated in the output. The obvious example of |
| 78 | +this is requiring javascript source files. Javascript sources are |
| 79 | +treated as first class citizens in Opal. The Opal gem also supports |
| 80 | +compiling `.erb` files using the same process. |
0 commit comments