如果需要下划线,浏览器源图不起作用

时间:2015-06-25 16:19:12

标签: angularjs underscore.js browserify

我使用browserify作为我的角度客户端应用程序。我需要使用下划线。使用bower安装angular,使用npm安装下划线

这就是我运行browserify(npm)并在gulp(npm)中创建源地图的方法

gulp.task('browserify', function() {
    return browserify(dir.script_from + '/main.js', {debug: true})
        .bundle()
        .pipe(source('bundle.js')) // gives streaming vinyl file object
        .pipe(buffer()) // <----- convert from streaming to buffered vinyl file object
        .pipe(sourcemaps.init({loadMaps: true}))
        .pipe(uglify()) // now gulp-uglify works
        .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest(dir.script_to));
});

在我的main.js中,我有

//require('underscore')
require('angular')
require('angular-resource')
require('angular-route')
require('./home/home_page.js')
...

如果我不需要('下划线'),则源地图正常工作。我可以查看原始文件并设置断点。

enter image description here

但如果我需要('下划线'),源地图就不再有用了。我甚至无法查看文件。

enter image description here

我也尝试用bower安装下划线,但是我收到以下错误:

[23:59:02] Starting 'browserify'...
events.js:85
      throw er; // Unhandled 'error' event
            ^
Error: Cannot find module 'underscore' from '/Users/[my path]/app/client/script'

请注意,bower(我配置了路径)和npm将模块放在'/ Users / [my path] / node_modules'文件夹中

我甚至尝试过只有一行的main.js:require('underscore')并且没有工作,但是空的main.js文件正常工作

2 个答案:

答案 0 :(得分:3)

gulp正在检查根目录中的下划线。尝试在config.js或package.json

中添加下划线路径

config.js

require: ['node_modules/underscore']

的package.json

"underscore": "./node_modules/underscore"

答案 1 :(得分:1)

我与stderr有同样的问题。也许更好的方法是将其排除在捆绑中。只需将underscore/browserify标记链接到您的<script>文件即可。在捆绑包中,您可以直接使用underscore

如果您使用的是角度,建议您也使用单独的_,因为大多数用户可能已经从CDN获得<script>用于另一个角度网站。