opal-zeitwerk 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +456 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 52b397fd96aa14fd2b196f67db7325010a66d7fb3fd94623d3ed4b957be1181c
|
4
|
+
data.tar.gz: 4e1e32b7c582f1280f7c81c1c7ccbb69091521c0b6f0e1d73d832d1f845b689d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 436625b6e442aa157c064e6a51501b743d7b419900bb085771411f8c174bdf7ed35c2b79da1eb67f6a50c17f232178b5045545e8d1b70ead2d759ff406588733
|
7
|
+
data.tar.gz: 047c69f853ecac1960a85f2dfd4f608f188cd47cb52ce6a6571ab63c55e6d49a92c6775e22240cf5a6a991726057f94b43608d4b65a5f68723bc946deca0b89e
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2019–ω Xavier Noria, Jan Biedermann
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,456 @@
|
|
1
|
+
# Opal Zeitwerk
|
2
|
+
|
3
|
+
## Community and Support
|
4
|
+
At the [Isomorfeus Project](http://isomorfeus.com) - isomorphic full stack Ruby for the web.
|
5
|
+
|
6
|
+
## TOC
|
7
|
+
<!-- TOC -->
|
8
|
+
|
9
|
+
- [Introduction](#introduction)
|
10
|
+
- [Synopsis](#synopsis)
|
11
|
+
- [File structure](#file-structure)
|
12
|
+
- [Implicit namespaces](#implicit-namespaces)
|
13
|
+
- [Explicit namespaces](#explicit-namespaces)
|
14
|
+
- [Nested root directories](#nested-root-directories)
|
15
|
+
- [Usage](#usage)
|
16
|
+
- [Setup](#setup)
|
17
|
+
- [Autoloading](#autoloading)
|
18
|
+
- [Eager loading](#eager-loading)
|
19
|
+
- [Reloading](#reloading)
|
20
|
+
- [Inflection](#inflection)
|
21
|
+
- [Zeitwerk::Inflector](#zeitwerkinflector)
|
22
|
+
- [Custom inflector](#custom-inflector)
|
23
|
+
- [Ignoring parts of the project](#ignoring-parts-of-the-project)
|
24
|
+
- [Use case: Files that do not follow the conventions](#use-case-files-that-do-not-follow-the-conventions)
|
25
|
+
- [Edge cases](#edge-cases)
|
26
|
+
- [Rules of thumb](#rules-of-thumb)
|
27
|
+
- [Autoloading, explicit namespaces, and debuggers](#autoloading-explicit-namespaces-and-debuggers)
|
28
|
+
- [Pronunciation](#pronunciation)
|
29
|
+
- [Supported Opal versions](#supported-opal-versions)
|
30
|
+
- [Motivation](#motivation)
|
31
|
+
- [License](#license)
|
32
|
+
|
33
|
+
<!-- /TOC -->
|
34
|
+
|
35
|
+
<a id="markdown-introduction" name="introduction"></a>
|
36
|
+
## Introduction
|
37
|
+
|
38
|
+
Opal Zeitwerk is a port of the Ruby [Zeitwerk](https://github.com/fxn/zeitwerk) loader to Opal for autoloading ruby code in the Browser.
|
39
|
+
Autoloading reduces upfront TTI(time to interactive) for large opal projects vastly.
|
40
|
+
|
41
|
+
Given a [conventional file structure](#file-structure), Opal Zeitwerk is able to load your project's classes and modules on demand (autoloading),
|
42
|
+
or upfront (eager loading)(currently untested). You don't need to write `require` calls for your own files, rather, you can streamline your programming,
|
43
|
+
knowing that your classes and modules are available everywhere. This feature is efficient, and matches Ruby's semantics for constants.
|
44
|
+
|
45
|
+
The gem is designed so that any project, gem dependency, application, etc. can have their own independent loader, managing their own project trees,
|
46
|
+
and independent of each other. Each loader has its own configuration, inflector, and optional logger.
|
47
|
+
|
48
|
+
Internally, Opal Zeitwerk issues `require` calls exclusively using absolute path names as recorded in Opal modules registry.
|
49
|
+
|
50
|
+
Differences to Ruby Zeitwerk:
|
51
|
+
- no logging (to keep asset size small and performance high)
|
52
|
+
- no gem specific support: GemInflector, for_gem, etc.
|
53
|
+
|
54
|
+
These don't make so much sense, as Opal Zeitwerk works on the global Opal.modules registry in the Browser, not the filesystem.
|
55
|
+
- Zeitwerk::Loader.set_autoloads_in_dir is public, so it can be called from lazy loaded code, after updating Opal.modules.
|
56
|
+
- There are no threads in javascript so thread support has been removed.
|
57
|
+
- Tests don't run yet.
|
58
|
+
<a id="markdown-synopsis" name="synopsis"></a>
|
59
|
+
## Synopsis
|
60
|
+
|
61
|
+
Main generic interface:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
loader = Zeitwerk::Loader.new
|
65
|
+
loader.push_dir(...)
|
66
|
+
loader.setup # ready!
|
67
|
+
```
|
68
|
+
|
69
|
+
The `loader` variable can go out of scope. Zeitwerk keeps a registry with all of them, and so the object won't be garbage collected.
|
70
|
+
|
71
|
+
You can reload if you want to:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
loader = Zeitwerk::Loader.new
|
75
|
+
loader.push_dir(...)
|
76
|
+
loader.enable_reloading # you need to opt-in before setup
|
77
|
+
loader.setup
|
78
|
+
...
|
79
|
+
loader.reload
|
80
|
+
```
|
81
|
+
|
82
|
+
and you can eager load all the code:
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
loader.eager_load
|
86
|
+
```
|
87
|
+
|
88
|
+
It is also possible to broadcast `eager_load` to all instances:
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
Zeitwerk::Loader.eager_load_all
|
92
|
+
```
|
93
|
+
|
94
|
+
<a id="markdown-file-structure" name="file-structure"></a>
|
95
|
+
## File structure
|
96
|
+
|
97
|
+
To have a file structure Zeitwerk can work with, just name files and directories after the name of the classes and modules they define:
|
98
|
+
|
99
|
+
```
|
100
|
+
lib/my_gem.rb -> 'my_gem' -> MyGem
|
101
|
+
lib/my_gem/foo.rb -> 'my_gem/foo' -> MyGem::Foo
|
102
|
+
lib/my_gem/bar_baz.rb -> 'my_gem/bar_baz' -> MyGem::BarBaz
|
103
|
+
lib/my_gem/woo/zoo.rb -> 'my_gem/woo/zoo' -> MyGem::Woo::Zoo
|
104
|
+
```
|
105
|
+
Second column shows how files are registered in Opals modules registry. These paths in the registry are relevant for Opal Zeitwerk running in the Browser.
|
106
|
+
Every directory configured with `push_dir` acts as root namespace. There can be several of them. For example, given
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
loader.push_dir("app/models")
|
110
|
+
loader.push_dir("app/controllers")
|
111
|
+
```
|
112
|
+
|
113
|
+
Zeitwerk understands that their respective files and subdirectories belong to the root namespace:
|
114
|
+
|
115
|
+
```
|
116
|
+
app/models/user -> User
|
117
|
+
app/controllers/admin/users_controller -> Admin::UsersController
|
118
|
+
```
|
119
|
+
|
120
|
+
<a id="markdown-implicit-namespaces" name="implicit-namespaces"></a>
|
121
|
+
### Implicit namespaces
|
122
|
+
|
123
|
+
Directories without a matching Ruby file get modules autovivified automatically by Zeitwerk. For example, in
|
124
|
+
|
125
|
+
```
|
126
|
+
app/controllers/admin/users_controller -> Admin::UsersController
|
127
|
+
```
|
128
|
+
|
129
|
+
`Admin` is autovivified as a module on demand, you do not need to define an `Admin` class or module in an `admin.rb` file explicitly.
|
130
|
+
|
131
|
+
<a id="markdown-explicit-namespaces" name="explicit-namespaces"></a>
|
132
|
+
### Explicit namespaces
|
133
|
+
|
134
|
+
Classes and modules that act as namespaces can also be explicitly defined, though. For instance, consider
|
135
|
+
|
136
|
+
```
|
137
|
+
app/models/hotel -> Hotel
|
138
|
+
app/models/hotel/pricing -> Hotel::Pricing
|
139
|
+
```
|
140
|
+
|
141
|
+
There, `app/models/hotel` defines `Hotel`, and thus Zeitwerk does not autovivify a module.
|
142
|
+
|
143
|
+
The classes and modules from the namespace are already available in the body of the class or module defining it:
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
class Hotel < ApplicationRecord
|
147
|
+
include Pricing # works
|
148
|
+
...
|
149
|
+
end
|
150
|
+
```
|
151
|
+
|
152
|
+
An explicit namespace must be managed by one single loader. Loaders that reopen namespaces owned by other projects are responsible for loading their constants before setup.
|
153
|
+
|
154
|
+
<a id="markdown-nested-root-directories" name="nested-root-directories"></a>
|
155
|
+
### Nested root directories
|
156
|
+
|
157
|
+
Root directories should not be ideally nested, but Zeitwerk supports them because in Rails, for example, both `app/models` and `app/models/concerns` belong to the autoload paths.
|
158
|
+
|
159
|
+
Zeitwerk detects nested root directories, and treats them as roots only. In the example above, `concerns` is not considered to be a namespace below `app/models`. For example, the file:
|
160
|
+
|
161
|
+
```
|
162
|
+
app/models/concerns/geolocatable
|
163
|
+
```
|
164
|
+
|
165
|
+
should define `Geolocatable`, not `Concerns::Geolocatable`.
|
166
|
+
|
167
|
+
<a id="markdown-usage" name="usage"></a>
|
168
|
+
## Usage
|
169
|
+
|
170
|
+
Add to the Gemfile:
|
171
|
+
```
|
172
|
+
gem 'opal-zeitwerk', '~> 0.0.1'
|
173
|
+
```
|
174
|
+
|
175
|
+
And to your loader of opal code:
|
176
|
+
```
|
177
|
+
require 'zeitwerk'
|
178
|
+
```
|
179
|
+
|
180
|
+
<a id="markdown-setup" name="setup"></a>
|
181
|
+
### Setup
|
182
|
+
|
183
|
+
Loaders are ready to load code right after calling `setup` on them:
|
184
|
+
|
185
|
+
```ruby
|
186
|
+
loader.setup
|
187
|
+
```
|
188
|
+
|
189
|
+
This method is synchronized and idempotent.
|
190
|
+
|
191
|
+
Customization should generally be done before that call. In particular, in the generic interface you may set the root module paths from which you want to load files:
|
192
|
+
|
193
|
+
```ruby
|
194
|
+
loader.push_dir(...)
|
195
|
+
loader.push_dir(...)
|
196
|
+
loader.setup
|
197
|
+
```
|
198
|
+
|
199
|
+
Zeitwerk works internally only with absolute paths.
|
200
|
+
|
201
|
+
<a id="markdown-autoloading" name="autoloading"></a>
|
202
|
+
### Autoloading
|
203
|
+
|
204
|
+
After `setup`, you are able to reference classes and modules from the project without issuing `require` calls for them. They are all available everywhere,
|
205
|
+
autoloading loads them on demand. This works even if the reference to the class or module is first hit in client code, outside your project.
|
206
|
+
|
207
|
+
If autoloading a file does not define the expected class or module, Zeitwerk raises `Zeitwerk::NameError`, which is a subclass of `NameError`.
|
208
|
+
|
209
|
+
<a id="markdown-eager-loading" name="eager-loading"></a>
|
210
|
+
### Eager loading (untested)
|
211
|
+
|
212
|
+
Zeitwerk instances are able to eager load their managed files:
|
213
|
+
|
214
|
+
```ruby
|
215
|
+
loader.eager_load
|
216
|
+
```
|
217
|
+
|
218
|
+
That skips [ignored files and directories](#ignoring-parts-of-the-project), and you can also tell Zeitwerk that certain files or directories are autoloadable, but should not be eager loaded:
|
219
|
+
|
220
|
+
```ruby
|
221
|
+
db_adapters = "#{__dir__}/my_gem/db_adapters"
|
222
|
+
loader.do_not_eager_load(db_adapters)
|
223
|
+
loader.setup
|
224
|
+
loader.eager_load # won't eager load the database adapters
|
225
|
+
```
|
226
|
+
|
227
|
+
Eager loading is synchronized and idempotent.
|
228
|
+
|
229
|
+
If eager loading a file does not define the expected class or module, Zeitwerk raises `Zeitwerk::NameError`, which is a subclass of `NameError`.
|
230
|
+
|
231
|
+
If you want to eager load yourself and all dependencies using Zeitwerk, you can broadcast the `eager_load` call to all instances:
|
232
|
+
|
233
|
+
```ruby
|
234
|
+
Zeitwerk::Loader.eager_load_all
|
235
|
+
```
|
236
|
+
|
237
|
+
This may be handy in top-level services, like web applications.
|
238
|
+
|
239
|
+
Note that thanks to idempotence `Zeitwerk::Loader.eager_load_all` won't eager load twice if any of the instances already eager loaded.
|
240
|
+
|
241
|
+
<a id="markdown-reloading" name="reloading"></a>
|
242
|
+
### Reloading (untested)
|
243
|
+
|
244
|
+
Zeitwerk is able to reload code, but you need to enable this feature:
|
245
|
+
|
246
|
+
```ruby
|
247
|
+
loader = Zeitwerk::Loader.new
|
248
|
+
loader.push_dir(...)
|
249
|
+
loader.enable_reloading # you need to opt-in before setup
|
250
|
+
loader.setup
|
251
|
+
...
|
252
|
+
loader.reload
|
253
|
+
```
|
254
|
+
|
255
|
+
There is no way to undo this, either you want to reload or you don't.
|
256
|
+
|
257
|
+
Enabling reloading after setup raises `Zeitwerk::Error`. Attempting to reload without having it enabled raises `Zeitwerk::ReloadingDisabledError`.
|
258
|
+
|
259
|
+
Generally speaking, reloading is useful while developing running services like web applications. Gems that implement regular libraries, so to speak, or services running in testing or production environments, won't normally have a use case for reloading. If reloading is not enabled, Zeitwerk is able to use less memory.
|
260
|
+
|
261
|
+
Reloading removes the currently loaded classes and modules and resets the loader so that it will pick whatever is in the file system now.
|
262
|
+
|
263
|
+
It is important to highlight that this is an instance method. Don't worry about project dependencies managed by Zeitwerk, their loaders are independent.
|
264
|
+
|
265
|
+
In order for reloading to be thread-safe, you need to implement some coordination. For example, a web framework that serves each request with its own thread may have a globally accessible RW lock. When a request comes in, the framework acquires the lock for reading at the beginning, and the code in the framework that calls `loader.reload` needs to acquire the lock for writing.
|
266
|
+
|
267
|
+
On reloading, client code has to update anything that would otherwise be storing a stale object. For example, if the routing layer of a web framework stores controller class objects or instances in internal structures, on reload it has to refresh them somehow, possibly reevaluating routes.
|
268
|
+
|
269
|
+
<a id="markdown-inflection" name="inflection"></a>
|
270
|
+
### Inflection
|
271
|
+
|
272
|
+
Each individual loader needs an inflector to figure out which constant path would a given file or directory map to. Zeitwerk ships with two basic inflectors.
|
273
|
+
|
274
|
+
<a id="markdown-zeitwerkinflector" name="zeitwerkinflector"></a>
|
275
|
+
#### Zeitwerk::Inflector
|
276
|
+
|
277
|
+
This is a very basic inflector that converts snake case to camel case:
|
278
|
+
|
279
|
+
```
|
280
|
+
user -> User
|
281
|
+
users_controller -> UsersController
|
282
|
+
html_parser -> HtmlParser
|
283
|
+
```
|
284
|
+
|
285
|
+
The camelize logic can be overridden easily for individual basenames:
|
286
|
+
|
287
|
+
```ruby
|
288
|
+
loader.inflector.inflect(
|
289
|
+
"html_parser" => "HTMLParser",
|
290
|
+
"mysql_adapter" => "MySQLAdapter"
|
291
|
+
)
|
292
|
+
```
|
293
|
+
|
294
|
+
The `inflect` method can be invoked several times if you prefer this other style:
|
295
|
+
|
296
|
+
```ruby
|
297
|
+
loader.inflector.inflect "html_parser" => "HTMLParser"
|
298
|
+
loader.inflector.inflect "mysql_adapter" => "MySQLAdapter"
|
299
|
+
```
|
300
|
+
|
301
|
+
Overrides need to be configured before calling `setup`.
|
302
|
+
|
303
|
+
There are no inflection rules or global configuration that can affect this inflector. It is deterministic.
|
304
|
+
|
305
|
+
Loaders instantiated with `Zeitwerk::Loader.new` have an inflector of this type, independent of each other.
|
306
|
+
|
307
|
+
<a id="markdown-custom-inflector" name="custom-inflector"></a>
|
308
|
+
#### Custom inflector
|
309
|
+
|
310
|
+
The inflectors that ship with Zeitwerk are deterministic and simple. But you can configure your own:
|
311
|
+
|
312
|
+
```ruby
|
313
|
+
# frozen_string_literal: true
|
314
|
+
|
315
|
+
class MyInflector < Zeitwerk::Inflector
|
316
|
+
def camelize(basename, abspath)
|
317
|
+
if basename =~ /\Ahtml_(.*)/
|
318
|
+
"HTML" + super($1, abspath)
|
319
|
+
else
|
320
|
+
super
|
321
|
+
end
|
322
|
+
end
|
323
|
+
end
|
324
|
+
```
|
325
|
+
|
326
|
+
The first argument, `basename`, is a string with the basename of the file or directory to be inflected. In the case of a file, without extension. In the case of a directory, without trailing slash. The inflector needs to return this basename inflected. Therefore, a simple constant name without colons.
|
327
|
+
|
328
|
+
The second argument, `abspath`, is a string with the absolute path to the file or directory in case you need it to decide how to inflect the basename. Paths to directories don't have trailing slashes.
|
329
|
+
|
330
|
+
Then, assign the inflector:
|
331
|
+
|
332
|
+
```ruby
|
333
|
+
loader.inflector = MyInflector.new
|
334
|
+
```
|
335
|
+
|
336
|
+
This needs to be done before calling `setup`.
|
337
|
+
|
338
|
+
<a id="markdown-ignoring-parts-of-the-project" name="ignoring-parts-of-the-project"></a>
|
339
|
+
### Ignoring parts of the project
|
340
|
+
|
341
|
+
Zeitwerk ignores automatically any file or directory whose name starts with a dot, and any files that do not have extension ".rb".
|
342
|
+
|
343
|
+
However, sometimes it might still be convenient to tell Zeitwerk to completely ignore some particular Ruby file or directory. That is possible with `ignore`, which accepts an arbitrary number of strings or `Pathname` objects, and also an array of them.
|
344
|
+
|
345
|
+
You can ignore file names, directory names, and glob patterns. Glob patterns are expanded when they are added and again on each reload.
|
346
|
+
|
347
|
+
Let's see some use cases.
|
348
|
+
|
349
|
+
<a id="markdown-use-case-files-that-do-not-follow-the-conventions" name="use-case-files-that-do-not-follow-the-conventions"></a>
|
350
|
+
#### Use case: Files that do not follow the conventions
|
351
|
+
|
352
|
+
Let's suppose that your gem decorates something in `Kernel`:
|
353
|
+
|
354
|
+
```ruby
|
355
|
+
# lib/my_gem/core_ext/kernel.rb
|
356
|
+
|
357
|
+
Kernel.module_eval do
|
358
|
+
# ...
|
359
|
+
end
|
360
|
+
```
|
361
|
+
|
362
|
+
That file does not define a constant path after the path name and you need to tell Zeitwerk:
|
363
|
+
|
364
|
+
```ruby
|
365
|
+
kernel_ext = "#{__dir__}/my_gem/core_ext/kernel.rb"
|
366
|
+
loader.ignore(kernel_ext)
|
367
|
+
loader.setup
|
368
|
+
```
|
369
|
+
|
370
|
+
You can also ignore the whole directory:
|
371
|
+
|
372
|
+
```ruby
|
373
|
+
core_ext = "#{__dir__}/my_gem/core_ext"
|
374
|
+
loader.ignore(core_ext)
|
375
|
+
loader.setup
|
376
|
+
```
|
377
|
+
|
378
|
+
<a id="markdown-edge-cases" name="edge-cases"></a>
|
379
|
+
### Edge cases
|
380
|
+
|
381
|
+
A class or module that acts as a namespace:
|
382
|
+
|
383
|
+
```ruby
|
384
|
+
# trip.rb
|
385
|
+
class Trip
|
386
|
+
include Geolocation
|
387
|
+
end
|
388
|
+
|
389
|
+
# trip/geolocation.rb
|
390
|
+
module Trip::Geolocation
|
391
|
+
...
|
392
|
+
end
|
393
|
+
```
|
394
|
+
|
395
|
+
has to be defined with the `class` or `module` keywords, as in the example above.
|
396
|
+
|
397
|
+
For technical reasons, raw constant assignment is not supported:
|
398
|
+
|
399
|
+
```ruby
|
400
|
+
# trip.rb
|
401
|
+
Trip = Class.new { ... } # NOT SUPPORTED
|
402
|
+
Trip = Struct.new { ... } # NOT SUPPORTED
|
403
|
+
```
|
404
|
+
|
405
|
+
This only affects explicit namespaces, those idioms work well for any other ordinary class or module.
|
406
|
+
|
407
|
+
<a id="markdown-rules-of-thumb" name="rules-of-thumb"></a>
|
408
|
+
### Rules of thumb
|
409
|
+
|
410
|
+
1. Different loaders should manage different directory trees. It is an error condition to configure overlapping root directories in different loaders.
|
411
|
+
|
412
|
+
2. Think the mere existence of a file is effectively like writing a `require` call for them, which is executed on demand (autoload) or upfront (eager load).
|
413
|
+
|
414
|
+
3. In that line, if two loaders manage files that translate to the same constant in the same namespace, the first one wins, the rest are ignored. Similar to what happens with `require` and `$LOAD_PATH`, only the first occurrence matters.
|
415
|
+
|
416
|
+
4. Projects that reopen a namespace defined by some dependency have to ensure said namespace is loaded before setup. That is, the project has to make sure it reopens, rather than define. This is often accomplished just loading the dependency.
|
417
|
+
|
418
|
+
5. Objects stored in reloadable constants should not be cached in places that are not reloaded. For example, non-reloadable classes should not subclass a reloadable class, or mixin a reloadable module. Otherwise, after reloading, those classes or module objects would become stale. Referring to constants in dynamic places like method calls or lambdas is fine.
|
419
|
+
|
420
|
+
6. In a given process, ideally, there should be at most one loader with reloading enabled. Technically, you can have more, but it may get tricky if one refers to constants managed by the other one. Do that only if you know what you are doing.
|
421
|
+
|
422
|
+
<a id="markdown-autoloading-explicit-namespaces-and-debuggers" name="autoloading-explicit-namespaces-and-debuggers"></a>
|
423
|
+
### Autoloading, explicit namespaces, and debuggers
|
424
|
+
|
425
|
+
As of this writing, Zeitwerk is unable to autoload classes or modules that belong to [explicit namespaces](#explicit-namespaces) inside debugger sessions. You'll get a `NameError`.
|
426
|
+
|
427
|
+
The root cause is that debuggers set trace points, and Zeitwerk does too to support explicit namespaces. A debugger session happens inside a trace point handler, and Ruby does not invoke other handlers from within a running handler. Therefore, the code that manages explicit namespaces in Zeitwerk does not get called by the interpreter. See [this issue](https://github.com/deivid-rodriguez/byebug/issues/564#issuecomment-499413606) for further details.
|
428
|
+
|
429
|
+
As a workaround, you can eager load. Zeitwerk tries hard to succeed or fail consistently both autoloading and eager loading, so switching to eager loading should not introduce any interference in your debugging logic, generally speaking.
|
430
|
+
|
431
|
+
<a id="markdown-pronunciation" name="pronunciation"></a>
|
432
|
+
## Pronunciation
|
433
|
+
|
434
|
+
"Zeitwerk" is pronounced [this way](http://share.hashref.com/zeitwerk/zeitwerk_pronunciation.mp3).
|
435
|
+
|
436
|
+
<a id="markdown-supported-opal-versions" name="supported-opal-versions"></a>
|
437
|
+
## Supported Opal versions
|
438
|
+
|
439
|
+
Opal Zeitwerk currently works only with the [Opal es6_modules_1_1 PR#1976](https://github.com/opal/opal/pull/1976) of Opal.
|
440
|
+
For the Gemfile:
|
441
|
+
```
|
442
|
+
gem 'opal', github: 'janbiedermann/opal', branch: 'es6_modules_1_1'
|
443
|
+
```
|
444
|
+
|
445
|
+
<a id="markdown-motivation" name="motivation"></a>
|
446
|
+
## Motivation
|
447
|
+
|
448
|
+
Since `require` has global side-effects, and there is no static way to verify that you have issued the `require` calls for code that your file depends on, in practice it is very easy to forget some.
|
449
|
+
That introduces bugs that depend on the load order. Zeitwerk provides a way to forget about `require` in your own code, just name things following conventions and done.
|
450
|
+
|
451
|
+
The original goal of Opal Zeitwerk was to bring a better autoloading mechanism for Isomorfeus.
|
452
|
+
|
453
|
+
<a id="markdown-license" name="license"></a>
|
454
|
+
## License
|
455
|
+
|
456
|
+
Released under the MIT License, Copyright (c) 2019–<i>ω</i> Xavier Noria, Jan Biedermann.
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: opal-zeitwerk
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jan Biedermann
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-11-04 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: |2
|
14
|
+
A port of the Zeitwerk autoloader to Opal.
|
15
|
+
Zeitwerk implements constant autoloading with Ruby semantics. Each gem
|
16
|
+
and application may have their own independent autoloader, with its own
|
17
|
+
configuration, inflector. Supports autoloading, preloading and eager loading.
|
18
|
+
email: jan@kursator.de
|
19
|
+
executables: []
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- MIT-LICENSE
|
24
|
+
- README.md
|
25
|
+
homepage: https://github.com/isomorfeus/opal-zeitwerk
|
26
|
+
licenses:
|
27
|
+
- MIT
|
28
|
+
metadata: {}
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 2.4.4
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubygems_version: 3.0.6
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: Opal constant autoloader using Zeitwerk
|
48
|
+
test_files: []
|