我通过以下方式使用grunt-browserify :
browserify:
build:
files:
'<%= paths.staticJS %>/Login.js' : '<%= paths.buildSrcJS %>/modules/login/Login.js'
'<%= paths.staticJS %>/Password.js' : '<%= paths.buildSrcJS %>/modules/password/Password.js'
'<%= paths.staticJS %>/RequestPasswordReset.js' : '<%= paths.buildSrcJS %>/modules/password/RequestPasswordReset.js'
'<%= paths.staticJS %>/ResetPassword.js' : '<%= paths.buildSrcJS %>/modules/password/ResetPassword.js'
'<%= paths.staticJS %>/ClientRouter.js' : '<%= paths.buildSrcJS %>/shared/ClientRouter.js'
通常它工作正常,但我仍然有一个问题,在我的代码的某些部分我使用这样的节点require
模块:
chartViewPath = "../../charts/view/#{chartViewName}"
resolvedPath = require.resolve(chartViewPath)
chartViewModule = require.modules[resolvedPath]
我收到以下错误:require.resolve is not a function
我试图在 grunt 任务中使用别名公开全局要求:
options:
alias: [
'<%= paths.buildSrcJS %>/modules/login/Login.js:Login'
'<%= paths.buildSrcJS %>/modules/password/Password.js:Password'
'<%= paths.buildSrcJS %>/modules/password/RequestPasswordReset.js:RequestPasswordReset'
'<%= paths.buildSrcJS %>/modules/password/ResetPassword.js:ResetPassword'
'<%= paths.buildSrcJS %>/shared/ClientRouter.js:ClientRouter'
]
它确实将require
变量公开为函数,但我仍然不能使用require.resolve
ormodules
方法。
我还应该提到,在转向 grunt 之前,我正在使用Cakefile
我Browserify
的文件
task 'build','build for development environment', (options)->
fs = require 'fs'
path = require 'path'
browserify = require 'browserify'
routers = [ "modules/login/Login","shared/ClientRouter","modules/password/Password","modules/password/RequestPasswordReset", "modules/password/ResetPassword" ]
routers.forEach((r)->
jsSource = path.resolve(__dirname + "/src/main/#{r}.coffee")
jsDestination = path.resolve( "#{__dirname}/../static/js/#{r.split('/').slice(-1)}.js" )
browserified = browserify(jsSource, { entry : [] }).bundle()
fs.writeFile(jsDestination, browserified, (err)-> if err then console.log "error writing file:#{err}")
)
并且以这种方式它工作正常,我可以使用require.resolve
,require.modules
因为出于某种原因,browserify 正在将这些方法添加到每个条目文件的开头,就像在这个gist中一样。