百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
G

grunt-bower-requirejs

> 编程语言
开源

[已弃用] Grunt 任务 - 自动将安装的 Bower 组件连接到您的 RequireJS 配置中

198 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

[已弃用] Grunt 任务 - 自动将安装的 Bower 组件连接到您的 RequireJS 配置中

grunt-bower-requirejs

Automagically wire-up installed Bower components into your RequireJS config

Grunt wrapper for the bower-requirejs module.

Getting Started

If you haven't used grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use grunt plugins. Once you're familiar with that process, install this plugin with this command:

bash
npm install grunt-bower-requirejs --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

javascript
grunt.loadNpmTasks('grunt-bower-requirejs');

Example usage

javascript
grunt.initConfig({
  bowerRequirejs: {
    target: {
      rjsConfig: 'app/config.js'
    }
  }
});

grunt.loadNpmTasks('grunt-bower-requirejs');

grunt.registerTask('default', ['bowerRequirejs']);

Documentation

When the bowerRequirejs task is run it merges the paths of installed Bower components into the paths property of your RequireJS config.

You trigger this task from another task in your Gruntfile or through the CLI: grunt bowerRequirejs

rjsConfig

Required
Type: String

Specify a relative path to your RequireJS config.

Make sure to specify the baseUrl property in your RequireJS config if you want to use relative paths.

Options

exclude

Default: []
Type: Array

Specify components to be excluded from being added to the RequireJS config.

javascript
bowerRequirejs: {
  all: {
    rjsConfig: '/scripts/main.js',
    options: {
      exclude: ['modernizr', 'sass-bootstrap', 'qtip2']
    }
  }
}

baseUrl

Default: null
Type: String

Generate paths relative to a specific directory. This option is for anyone not using data-main who wishes to set their own base.

javascript
bowerRequirejs: {
  all: {
    rjsConfig: '/path/to/main.js',
    options: {
      baseUrl: './'
    }
  }
}

transitive

Default: false
Type: Boolean

If the transitive option is set to true, then transitive dependencies will be also added to the require config.

For example, say we explicitly have an entry in our bower config for module myTotallyCoolModule, which depends on jQuery and underscore. If the transitive option is set to true, there will be config entries for myTotallyCoolModule, jQuery, and underscore. Otherwise, if the transitive option is set to false, there will only be a config entry for myTotallyCoolModule.

Each transitive dependency is only included once, even if the dependency is used multiple times.

javascript
bowerRequirejs: {
  all: {
    rjsConfig: '/scripts/main.js',
    options: {
      transitive: true
    }
  }
}

excludeDev

Default: false Type: Boolean

If the excludeDev option is set to true, then dev-pendencies won't be added to the require config.

javascript
bowerRequirejs: {
  all: {
    rjsConfig: '/scripts/main.js',
    options: {
      excludeDev: true
    }
  }
}

Using Bower Hooks

Bower >=v1.3.1 includes hooks for preinstall, postinstall and preuninstall actions. To run grunt-bower-requirejs after every bower install, add a scripts block to your .bowerrc.

{
  "scripts": {
    "postinstall": "grunt bowerRequirejs"
  }
}

Things to remember

Config file

If you do not already have a config.js file at the location specified by the --config option then one will be generated for you. A basic config.js file looks like this:

javascript
requirejs.config({
  shim: {},
  paths: {}
});

You still need to create a path for your js files. This tool will only create paths for third party libraries specified in bower.json.

javascript
requirejs.config({
  shim: {},
  paths: {
    myComponent: 'js/myComponent.js'  // make sure to add your components!
  }
});

The tool does not overwrite the config file, it just adds additional paths to it. So paths you add will be preserved. Keep in mind that if you change or remove one of your Bower dependencies after you've run the task, that path will still exist in the config file and you'll need to manually remove it.

RequireJS component

Although RequireJS does not provide a bower.json file, a path to require.js will still be created in your rjsConfig file. The path's name will be requirejs. If you are optimizing your scripts with r.js you can use this path to make sure RequireJS is included in your bundle.

Package Support

If a dependency's moduleType is set to node in bower.json it will be treated as a CommonJS Package.

The following bower.json file:

javascript
{
  "name": "node-module-type-stub",
  "version": "0.0.1",
  "moduleType": ["node"],
  "main": "myMain.js"
}

Will generate this entry in your config.js file:

require.config({
  shim: {},
  packages: [
    {
      name: 'node-module-type-stub',
      main: 'myMain.js',
      location: 'bower_components/node-module-type-stub'
    }
  ],
  paths: {}
});

Configuring location

By default, the task will set the package location to the root directory of the dependency. If the dependency includes a location property in its bower.json, then the location will be a combination of the root directory and the location dir.

For example, a bower.json like this:

javascript
{
  "name": "node-module-type-stub",
  "version": "0.0.1",
  "moduleType": ["node"],
  "main": "myMain.js",
  "location": "src"
}

Will generate this entry in your config.js file:

javascript
require.config({
  shim: {},
  packages: [
    {
      name: 'node-module-type-stub',
      main: 'myMain.js',
      location: 'bower_components/node-module-type-stub/src'
    }
  ],
  paths: {}
});

License

BSD license and copyright Google

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言