Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building a library for consumption that uses requirejs?

I have a question that I can't seem to find an answer to. I'm currently building a library for consumption and I want to use requirejs to make it more modular. I have the following project structure...

- example
    - scripts
        - otter
            - libs
                - signals
                    signals.js
            - modules
                subModule1.js
                subModule2.js
            otter.js
    index.htm

- src
    - libs (copied to example folder at build time)
        - signals
            signals.js
    - modules
        subModule1.coffee
        subModule2.coffee
    - otter.coffee

What I want to do is be able to require the signals.js file, and any of my modules, from any of my other files without requiring a path to be setup in require, or without me having to know about how the project is setup at development time.

For example, what I've attempted to do in my otter.coffee file is this:

define ['/libs/signals/signals'], (signals) ->
    # I've also tried './libs/signals/signals' and 'libs/signals/signals'

This doesn't seem to work as require can't find the signals.js file.

How can I get requirejs to load modules relative to the current file? I've read that I can ask for require as a dependency and that the path for that require will be set to the path of the file, but that doesn't seem to work either. It throws a not loaded yet error i:e -

define (require) ->
    signals = require './libs/signals/signals'

Any ideas how I can accomplish this? Do I need to change the structure of my library? Do I have to force a specific structure? Do I ask those who use it to include certain paths in their require config?

Many thanks in advance!

EDIT -

I've noticed that if I alias my library inside my main.js file for require, the require (through require or define) of signals doesn't work (keep getting 404 not found). However, if I don't alias my library everything works fine..

For example, if my require main.js file has..

...
paths: {
    'otter': 'scripts/otter/otter'
}
...

then the require of signals fails. However, if I don't have the path and I require otter through it's direct path, the require of signal works. I'm requiring signal like this in otter.js ...

define (require) ->
    signals = require './libs/signals/signals'

It this a bug with require.js?

like image 488
Jason L. Avatar asked Nov 03 '22 20:11

Jason L.


1 Answers

RequireJS uses two functions to do its job: define() and require().

My understanding is that define() lists dependencies and only loads modules; loading dependencies that are not modules (those who not use define()) is done with require().

Because only modules can load modules, there have to be someone else to load the first module. In your case, to use signals.js in otter.js the first has to be a module and the second has to use require(). I guess that a require() is always needed before any define() uses.

otter.js:

require.config({
  paths: {
    'signals': 'libs/signals/signals',
    'module1': 'modules/subModule1'
  }
});

require(['signals'], function(signals) {
  signals.initialize();
});

signals.js:

define(['module1'], function(module) {
  return {
    initialize: function() {
      module.doStuff();
    }
  }
});

subModule1.js:

define([], function() {
  return {
    doStuff: function() {
      console.log('hello world');
    }
  }
});

ps. Loading modules relative to the current file do not seems to be allowed: a baseUrl property is used to configure every modules paths in requirejs.config().

like image 130
yves amsellem Avatar answered Nov 09 '22 04:11

yves amsellem