Coder Social home page Coder Social logo

grunt-bower-requirejs's Introduction

grunt-bower-requirejs Build Status

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:

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:

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

Example usage

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.

bowerRequirejs: {
  all: {
    rjsConfig: '<%= yeoman.app %>/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.

bowerRequirejs: {
  all: {
    rjsConfig: '<%= yeoman.app %>/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.

bowerRequirejs: {
  all: {
    rjsConfig: '<%= yeoman.app %>/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.

bowerRequirejs: {
  all: {
    rjsConfig: '<%= yeoman.app %>/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:

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.

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:

{
  "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:

{
  "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:

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

grunt-bower-requirejs's People

Contributors

arthurvr avatar bezoerb avatar emarcotte avatar jorrit avatar kevva avatar lbunschoten avatar robdodson avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

grunt-bower-requirejs's Issues

baseURL mismatch feature improvements

Either:

  • add a warning if the options.baseURL does not match the one in config.js
  • add option to pull baseURL from config.js adjusting for relative position of config.js

Fatal error:spawn bower ENOENT

on windows
when I used the command "grunt test"
well be have this error

I change the file Gruntfile.js in line 100
var ls = spawn(process.platform === "win32" ? "bower.cmd" : "bower", ['install']);

it's no this error but it have more more error

Respect formatting/code style

Fe. if I have a long list of shims...

require.config({
    paths: {},
    shim: {
        module1: { deps: ["jquery"], exports: "$.fn.popover" },
        module2: { deps: ["jquery"], exports: "$.fn.popover" },
        module3: { deps: ["jquery"], exports: "$.fn.popover" },
        ...
    }

When I run grunt bower, this will be munged into...

require.config({
    paths: {},
    shim: {
        module1: {
            deps: [
                "jquery"
            ],
            exports: "$.fn.popover"
        },
        module2: {
            deps: [
                "jquery"
            ],
            exports: "$.fn.popover"
        },
        module3: {
            deps: [
                "jquery"
            ],
            exports: "$.fn.popover"
        },
        ...
    }

...which in this case then doesn't match the defined code style.

It would be awesome if previously shimmed modules are preset, to respect the formatting they might have.

I mean it doesn't even add shims so why reformat it to some arbitrary format?

Minification Question

How are people handling minification of bower components considering the task seems to need to link directly to bower_components directory and bower packages are not responsible for providing a minified version of their packages?

A path to require.js is not created in your rjsConfig file after installing grunt-bower-requirejs.

A path to require.js is not created in your rjsConfig file after installing grunt-bower-requirejs as the instruction.

I'm using grunt angular generator.

The generated config.js is

require.config({
shim: {

},
paths: {
angular: "../../bower_components/angular/angular",
"angular-mocks": "../../bower_components/angular-mocks/angular-mocks",
"angular-scenario": "../../bower_components/angular-scenario/angular-scenario",
"es5-shim": "../../bower_components/es5-shim/es5-shim",
json3: "../../bower_components/json3/lib/json3"
},
packages: [

]
});

RequireJS config file not created

When I run this command, the configuration file is not created :

grunt bower:target --config

My Grunt file :

target: {
    rjsConfig: 'config.js',
    options: {
        baseUrl: './'
    }
}

thanks for your help.

Generate shims

I ran into a problem when using bower components with dependencies. When these components are not AMD components, their dependencies are not defined.

However, we could read these dependencies from the bower.json and generate a shim configuration for requirejs

Any plans on this?

Config file not created

Using the following code a config file is not made, yet the cli version DOES write it.

grunt.config.set('bowerRequirejs',{
    dist: {
      target: {
        rjsConfig: './assets/config/requirejs.js',
        options: {
          'exclude-dev': true
        }
      }
    },
    dev: {
      target: {
        rjsConfig: './assets/config/requirejs.js'
      }
    }
  });

bower_components custom location

Hi, is there any means to tell (grunt-)bower-requirejs the location of 'bower_components'? Is it mandatory to have the folder in the same path as the Gruntfile.js?

My issue comes from the following directory structure:

|- web
  |- Gruntfile.js
  |- src
  |- ...
    |- bower.json
    |- bower_components
    |- ...

The reason why I put bower* inside the 'src' folder is because, thats the path the server is pointing to otherwise the requests to the dependencies inside the 'bower_components' would fail!

Best

Option to avoid overwriting customized paths

It would be nice if there was a simple toggle option that avoids overwriting existing paths in the requirejs config file.

For instance: some packages are setup so that the "main" script is not what I want (say it's a minified version of the script instead of source, or it's an index.js file that only works with node.js), but grunt-bower-requirejs automatically sets that path. Even if I correct the path, every time I update my bower dependencies it gets overwritten with the wrong path.

I recognize that I could explicitly fix this with the exclude option, but a more desired behavior would be for the task to assume that if there is already a path for a certain module in the config, that it should leave it alone.

Maybe if there were a simple overwrite option that defaulted to true, but when set to false would leave existing paths?

Ignore the comment in existing config file

Say, in my config.js file, I have a commented urlArgs code, which I can uncommented to prevent caching.

// Prevent caching issues
//urlArgs: 'v=' + (new Date()).getTime()

my grunt task

module.exports = function(grunt) {

  grunt.config( 'bower', {
    all: {
      options: {
        exclude: [ 'modernizr', '_normalize.scss' ]
      },
      rjsConfig: '<%= project.js %>/config.js'
    }
  });

};

As soon as run the bower task, both line will be removed. Is this wrong configuration on my part, or the bower task seems to prettify my shim configuration as well.

Other info: Windows 8, Grunt 1.4.2, grunt-bower-requirejs 0.8.0

Task is eating comments and executing JavaScript in custom `urlArgs`

Is there any way of having the task respect comments and JavaScript in my custom urlArgs that I pass to requirejs.config?

Usually I like to include something like this (for cache-busting while developing) in my configuration:

requirejs.config({
    //>>excludeStart("build", true);
    urlArgs: "bust=" + Date.now()
    //>>excludeEnd("build");
});

yet after running the bower task this will look like:

requirejs.config({
    urlArgs: "bust=1398586856365"
});

Is there anything I can do about this? Can this be fixed task-side?

ability to take main file from package json if bower.json is not exists

Some components author ( cferdinandi/smooth-scroll#155 ), does not want to
add official support specific package managers.

In this case, it not have bower.json file with specified main property. So, after bowerRequirejs task execution it takes first file in package root folder - in this case it is gulp.js

However, in this particular module, main file is specified in package json, and it will be nice if bowerRequirejs task can fallback to it when resolve mappings

Deprecated?

Why is this deprecated? Any alternatives recommended?

Pull all bower dependencies (recursively) from sub-projects

I'm not sure if this is outside the scope of this project, but it seems like it would be relevant.

Bower is nice because it recursively installs the dependencies of dependencies for a nice, flat listing (bower list shows this). But it appears that grunt-bower-requirejs only looks in the top-level bower.json file. Instead, it should use bower to get the full list of dep's and add them to the require config paths.

I'm new to bower, so maybe there's a convention I'm not aware of, but it seems antithetical to the idea of bower to have to list all sub-dependencies inside the top-level bower.json file.

Loading multimodule package does not work

I installed my own package ptp.js. From bower.json:

"dependencies": {
    "ptp.js": "2.x",
    "requirejs": "2.x"
},

From Gruntfile.js:

bower: {
    target: {
        rjsConfig: 'package/config.js'
    }
},

Resulting config.js:

require.config({
  shim: {

  },
  paths: {
    ptp: "bower_components/ptp.js/scripts/ptp",
    requirejs: "bower_components/requirejs/require"
  },
  packages: [

  ]
});

Now, to load ptp.js, the app's main module has:

defined(['ptp'], function (ptp) {
    // …
}

ptp.js loads fine, but not any modules that it depends on. To fix this, I modify config.js to match what @jrburke does in his template for a simple one-page app:

  paths: {
    ptp: "bower_components/ptp.js/scripts",

Then to load the module, I use:

defined(['ptp/ptp'], function (ptp) {
    // …
}

Now, the dependencies load. The trick is to set the path to the directory containing the main module of the package to be loaded. If loaded as ptp/ptp, then relative paths work fine, otherwise not.

I think there is design issue that needs to be discussed, or there is something that I don't understand.

What do you think?

New option that should override package main file reference

Sometime library vendor provides few different version of the script within single bower package, AMD and non-amd are the most common use cases. But they can only reference to one of them with bower.json main attribute.

I would like to be able to specify for each library, which file it should grab. If the library is not specified, default main attribute value should be used.

File path should be related to library's base path.

I imagine this to have the following looks:

....
paths: {
    'jquery': 'jquery.js',
    'underscore': 'underscore.min.js'
    'backbone': 'backbone.amd.js'
}
...

Aborted due to warnings

Hi,
I tried to use this task many times but with no luck, I always get "Aborted due to warnings."

Here's the task :

bowerRequirejs: {
	all: {
		rjsConfig: 'scripts/config-js.js',
		options: {
			baseUrl: 'src/main/resources'
		}
	}
}

Here's a stacktrace :

Running "bowerRequirejs:all" (bowerRequirejs) task
Verifying property bowerRequirejs.all exists in config...OK                                                        
File: [no files]                                                                                              
Options: config="scripts/config-js.js", exclude=[], baseUrl="src/main/resources", transitive=false, excludeDev=false
Warning: Task "bowerRequirejs:all" failed. Use --force to continue. 
Aborted due to warnings.

Can anyone help me please ?

Failed to read existing bower.json

Hello, I'm experiencing problems with your plugin (with bower-install, although, all works, dependencies just inserted in html)

What's the problem and how can I fix it?

2014-07-02-232406_593x386_scrot

prefix function for module ids

Hey guys, what do you think about an option for prefixing all loaded module ids?
e.g. loglevel: 'js/loglevel/dist/loglevel.min' becomes base-loglevel: 'js/loglevel/dist/loglevel.min'

use minified file

It will be good to have an option to use minified version of file, if exists.

The global `require` property

You can configure require.js prior to loading the library by setting a global require variable:

// require-config.js
var require = { paths: {}, shim: {} };

I tried throwing grunt-bower-requirejs at this file but it did nothing. Any thoughts on supporting this feature?

auto-create rjsConfig file

It would be nice if this app could automatically create a boilerplate file specified in the rjsConfig option if it does not already exist.

Incorrect main path chosen

Hey!

First of all, thanks for this great plugin! But I've encountered a little problem.

I'm using Bower to install jQuery qTip2 plugin. It has a following configuration in it's bower.json:

{
  "main": [
    "./jquery.qtip.js",
    "./basic/jquery.qtip.js"
  ],
}

I don't know why, but the second path is for an older version of the qTip2 plugin and your Grunt extension chooses the latest path in configuration for require.js path config. And I need to use the first one.

Please advise. Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.