install.html 31.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
<!DOCTYPE html>

<html>
<head>
  <title>install.js</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
  <link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
  <div id="container">
    <div id="background"></div>
    
    <ul class="sections">
        
          <li id="title">
              <div class="annotation">
                  <h1>install.js</h1>
              </div>
          </li>
        
        
        
        <li id="section-1">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-1">&#182;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">global, undefined</span>) </span>{
  <span class="hljs-keyword">if</span> (global.makeInstaller) {
    <span class="hljs-keyword">return</span>;
  }

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">makeInstaller</span>(<span class="hljs-params">options</span>) </span>{
    <span class="hljs-keyword">var</span> root = <span class="hljs-keyword">new</span> File({});</pre></div></div>
            
        </li>
        
        
        <li id="section-2">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-2">&#182;</a>
              </div>
              <p>Set up a simple queue for tracking required modules with unmet
dependencies. See also <code>queueAppend</code> and <code>queueFlush</code>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="hljs-keyword">var</span> q = root.q = {};
    q.h = q.t = {}; <span class="hljs-comment">// Queue head, queue tail.</span></pre></div></div>
            
        </li>
        
        
        <li id="section-3">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-3">&#182;</a>
              </div>
              <p>Configurable function for deferring queue flushes.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    q.d = options &amp;&amp; options.defer || <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">fn</span>) </span>{
      setTimeout(fn, <span class="hljs-number">0</span>);
    };

    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">install</span>(<span class="hljs-params">tree</span>) </span>{
      <span class="hljs-keyword">if</span> (isObject(tree)) {
        fileMergeContents(root, tree);
        queueFlush(root.q);
      }
      <span class="hljs-keyword">return</span> root.r;
    };
  }

  global.makeInstaller = makeInstaller;

  <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> exports === <span class="hljs-string">"object"</span>) {
    exports.makeInstaller = makeInstaller;
  }

  <span class="hljs-keyword">var</span> extensions = [<span class="hljs-string">""</span>, <span class="hljs-string">".js"</span>, <span class="hljs-string">".json"</span>];
  <span class="hljs-keyword">var</span> MISSING = {};
  <span class="hljs-keyword">var</span> hasOwn = MISSING.hasOwnProperty;
  <span class="hljs-keyword">var</span> Ap = <span class="hljs-built_in">Array</span>.prototype;

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getOwn</span>(<span class="hljs-params">obj, key</span>) </span>{
    <span class="hljs-keyword">return</span> hasOwn.call(obj, key) &amp;&amp; obj[key];
  }

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">isObject</span>(<span class="hljs-params">value</span>) </span>{
    <span class="hljs-keyword">return</span> value &amp;&amp; <span class="hljs-keyword">typeof</span> value === <span class="hljs-string">"object"</span>;
  }

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">isFunction</span>(<span class="hljs-params">value</span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">typeof</span> value === <span class="hljs-string">"function"</span>;
  }

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">isString</span>(<span class="hljs-params">value</span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">typeof</span> value === <span class="hljs-string">"string"</span>;
  }

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">queueAppend</span>(<span class="hljs-params">q, file</span>) </span>{</pre></div></div>
            
        </li>
        
        
        <li id="section-4">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-4">&#182;</a>
              </div>
              <p>Property names shortened to shave bytes: <code>.t</code> means <code>.tail</code>, <code>.h</code>
means <code>.head</code>, <code>.n</code> means <code>.next</code>, and <code>.f</code> means <code>.file</code>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    q.t = q.t.n = { f: file };
    <span class="hljs-keyword">if</span> (q.h.n === q.t) {</pre></div></div>
            
        </li>
        
        
        <li id="section-5">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-5">&#182;</a>
              </div>
              <p>If the queue contains only one File (the one we just added), go
ahead and schedule a flush.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      queueFlush(q);
    }
  }

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">queueFlush</span>(<span class="hljs-params">q</span>) </span>{</pre></div></div>
            
        </li>
        
        
        <li id="section-6">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-6">&#182;</a>
              </div>
              <p>The <code>q.p</code> property is set to indicate a flush is pending.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    q.p || (q.p = <span class="hljs-literal">true</span>, q.d(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
      q.p = <span class="hljs-literal">undefined</span>;
      <span class="hljs-keyword">var</span> next = q.h.n;
      <span class="hljs-keyword">if</span> (next &amp;&amp; fileReady(next.f)) {
        queueFlush(q); <span class="hljs-comment">// Schedule the next flush.</span>
        q.h = next;
        fileEvaluate(next.f);
      }
    }));
  }</pre></div></div>
            
        </li>
        
        
        <li id="section-7">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-7">&#182;</a>
              </div>
              <p>These <code>unbound{Require,Ensure}</code> functions need to be bound to File
