browserify要求在所需文件中找不到模块

时间:2015-03-19 14:51:41

标签: browserify browserify-shim

我试图使用browserify将我的角度js打包成一个脚本。当我在主文件require中使用app.js时,它很有效,但当app.js中所需的其中一个模块包含它自己的require时,依赖关系不包含在我的包中。

回顾一下,app.js (link to line in repo)

require("splash-header");
// more code here

splashHeader.js

require("social-button-directive");
// more code here

socialButtons.js没有依赖关系。

browserify --list app.js未按预期列出splashHeader.js,并且通过browserify app.js -o bundle.js生成的捆绑包不包含socialButton的javascript。因此,当然如果我尝试加载网站,我会在splashHeader的Uncaught Error: Cannot find module 'social-button-directive'获得require('social-button-directive')

同样重要的是要注意我使用"浏览器"将模块名称映射到文件位置。 package.json中的选项,并使用browerify-shim,因为我的模块都不兼容common-js。

package.json的相关部分:

"dependencies": {
    "browserify": "~9.0.3",
    "browserify-shim": "~3.8.3"
},
"browserify": {
    "transform": [
        "browserify-shim"
    ]
},
"browserify-shim": {
    "splash-header": {
        "depends": "angular",
        "exports": "angular.module('splash-header').name"
    },
    "social-button-directive": {
        "depends": "angular",
        "exports": "angular.module('social-button-directive').name"
    }
},
"browser":{
    "splash-header": "./ng-modules/splashHeader/splashHeader.js",
    "social-button-directive": "./ng-modules/socialButtons/socialButtons.js"
}

我整个上午一直在这,并且完全被难过了。我已将有问题的项目裁减为a minimal example of the issue。要测试一下只需克隆和npm安装。如果您正在详细查看,请访问working/not working version diff

我尝试通过相对路径(require('./ng-modules/socialButtons/socialButtons.js)`)加载子模块,没有运气。也许我把事情弄错了?我对此的处理来自this article

编辑:这肯定是我的browserify-shim配置。将模块转换为commonjs并从package.json中删除browserfy-shim选项后,一切正常。我想我只需写下这一切就可以把它固定下来。 = /

0 个答案:

没有答案
相关问题