Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

relative path doesn't work with paths

I'm having a problem with relative paths in requirejs.

First of all, I have the following structure. I'm running it with a virtual host (os.com) and the path is os.com/test

enter image description here

index.html

<script data-main="config" src="require.js"></script>

config.js

require.config({

  baseUrl: "./apps",
  deps: ['ui'],
  paths: {
    ui: 'ui/ui',
    system: 'system/system',
    core: 'core/core'
  }


});

ui.js

define(['./class/menuBuilder',"./class/window"], function(menuBuilder, windowBuilder){


    return {
        menuBuilder: menuBuilder,
        windowBuilder: windowBuilder
    }

});

When I run it, I get the following errors.

GET http://os.com/test/apps/class/menuBuilder.js 404 (Not Found)
GET http://os.com/test/apps/class/window.js 404 (Not Found)

If I take out 'ui' property from the 'paths' property then change deps to ['ui/ui'], it works, but I would like to use paths.

Changed config.js

require.config({

  baseUrl: "./apps",
  deps: ['ui/ui'],
  paths: {
    system: 'system/system',
    core: 'core/core'
  }


});

How do I change my config to make paths and relative path work together?

like image 842
Moon Avatar asked Sep 04 '12 20:09

Moon


People also ask

Why is it better to use relative paths instead of absolute paths?

Relative links show the path to the file or refer to the file itself. A relative URL is useful within a site to transfer a user from point to point within the same domain. Absolute links are good when you want to send the user to a page that is outside of your server.

How does Python deal with relative paths?

A relative path starts with / , ./ or ../ . To get a relative path in Python you first have to find the location of the working directory where the script or module is stored. Then from that location, you get the relative path to the file want.

Can absolute path and relative path be the same?

In simple words, an absolute path refers to the same location in a file system relative to the root directory, whereas a relative path points to a specific location in a file system relative to the current directory you are working on.

How do you use relative paths?

Relative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate into the current directory and the parent directory. Double dots are used for moving up in the hierarchy. A single dot represents the current directory itself.


2 Answers

I had similar problem. I didn't have deps:'ui' part in my configuration, just the path setting, but still the relative module reference ('./class/menuBuilder') would not work from the module loaded with path ("ui: 'ui/ui'") and would use baseUrl instead. To solve it, I defined 'ui' as a package:

require.config({

  baseUrl: "./apps",
  deps: ['ui'],
  paths: {
    system: 'system/system',
    core: 'core/core'
  },
  packages : [
    { 
        name: 'ui',
        location : 'ui',
        main : 'ui'
    },
  ]

});

In this case require will load relatively-pathed modules correctly.

Here is a useful post: Relative paths with RequireJS modules/packages

like image 73
Ashot Avatar answered Oct 19 '22 01:10

Ashot


The solution here is to use map configuration, not paths configuration. Paths configurations should only be used for folders, not modules themselves. Map configurations apply to individual modules.

So try:

require.config({
  map: {
    '*': {
      'ui': 'ui/ui'
    }
  }
});
like image 45
Guy Bedford Avatar answered Oct 19 '22 00:10

Guy Bedford