objects before they can be used. See <code>makeRequire</code>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">unboundRequire</span>(<span class="hljs-params">id</span>) </span>{
    <span class="hljs-keyword">var</span> result = fileEvaluate(fileResolve(<span class="hljs-keyword">this</span>, id));
    <span class="hljs-keyword">if</span> (result === MISSING) {
      <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">"Cannot find module '"</span> + id + <span class="hljs-string">"'"</span>);
    }
    <span class="hljs-keyword">return</span> result;
  }

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">unboundEnsure</span>(<span class="hljs-params"></span>) </span>{</pre></div></div>
            
        </li>
        
        
        <li id="section-8">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-8">&#182;</a>
              </div>
              <p>Flatten arguments into an array containing relative module
identifier strings and an optional callback function, then coerce
that array into a callback function with a <code>.d</code> property.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="hljs-keyword">var</span> flatArgs = Ap.concat.apply(Ap, <span class="hljs-built_in">arguments</span>);
    <span class="hljs-keyword">var</span> callback = ensureObjectOrFunction(flatArgs);</pre></div></div>
            
        </li>
        
        
        <li id="section-9">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-9">&#182;</a>
              </div>
              <p>Note that <code>queueAppend</code> schedules a flush if there are no other
callbacks waiting in the queue.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    queueAppend(<span class="hljs-keyword">this</span>.q, <span class="hljs-keyword">new</span> File(callback, <span class="hljs-keyword">this</span>));
  }

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">makeRequire</span>(<span class="hljs-params">file</span>) </span>{
    <span class="hljs-keyword">var</span> <span class="hljs-built_in">require</span> = unboundRequire.bind(file);
    <span class="hljs-built_in">require</span>.ensure = unboundEnsure.bind(file);</pre></div></div>
            
        </li>
        
        
        <li id="section-10">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-10">&#182;</a>
              </div>
              <p>TODO Consider adding <code>require.promise</code>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="hljs-keyword">return</span> <span class="hljs-built_in">require</span>;
  }</pre></div></div>
            
        </li>
        
        
        <li id="section-11">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-11">&#182;</a>
              </div>
              <p>File objects represent either directories or modules that have been
