无法使用 browserify 和 debowerify 获取外部库

Can#39;t get external library with browserify and debowerify(无法使用 browserify 和 debowerify 获取外部库)
本文介绍了无法使用 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 之后(对于 angularRoutes),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.

这篇关于无法使用 browserify 和 debowerify 获取外部库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How do I can get a text of all the cells of the table using testcafe(如何使用 testcafe 获取表格中所有单元格的文本)
node_modules is not recognized as an internal or external command(node_modules 未被识别为内部或外部命令)
How can I create conditional test cases using Protractor?(如何使用 Protractor 创建条件测试用例?)
PhantomJS and clicking a form button(PhantomJS 并单击表单按钮)
Clicking #39;OK#39; on alert or confirm dialog through jquery/javascript?(在警报上单击“确定或通过 jquery/javascript 确认对话框?)
QunitJS-Tests don#39;t start: PhantomJS timed out, possibly due to a missing QUnit start() call(QunitJS-Tests 不启动:PhantomJS 超时,可能是由于缺少 QUnit start() 调用)