gpt4 book ai didi

javascript - RequireJS 优化器 - 它实际上是做什么的?

转载 作者:行者123 更新时间:2023-11-28 01:14:17 25 4
gpt4 key购买 nike

我的印象是,RequireJS 优化器会查看定义的依赖项,收集应用程序中所有引用的 js 文件,并将它们捆绑到一个大的 js 文件中。

然后您就可以在 html 脚本包含中引用该单个文件。

但事实似乎并非如此。当我运行它时,我得到一个大文件,但它包含原始的 main.js 文件,其中包含目录结构中文件的路径。

这有什么意义呢?如果需要的所有内容都包含在其中,为什么新的大文件包含自身外部的路径?优化器似乎会重写路径以指向“./”或其他内容。

当我捆绑整个应用程序并在页面中引用它时,我收到有关大型 js 文件中包含的丢失文件的错误:

Uncaught object                                                     require.js:70
GET http://localhost/ui/js/modules/mod_limeLight.js 404 (Not Found) require.js:729
Uncaught Error: Script error for: mod_limelight
http://requirejs.org/docs/errors.html#scripterror

构建.js:

({
baseUrl: "./src/ui/scripts",
name: "main",
mainConfigFile : "src/ui/scripts/main.js",
out: "dist/ui/scripts/main-built.js"
})

main.js

'use strict';
require.config({
"paths": {
"jquery": "libs/jquery-1.11.0.min",
"twitter_bootstrap": "../bower_components/bootstrap/dist/js/bootstrap.min",
"respondjs": "../bower_components/respond/dest/respond.min",
"debouncejs": "libs/dw-debounce",
"carousel": "libs/jquery.carouFredSel-6.2.1-packed",
"swipe": "libs/jquery.touchSwipe.min",

"app": "app",
"OOo_config": 'libs/oo_conf_entry-ck', // Opinion Lab pop-up

//modules
"addthis": "//s7.addthis.com/js/300/addthis_widget",
"mod_addThis": "modules/mod_AddThis",
"limelight": "//assets.delvenetworks.com/player/embed",
"mod_limelight": "modules/mod_limeLight"
},
"shim": {
"twitter_bootstrap": ["jquery"],
"carousel": ["jquery"],
"swipe": ["jquery"],
"packeryjs": ["jquery"]
}
});

require([
"jquery",
"app",
"OOo_config",
"respondjs",
"mod_addThis",
"mod_limelight"
], function ($, app) {

app.init();

});

示例模块开始如下:

define([
"jquery", "debouncejs", "limelight"
],
function ($) {
'use strict';

var playerElement = ...
});

然后运行:

node r.js -o build.js

我错过了什么?为什么它试图获取那个大 js 文件中包含的文件?

谢谢,斯科特

最佳答案

它使用通常的路径来识别包含的模块,因为这简单且明确,而且它有效。当然,这些文件不会被获取。

关于javascript - RequireJS 优化器 - 它实际上是做什么的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24028600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com