installed. When a <code>File</code> respresents a directory, its <code>.c</code> (contents)
property is an object containing the names of the files (or
directories) that it contains. When a <code>File</code> represents a module, its
<code>.c</code> property is a function that can be invoked with the appropriate
<code>(require, exports, module)</code> arguments to evaluate the module. The
<code>.p</code> (parent) property of a File is either a directory <code>File</code> or
<code>null</code>. Note that a child may claim another <code>File</code> as its parent even
if the parent does not have an entry for that child in its <code>.c</code>
object.  This is important for implementing anonymous files, and
preventing child modules from using <code>../relative/identifier</code> syntax
to examine unrelated modules.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">File</span>(<span class="hljs-params">contents, <span class="hljs-comment">/*optional:*/</span> parent, name</span>) </span>{
    <span class="hljs-keyword">var</span> file = <span class="hljs-keyword">this</span>;</pre></div></div>
            
        </li>
        
        
        <li id="section-12">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-12">&#182;</a>
              </div>
              <p>Link to the parent file.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    file.p = parent = parent || <span class="hljs-literal">null</span>;

    <span class="hljs-keyword">if</span> (name) {</pre></div></div>
            
        </li>
        
        
        <li id="section-13">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-13">&#182;</a>
              </div>
              <p>If this file was created with <code>name</code>, join it with <code>parent.id</code> to
generate a module identifier.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      file.id = (parent &amp;&amp; parent.id || <span class="hljs-string">""</span>) + <span class="hljs-string">"/"</span> + name;
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-14">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-14">&#182;</a>
              </div>
              <p>Queue for tracking required modules with unmet dependencies,
inherited from the <code>parent</code>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    file.q = parent &amp;&amp; parent.q;</pre></div></div>
            
        </li>
        
        
        <li id="section-15">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-15">&#182;</a>
              </div>
              <p>Each directory has its own bound version of the <code>require</code> function
that can resolve relative identifiers. Non-directory Files inherit
the require function of their parent directories, so we don’t have
to create a new require function every time we evaluate a module.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    file.r = isObject(contents)
      ? makeRequire(file)
      : parent &amp;&amp; parent.r;</pre></div></div>
            
        </li>
        
        
        <li id="section-16">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-16">&#182;</a>
              </div>
              <p>Set the initial value of <code>file.c</code> (the “contents” of the File).</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    fileMergeContents(file, contents);</pre></div></div>
            
        </li>
        
        
        <li id="section-17">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-17">&#182;</a>
              </div>
              <p>When the file is a directory, <code>file.ready</code> is an object mapping
module identifiers to boolean ready statuses. This information can
be shared by all files in the directory, because module resolution
always has the same results for all files in a given directory.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    file.ready = fileIsDirectory(file) &amp;&amp; {};
  }</pre></div></div>
            
        </li>
        
        
        <li id="section-18">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-18">&#182;</a>
              </div>
              <p>A file is ready if all of its dependencies are installed and ready.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fileReady</span>(<span class="hljs-params">file</span>) </span>{
    <span class="hljs-keyword">var</span> result = !! file;
    <span class="hljs-keyword">var</span> <span class="hljs-built_in">module</span> = file &amp;&amp; file.c;
    <span class="hljs-keyword">var</span> deps = isFunction(<span class="hljs-built_in">module</span>) &amp;&amp; <span class="hljs-built_in">module</span>.d;
    <span class="hljs-keyword">if</span> (deps &amp;&amp; ! getOwn(<span class="hljs-built_in">module</span>, <span class="hljs-string">"seen"</span>)) {
      <span class="hljs-built_in">module</span>.seen = <span class="hljs-literal">true</span>;
      <span class="hljs-keyword">var</span> parentReadyCache = file.p.ready;
      result = <span class="hljs-built_in">Object</span>.keys(deps).every(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">dep</span>) </span>{</pre></div></div>
            
        </li>
        
        
        <li id="section-19">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-19">&#182;</a>
              </div>
              <p>By storing the results of these lookups in <code>parentReadyCache</code>,
we benefit when any other file in the same directory resolves
the same identifier.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="hljs-keyword">return</span> parentReadyCache[dep] =
          parentReadyCache[dep] ||
          fileReady(fileResolve(file.p, dep));
      });
      <span class="hljs-built_in">module</span>.seen = <span class="hljs-literal">undefined</span>;
    }
    <span class="hljs-keyword">return</span> result;
  }

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fileEvaluate</span>(<span class="hljs-params">file</span>) </span>{
    <span class="hljs-keyword">var</span> <span class="hljs-built_in">module</span> = file &amp;&amp; file.c;
    <span class="hljs-keyword">if</span> (isFunction(<span class="hljs-built_in">module</span>)) {
      <span class="hljs-keyword">if</span> (! hasOwn.call(<span class="hljs-built_in">module</span>, <span class="hljs-string">"exports"</span>)) {
        <span class="hljs-built_in">module</span>.id = file.id;
        <span class="hljs-built_in">module</span>.call(global, file.r, <span class="hljs-built_in">module</span>.exports = {}, <span class="hljs-built_in">module</span>);
      }
      <span class="hljs-keyword">return</span> <span class="hljs-built_in">module</span>.exports;
    }
    <span class="hljs-keyword">return</span> MISSING;
  }

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fileIsDirectory</span>(<span class="hljs-params">file</span>) </span>{
    <span class="hljs-keyword">return</span> isObject(file.c);
  }

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fileMergeContents</span>(<span class="hljs-params">file, contents</span>) </span>{
    <span class="hljs-keyword">if</span> ((contents = ensureObjectOrFunction(contents))) {
      <span class="hljs-keyword">var</span> fileContents = file.c = file.c || (
        isFunction(contents) ? contents : {}
      );

      <span class="hljs-keyword">if</span> (isObject(contents) &amp;&amp; fileIsDirectory(file)) {
        <span class="hljs-built_in">Object</span>.keys(contents).forEach(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">key</span>) </span>{
          <span class="hljs-keyword">var</span> child = getOwn(fileContents, key);
          <span class="hljs-keyword">if</span> (child) {
            fileMergeContents(child, contents[key]);
          } <span class="hljs-keyword">else</span> {
            fileContents[key] = <span class="hljs-keyword">new</span> File(contents[key], file, key);
          }
        });
      }
    }
  };

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">ensureObjectOrFunction</span>(<span class="hljs-params">contents</span>) </span>{</pre></div></div>
            
        </li>
        
        
        <li id="section-20">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-20">&#182;</a>
              </div>
              <p>If contents is an array of strings and functions, return the last
