1

I have a directory structure as follows.

|-- app.js
|-- config.js
|-- index.html
|-- js
|   |-- firstpage
|   |   `-- firstpage.js
|   |-- lib
|   |   `-- jquery-3.2.0.min.js
|   `-- secondpage
|       `-- secondpage.js
`-- require.js

app.js

define(function(require, module){
  var $ = require("jquery"),
  firstpage = require("./firstpage/firstpage"),
  secondpage = require("./secondpage/secondpage");
})

if I load index.html in browser "firstpage" and "secondpage" loads properly.

but what i am looking for is to load these scripts as follows with just the name:

  firstpage = require("firstpage"),
  secondpage = require("secondpage");

I found "paths" attribute to be useful in docs, but this seems to be overkill when i have large number of files which i want to load and would need to give separate entry for each of the file.

Can someone please suggest a better way to achieve this ?

There seems to be a lot of noise(for the lack of better word) around this on internet but i am unable to narrow down to the exact solution using those (there is duplicate question as well which does not seem to answer this)

Community
  • 1
  • 1
Gaurav Ingalkar
  • 1,217
  • 11
  • 20

1 Answers1

0

If your modules were all under the same directory, you could set baseUrl to the directory that contains them and then you could just require the module with an absolute name, without having to specify one path per module in your configuration. However, your modules are under different directories, so you cannot do this.

The functionality you need is equivalent to performing the following transformation: moduleName.replace(/^(.*)$/, "./$1/$1"). Unfortunately, RequireJS' runtime configuration does not support this kind of pattern matching. If you want to avoid having to create one entry per module, you can write a script that fills out paths automatically for you.

Louis
  • 146,715
  • 28
  • 274
  • 320