我正在使用 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>
最佳答案
问题:
您的一个依赖项在文件的顶部(概率)有一个 require('someLib')
。
如何查找:
遍历每个库并搜索关键字 require
为什么 eval 会失败?
eval()
在窗口上而不是在节点中。因此,它不知道如何处理该关键字。我会建议找到所有库的预压缩版本,而不是使用库源。
关于javascript - 捆绑的 JS 库,例如angular, jQuery, lodash 在 eval 抛出 "require is not defined"之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46406349/