Browserify外部需要Grunt Browserify
Browserify External Requires with Grunt-Browserify
关于外部需求的Browserify docs部分展示了如何使捆绑包中的模块可用于全局环境:
browserify -r through -r duplexer -r ./my-file.js:my-module > bundle.js
但我在配置GruntBrowserify时遇到了问题。
-r
标志似乎对应于Grunt Browserify中的require
选项,但该选项的文档描述没有提及外部需求或导出require()
函数。
在我的Gruntfile中,我尝试将require
选项设置为我需要公开的模块(顺便说一句,它已经在捆绑包中了):
options: {
require: ['./dev/js/foomod.js'],
}
然后在我的页面脚本中,我尝试require()
文档中显示的模块:
<script>
var Foomod = require('./foomod.js');
Foomod.init({foo: 'bar'});
</script>
但这会记录错误require is not defined
。
我的目标是调用模块的init()
方法,如图所示,这样我就可以在不将其放入window
全局的情况下传入运行时数据。
使用browserfy版本5.11.1和grunt browserify版本3.0.1,我成功地获得了类似的设置:
options:{
preBundleCB: function (b) {
b.require("./dev/js/foomod.js",{expose: 'foomod'});
}
}
相关文章:
- Grunt匹配正则表达式
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 如何将package.json数组传递给grunt.js
- 当使用Watchify时,Browserify+Babelify Gulp任务没有终止
- Grunt-connect在根目录之外提供静态文件
- 如果助手不在,如何从Grunt中的代码中调用任务
- Grunt构建导致Angular应用程序在dist上崩溃
- Gulp Browserify combine JS
- grunt上的压缩文件夹不起作用
- 你能把grunt.js中的linter改成jslint吗
- Grunt-将多个文件最小化/处理为一个文件
- Grunt concat js angularjs 不起作用
- 使用Browserify和Typescript的源映射进行Webstorm调试
- Grunt Browserify-require.resolve不是函数
- Grunt Browserify扩展标志不起作用
- Browserify+Grunt+Remapify:找不到模块
- Browserify外部需要Grunt Browserify
- 使用grunt从browserify生成源映射
- Grunt-browserify+mapify+coffeescript =模块没有找到相对路径
- CKEditor资产使用shimmed package.json和grunt-browserify缓存