I have an issue to make two different builds on my app directory. I am using
Backbone+CoffeeScript, so it's a lot of staff in the folders.
I used build.js to generate one big result.js and load it on the page.
Now i want to make two different router.coffee files to include router1.coffee in one build(excluding router2.coffee) and vise versa to the other build.
Project structure:
app/
models/
model.coffee
collections/
collection.coffee
.............
lib/
settings.coffee
router1.coffee
router2.coffee
main.js
build.js
build/
result.js
My build.js:
({
baseUrl: ".",
name: "main",
out: "../build/result.js",
stubModules: ['cs', 'text'],
exclude: ['coffee-script'],
fileExclusionRegExp: /^views$/,
preserveLicenseComments: false,
optimize: "none",
paths: {
"cs" : "libs/cs",
'coffee-script' : 'libs/coffee-script',
'jquery' : 'libs/jquery-1.9.1',
'jquery_ui' : 'libs/jquery-ui.custom.min',
'backbone' : 'libs/backbone',
'backbone-relational': 'libs/backbone-relational',
//require.js dependency
"requireLib" : "libs/require"
},
shim: {
'backbone': {
deps: ['underscore', 'jquery'],
exports: 'Backbone'
},
'underscore': {
exports: '_'
},
'jquery_ui' : ['jquery'],
'fileDownload' : ['jquery']
},
include: "requireLib"
})
How can i have result1.js with only router1 included, and result2.js with only router2 included?
You should use two build.js file, one for each router.
Related
I have a project with a lot of libraries
require.config({
shim: {
'fileB' : { deps: ['fileS'] },
'angular' : { exports: 'angular', deps: ['fileB'] },
'uiRouter' : { deps: ['angular'] },
'ngAnimate' : { deps: ['angular'] },
'lodash' :{ deps:['angular'] },
'angular-lodash' : { deps:['angular', 'lodash'] }
},
paths: {
'angular' : '../libs/angular.min',
'ngAnimate' : '../libs/angular-animate.min',
'uiRouter' : '../libs/angular-ui-router.min',
'fileS' : 'fileS',
'lodash' : '../libs/lodash.min',
'angular-lodash': '../libs/angular-lodash'
},
deps: [
'./start'
]
});
My goal is to put everything in one file except the files in the libs directory
requirejs: {
compile: {
options: {
almond: true,
replaceRequireScript: [
{
files: ['dist/index.html'],
module: 'config',
modulePath: 'config'
}
],
name: 'config',
mainConfigFile: 'tmp/config.js',
baseUrl: 'tmp',
out: 'dist/script.min.js'
}
}
},
The problem is, when I try to exclude angular it excludes everything. I want to have the files in the dist directory, everything in the temp should be uglified into one file and libs seperate files.
Is this possible? Thanks for the help.
I am using this awesome plugin for managing Handlebars templates in my Backbone app created by Yeoman: SlexAxton/require-handlebars-plugin
For some reason, the i18n translations are not picked up when running a production ready compilation created by Grunt with RequireJS.
My app config looks as follows:
require.config({
shim: {
backbone: {
deps: ['jquery', 'underscore'],
exports: 'Backbone'
},
underscore: {
exports: '_'
}
},
paths: {
jquery: '../bower_components/jquery/dist/jquery',
backbone: '../bower_components/backbone/backbone',
underscore: '../bower_components/lodash/dist/lodash.underscore',
hbs: '../bower_components/require-handlebars-plugin/hbs'
},
hbs: {
i18n: true
}
});
// Second call to assign dynamic variables and not have RequireJS break
require.config({
locale: localStorage.getItem('locale') || 'en_us'
});
And my requireJS options set in Gruntfile.js are as follows:
requirejs: {
dist: {
options: {
baseUrl: '<%= yeoman.app %>/scripts',
useStrict: true,
wrap: true,
findNestedDependencies: true
}
}
},
For some reason, the generated build JS file has default translations already compiled into templates. Am I missing something?
Should I switch to RequireJS i18n plugin?
Thanks.
EDIT:
What is the common practice to handle production build with the "require-handlebars-plugin"? Is the plugin compatible with Grunt's "grunt-requirejs" compiler?
I'm trying to build my single page application that uses Require.js, AMD style modules.
I already configured all the dependencies, paths and shims in my init.js file that runs first and then calls my main.js file.
Here is my init.js file :
(function() {
'use strict';
var me = this;
me.require.config({
deps: ['backbone.marionette', 'main'],
shim: {
jquery: {
exports: 'jQuery'
},
underscore: {
exports: '_'
},
backbone: {
deps: [
'underscore',
'jquery'
],
exports: 'Backbone'
},
'backbone.marionette': {
deps: ['jquery', 'underscore', 'backbone'],
exports: 'Marionette'
}
},
paths: {
jquery: '../lib/jquery/dist/jquery.min',
html5shiv: '../lib/html5shiv/html5shiv.js',
underscore: '../lib/underscore/underscore-min',
backbone: '../lib/backbone/backbone',
'backbone.marionette': '../lib/backbone.marionette/lib/core/amd/backbone.marionette.min',
'backbone.wreqr': '../lib/backbone.wreqr/lib/amd/backbone.wreqr.min',
'backbone.babysitter': '../lib/backbone.babysitter/lib/amd/backbone.babysitter.min',
text: '../lib/requirejs-text/text'
}
});
}).call(this);
and here is my r.js build config file :
({
baseUrl: '../../development/js',
optimize: 'closure',
paths: {
requireLib: '../lib/requirejs/require'
},
mainConfigFile: '../../development/js/init.js',
out: '../../distribution/main-build.js',
include: ['requireLib']
})
When I build, I only get Require.js inlined in my main-build.js file.
R.js doesn't follow the deps property defined in the require.config for some reason.
Any ideas on how to get a single file containing my Backbone.js / Marionette.js files and the dependencies described in the require.config found in the init.js file ?
Thanks in advance !
I wonder if it is the way you defined your config. Is there any reason for the init.js as opposed to just putting the require.config in main? It seems unnecessary to create an anonymous function just to encapsulate it.
From the example r.js build file:
The first requirejs({}), require({}), requirejs.config({}), or
require.config({}) call found in that file will be used.
What has always worked for me is to follow the more standard method of calling require.config() inside main.js
I have a problem with an r.js build in my app loader.
Unbuild mode is working perfect but after a build with r.js the variables in app_loader.js#L7 bb and hb are undefined. So far I work around by using global variables Handlebars und Backbone but what is wrong with this shim?
I have removed your global references and tested this locally. It works.
build.js - updated to use app_main as config file
({
optimizeCss: "standard",
removeCombined: true,
preserveLicenseComments: false,
appDir: "../www-root-dev",
dir: "../www-root",
keepBuildDir: false,
optimize: "uglify2",
mainConfigFile: "../www-root-dev/assets/js/app_main.js",
modules: [{
name: "app_main"
}]
})
app.js
define(["app_loader"], function(loader){
var $ = loader.$, Backbone = loader.Backbone;
...
});
app_loader.js
define(["jquery","underscore","backbone","handlebars","app_routes"],
function($, _, Backbone, Handlebars, router){
return {
$: $.noConflict(),
_: _,
Backbone: Backbone,
router: router,
Handlebars: Handlebars
};
});
app_main.js - updated to simplify paths
require.config({
baseUrl: './assets/js',
paths: {
mvc: '../../mvc'
},
shim: {
underscore: {
exports: '_' //the exported symbol
},
backbone : {
deps: ['underscore', 'jquery'],
exports: 'Backbone'
},
handlebars: {
deps: ['jquery'],
exports: 'Handlebars'
}
}
});
require(['app'], function(App){
App.initialize();
});
app_routes.js
define(["jquery", "underscore", "backbone", "mvc/demo.view.js", "mvc/demo.model.js"], function($, _, Backbone, DemoView, DemoModel) { ... });
demo.model.js
define(["backbone"], function(Backbone) { ... });
demo.view.js
define(["jquery","backbone","text!mvc/demo.html"], function($, Backbone,demoTemplate) { ... });
I think the error is because in your shim you have exported backbone and handlebar as Backbone and Handlebars respectively,
backbone : {
deps: ['underscore', 'jquery'],
exports: 'Backbone'
},
handlebars:{
deps: ['jquery'],
exports: 'Handlebars'
},
and in your app_loader.js#L7 you are using it as bb and hb.
Either in shim export it as bb and hb:
backbone : {
deps: ['underscore', 'jquery'],
exports: 'bb'
},
handlebars:{
deps: ['jquery'],
exports: 'hb'
},
or use Backbone and Handlebars in app_loader.js#L7 instead of bb and hb:
define("app_loader",[
"jquery",
"underscore",
"backbone",
"handlebars",
"order!assets/js/app_routes.js"
], function(jQuery, underscore, Backbone, Handlebars, router){
I am also new to backbone and requirejs but it should help.
I have forked your github project and use grunt requirejs to run the optimization.
I manage to run the site and didn't notice any console errors in Chrome.
I took your project and noticed the errors in your build. I then made some changes to your app_main.js file as follows. Let me know if this solves your issues? Solved mine.
// Require.js allows us to configure shortcut alias
// Their usage will become more apparent futher along in the tutorial.
require.config({
shim: {
underscore: {
exports: '_' //the exported symbol
},
backbone : {
deps: ['underscore', 'jquery'],
exports: 'Backbone'
},
handlebars: {
exports: 'Handlebars'
}
},
paths: {
appLoader: 'app_loader',
jquery: 'jquery',
underscore: 'underscore',
backbone : 'backbone' ,
handlebars: 'handlebars'
}
});
require([
'app'
], function(App){
App.initialize();
});
Here is my require.js load file:
require.config({
urlArgs: "noCache=" + (new Date).getTime(),
paths: {
jquery: "vendor/jquery-1.8.2.min",
bootstrap: "vendor/bootstrap.min",
underscore: "vendor/underscore-min",
backbone: "vendor/backbone-min",
template: "libs/template"
}
});
require(["jquery", "bootstrap", "underscore", "backbone", "template", "main"],
function ($, bootstrap, underscore, backbone, template, main) {
})
And inside the main.js file I have the following code:
define(["jquery", "underscore", "backbone"], function ($, _, Backbone) {
//Backbone and _ are undefined here, why?
})
So why ,,_" and ,,Backbone" are undefined here, what am I doing wrong?
How to properly use them inside other files?
If you are new to backbone and require js integration take a look to the following tutorial:
http://backbonetutorials.com/organizing-backbone-using-modules/
You are probably using the non-AMD version of Backbone and underscore. Using shim config you can load any library, even non-AMD modules, here is a shim config snippet from one of my source files:
requirejs.config({
baseUrl: 'assets/js/',
paths : {
'jquery': 'lib/jquery/jquery.min',
'underscore': 'lib/underscore/underscore-amd-min',
'backbone': 'lib/backbone/backbone-amd-min',
'bootstrap': 'lib/bootstrap/bootstrap.min',
},
shim: {
'backbone': {
deps: ['jquery', 'underscore'],
exports: 'Backbone'
},
'underscore': {
exports: '_'
},
'bootstrap': {
deps: ['jquery']
}
}
});
An alternative solution would be using the AMD version of those libraries you will find them here:
https://github.com/amdjs
Download and point to them in your path section.