[Pkg-javascript-commits] [node-coffeeify] 01/06: Imported Upstream version 2.0.1
Ross Gammon
ross-guest at moszumanska.debian.org
Sun Dec 27 20:20:20 UTC 2015
This is an automated email from the git hooks/post-receive script.
ross-guest pushed a commit to branch master
in repository node-coffeeify.
commit 2f7c3c3a42dde0de0e7a710022c8748822591e9c
Author: Ross Gammon <rossgammon at mail.dk>
Date: Sun Dec 27 19:10:58 2015 +0100
Imported Upstream version 2.0.1
---
.travis.yml | 2 ++
README.md | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
index.js | 71 ++++++++++++++++++++++++-------------
no-debug.js | 3 --
package.json | 7 ++--
readme.markdown | 67 -----------------------------------
test/bundle.js | 68 ++++++++++++++++++++---------------
test/no-debug.js | 14 --------
test/transform.js | 16 ++++-----
9 files changed, 203 insertions(+), 149 deletions(-)
diff --git a/.travis.yml b/.travis.yml
index 6c0d981..5aee415 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -2,4 +2,6 @@ language: node_js
node_js:
- 0.10
- 0.12
+ - 4
+ - 5
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..a558b4d
--- /dev/null
+++ b/README.md
@@ -0,0 +1,104 @@
+# coffeeify [![Build Status](https://travis-ci.org/jnordberg/coffeeify.svg?branch=master)](https://travis-ci.org/jnordberg/coffeeify)
+
+CoffeeScript browserify transform. Mix and match `.coffee` and `.js` files in the same project.
+
+## Example
+
+Given some files written in a mix of `js` and `coffee`:
+
+foo.coffee:
+
+```coffee
+console.log require './bar.js'
+```
+
+bar.js:
+
+```javascript
+module.exports = require('./baz.coffee')(5)
+```
+
+baz.coffee:
+
+```coffee
+module.exports = (n) -> n ** n
+```
+
+Install coffeeify into your app:
+
+```
+$ npm install coffeeify
+```
+
+When you compile your app, just pass `-t coffeeify` to browserify:
+
+```shell
+$ browserify -t coffeeify foo.coffee > bundle.js
+$ node bundle.js
+3125
+```
+
+You can omit the `.coffee` extension from your requires if you add the extension to browserify's module extensions:
+
+```javascript
+module.exports = require('./baz')(5)
+```
+
+```
+$ browserify -t coffeeify --extension=".coffee" foo.coffee > bundle.js
+$ node bundle.js
+3125
+```
+
+You can also pass options to the CoffeeScript compiler:
+
+```
+$ browserify -t [ coffeeify --bare false --header true ] --extension=".coffee" foo.coffee
+..
+// Generated by CoffeeScript 1.10.0
+(function() {
+ console.log(require('./bar.js'));
+
+}).call(this);
+..
+```
+
+## Options
+
+Name | Default | Description
+-----------|-----------|-------------------------------------------------------------------------------------------
+sourceMap | `null` | Generate source maps, deteremined from browserify's `--debug` option if not set.
+bare | `true` | Omit the `(function(){ .. }).call(this);` wrapper.
+header | `false` | Include the `// Generated by CoffeeScript <version>` header in every file processed.
+
+When using browserify programatically options can be passed as an object, example:
+
+```coffee
+browserify = require 'browserify'
+coffeeify = require 'coffeeify'
+
+bundle = browserify
+ extensions: ['.coffee']
+
+bundle.transform coffeeify,
+ bare: false
+ header: true
+
+bundle.add 'foo.coffee'
+
+bundle.bundle (error, result) ->
+ throw error if error?
+ process.stdout.write result
+```
+
+## Install
+
+With [npm](https://npmjs.org) do:
+
+```
+npm install coffeeify
+```
+
+## License
+
+MIT
diff --git a/index.js b/index.js
index 72024a8..843aaba 100644
--- a/index.js
+++ b/index.js
@@ -1,9 +1,12 @@
var coffee = require('coffee-script');
-var through = require('through');
var convert = require('convert-source-map');
+var path = require('path');
+var through = require('through2');
+
+var filePattern = /\.((lit)?coffee|coffee\.md)$/;
function isCoffee (file) {
- return (/\.((lit)?coffee|coffee\.md)$/).test(file);
+ return filePattern.test(file);
}
function isLiterate (file) {
@@ -42,58 +45,78 @@ ParseError.prototype.inspect = function () {
return this.annotated;
};
-function compile(file, data, callback) {
+function compile(filename, source, options, callback) {
var compiled;
try {
- compiled = coffee.compile(data, {
- sourceMap: coffeeify.sourceMap,
- generatedFile: file,
+ compiled = coffee.compile(source, {
+ sourceMap: options.sourceMap,
inline: true,
- bare: true,
- literate: isLiterate(file)
+ bare: options.bare,
+ header: options.header,
+ literate: isLiterate(filename)
});
} catch (e) {
var error = e;
if (e.location) {
- error = new ParseError(e, data, file);
+ error = new ParseError(e, source, filename);
}
callback(error);
return;
}
- if (coffeeify.sourceMap) {
+ if (options.sourceMap) {
var map = convert.fromJSON(compiled.v3SourceMap);
- map.setProperty('sources', [file]);
+ var basename = path.basename(filename);
+ map.setProperty('file', basename.replace(filePattern, '.js'));
+ map.setProperty('sources', [basename]);
callback(null, compiled.js + '\n' + map.toComment() + '\n');
} else {
callback(null, compiled + '\n');
}
-
+
}
-function coffeeify(file) {
- if (!isCoffee(file)) return through();
+function coffeeify(filename, options) {
+ if (!isCoffee(filename)) return through();
+
+ if (typeof options === 'undefined' || options === null) options = {};
- var data = '', stream = through(write, end);
+ var compileOptions = {
+ sourceMap: (options._flags && options._flags.debug),
+ bare: true,
+ header: false
+ };
- return stream;
+ for (var i = 0, keys = Object.keys(compileOptions); i < keys.length; i++) {
+ var key = keys[i], option = options[key];
+ if (typeof option !== 'undefined' && option !== null) {
+ if (option === 'false' || option === 'no' || option === '0') {
+ option = false;
+ }
+ compileOptions[key] = !!option;
+ }
+ }
- function write(buf) {
- data += buf;
+ var chunks = [];
+ function transform(chunk, encoding, callback) {
+ chunks.push(chunk);
+ callback();
}
- function end() {
- compile(file, data, function(error, result) {
- if (error) stream.emit('error', error);
- stream.queue(result);
- stream.queue(null);
+ function flush(callback) {
+ var stream = this;
+ var source = Buffer.concat(chunks).toString();
+ compile(filename, source, compileOptions, function(error, result) {
+ if (!error) stream.push(result);
+ callback(error);
});
}
+
+ return through(transform, flush);
}
coffeeify.compile = compile;
coffeeify.isCoffee = isCoffee;
coffeeify.isLiterate = isLiterate;
-coffeeify.sourceMap = true; // use source maps by default
module.exports = coffeeify;
diff --git a/no-debug.js b/no-debug.js
deleted file mode 100644
index 80c0ace..0000000
--- a/no-debug.js
+++ /dev/null
@@ -1,3 +0,0 @@
-coffeeify = require('./index');
-coffeeify.sourceMap = false;
-module.exports = coffeeify;
diff --git a/package.json b/package.json
index 056a3b3..ea9db33 100644
--- a/package.json
+++ b/package.json
@@ -1,16 +1,17 @@
{
"name": "coffeeify",
- "version": "1.2.0",
+ "version": "2.0.1",
"description": "browserify plugin for coffee-script with support for mixed .js and .coffee files",
"main": "index.js",
"dependencies": {
"coffee-script": "^1.10.0",
"convert-source-map": "^1.1.2",
- "through": "^2.3.8"
+ "through2": "^2.0.0"
},
"devDependencies": {
"browserify": "^12.0.1",
- "tap": "^2.3.0"
+ "tap": "^2.3.0",
+ "through": "^2.3.8"
},
"scripts": {
"test": "tap test/*.js"
diff --git a/readme.markdown b/readme.markdown
deleted file mode 100644
index 62b944a..0000000
--- a/readme.markdown
+++ /dev/null
@@ -1,67 +0,0 @@
-# coffeeify
-
-browserify v2 plugin for coffee-script
-
-mix and match `.coffee` and `.js` files in the same project
-
-[![Build Status](https://travis-ci.org/jnordberg/coffeeify.png?branch=master)](https://travis-ci.org/jnordberg/coffeeify)
-
-# example
-
-given some files written in a mix of `js` and `coffee`:
-
-foo.coffee:
-
-``` coffee
-console.log(require './bar.js')
-```
-
-bar.js:
-
-``` js
-module.exports = require('./baz.coffee')(5)
-```
-
-baz.coffee:
-
-``` js
-module.exports = (n) -> n * 111
-```
-
-install coffeeify into your app:
-
-```
-$ npm install coffeeify
-```
-
-when you compile your app, just pass `-t coffeeify` to browserify:
-
-```
-$ browserify -t coffeeify foo.coffee > bundle.js
-$ node bundle.js
-555
-```
-
-you can omit the `.coffee` extension from your requires if you add the extension to browserify's module extensions:
-
-``` js
-module.exports = require('./baz')(5)
-```
-
-```
-$ browserify -t coffeeify --extension=".coffee" foo.coffee > bundle.js
-$ node bundle.js
-555
-```
-
-# install
-
-With [npm](https://npmjs.org) do:
-
-```
-npm install coffeeify
-```
-
-# license
-
-MIT
diff --git a/test/bundle.js b/test/bundle.js
index 26ab95e..a4b5156 100644
--- a/test/bundle.js
+++ b/test/bundle.js
@@ -1,45 +1,57 @@
var test = require('tap').test;
var browserify = require('browserify');
var vm = require('vm');
+var convert = require('convert-source-map');
+var path = require('path');
+var fs = require('fs');
function bundle (file) {
test('bundle transform', function (t) {
- t.plan(1);
-
- var b = browserify();
- b.add(__dirname + file);
- b.transform(__dirname + '/..');
- b.bundle(function (err, src) {
- if (err) t.fail(err);
- vm.runInNewContext(src, {
- console: { log: log }
- });
+ t.plan(2);
+
+ var bundle = browserify();
+ var sourcepath = path.join(__dirname, file);
+
+ bundle.add(sourcepath);
+ bundle.transform(path.join(__dirname, '..'));
+ bundle.bundle(function (error, src) {
+ if (error) t.fail(error);
+ vm.runInNewContext(src, {console: {log: log}});
+ var map = convert.fromSource(String(src));
+ t.ok(map == null, 'should not have a map when debug is off');
});
function log (msg) {
t.equal(msg, 555);
}
});
-
- test('bundle transform (no-debug)', function (t) {
- t.plan(1);
-
- var b = browserify();
- b.add(__dirname + file);
- b.transform(__dirname + '/../no-debug');
- b.bundle(function (err, src) {
- if (err) t.fail(err);
- vm.runInNewContext(src, {
- console: { log: log }
+
+ test('bundle transform includes sourcemaps with debug on', function (t) {
+ t.plan(3);
+
+ var bundle = browserify({debug: true});
+ var sourcepath = path.join(__dirname, file);
+
+ bundle.add(sourcepath);
+ bundle.transform(path.join(__dirname, '..'));
+ bundle.bundle(function (error, src) {
+ if (error) t.fail(error);
+ vm.runInNewContext(src, {console: {log: log}});
+ var map = convert.fromSource(String(src));
+ var relpath = path.relative(process.cwd(), sourcepath);
+ var sourceIdx = map.sourcemap.sources.indexOf(relpath);
+ t.ok(sourceIdx !== -1, 'should have source filename in map')
+ var filesrc = fs.readFileSync(sourcepath).toString()
+ t.equal(filesrc, map.sourcemap.sourcesContent[sourceIdx], 'should have source content in map')
});
- });
- function log (msg) {
- t.equal(msg, 555);
- }
+ function log (msg) {
+ t.equal(msg, 555);
+ }
});
-
+
+
}
-bundle('/../example/foo.coffee');
-bundle('/../example/foo.litcoffee');
+bundle('../example/foo.coffee');
+bundle('../example/foo.litcoffee');
diff --git a/test/no-debug.js b/test/no-debug.js
deleted file mode 100644
index 36d0b29..0000000
--- a/test/no-debug.js
+++ /dev/null
@@ -1,14 +0,0 @@
-var test = require('tap').test;
-
-test('sourceMap use', function (t) {
-
- t.plan(2);
-
- defaultCoffeeify = require(__dirname + '/..');
- t.equal(defaultCoffeeify.sourceMap, true, "sourceMap is true by default");
-
- noDebugCoffeeify = require(__dirname + '/../no-debug');
- t.equal(noDebugCoffeeify.sourceMap, false, "sourceMap is false for no-debug");
-
-});
-
diff --git a/test/transform.js b/test/transform.js
index 5979c83..e62694d 100644
--- a/test/transform.js
+++ b/test/transform.js
@@ -5,15 +5,13 @@ var through = require('through');
var convert = require('convert-source-map');
var transform = require('..');
-test('transform adds sourcemap comment when sourceMap is true', function (t) {
+test('transform adds sourcemap comment when sourceMap option is true', function (t) {
t.plan(1);
var data = '';
- transform.sourceMap = true;
-
var file = path.join(__dirname, '../example/foo.coffee');
fs.createReadStream(file)
- .pipe(transform(file))
+ .pipe(transform(file, {sourceMap: true}))
.pipe(through(write, end));
function write (buf) { data += buf }
@@ -23,9 +21,9 @@ test('transform adds sourcemap comment when sourceMap is true', function (t) {
t.deepEqual(
sourceMap,
{ version: 3,
- file: file,
+ file: 'foo.js',
sourceRoot: '',
- sources: [ file ],
+ sources: [ 'foo.coffee' ],
names: [],
mappings: 'AAAA,OAAO,CAAC,GAAR,CAAY,OAAA,CAAQ,UAAR,CAAZ',
sourcesContent: [ 'console.log(require \'./bar.js\')\n' ] },
@@ -34,16 +32,14 @@ test('transform adds sourcemap comment when sourceMap is true', function (t) {
}
});
-test('transform does not add sourcemap when sourceMap is false', function (t) {
+test('transform does not add sourcemap when sourceMap option is false', function (t) {
t.plan(1);
var data = '';
- transform.sourceMap = false;
-
var file = path.join(__dirname, '../example/foo.coffee');
fs.createReadStream(file)
- .pipe(transform(file))
+ .pipe(transform(file, {sourceMap: false}))
.pipe(through(write, end));
function write (buf) { data += buf }
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-coffeeify.git
More information about the Pkg-javascript-commits
mailing list