Creating a library for consumption that uses requirejs?

I have a question that I cannot find an answer to. I am currently creating 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

I want me to be able to require the signal.js file and any of my modules from any of my other files, without requiring an installation requiring installation, or without me, to find out how the project is installed during development.

For example, what I tried to do in my otter.coffee file is the following:

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

This does not seem to work, since it is required not to find the signal.js file.

requirejs ? , , . i: e -

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

, ? ? ? , , config?

!

-

, main.js , ( ) ( 404 ). , , .

, main.js ...

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

. , , , . , otter.js...

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

require.js?

+5
1

RequireJS : define() require().

, define() ; , (, define()), require().

, - . , signals.js otter.js, , require(). , a require() define().

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');
    }
  }
});

. : a baseUrl requirejs.config().

+2

All Articles