Skip to content

Commit

Permalink
cleanup for an npm release
Browse files Browse the repository at this point in the history
With many apologies.
  • Loading branch information
bewest committed Mar 17, 2013
1 parent fead3ee commit bf8f3a0
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 233 deletions.
240 changes: 25 additions & 215 deletions README.md
@@ -1,225 +1,35 @@
[![Build Status](https://travis-ci.org/SuperSkunk/preprocessor-brunch.png)](https://travis-ci.org/SuperSkunk/preprocessor-brunch)
[![Build Status](https://travis-ci.org/bewest/npm-vendorize-brunch.png)](https://travis-ci.org/bewest/npm-vendorize-brunch)

## preprocessor-brunch
## npm-vendorize-brunch

Easy include/require in Coffeescript to write less code.<br>
It's not intrusive, so you can always use the normal way:

```coffeescript

MyModule = require 'modules/my_module-module'
@require modules.robot
# ModuleRobot = require 'modules/robot-module'

class MyModule
# module.exports = class MyModule
Include npm sources in your brunch vendor output.

If you put, eg
```json
"dependencies": {
"dc": "> 1.0.0"
}
```
NPM will install the browser client dc.js in `node\_modules`, and it'd
be nice to have a way to include npm installed sources in the compiled
brunch output.

## Features

* Use Brunch notifications when a file is missing

## Usage

Add `"preprocessor-brunch": "x.y.z"` to `package.json` of your brunch app.

## App inclusion

Example of use:

```coffeescript

@require application
# Application = require 'application'

# if you prefer to specify the variable name
mediator = @require mediator
# mediator = require 'mediator'

# the module filename are automatically suffixed
@require controller.posts
# PostsController = require 'controllers/posts-controller'
@require view.post
# PostView = require 'views/post-view'
@require view.post.avatar
# PostAvatarView = require 'views/post/avatar-view'
@require collectionView.posts
# PostsCollectionView = require 'views/posts-collection-view'

# the model don't have suffix
@require model.post
# Post = require 'models/post'

# you may just want to include a module
@include helper.view
# require 'helpers/view-helper'

# you may want to put your modules in a subfolder
@require template.spinner
# SpinnerTemplate = require 'views/templates/spinner'

# the file are loaded even with shortcut
@require template.album
# AlbumTemplate = require 'views/album/template/album'
@require view.album
# AlbumView = require 'views/album/album-view'
@require view.album.slider
# AlbumSliderView = require 'views/album/slider/slider-view'

# you could also specify a variable name for conveniency
template = @require template.album
# template = require 'views/album/template/album'

```

## Vendor inclusion

The vendor include/require are the same but the path are not checked.
Be aware that the path will be used to generate the variable name, so you may want to specify them.

```coffeescript

# automatic variable naming based on module path
$require chaplin
# Chaplin = require 'chaplin'
$require chaplin/lib/router
# ChaplinLibRouter = require 'chaplin/lib/router'

# you can, like the app module, specify the variable name
ChaplinRouter = $require chaplin.lib.router
# ChaplinRouter = require 'chaplin/lib/router'
Dispatcher = $require chaplin.dispatcher
# Dispatcher = require 'chaplin/dispatcher'

config.coffee
```coffee
exports.config =
plugins:
vendorize:
dc:
include: 'dc.js'
```
This plugin passes 'dc/dc.js' to `require.resolve`, and returns the
result to brunch via the plugin `include` method.

## Types Configuration

If you want to add types:

```coffeescript

types:
factory:
path: 'factories'
suffix: '-factory'
extension: 'js'
module:
path: 'modules'

@require factory.notification
# NotificationFactory = require 'factories/notification-factory'

@require module.robot
# RobotModule = require 'modules/robot'

```
Very crude, but seems to work for npm sources that have a compiled
file that is ready for minification/include.

Of course, you may want to edit existing types:

```coffeescript

types:
controller:
path: 'app-controllers'
template:
path: 'theme/awesome/template'
extension: 'mustache'

```

Configurated types:

* controller
* view
* model
* collectionView
* template
* helper
* config
* lib

Available type options:

```coffeescript

# main folder of the type
# the field is required
path: 'controllers'

# filename suffix
# for example, want you require controller.posts,
# the file controllers/posts-controller will be used
# default value: null
suffix: '-controller'

# subfolder of the type
# default value: null
subFolder: null

# file extension
# default value: coffee
# this field is required
extension: 'coffee'

# generate the variable name from the filename
# for example, for the lib modules,
# you don't want LibDispatcher, you'd want:
# Dispatcher = require 'lib/dispatcher'
# default value: no
varNameWithFilename: no

# generate the variable name without the type suffixed
# for example, it's better to have Post rather than PostModel
# default value: no
varNameWithoutType: no

```

## Naming configuration

Maybe you don't want to use @require or @include:

```coffeescript

naming:
require: '~'
include: '-'
prefixApp: '!'
prefixVendor: '$'

!~ controllers.post
# PostController = require 'controllers/post'
!- helper.view
# require 'helpers/view-helper'

```

```coffeescript

naming:
require: 'import'
include: 'include'
prefixApp: ''
prefixVendor: 'vendor_'

import controllers.post
# PostController = require 'controllers/post'
include helper.view
# require 'helpers/view-helper'
vendor_import chaplin
# Chaplin = require 'chaplin'

```

## Module exportation configuration

```coffeescript
## Usage

# prefix all class definition by the defined string
# set to null to disable
# the string that will prefix the class definition
export: 'module.exports = '
Add `"npm-vendorize-brunch": "x.y.z"` to `package.json` of your brunch app.
Add config as shown above.

```
16 changes: 8 additions & 8 deletions lib/preprocessor.js → lib/index.js
@@ -1,19 +1,19 @@
var Preprocessor,
var NPMVendorize,
__hasProp = {}.hasOwnProperty;

module.exports = Preprocessor = (function() {
module.exports = NPMVendorize = (function() {

Preprocessor.prototype.brunchPlugin = true;
NPMVendorize.prototype.brunchPlugin = true;

Preprocessor.prototype.type = 'javascript';
NPMVendorize.prototype.type = 'javascript';

Preprocessor.prototype.extension = 'coffee';
NPMVendorize.prototype.extension = 'coffee';

function Preprocessor(conf) {
function NPMVendorize(conf) {
this.conf = conf;
}

Preprocessor.prototype.include = function() {
NPMVendorize.prototype.include = function() {
var key, p, paths, value, _ref;
paths = [];
_ref = this.conf.plugins.vendorize;
Expand All @@ -29,6 +29,6 @@ module.exports = Preprocessor = (function() {
return paths;
};

return Preprocessor;
return NPMVendorize;

})();
9 changes: 4 additions & 5 deletions package.json
@@ -1,16 +1,16 @@
{
"name": "include-preprocessor-brunch",
"name": "npm-vendorize-brunch",
"version": "0.1.0",
"description": "includes npm sources in vendor.js Brunch output",
"repository": {
"type": "git",
"url": "git://github.com/bewest/include-preprocessor-brunch.git"
"url": "git://github.com/bewest/npm-vendorize-brunch.git"
},
"author": {
"name": "bewest",
"url": "https://github.com/bewest"
},
"main": "./lib/preprocessor",
"main": "./lib/index",
"scripts": {
"prepublish": "grunt build",
"test": "grunt test"
Expand All @@ -29,6 +29,5 @@
"grunt-contrib-watch": "0.2.x",
"mocha": "1.8.x",
"chai": "1.5.x"
},
"readme": "physically include npm sources in vendor.js output"
}
}
2 changes: 1 addition & 1 deletion src/preprocessor.coffee → src/index.coffee
@@ -1,5 +1,5 @@

module.exports = class Preprocessor
module.exports = class NPMVendorize
brunchPlugin: yes
type: 'javascript'
extension: 'coffee'
Expand Down
@@ -1,6 +1,6 @@
fs = require 'fs'

Preprocessor = require './lib/preprocessor'
NPMVendorize = require './lib/index'
expect = require('chai').expect

describe 'preprocessor', ->
Expand All @@ -13,17 +13,17 @@ describe 'preprocessor', ->
#t = './fixtures'
vendor = { }
vendor[t] = { include: 'foo.js' }
preprocessor = new Preprocessor
preprocessor = new NPMVendorize
paths:
app: ''
plugins:
vendorize: vendor

it 'should instantiate', ->
expect(preprocessor).to.be.ok
expect(preprocessor).to.be.an.instanceof Preprocessor
expect(preprocessor).to.be.an.instanceof NPMVendorize

it 'should have an include function', ->
it 'should have an include method', ->
expect(preprocessor['include']).to.be.an.instanceof Function

it 'include method should return sources list', ->
Expand Down

0 comments on commit bf8f3a0

Please sign in to comment.