I building a project with requireJs my file structure is the following:
js/
lib/
noty/
layouts/
bottom.js
top.js
...
themes/
default.js
noty.jquery.js
jquery.js
jquery-ui.js
user/
user.js
app.js
And my configuration :
requirejs.config({
baseUrl: 'js/lib',
urlArgs: 'bust=' + (new Date()).getTime(), //only for dev : no-cache
paths: {
user: '../user'
},
shim: {
'jquery-ui': ['jquery'],
'jquery-tmpl': ['jquery'],
'gridy': ['jquery']
}
});
requirejs(['jquery', 'jquery-ui'], function($){
....
});
My concern is about to integrate noty which is a notification plugin that I may use in any modules. This plugin requires to load :
js/lib/noty/noty.jquery.js
js/lib/noty/layout/top.js
js/lib/noty/themes/bottom.js
I'm not sure what is the way to do that?
Concatenate the files ?
Load each file as a dependency? :
requirejs(['jquery', 'jquery-ui', 'noty/noty.jquery.js', 'noty/layout/top.js', etc. ]
Creates some kind of plugin/module for requireJs?
Thanks
or like this:
paths: {
'jquery': 'jquery/1.10.2/jquery',
'noty': 'noty/2.0/jquery.noty',
'noty.themes.default': 'noty/2.0/themes/default',
'noty.layouts.topCenter': 'noty/2.0/layouts/topCenter',
app: '../app',
util: '../util'
},
shim: {
'noty': ['jquery'],
'noty.themes.default': {
deps: ['jquery', 'noty'],
exports: 'jquery'
},
'noty.layouts.topCenter': {
deps: ['jquery', 'noty'],
exports: 'jquery'
}
}
Finally I managed to implement the 3rd solution: I've created a web module that wrap the library in a file named notiy.js :
define(['jquery',
'noty/layouts/topCenter',
'noty/layouts/bottomRight',
'noty/themes/default'],
function($){
$.noty.defaults.timeout = 20000;
return function(type, msg){
var topLayout = 'topCenter',
bottomLayout = 'bottomRight',
layout = {
'alert' : topLayout,
'info' : bottomLayout,
'confirm' : topLayout,
'success' : bottomLayout,
'error' : topLayout,
'warning' : topLayout
};
if(msg && type){
return noty({
text : msg,
layout: layout[type] || topLayout,
type : type
});
}
}
});
I've declared the dependencies in the shim config (to fix the dependency order) in my app.js :
requirejs.config({
baseUrl: 'js/lib',
urlArgs: 'bust=' + (new Date()).getTime(), //only for dev : no-cache
paths: {
user: '../user'
},
shim: {
'jquery-ui' : ['jquery'],
'jquery-tmpl' : ['jquery'],
'gridy' : ['jquery'],
'noty/jquery.noty' : ['jquery'],
'notify' : ['noty/jquery.noty']
}
});
requirejs(['jquery', 'jquery-ui', 'jquery-tmpl', 'notify'], function($, ui, tmpl, notify){
//...
});
Ans it works like a charm!
Related
I have been using requirejs and am having trouble getting the optimization tool to work. All I want to do is optimize my javascript files into one script file with all my dependencies included.
All my file are in the one js/ folder and I have a sub folder vendors/
I have a config file which looks like this(config.js):
requirejs.config({
baseUrl: ".",
out: "main-built.js",
deps: ["main"],
paths: {
jquery: "vendor/jquery", // v.1.10.2
bootstrap: "vendor/bootstrap",
ajaxform: "vendor/ajaxform",
skrollr: "vendor/skrollr",
jqueryform:"vendor/jqueryform"
},
shim: {
bootstrap: {
deps: ["jquery"],
exports: "bootstrap",
},
ajaxform: {
deps: ["jquery"],
exports: "ajaxform",
},
jqueryform: {
deps: ["jquery"],
exports: "jqueryform",
}
}
});
And I have an main javascript file (main.js) which looks like this:
if (typeof jQuery === 'function') {
define('jquery', function () { return jQuery; });
}
define(["jquery","bootstrap", "ajaxform","skrollr"],
function($, bootstrap, ajaxform, skrollr) {
//have other code here
});
I also created a build file:
({
baseUrl: ".",
paths: {
jquery: "vendor/jquery", // v.1.10.2
bootstrap: "vendor/bootstrap",
ajaxform: "vendor/ajaxform",
skrollr: "vendor/skrollr",
jqueryform:"vendor/jqueryform"
},
shim: {
bootstrap: {
deps: ["jquery"],
exports: "bootstrap",
},
ajaxform: {
deps: ["jquery"],
exports: "ajaxform",
},
jqueryform: {
deps: ["jquery"],
exports: "jqueryform",
}
},
name: "main",
out: "main-built.js"
})
All the build file does is compress the main.js file itself and doesn't include all needed files.
I've been trying to get this working on and off for weeks and can't seem to get it right so any help would be REALLY appreciated.
Thanks
Try adding a modules entry to your build file with your main module, as in:
modules: [
{ name: 'main' }
]
You can see all the options for the build file here: https://github.com/requirejs/r.js/blob/master/build/example.build.js
I recently worked on a web app, and i wanted to separate it into modules.
I searched and found out that RequireJs is the best for this. I read the doc, and some tutorials about it and came up with this
requirejs.config({
baseUrl: "js/lib",
shim: {
'jquery-1.12.1.min': ['jquery-1.12.1.min'],
'materialize.min': ['materialize.min'],
'knockout-3.4.0': ['knockout-3.4.0'],
'pouchdb-5.3.0.min': ['pouchdb-5.3.0.min']
},
waitSeconds: 0,
paths: {
'jquery-1.12.1.min': 'jquery-1.12.1.min',
'materialize.min': 'materialize.min',
'knockout-3.4.0': 'knockout-3.4.0',
'pouchdb-5.3.0.min': 'pouchdb-5.3.0.min',
}
});
require(["jquery-1.12.1.min", "knockout-3.4.0", "materialize.min", "pouchdb-5.3.0.min"], function($, ko) { //My main functions and other stuff ... }
My index has the following:
<script data-main="js/main" src="js/lib/require.js"></script>
<script type="text/javascript" src="js/converter.js"></script>
But it seems that my Jquery, KO, materialize.css and pouchdb aren't loading as i see on my chrome dev tools in Network:
I also read about the shim config how to set up the dependences and i want them in that order. I don't really know what am i missing, any help would really be appreciated.
Folder structure is:
js:
lib:
->jquery-1.12.1.min.js
->knockout-3.4.0.js
->pouchdb-5.3.0.min.js
->materialize.min.js
->require.js
main.js
converter.js
Ok it seems require and materialze.css have a problem with hammer.js, what i recommend is you go and download Hammer.min.js and include it in your lib folder.
Then in your main.js add the following:
requirejs.config({
baseUrl: 'js/lib',
waitSeconds: 0,
paths: {
app: '../app',
jquery: 'jquery-1.12.1.min',
materialize: 'materialize.min',
knockout: 'knockout-3.4.0',
pouchdb: 'pouchdb-5.3.0.min',
hammer: 'hammer.min'
},
shim: {
hammer: {
deps: ['jquery']
},
pouchdb: {
deps: ['jquery']
},
knockout: {
deps: ['jquery'],
exports: 'ko'
},
materialize: {
deps: ['jquery', 'hammer'],
},
jquery: {
exports: '$'
}
}
});
You include you hammer and also the dependency for jquery on it. And i see that you didn't put the PouchDb object in the function:
define(["jquery", "knockout", "pouchdb", "materialize" ], function($, ko, PouchDB) {...
Hope this helps
Using path, requirejs replace module id prefixes (name in config) with the corresponding value.
Using shim you can export a global variable and configure the dependencies ( materialize depends jquery so you have to import jQuery before importing materialize.js )
requirejs.config({
baseUrl: "js/lib",
waitSeconds: 0,
paths: {
'jquery': 'jquery-1.12.1.min',
'materialize': 'materialize.min',
'knockout': 'knockout-3.4.0',
'pouchdb': 'pouchdb-5.3.0.min',
'hammer': 'hammer.min',
},
shim: {
pouchdb: {
deps: ['jquery']
},
knockout: {
exports: 'ko'
},
materialize: {
deps: ['jquery', 'hammer']
},
jquery: {
exports: '$'
}
}
});
require(["jquery", "knockout", "materialize", "pouchdb"], function ($, ko) {
console.log($);
console.log(ko);
});
So, I export PNotify from bower to the js folder. I use requirejs to include my libraries
HTML
<script data-main="assets/js/app" type="text/javascript" src="assets/js/lib/require.js"></script>
My architecture is like this :
js
--- app.js
--- lib
------ pnotify.core.js
------ pnotify.desktop.js
------ pnotify.buttons.js
------ pnotify.callbacks.js
------ pnotify.confirm.js
------ pnotify.history.js
------ pnotify.nonblock.js
------ pnotify.reference.js
------ jquery.js
------ ...
I add the primary module which is pnotify.core
APP.JS
requirejs.config({
base: '/assets/js',
paths: {
'jQuery': 'lib/jquery.min',
'underscore': 'lib/underscore-min',
'Backbone': 'lib/backbone',
'Modernizr' : 'lib/modernizr',
'PNotify' : 'lib/pnotify.core',
'LoginView' : 'views/login'
},
shim: {
'jQuery': {
exports: '$'
},
'underscore': {
exports: '_'
},
'Backbone': {
exports: 'Backbone'
},
'Modernizr': {
exports: 'Modernizr'
},
'LoginView': {
exports: 'LoginView'
},
'PNotify': {
exports: 'PNotify'
}
}
});
PNotify is loaded in my page. Normally, PNotify works with requirejs : https://github.com/sciactive/pnotify#using-pnotify-with-requirejs
I don't know how can I import all modules, maybe concat these modules in one file pnotify.min.js?
Here, when I call the PNotify object under the requirejs.config
define(['jQuery', 'underscore', 'Backbone', 'Modernizr', 'LoginView', 'PNotify'], function ($, _, Backbone, Modernizr, LoginView, PNotify) {
$(document).ready(function(){
new PNotify({
title: 'Desktop Notice',
text: 'If you\'ve given me permission, I\'ll appear as a desktop notification. If you haven\'t, I\'ll still appear as a regular PNotify notice.'
});
});
});
I have this error : Uncaught TypeError: undefined is not a function on the line "new PNotify"...
Do you have some ideas?
When they detect AMD/RequireJS, PNotify core defines the named module
"pnotify", and PNotify's modules each define names like
"pnotify.module". The following example shows the use of the nonblock
and desktop modules with RequireJS.
So my error is here
requirejs.config({
base: '/assets/js',
paths: {
'jQuery': 'lib/jquery.min',
'underscore': 'lib/underscore-min',
'Backbone': 'lib/backbone',
'Modernizr' : 'lib/modernizr',
'PNotify' : 'lib/pnotify.core',
^_____ 'replace by pnotify'
'LoginView' : 'views/login'
},
...
Compile All the modules into one minified file.
These Settings work for me in Require.js
paths: {
'pnotify': 'lib/pnotify.min',
'pnotify.nonblock': 'lib/pnotify.min',
'pnotify.desktop': 'lib/pnotify.min,
'jquery' : 'lib/jquery'
}
define(['jquery', 'pnotify','pnotify.nonblock', 'pnotify.desktop', function($, pnotify){
new PNotify({
title: 'Desktop Notice',
text: 'If you\'ve given me permission, I\'ll appear as a desktop notification. If you haven\'t, I\'ll still appear as a regular PNotify notice.',
desktop: {
desktop: true
},
nonblock: {
nonblock: true
}
});
});
I have this code in my main.js script, but I can't make it works with jQueryUI, because somehow when trying to load jQueryMobile, it always loads jquery.ui.tabs before jquery.ui.widget, with the latter being a dependency of the former
requirejs.config({
baseUrl: './js',
paths: {
'libs' : '../libs',
'jquery' : '../libs/jquery/jquery.min',
'jquery-plugins' : '../libs/jquery-mobile/external/jquery/plugins',
'jquery-ui' : '../libs/jquery-ui/ui',
"jquery.ui.widget" : '../libs/jquery-ui/ui/jquery.ui.widget',
'jquery.ui.tabs' : '../libs/jquery-ui-tabs/ui/jquery.ui.tabs',
'jquery-mobile' : '../libs/jquery-mobile/js/',
'backbone' : '../libs/backbone/backbone',
'underscore' : '../libs/underscore/underscore',
'json' : '../libs/json2/json',
'json2' : '../libs/json2',
'jstorage' : '../libs/jstorage/jstorage.min',
'mustache' : '../libs/mustache'
},
shim: {
'underscore': {
exports: '_'
},
'backbone': {
deps: ['underscore', 'jquery'],
exports: 'Backbone'
},
'json': {
exports:'JSON'
},
'jstorage': ['jquery', 'json'],
'mustache': {
exports: 'Mustache'
},
'jquery-plugins' : ['jquery'],
'jquery.ui.widget' : ['jquery', 'jquery.ui.core'],
'jquery.ui.tabs' : [ 'jquery' , 'jquery.ui.widget']
}
});
require(['app'], function(App){
$(document).on('mobileinit', function(){
console.log('Evento mobileinit');
App.initialize();
});
})
I'm trying to use both Underscore and Underscore.string with RequireJS.
Contents of main.js:
require.config({
paths: {
'underscore': '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min',
'underscore-string': '//cdnjs.cloudflare.com/ajax/libs/underscore.string/2.3.0/underscore.string.min',
},
shim: {
'underscore': {
exports: '_'
},
'underscore-string': {
deps: ['underscore'],
exports: '_s'
},
}
});
var modules = ['underscore-string'];
require(modules, function() {
// --
});
Browser sees the _, but doesn't see the _s - it is undefined.
Ideally i want to have Underscore under _ and Underscore.string under _.str, but _ and _s are fine too. How can i do that?
Versions: RequireJS 2.1.5, Underscore 1.4.4, Underscore.string 2.3.0
Note: Thanks to #jgillich make sure, that paths have two slashes (//cdnjs.cloudfare.com/...), otherwise the browser would think that URL is relative to the server, and Firebug will throw:
Error: Script error
http://requirejs.org/docs/errors.html#scripterror
I found the error. For some reason RequireJS doesn't work with version of Underscore.string from cdnjs.com, so i replaced it with Github version. I guess it has something to do with the commit 9df4736.
Currently my code looks like the following:
require.config({
paths: {
'underscore': '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min',
'underscore-string': '//raw.github.com/epeli/underscore.string/master/dist/underscore.string.min',
},
shim: {
'underscore': {
exports: '_'
},
'underscore-string': {
deps: ['underscore'],
},
}
});
var modules = ['underscore', 'underscore-string'];
require(modules, function(_) {
// --
});
Underscore.string resides in _.str.
Edit: As of 16 July 2013 the CDNJS version is updated with the upstream.
Battling with this for hours before i understand what i was doing wrong
This is what i did wrong
You should not rename the file underscore.string in main.js
even though in my library i did rename the file in paths i name it back to 'underscore.string'
This is how your main.js should look like
require.config({
paths: {
underscore: 'lib/underscore',
'underscore.string' : 'lib/_string' ,
},
shim: {
underscore: {
exports: '_',
deps: [ 'jquery', 'jqueryui' ]
},
'underscore.string': {
deps: [ 'underscore' ]
},
}
....
You could then either add it as dependency with in your shim like i did for my mixin file
shim: {
mixin : {
deps: [ 'jquery', 'underscore', 'underscore.string' , 'bootstrap' ]
},
Or just define it in your different pages like
/*global define */
define([
'underscore.string'
], function ( ) {
it just work now you can access it through _.str or _.string
This is why you should do it this way and not try to name it something else
on line 663 of underscore.string.js
// Register as a named module with AMD.
if (typeof define === 'function' && define.amd)
define('underscore.string', [], function(){ return _s; });
Which means that it will only register it with AMD require JS if you are defining 'underscore.string'
works for my ONLY if I use exact "underscore.string" module name in shim. Seems related to hardcoded name in underscore.string itself
Exempt from underscore.string source code (this branch is executed when require used):
// Register as a named module with AMD.
if (typeof define === 'function' && define.amd)
define('underscore.string', [], function(){ return _s; });
So for me the only working configuration is:
require.config({
paths: {
'underscore': '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min',
'underscore.string': '//raw.github.com/epeli/underscore.string/master/dist/underscore.string.min',
},
shim: {
'underscore': {
exports: '_'
},
'underscore.string': {
deps: ['underscore'],
},
}
});
var modules = ['underscore', 'underscore.string'];
require(modules, function(_) {
// --
});
Here's a working code using Requirejs "order" plugin, also includes Jquery, and everything loads without any conflict:
requirejs.config({
baseUrl: "assets",
paths: {
order: '//requirejs.org/docs/release/1.0.5/minified/order',
jquery: 'http://code.jquery.com/jquery-2.1.0.min',
underscore: '//underscorejs.org/underscore-min',
underscorestring: '//raw.githubusercontent.com/epeli/underscore.string/master/dist/underscore.string.min',
underscoremixed: 'js/underscore.mixed' // Create separate file
},
shim: {
underscore: { exports: '_' },
underscorestring: { deps: ['underscore'] }
}
});
require(['order!jquery','order!underscoremixed'], function($,_) {
// test
console.log( _.capitalize('capitalized text') );
});
Inside js/underscore.mixed.js put the following...
define(['underscore','underscorestring'], function() {
_.mixin(_.str.exports());
return _;
});
Cheers! :)