Comparing version 3.0.0 to 3.1.0
{ | ||
"name": "stringify", | ||
"description": "Browserify middleware to be able to require() text files (including templates) inside of your client-side JavaScript files.", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"main": "./index.js", | ||
@@ -11,3 +11,4 @@ "author": "John Postlethwait <john.postlethwait@gmail.com>", | ||
"Livoras <livoras@163.com>", | ||
"Sébastien David <sebastien.david.1983@gmail.com>" | ||
"Sébastien David <sebastien.david.1983@gmail.com>", | ||
"Kenneth Skovhus <kenneth.skovhus@gmail.com>" | ||
], | ||
@@ -38,2 +39,3 @@ "website": "http://johnpostlethwait.github.com/stringify/", | ||
"html-minifier": "^0.6.9", | ||
"stream-spec": "~0.3.5", | ||
"through": "^2.3.4" | ||
@@ -40,0 +42,0 @@ }, |
@@ -13,4 +13,8 @@ # Stringify # | ||
Setup Browserify to use this middleware in your app: | ||
### Browserify Command Line ### | ||
`browserity -t stringify myfile.js` | ||
### Browserify Middleware ### | ||
```javascript | ||
@@ -54,3 +58,3 @@ var browserify = require('browserify'), | ||
__minifier__ options are optional. | ||
__minifier__ options are optional. | ||
@@ -92,2 +96,19 @@ Default __minifier.extensions__: | ||
## Usage with gulp and gulp-browserify | ||
To incorporate stringify into a `gulp` build process using `gulp-browserify`, register `stringify` as a transform as follows: | ||
```javascript | ||
gulp.task('js', function() { | ||
return gulp.src('src/main.js', { read: false }) | ||
.pipe(browserify({ | ||
transform: stringify({ | ||
extensions: ['.html'], minify: true | ||
}) | ||
})) | ||
.pipe(gif(env !== 'dev', uglify())) | ||
.pipe(gulp.dest(paths.build)); | ||
}); | ||
``` | ||
## More Realistic Example & Use-Case ## | ||
@@ -94,0 +115,0 @@ |
@@ -128,11 +128,17 @@ 'use strict'; | ||
/** | ||
* Creates the Browserify transform function which Browserify will pass the | ||
* file to. | ||
* Exposes the Browserify transform function. | ||
* | ||
* This handles two use cases: | ||
* - Factory: given no arguments or options as first argument it returns | ||
* the transform function | ||
* - Standard: given file (and optionally options) as arguments a stream is | ||
* returned. This follows the standard pattern for browserify transformers. | ||
* | ||
* @param {string} file | ||
* @param {object | array} options | ||
* @param {object} options.extensions | ||
* @returns {stream} | ||
* @returns {stream | function} depending on if first argument is string. | ||
*/ | ||
module.exports = function (options) { | ||
var extensions = getExtensions(options); | ||
module.exports = function (file, options) { | ||
@@ -145,2 +151,4 @@ /** | ||
function browserifyTransform (file) { | ||
var extensions = getExtensions(options); | ||
if (!hasStringifiableExtension(file, extensions)) { | ||
@@ -165,3 +173,11 @@ return through(); | ||
return browserifyTransform; | ||
if (typeof file !== 'string') { | ||
// Factory: return a function. | ||
// Set options variable here so it is ready for when browserifyTransform | ||
// is called. Note: first argument is the options. | ||
options = file; | ||
return browserifyTransform; | ||
} else { | ||
return browserifyTransform(file); | ||
} | ||
}; | ||
@@ -168,0 +184,0 @@ |
155
test/main.js
/* jshint expr: true */ | ||
/* global describe: false, it: false, before: false */ | ||
'use strict'; | ||
var path = require('path'), | ||
should = require('should'), | ||
Stringify = require('../index'); | ||
var should = require('should'), | ||
spec = require('stream-spec'), | ||
stringify = require('../index'); | ||
var input = '<p style="color: grey;" > should be minified </p>'; | ||
var outputTransformed = 'module.exports = \"<p style=\\"color: grey\\">should be minified</p>";\n'; | ||
function write(data, stream) { | ||
function next() { | ||
if(stream.write(data) === false) { | ||
return stream.once('drain', next); | ||
} | ||
stream.end(); | ||
} | ||
next(); | ||
} | ||
function read(stream, callback) { | ||
var returned = []; | ||
stream.on('data', function (data) { | ||
returned.push(data); | ||
}); | ||
stream.once('end', function () { | ||
callback(null, returned); | ||
}); | ||
stream.once('error', function (err) { | ||
callback(err); | ||
}); | ||
} | ||
describe('when the module is required', function () { | ||
it('should return a function', function () { | ||
Stringify.should.be.a.Function; | ||
stringify.should.be.a.Function; | ||
}); | ||
}); | ||
describe('when the module is instantiated', function () { | ||
describe('when the module called', function () { | ||
describe('with no options', function () { | ||
before(function () { | ||
this.stringify = new Stringify(); | ||
this.transformerFactory = stringify(); | ||
}); | ||
it('should return a function named "browserifyTransform"', function () { | ||
this.stringify.name.should.be.exactly('browserifyTransform'); | ||
it('should return a factory function named "browserifyTransform"', function () { | ||
this.transformerFactory.name.should.be.exactly('browserifyTransform'); | ||
}); | ||
describe('when the return value is called with a valid file path', function () { | ||
describe('when the returned function is called with a valid file path', function () { | ||
before(function () { | ||
var fixture = path.join(path.dirname('.'), 'file_fixture.txt'); | ||
this.stringified = this.stringify(fixture); | ||
this.transformer = this.transformerFactory('file_fixture.txt'); | ||
}); | ||
@@ -33,9 +59,106 @@ | ||
it('should return a Stream object', function () { | ||
should(this.stringified.writable).ok; | ||
should(this.stringified.readable).ok; | ||
this.stringified.write.should.be.a.Function; | ||
this.stringified.end.should.be.a.Function; | ||
should(this.transformer.writable).ok; | ||
should(this.transformer.readable).ok; | ||
this.transformer.write.should.be.a.Function; | ||
this.transformer.end.should.be.a.Function; | ||
}); | ||
}); | ||
}); | ||
describe('with options as first argument', function () { | ||
before(function () { | ||
console.log('xxxxxxxxxxxxxxxxxxxxxxxxxxx'); | ||
this.transformerFactory = stringify({ | ||
extensions: ['.xxx'], | ||
minify: true, | ||
minifier: { | ||
extensions: ['.xxx'] | ||
} | ||
}); | ||
}); | ||
it('should return a function named "browserifyTransform"', function () { | ||
this.transformerFactory.name.should.be.exactly('browserifyTransform'); | ||
}); | ||
it('should respond to input with the given options', function () { | ||
var transformer = this.transformerFactory('a_file.xxx'); | ||
var s = spec(transformer).pausable(); | ||
read(transformer, function (err, returnedData) { | ||
should(err).be.null; | ||
should(returnedData).be.an.array; | ||
should(returnedData.length).be.equal(1); | ||
should(returnedData[0]).be.equal(outputTransformed); | ||
}); | ||
transformer.on('close', s.validate); | ||
write(input, transformer); | ||
}); | ||
}); | ||
describe('with a HTML file as first argument', function () { | ||
before(function () { | ||
this.transformer = stringify('a_file.html', { | ||
extensions: ['.html'], | ||
minify: true, | ||
minifier: { | ||
extensions: ['.html'] | ||
} | ||
}); | ||
}); | ||
it('should return a Stream object', function () { | ||
should(this.transformer.writable).ok; | ||
should(this.transformer.readable).ok; | ||
this.transformer.write.should.be.a.Function; | ||
this.transformer.end.should.be.a.Function; | ||
}); | ||
it('should respond to input', function () { | ||
var s = spec(this.transformer).pausable(); | ||
read(this.transformer, function (err, returnedData) { | ||
should(err).be.null; | ||
should(returnedData).be.an.array; | ||
should(returnedData.length).be.equal(1); | ||
should(returnedData[0]).be.equal(outputTransformed); | ||
}); | ||
this.transformer.on('close', s.validate); | ||
write(input, this.transformer); | ||
}); | ||
}); | ||
describe('with an unknown file as first argument', function () { | ||
before(function () { | ||
this.transformer = stringify('a_file.foo'); | ||
}); | ||
it('should return a Stream object', function () { | ||
should(this.transformer.writable).ok; | ||
should(this.transformer.readable).ok; | ||
this.transformer.write.should.be.a.Function; | ||
this.transformer.end.should.be.a.Function; | ||
}); | ||
it('should respond without transformation', function () { | ||
var s = spec(this.transformer).pausable(); | ||
read(this.transformer, function (err, returnedData) { | ||
should(err).be.null; | ||
should(returnedData).be.an.array; | ||
should(returnedData.length).be.equal(1); | ||
should(returnedData[0]).be.equal(input); | ||
}); | ||
this.transformer.on('close', s.validate); | ||
write(input, this.transformer); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
25321
516
175
0
3
+ Addedstream-spec@~0.3.5
+ Addedmacgyver@1.10.1(transitive)
+ Addedstream-spec@0.3.6(transitive)