无法使用 browserify 和 debowerify 获取外部库
我的手很头疼.这是我当前的设置:
I have a headache on my hands. Here's my current setup:
- 获取供应商库(在本例中为 Angular)
- 运行 browserify 的 gulp 任务
- debowerify 使 bower 库与 browserify 兼容
App.js(在 browserify 之前):
'use strict';
var angular = require("angular");
var Routes = require("./routes");
angular.module('MyAngularApp')
.config(Routes);
App.js(在 browserify 之后/在 bundle.js 中):
var angular = require("./../ext/angular/angular.js");
var Routes = require("./routes");
angular.module('MyAngularApp')
.config(Routes);
到目前为止一切都很好,对吧?似乎 debowerify 完成了它的工作,并将 angular
替换为来自 bower 的 angular.js
的相对路径.
So far so good, right? It seems like debowerify did it's job and replaced the angular
with the relative path to the angular.js
from bower.
但是当我在浏览器命令行中调试 bundle.js
时,在执行了前两行 require
之后(对于 angular
和 Routes
),angular
是一个空 obj,但 Routes
正是我在导出中设置的正确函数.
But when I debug the bundle.js
in the browser command line, after executing the first two require
lines (for angular
and Routes
), angular
is an empty obj, but Routes
is exactly the correct function that I setup in the export.
问题:为什么使用 require
函数不能正确导入 angular
?
Question: Why isn't angular
being imported correctly using the require
function?
我将它放入我的 package.json
以使 debowerify
工作:
I put this into my package.json
to get the debowerify
working:
"browserify": {
"transform": [
"debowerify"
]
},
推荐答案
AngularJS 目前不支持 CommonJS,所以 var angular = require("angular")
不起作用.而不是它,只使用 require('angular')
.
AngularJS doesn't support CommonJS at the moment, so var angular = require("angular")
doesn't work. Instead of it, use just require('angular')
.
'use strict';
require('angular');
var Routes = require("./routes");
angular.module('MyAngularApp')
.config(Routes);
Angular 对象将被全局加载,它也将能够被其他 JS 文件访问.
The Angular object will be loaded globally and it'll be able to be accessed by other JS files, too.
相关文章