function with a <code>.d</code> property containing all the strings.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="hljs-keyword">if</span> (<span class="hljs-built_in">Array</span>.isArray(contents)) {
      <span class="hljs-keyword">var</span> deps = {};
      <span class="hljs-keyword">var</span> func;

      contents.forEach(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">item</span>) </span>{
        <span class="hljs-keyword">if</span> (isString(item)) {
          deps[item] = <span class="hljs-literal">false</span>; <span class="hljs-comment">// Initially unsatisfied.</span>
        } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (isFunction(item)) {
          func = item;
        }
      });</pre></div></div>
            
        </li>
        
        
        <li id="section-21">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-21">&#182;</a>
              </div>
              <p>If no function was found in the array, provide a default function
that simply requires each dependency (really common case).</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      contents = func || <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">module, require</span>) </span>{
        <span class="hljs-built_in">Object</span>.keys(deps).forEach(<span class="hljs-built_in">require</span>);
      };

      contents.d = deps;

    } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (isFunction(contents)) {</pre></div></div>
            
        </li>
        
        
        <li id="section-22">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-22">&#182;</a>
              </div>
              <p>If contents is already a function, make sure it has deps.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      contents.d = contents.d || {};

    } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (! isObject(contents)) {</pre></div></div>
            
        </li>
        
        
        <li id="section-23">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-23">&#182;</a>
              </div>
              <p>If contents is neither an array nor a function nor an object,
just give up and return null.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      contents = <span class="hljs-literal">null</span>;
    }

    <span class="hljs-keyword">return</span> contents;
  }

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fileAppendIdPart</span>(<span class="hljs-params">file, part, isLastPart</span>) </span>{</pre></div></div>
            
        </li>
        
        
        <li id="section-24">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-24">&#182;</a>
              </div>
              <p>Always append relative to a directory.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="hljs-keyword">while</span> (file &amp;&amp; ! fileIsDirectory(file)) {
      file = file.p;
    }

    <span class="hljs-keyword">if</span> (! file || ! part || part === <span class="hljs-string">"."</span>) {
      <span class="hljs-keyword">return</span> file;
    }

    <span class="hljs-keyword">if</span> (part === <span class="hljs-string">".."</span>) {
      <span class="hljs-keyword">return</span> file.p;
    }

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> e = <span class="hljs-number">0</span>; e &lt; extensions.length; ++e) {
      <span class="hljs-keyword">var</span> withExtension = part + extensions[e];

      <span class="hljs-keyword">var</span> child = getOwn(file.c, withExtension);
      <span class="hljs-keyword">if</span> (child) {
        <span class="hljs-keyword">return</span> child;
      }

      <span class="hljs-keyword">if</span> (! isLastPart) {</pre></div></div>
            
        </li>
        
        
        <li id="section-25">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-25">&#182;</a>
              </div>
              <p>Only consider multiple file extensions if this part is the last
part of a module identifier, and not <code>.</code> or <code>..</code>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="hljs-keyword">break</span>;
      }
    }
  };

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fileAppendId</span>(<span class="hljs-params">file, id</span>) </span>{
    <span class="hljs-keyword">var</span> parts = id.split(<span class="hljs-string">"/"</span>);</pre></div></div>
            
        </li>
        
        
        <li id="section-26">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-26">&#182;</a>
              </div>
              <p>Use <code>Array.prototype.every</code> to terminate iteration early if
<code>fileAppendIdPart</code> returns a falsy value.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    parts.every(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">part, i</span>) </span>{
      <span class="hljs-keyword">return</span> file = fileAppendIdPart(file, part, i === parts.length - <span class="hljs-number">1</span>);
    });
    <span class="hljs-keyword">return</span> file;
  };

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fileGetRoot</span>(<span class="hljs-params">file</span>) </span>{
    <span class="hljs-keyword">return</span> file &amp;&amp; fileGetRoot(file.p) || file;
  }

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fileResolve</span>(<span class="hljs-params">file, id</span>) </span>{
    file =</pre></div></div>
            
        </li>
        
        
        <li id="section-27">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-27">&#182;</a>
              </div>
              <p>Absolute module identifiers (i.e. those that begin with a <code>/</code>
character) are interpreted relative to the root directory, which
is a slight deviation from Node, which has access to the entire
file system.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      id.charAt(<span class="hljs-number">0</span>) === <span class="hljs-string">"/"</span> ? fileAppendId(fileGetRoot(file), id) :</pre></div></div>
            
        </li>
        
        
        <li id="section-28">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-28">&#182;</a>
              </div>
              <p>Relative module identifiers are interpreted relative to the
current file, naturally.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      id.charAt(<span class="hljs-number">0</span>) === <span class="hljs-string">"."</span> ? fileAppendId(file, id) :</pre></div></div>
            
        </li>
        
        
        <li id="section-29">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-29">&#182;</a>
              </div>
              <p>Top-level module identifiers are interpreted as referring to
packages in <code>node_modules</code> directories.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      nodeModulesLookup(file, id);</pre></div></div>
            
        </li>
        
        
        <li id="section-30">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-30">&#182;</a>
              </div>
              <p>If the identifier resolves to a directory, we use the same logic as
Node to find an <code>index.js</code> or <code>package.json</code> file to evaluate.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="hljs-keyword">while</span> (file &amp;&amp; fileIsDirectory(file)) {</pre></div></div>
            
        </li>
        
        
        <li id="section-31">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-31">&#182;</a>
              </div>
              <p>If <code>package.json</code> does not exist, <code>fileEvaluate</code> will return the
<code>MISSING</code> object, which has no <code>.main</code> property.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">var</span> pkg = fileEvaluate(fileAppendIdPart(file, <span class="hljs-string">"package.json"</span>));
      file = pkg &amp;&amp; isString(pkg.main) &amp;&amp;
        fileAppendId(file, pkg.main) || <span class="hljs-comment">// Might resolve to another directory!</span>
        fileAppendIdPart(file, <span class="hljs-string">"index.js"</span>);
    }

    <span class="hljs-keyword">return</span> file;
  };

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">nodeModulesLookup</span>(<span class="hljs-params">file, id</span>) </span>{
    <span class="hljs-keyword">return</span> fileIsDirectory(file) &amp;&amp;
      fileAppendId(file, <span class="hljs-string">"node_modules/"</span> + id) ||
      (file.p &amp;&amp; nodeModulesLookup(file.p, id));
  }
})(<span class="hljs-string">"object"</span> === <span class="hljs-keyword">typeof</span> global ? global :
   <span class="hljs-string">"object"</span> === <span class="hljs-keyword">typeof</span> <span class="hljs-built_in">window</span> ? <span class="hljs-built_in">window</span> :
   <span class="hljs-string">"object"</span> === <span class="hljs-keyword">typeof</span> self ? self : <span class="hljs-keyword">this</span>);</pre></div></div>
            
        </li>
        
    </ul>
  </div>
</body>
</html>