如何使用browserify安装不在NPM上的库



我一直在使用angular并使用bower作为包管理器。对于当前的项目,我想在浏览器中使用一些npm模块,并从browserify

开始

对于我的初始项目,我能够使用npm install angular angular-ui-router --save,因为他们有npm包,但我习惯使用bower install安装依赖项

在构建我的browserify-angular应用程序时,我该如何安装npm上没有列出的依赖项?基本上,browserify有一个替代bower install,或者我可以使用bower与browserify?

对于当前项目,我有一个package.json看起来像这样:

{
  "name": "browserify-begin",
  "version": "0.0.0",
  "dependencies": {
    "7digital-api": "^0.15.2",
    "angular": "^1.2.16",
    "angular-ui-router": "^0.2.10"
  },
  "devDependencies": {
    "browserify": "^4.1.5",
    "grunt": "^0.4.5",
    "grunt-browserify": "^2.1.0",
    "grunt-contrib-connect": "^0.7.1",
    "grunt-contrib-copy": "^0.5.0"
  }
}

你可以在npm中安装git-repos,而不需要将它们发布到npm。

"dependencies": {
    "package": "git+https://github.com/path/to/repo#commitSHAhash"
}

您可以尝试通过debowerify安装

包。Json可能如下所示:

{
  "name": "browserify-begin",
  "version": "0.0.0",
  "dependencies": {
    "7digital-api": "^0.15.2",
    "angular": "^1.2.16",
    "angular-ui-router": "^0.2.10"
  },
  "browserify": {
    "transform": [
      "debowerify"
    ]
  },
  "devDependencies": {
    "browserify": "^4.1.5",
    "debowerify": "^0.7.1",
    "grunt": "^0.4.5",
    "grunt-browserify": "^2.1.0",
    "grunt-contrib-connect": "^0.7.1",
    "grunt-contrib-copy": "^0.5.0"
  }
}

2014年5月24日更新

给定源javascript文件为source.js你想要浏览器化到build.js

使用debowerify,如果你的source.js中包含bootstrap等较低的组件,例如:

require('bootstrap')

Gruntfile.js如下所示:

module.exports = function(grunt) {
  // Project configuration.
  grunt.initConfig({
    // Metadata.
    pkg: grunt.file.readJSON('package.json'),
    browserify: {
      bundleOptions: {
        debug: true
      },
      js: {
        src:['source.js'],
        dest: 'build.js'
      }
    }
  }),
  grunt.loadNpmTasks('grunt-browserify');    
}

build.js将包含bootstrap组件

最新更新