requirejs

RequireJS实战-优化oschina前端JS(完)

瘦欲@ 提交于 2019-12-06 16:11:47
本人文笔很烂请见谅,欢迎吐槽和各种拍砖!分享快乐! 关于RequireJS入门与好处,这里就直接略过了。(网上有很多), 我们就以OSchina.net为例,看看应用RequireJS是如何模块与优化JS的。 首先我们来看看 OSchina.net的首页JS的加载情况与页面源代码。 从这个加载可以看出如下问题: JS没有做任何的压缩与合并。 页面与JS代码耦合在一起。 看看应用RequireJS是如何优化OSchina站点的。 第一步:将页面所需要的JS,在require.config()进行配置。如: require.config({ baseUrl : 'scripts/', paths : { 'jquery':'vendor/jquery/1.7.1/jquery', }, shim : {//配置依赖,关系到加载顺序 'vendor/jquery.form':['jquery'], 'vendor/jquery.fancybox-fixed':['jquery'], 'vendor/jquery.poshytip.min':['jquery'], 'vendor/oschina':['jquery'], 'vendor/utils':['jquery'], 'vendor/channel':['jquery'] } }); 第二步:对首页进行,模块化封装。oschina

Requirejs, what it means “Requirejs loads each module once”

ⅰ亾dé卋堺 提交于 2019-12-06 16:03:17
So if i have some module like: //test1.js define([], function() { var counter = 0; return { increment: function() { counter++ } }; }); Requirejs will load this module once(to be aware of local variables of this module) and never again? Variable counter will be alive through closure from returning object(through increment function)? What about next example? //test2.js define([], function() { var value = 10; document.getElementById('asd').addEventListener('mousedown', function(e) { value = Math.random(); }); return value; }); With this structure of code this event never get triggered because

angularjs + requirejs structure to build a huge modular app

懵懂的女人 提交于 2019-12-06 15:18:27
I'm trying to build a huge Modular Web App with AngularJs and RequireJS. This is my directory that I want to build: |--index.html |--css |--images |--libs | └--angular.js | └--angular-route.js | └--require.js | |--js | └--app.js | └--controller.js | └--module |--user | |--controller | | └--index.js (that is controller to list all users) | | └--add.js (this controller to add new user) | |--service | | └--user.js | └--template | └--index.tpl.html | └--add.tpl.html | └--photo |--controller | └--index.js | └--add.js |--service | └--photo.js └--template └--index.tpl.html └--add.tpl.html In app.js

Uncaught Error: No module: MyApp

那年仲夏 提交于 2019-12-06 15:16:09
问题 I load AngularJs and jQuery using RequireJs in nodeJs framework. That is main.js require.config({ paths: { angular: 'vendor/angular.min', bootstrap: 'vendor/twitter/bootstrap', jquery: 'vendor/jquery-1.9.0.min', domReady: 'vendor/require/domReady', underscore: 'vendor/underscore.min' }, shim: { angular: { deps: [ 'jquery' ], exports: 'angular' } } }); require([ 'app', 'angular-boot' ], function() { }); in app.js define(['angular'], function (angular) { return angular.module('MyApp', []); })

Accessing variable from different files(module) using requirejs

大城市里の小女人 提交于 2019-12-06 15:00:17
问题 I am trying to make MVC structure of application in canjs. For that I am using requireJS to separate the code in different file. I have searched in google and i am following this tutorial but in that tutorail I dont find to access module variables in different modules. therefore I am following this method to do so. But I cannot achieve that. This is my code: requirejsconfig.js file : requirejs.config({ paths :{ enforceDefine: true, waitSeconds : 0, jquery : "https://ajax.googleapis.com/ajax

requirejs - what export exactly do here?

末鹿安然 提交于 2019-12-06 14:29:43
I use the require.js - when i use the requiredjs, i am not get the underscore and backbone while i console without using shim's export modoule. But jquery not asking this export shim dependent.. so, why we need to use the shim and it's export for the underscore and backbone? here is my code: requirejs.config({ baseUrl: 'js', paths: { "jquery":'lib/jquery-1.9.1.min', "underscore":"lib/underscore-min", "backbone" : "lib/backbone-min" }, shim:{ "underscore":{ exports: '_' //what is does here? without this i am getting undefined }, "backbone":{ exports: 'Backbone' //what is does here? without this

Requirejs optimiser - Create minified groupings of scripts for sections of the site

守給你的承諾、 提交于 2019-12-06 14:16:06
I'm using the requirejs optimiser and I have a lot of JavaScript files for this webapp. I would like to create separate minified scripts which group scripts for certain sections of the site. My question is: how do I create minified groups of scripts and how I prevent script libraries for example JQuery from being added into these module groups which already exist in the scripts.min.js core script? ({ baseUrl: '../static/js', mainConfigFile: '../static/js/main.js', name: 'main', out: '../static/js/scripts.min.js', paths: { requireLib: 'vendor/require/require.min' }, include: ['requireLib', '

Configuring RequireJS to load from multiple CDNs

寵の児 提交于 2019-12-06 14:14:40
I'm using RequireJS on our site and before the files are deployed to production we are using rjs to minify the files. Currently I am creating several minified files, not a single file. For better load times on our site we are also using multiple CDNs so the browser will open more download streams and the static content will load as fast as possible. Here lies my problem. I can configure RequireJS through the baseUrl parameter to load my scripts from our CDN, but is there a way to configure RequireJS to use a random baseUrl? I know by default this isn't possible, but I was hoping maybe someone

Understanding r.js options w/grunt & backbone and relative paths

旧街凉风 提交于 2019-12-06 13:45:57
I'm trying to figure out how to use r.js. I keep getting errors thrown such a module path does not exist, or files getting dumped where they shouldn't be. I've got a single page application that is integrated with wordpress. I've adopted this backbone boilerplate for my general structure, although I've set things up quite different. My file structure is shown below. .Theme Folder ├── _assets | ├── _application | | ├── css | | ├── fonts | | ├── img | | ├── _js | | | ├── main.js //this is my require.js config file | | | ├── _app //here's where the boilerplate structure comes into play | | | ├──

File structure for a web app using requirejs and backbone

谁都会走 提交于 2019-12-06 12:43:23
问题 I am thinking about the best way to define a file structure for a web app using requirejs and backbone. I have two ideas (1) (2). According to you what is the best or what would you change? (1) |-- vendor |-- js (jquery, underscore ....) |-- images |-- css |-- spec |-- templates |-- js | |-- utils | |-- models | |-- collections | |-- views | conf.js | app.js | router.js |- index.html (2) |-- assets |-- js (jquery, underscore ....) |-- css |-- images |-- src |-- templates |-- js |-- views |--