我创建了多页Web应用程序,包括: - 一个requirejs配置文件,requestjsConfig.js - 一些库,如jquery.js等... - 页面js,如homePage.js - 表单事件绑定js,如pageHeader.js - 常见的逻辑处理,如shoppingCart.js
我尝试使用r.js来解释我的脚本,并合并为1个js文件。一般来说,它有效,但有一些小问题。编译完成后,浏览器仍会加载表单绑定js文件,事件仍然会合并到页面JS文件中。
requireJSConfig.js
requirejs.config({
baseUrl: 'frontjs/lib',
paths: {
eventHandler: '../page/eventHandler',
feature: '../feature'
}
});
homePage.js
requirejs(['../page/requestjsConfig'], function (requestjsConfig) {
//requirejs(['feature/page-header', 'feature/common', 'feature/index', 'feature/transport', 'feature/utils']);
requirejs(['eventHandler/pageHeader']);
});
pageHeader.js
define(function (require) {
var $ = require('jquery');
require('jquery-cookie');
require('jquery-storageapi');
var commonString = require('feature/commonString');
var shoppingCart = require('feature/shoppingCart');
...
}
r.js的build.js优化
appDir: '../ecomm',
mainConfigFile: '../ecomm/frontjs/page/requestjsConfig.js',
dir: '../ecomm-built',
modules: [
//First set up the common build layer.
{
//module names are relative to baseUrl
name: '../page/requestjsConfig',
//List common dependencies here. Only need to list
//top level dependencies, "include" will find
//nested dependencies.
include: ['jquery',
'jquery.md5'
]
},
{
//module names are relative to baseUrl/paths config
name: '../page/homePage',
include: ['../page/eventHandler/pageHeader'],
exclude: ['../page/requestjsConfig']
},
{
//module names are relative to baseUrl/paths config
name: '../page/subCategory',
include: ['../page/eventHandler/pageHeader'],
exclude: ['../page/requestjsConfig']
}
]
}
运行节点工具/ r.js -o tools / build.js后, 当访问主页时,仍然需要pageHeader.js。
答案 0 :(得分:0)
我做到了,感谢大家,根本原因就是道路。 在homePage.js中,模块定义为
requirejs(['eventHandler/pageHeader']);
在build.js中,模块定义为
include: ['../page/eventHandler/pageHeader'],