使用requirejs并尝试将javascript优化为供应商文件和应用程序文件
我正在使用requirejs进行一个项目,并试图将我的javascript优化为两个文件:供应商库和应用程序代码。不幸的是,我似乎无法让它发挥作用 文件夹结构:使用requirejs并尝试将javascript优化为供应商文件和应用程序文件,javascript,module,requirejs,code-organization,Javascript,Module,Requirejs,Code Organization,我正在使用requirejs进行一个项目,并试图将我的javascript优化为两个文件:供应商库和应用程序代码。不幸的是,我似乎无法让它发挥作用 文件夹结构: require.config({ baseUrl: '../js/', paths: { jquery: '../../bower_components/jquery/dist/jquery', underscore: '../../bower_components/lodash/dist/lodash'
require.config({
baseUrl: '../js/',
paths: {
jquery: '../../bower_components/jquery/dist/jquery',
underscore: '../../bower_components/lodash/dist/lodash',
backbone: '../../bower_components/backbone/backbone',
text: '../../bower_components/requirejs-text/text'
},
enforeceDefine: true
});
define(['backbone', 'main'], function(Backbone, app) {
});
requirejs: {
options: {
dir: 'dist/js/',
mainConfigFile: 'app/js/config.js',
optimize: 'none',
normalizeDirDefines: 'all',
skipDirOptimize: true
},
dist: {
options: {
modules: [
{
name: 'vendor',
include: ['jquery', 'underscore', 'backbone', 'text'],
},
{
name: 'app',
exclude: ['vendor']
}
]
}
}
});
- 主干单元组件/
- 应用程序/
- 少/
- js/
- 组件1/
- 组件2/
- 布局/
- 仪表板/
- 关于/
- 解放党/
- config.js
- main.js
- index.html
- 距离/
- Grunfile.js
require.config({
baseUrl: '../js/',
paths: {
jquery: '../../bower_components/jquery/dist/jquery',
underscore: '../../bower_components/lodash/dist/lodash',
backbone: '../../bower_components/backbone/backbone',
text: '../../bower_components/requirejs-text/text'
},
enforeceDefine: true
});
define(['backbone', 'main'], function(Backbone, app) {
});
requirejs: {
options: {
dir: 'dist/js/',
mainConfigFile: 'app/js/config.js',
optimize: 'none',
normalizeDirDefines: 'all',
skipDirOptimize: true
},
dist: {
options: {
modules: [
{
name: 'vendor',
include: ['jquery', 'underscore', 'backbone', 'text'],
},
{
name: 'app',
exclude: ['vendor']
}
]
}
}
});
grunfile.js:
require.config({
baseUrl: '../js/',
paths: {
jquery: '../../bower_components/jquery/dist/jquery',
underscore: '../../bower_components/lodash/dist/lodash',
backbone: '../../bower_components/backbone/backbone',
text: '../../bower_components/requirejs-text/text'
},
enforeceDefine: true
});
define(['backbone', 'main'], function(Backbone, app) {
});
requirejs: {
options: {
dir: 'dist/js/',
mainConfigFile: 'app/js/config.js',
optimize: 'none',
normalizeDirDefines: 'all',
skipDirOptimize: true
},
dist: {
options: {
modules: [
{
name: 'vendor',
include: ['jquery', 'underscore', 'backbone', 'text'],
},
{
name: 'app',
exclude: ['vendor']
}
]
}
}
});
当我运行
grunt
时,我得到以下错误:“错误:错误:错误:模块路径不存在:/path/to/project/app/js/app/js/vendor.js,用于名为:vendor的模块。”
为什么它要在“供应商”还不存在的情况下寻找它呢?我认为您要优化的源代码中没有名为
vendor
的模块。如果这是正确的,那么您得到的错误是因为r.js
正在查找名为vendor
的模块。您必须告诉它使用create
选项创建它:
modules: [
{
name: 'vendor',
create: true, // <--- Add this option!
include: ['jquery', 'underscore', 'backbone', 'text']
},
{
name: 'app',
exclude: ['vendor']
}
]
模块:[
{
名称:'供应商',
create:true,//谢谢Louis。当你的评论进来时,我刚刚读到了关于“create”的内容。这使它起了作用。出于好奇,所有javascript文件都被复制到了“dist/js”中。我希望只有vendor.js和main.js在那里。我还错过了什么吗?removeCombined:true
我想。你可以在与create
相同的文件中找到它。是的,这样做了。我希望removeCombined
的默认值为true。感觉好多了。再次感谢。