<html> <body> ... <script src="/public/base.js"></script> <script src="/public/core.js"></script> <script src="/public/el.js"></script> <script src="/public/boot.js"></script> <script src="/public/under.js"></script> <script src="/public/almond.js"></script> <script src="/module/event.js"></script> <script src="/module/bootstrap.js"></script> <script src="/module/component.js"></script> <script src="/widget/dialog.js"></script> </body> </html>?
var module1 = require( ‘module1‘), module2 = require( ‘module2‘ ); function module3(){ } exports.module3 = module3;?
=============main.js=================== <script src="js/require.js" data-main="js/Dialog"></script> ==============Dialog.js======================= define( [‘event‘, ‘bootstrap‘, ‘component‘ ], function( event, bootstrap, component ){ event.doSomthing(); bootstrap.doSomthing(); component.doSomthing(); var Dialog = function(){ ... } return { Dialog: Dialog } });?
=============main.js=================== <script src="js/sea.js"></script> <script>seajs.use("../static/hello/src/main")</script> ==============Dialog.js======================= define( function( require, exports, module ){ var event = require( ‘event‘ ); event.doSomthing(); var bootstrap = require( ‘bootstrap‘ ); bootstrap.doSomthing(); var component = require( ‘component‘ ); component.doSomthing(); var Dialog = function(){ ... } exports.Dialog = Dialog; })?
require.config({ baseUrl: ‘module/js‘, //配置path下的统一目的,以便path中不会分别配置 path: { //配置module路径,以便引用时不用写全路径 module1: ‘module1‘, module2: ‘module2‘, module3: ‘http://www.site.com/module3‘ //若引用的是第三方,可以全部全路径 }, shim: { //对不符合AMD规则的模块引用 ‘underscore‘: { exports: ‘_‘ //配置其原对外接口 }, ‘backbone‘: { deps: [‘underscore‘, ‘jquery‘], //配置其依赖项 exports: ‘Backbone‘ } } }); define( [‘module1‘, ‘module2‘ ], function( module1, module2 ){ module1.doSomthing(); module2.doSomthing(); function module3(){ ... } return { module3: module3 } });?
load: function (id, url) { url += ‘?‘ + timestamp; req.load(context, id, url); },?
<script> var timestamp = ‘<%= timestamp %>‘; </script>?
({ appDir: ‘./‘, //项目根目录,给后面的baseUrl做参考;非必要项;默认为与r.js平级目录 dir: ‘./min‘, //压缩后的文件的输出目录 baseUrl: ‘./‘, //模块相对目录,与后面的paths配置使用 modules: [ { name:‘login‘}, { name: ‘register‘ }, { name: ‘center‘ } ], fileExclusionRegExp: /^(r|build)\.js|.*\.scss$/, //过滤文件不压缩 optimizeCss: ‘standard‘, //css的标准方式 optimize: ‘uglify‘, //设置是否压缩;uglify(压缩格式)|none(只合并不压缩) removeCombined: true, //设置移除压缩后的文件 paths: { //设置模块别名,方便引用,压缩后require.js引用的模块路径就在这里定义的,不是require.config里的 md5: ‘core/js/md5‘, validate: ‘widget/validate/validate‘, all: ‘public/js/all‘, login: ‘module/issue/login/login‘, register: ‘module/issue/register/register‘, center: ‘module/user/center/center‘ } });?
原文:http://jiangxiao-2000.iteye.com/blog/2245449