为什么 Foundation 似乎在丢弃 RequireJS 模块?
Why does Foundation seem to trash RequireJS modules?
我的HTML是这样的:
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script data-main="/static/scripts/main" src="/static/scripts/require.js"></script>
</head>
<body></body>
</html>
static/scripts/main
看起来像这样:
requirejs.config({
paths: {
"foundation": '/static/scripts/foundation-6/js/foundation.min'
}
});
require(["foundation", "moduleA"], function(foundation, moduleA) {
console.log("main.js loaded dependencies");
});
而static/scripts/moduleA.js
是:
console.log("ModuleA loaded");
define(function() {
console.log("inside moduleA callback");
return {
Blah: "Blah!"
}
});
我可以看到 moduleA.js
脚本被 require
加载,但它没有被视为一个模块,我从控制台看到内部回调从未执行过。 main.js
中 console.log
行中的断点显示返回的 moduleA
不是我期望的模块,而是 Interchange(element, options)
的实例,而返回的 foundation
对象是 Abide(element, options)
的实例。这些似乎是 Foundation 组件,但我不明白为什么要在此处返回它们。
但是,假设我随后通过将 main.js
中的 require 语句更改为以下内容来删除 Foundation 依赖项:
require(["moduleA"], function(moduleA) {
console.log("main.js loaded dependencies");
});
运行 使一切都按预期工作 - 我现在在控制台中看到 "inside moduleA callback" 消息,断点显示我的 moduleA
对象包含一个 Blah 成员,如预期的那样.
这是怎么回事? Foundation 如何拦截这个 require() 调用并返回不需要的东西?我应该以其他方式将 Foundation 纳入该项目吗?
首先你要弄清楚基础库是AMD模块。你应该打开foundation.js并尝试在开头找到这段代码:
if (typeof define === 'function' && define['amd']) {
// [1] AMD anonymous module
define(['exports', 'require'], factory);
}
所以foundation.js不是AMD模块。 RequireJS 有 shim option for non-AMD modules. Next you should check what variable foundation returns to Global Scope. At 302 行你可以看到这段代码:window.Foundation = Foundation;
。全局变量是 Foundation
。结果代码应为:
requirejs.config({
paths: {
"foundation": 'https://cdnjs.cloudflare.com/ajax/libs/foundation/6.2.0/foundation',
},
shim:{
"foundation":{
exports:"Foundation"
}
}
});
但不幸的是 foundation.js 还不够。在 303 行你可以看到 foundation.js 是 jquery 插件。所以你应该通过 RequireJS 包含 jquery。 jQuery 是 AMD 模块。你可以在文件中看到它。您应该在 shim 中包含 jQuery 作为 foundation.js 的依赖项。所以结果代码将是:
main.js:
requirejs.config({
paths: {
"foundation": 'https://cdnjs.cloudflare.com/ajax/libs/foundation/6.2.0/foundation',
"jquery": "https://code.jquery.com/jquery-2.2.1"
},
shim:{
"foundation":{
deps: ['jquery'],
exports:"Foundation"
}
}
});
require(["foundation", "moduleA"], function(foundation,moduleA) {
debugger;
console.log("main.js loaded dependencies");
});
index.html:
<html>
<head>
<script data-main="main.js" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.22/require.js"> </script>
</head>
<body>
</body>
</html>
moduleA.js同理
ps: 如果您能纠正我的语法错误,我将不胜感激。
我的HTML是这样的:
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script data-main="/static/scripts/main" src="/static/scripts/require.js"></script>
</head>
<body></body>
</html>
static/scripts/main
看起来像这样:
requirejs.config({
paths: {
"foundation": '/static/scripts/foundation-6/js/foundation.min'
}
});
require(["foundation", "moduleA"], function(foundation, moduleA) {
console.log("main.js loaded dependencies");
});
而static/scripts/moduleA.js
是:
console.log("ModuleA loaded");
define(function() {
console.log("inside moduleA callback");
return {
Blah: "Blah!"
}
});
我可以看到 moduleA.js
脚本被 require
加载,但它没有被视为一个模块,我从控制台看到内部回调从未执行过。 main.js
中 console.log
行中的断点显示返回的 moduleA
不是我期望的模块,而是 Interchange(element, options)
的实例,而返回的 foundation
对象是 Abide(element, options)
的实例。这些似乎是 Foundation 组件,但我不明白为什么要在此处返回它们。
但是,假设我随后通过将 main.js
中的 require 语句更改为以下内容来删除 Foundation 依赖项:
require(["moduleA"], function(moduleA) {
console.log("main.js loaded dependencies");
});
运行 使一切都按预期工作 - 我现在在控制台中看到 "inside moduleA callback" 消息,断点显示我的 moduleA
对象包含一个 Blah 成员,如预期的那样.
这是怎么回事? Foundation 如何拦截这个 require() 调用并返回不需要的东西?我应该以其他方式将 Foundation 纳入该项目吗?
首先你要弄清楚基础库是AMD模块。你应该打开foundation.js并尝试在开头找到这段代码:
if (typeof define === 'function' && define['amd']) {
// [1] AMD anonymous module
define(['exports', 'require'], factory);
}
所以foundation.js不是AMD模块。 RequireJS 有 shim option for non-AMD modules. Next you should check what variable foundation returns to Global Scope. At 302 行你可以看到这段代码:window.Foundation = Foundation;
。全局变量是 Foundation
。结果代码应为:
requirejs.config({
paths: {
"foundation": 'https://cdnjs.cloudflare.com/ajax/libs/foundation/6.2.0/foundation',
},
shim:{
"foundation":{
exports:"Foundation"
}
}
});
但不幸的是 foundation.js 还不够。在 303 行你可以看到 foundation.js 是 jquery 插件。所以你应该通过 RequireJS 包含 jquery。 jQuery 是 AMD 模块。你可以在文件中看到它。您应该在 shim 中包含 jQuery 作为 foundation.js 的依赖项。所以结果代码将是:
main.js:
requirejs.config({
paths: {
"foundation": 'https://cdnjs.cloudflare.com/ajax/libs/foundation/6.2.0/foundation',
"jquery": "https://code.jquery.com/jquery-2.2.1"
},
shim:{
"foundation":{
deps: ['jquery'],
exports:"Foundation"
}
}
});
require(["foundation", "moduleA"], function(foundation,moduleA) {
debugger;
console.log("main.js loaded dependencies");
});
index.html:
<html>
<head>
<script data-main="main.js" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.22/require.js"> </script>
</head>
<body>
</body>
</html>
moduleA.js同理
ps: 如果您能纠正我的语法错误,我将不胜感激。