Browserify and bower. Canonical approach

試著忘記壹切 提交于 2019-12-03 02:54:22

问题


The way I'm using packages that not available out of the box in npm, right now is like that:

package.json has:

 "napa": {
     "angular": "angular/bower-angular",
     "angular-animate": "angular/bower-angular-animate",
     "d3": "mbostock/d3",
     "ui-router":"angular-ui/ui-router",
     "bootstrap":"twbs/bootstrap"
  },
  "scripts": {
     "install": "node node_modules/napa/bin/napa"

and that installs files into node_modules directory, and I use them natively like this

require('angular/angular')
require('ui-router') 
... etc

That works, but I was thinking if it's possible to use packages installed with bower (into bower specific folder) and use them natively as node modules? Is it possible to tweak node's module resolution and force it to look for modules not just inside node_modules directory, but also in bower directory as well? Or maybe using npm link or whatever?

Is there some sort of convention to use browserify with bower?


回答1:


You can use browserify-shim and configure the bower-installed modules in your package.json like this:

"browser": {
  "angular": "./bower_components/angular/angular.js",
  "angular-resource": "./bower_components/angular-resource/angular-resource.js"
},
"browserify-shim": {
  "angular": {
    "exports": "angular"
  },
  "angular-resource": {
    "depends": ["./bower_components/angular/angular.js:angular"]
  }
},

Then your code can require them by their short name as if there were regular npm modules.

Here is the spec for the "browser" package.json property.




回答2:


You can try to install via debowerify

The package.json may then look as follows:

{
  "name": "browserify-begin",
  "version": "0.0.0",
  "dependencies": {
  },
  "browserify": {
    "transform": [
      "debowerify"
    ]
  },
  "devDependencies": {
    "browserify": "^4.1.5",
    "debowerify": "^0.7.1",
    "grunt": "^0.4.5"
  }
}

Given angular is installed with

bower install angular

Then within the js file will be as follows:

require("angular");


来源:https://stackoverflow.com/questions/23691795/browserify-and-bower-canonical-approach

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!