0

Here is a sample application demonstrating the issue (sorry no fiddle since it's about r.js which is node based)

I can't get the requirejs optimizer to work properly with relative paths.

my web app has the following directories

  • app
    • index.html - includes requirejs, main.js, then require(['plugins']) (standard multipage setup)
    • scripts
      • main.js - bootstraps requirejs, sets baseUrl to /scripts
      • plugins.js - references and exposes modules from app-plugins. References via ../app-plugins/a.js
    • app-plugins
      • a.js

everything works fine in the non-optimized version (run a webserver at app, open console and navigate to the index page).

Optimizing, however, gives me an error because of the ../ in plugins.js. To see this cd to the optimization directory and run node .\node_modules\requirejs\bin\r.js -o .\build.json. The error will be

W:\temp\requireop\optimization [master]> .\Build-RequireJs.ps1

Tracing dependencies for: main

Tracing dependencies for: plugins
Error: ENOENT, no such file or directory 'W:\temp\requireop\build\app-plugins\a.js'
In module tree:
    plugins

Error: Error: ENOENT, no such file or directory 'W:\temp\requireop\build\app-plugins\a.js'
In module tree:
    plugins

    at Object.fs.openSync (fs.js:427:18)
George Mauer
  • 103,465
  • 117
  • 349
  • 581

1 Answers1

1

I did the following changes and it worked:

  • main.js:

    baseUrl: 'scripts'
    

    In the general case paths are case sensitive; also better not to include the leading slash.

  • optimization/build.json becomes:

    {
        appDir: '../app',
        baseUrl: 'scripts',
        dir: '../build',
        modules: [ // the same
        ]
    }
    

    You can find the details here, but some explanations:

    1. appDir lets r.js know where the sources are. I believe this also makes r.js know how to handle relative paths - not quite sure though.
    2. Then baseUrl becomes relative to the appDir.
    3. And the dir is where the output goes, it will also copy files found in your appDir in there.

This process overwrites files in /build/.

Nikos Paraskevopoulos
  • 36,975
  • 10
  • 83
  • 85