捆绑的JS库,例如在eval抛出“angular is not defined”后,angular,jQuery,lodash

时间:2017-09-25 13:22:13

标签: javascript jquery angularjs gruntjs fetch

我正在使用grunt-contrib-concat捆绑不同的JS库,如下所示:

dist7: {
    src: [
        'lib/jquery/dist/jquery.js',
        'lib/ng1.6/angular-animate/angular-animate.js',
        'lib/ng1.6/angular-aria/angular-aria.js',
        'lib/ng1.6/angular-messages/angular-messages.js',
        'lib/api-check/dist/api-check.js',
        'lib/angular-formly/dist/formly.js',
        'lib/ng1.6/angular-sanitize/angular-sanitize.js',
        'lib/angular-ui-router/release/angular-ui-router.js',
        'lib/lodash/lodash.js',
        'lib/angularjs-toaster/toaster.js',
        'lib/moment/min/moment-with-locales.js',
        'lib/eventie/eventie.js',
        'lib/wolfy87-eventemitter/EventEmitter.js',
        'lib/imagesloaded/imagesloaded.js',
        'lib/desandro-matches-selector/matches-selector.js',
        'lib/doc-ready/doc-ready.js',
        'lib/fizzy-ui-utils/utils.js',
        'lib/desandro-get-style-property/get-style-property.js',
        'lib/get-size/get-size.js',
        'lib/outlayer/item.js',
        'lib/outlayer/outlayer.js',
        'lib/masonry-layout/masonry.js'],
    dest: 'dist/js/my-dependencies.js'
}

当我在脚本标记中添加dist/js/my-dependencies.js时,它可以正常工作。

但是当我正在进行获取请求时,我会像以下一样进行评估:

fetch('dist/js/my-dependencies.js', {method: 'GET'})
    .then(response => response.text()))).then(scr => {
        eval(scr)
    });

它引发了以下错误:

Err in evaling:  /cuicui/scripts/js/my-dependencies.js  With err: 
 ReferenceError: require is not defined
    at installedModules (eval at <anonymous> (cd.component.js:12), <anonymous>:1:93)
    at eval (eval at <anonymous> (cd.component.js:12), <anonymous>:1:554)
    at cd.component.js:12
    at Array.forEach (<anonymous>)
    at cd.component.js:12
    at <anonymous>

1 个答案:

答案 0 :(得分:1)

问题: 您的一个依赖项在文件的顶部(概率)有一个require('someLib')

如何找到: 浏览每个图书馆并搜索关键字require

为什么eval会失败? eval()位于不在节点中的窗口上。因此,它不知道如何处理该关键字。我建议您找到所有库的预先制作版本,而不是使用库源。