Skip to content

Commit

Permalink
Use ES5, tap
Browse files Browse the repository at this point in the history
  • Loading branch information
zoubin committed Nov 28, 2015
1 parent dbd1276 commit e52538b
Show file tree
Hide file tree
Showing 19 changed files with 307 additions and 409 deletions.
1 change: 0 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ ecmaFeatures:
env:
browser: true
node: true
es6: true

rules:
comma-dangle: [2, "always-multiline"]
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.DS_Store
npm-debug.log
build/
/coverage/
coverage/
.nyc_output/
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
npm-debug.log
changelog.md
coverage/
example/
test/
.*
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
language: node_js
node_js:
- "4.1"
- "4.0"
- "stable"
- "4"
- "0.12"
- "0.11"
- "iojs"
25 changes: 9 additions & 16 deletions example/gulpfile.babel.js → example/bundle.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
import gulp from 'gulp'
import depsify from '../lib/main'
import path from 'path'
import del from 'del'
import postcss from 'postcss'
import url from 'postcss-url'
import source from 'vinyl-source-stream'
var depsify = require('../')
var path = require('path')
var del = require('del')
var postcss = require('postcss')
var url = require('postcss-url')

var processor = postcss([
require('postcss-import')(),
require('postcss-url')({ url: 'copy', assetsPath: 'i' }),
require('postcss-url')({ url: 'inline' }),
require('postcss-advanced-variables')(),
])

var fixtures = path.resolve.bind(path, __dirname, 'src')
var DEST = path.join(__dirname, 'build')
var common = path.join(DEST, 'common.css')

gulp.task('clean', function () {
return del(DEST)
})

gulp.task('default', ['clean'], function () {
return depsify({
del(DEST).then(function () {
depsify({
basedir: fixtures(),
entries: ['a.css', 'b.css'],
processor: function (result) {
Expand All @@ -35,7 +29,6 @@ gulp.task('default', ['clean'], function () {
},
})
.bundle()
.pipe(source('common.css'))
.pipe(gulp.dest(DEST))
.pipe(process.stdout)
})

Binary file added example/src/button/bin.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed example/src/button/button.png
Binary file not shown.
2 changes: 1 addition & 1 deletion example/src/button/index.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@import "color";
.button {
background-color: $blue;
background-image: url(button.png);
background-image: url(bin.jpg);
}
92 changes: 0 additions & 92 deletions gulpfile.babel.js

This file was deleted.

185 changes: 183 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,184 @@
require('babel-core/register')()
module.exports = require('./lib/main')
var EventEmitter = require('events')
var mix = require('util-mix')
var MDeps = require('css-module-deps')
var thr = require('through2')
var resolve = require('resolve')
var readonly = require('read-only-stream')
var topoSort = require('deps-topo-sort')
var path = require('path')
var splicer = require('labeled-stream-splicer')
var sink = require('sink-transform')

var inherits = require('util').inherits
inherits(Depsify, EventEmitter)

module.exports = Depsify

function Depsify(entries, opts) {
if (!(this instanceof Depsify)) {
return new Depsify(entries, opts)
}
if (typeof entries === 'string' || Array.isArray(entries)) {
opts = mix({}, opts, {
entries: [].concat(opts.entries || [], entries),
})
} else {
opts = mix({}, entries, opts)
}
this._options = opts
opts.basedir = opts.basedir || process.cwd()

this.pipeline = this._createPipeline(opts)

;[].concat(opts.entries).filter(Boolean).forEach(function (file) {
this.add(file, { basedir: opts.basedir })
}, this)
;[].concat(opts.plugin).filter(Boolean).forEach(function (p) {
this.plugin(p, { basedir: opts.basedir })
}, this)
}

Depsify.prototype._createPipeline = function (opts) {
var self = this
this._mdeps = new MDeps(Object.create(opts))
this._mdeps.on('file', function (file) {
pipeline.emit('file', file)
self.emit('file', file)
})
this._mdeps.on('package', function (pkg) {
pipeline.emit('package', pkg)
self.emit('package', pkg)
})
this._mdeps.on('transform', function (tr, file) {
pipeline.emit('transform', tr, file)
self.emit('transform', tr, file)
})

if (opts.pack) {
this.pack = opts.pack
}
// for factor-bundle
this._bpack = this.pack(opts)

var pipeline = splicer.obj([
'record', [ this._recorder() ],
'deps', [ this._mdeps ],
'unbom', [ this._unbom() ],
'syntax', [],
'sort', [],
'dedupe', [],
'label', [],
'emit-deps', [ this._emitDeps() ],
'debug', [],
'pack', [ this._bpack ],
'wrap', [],
])

return pipeline
}

Depsify.prototype._unbom = function() {
return thr.obj(function (row, enc, next) {
if (/^\ufeff/.test(row.source)) {
row.source = row.source.replace(/^\ufeff/, '')
}
this.push(row)
next()
})
}

Depsify.prototype._emitDeps = function() {
var self = this
return thr.obj(function (row, enc, next) {
self.emit('dep', row)
this.push(row)
next()
})
}

Depsify.prototype._recorder = function() {
var recorded = this._recorded = []
return thr.obj(function (row, _, next) {
recorded.push(row)
next(null, row)
})
}

Depsify.prototype.pack = function() {
return [
sink.obj(function (rows, done) {
rows.sort(function (a, b) {
return a.file < b.file ? -1 : 1
})
.forEach(function (row) {
// to use toposort
row.id = row.file
this.push(row)
}, this)
done()
}),
topoSort(),
thr.obj(function (row, _, next) {
next(null, row.source)
}),
]
}

Depsify.prototype.add = function(file, opts) {
opts = opts || {}
if (Array.isArray(file)) {
file.forEach(function (f) {
this.add(f, opts)
}, this)
} else if (typeof file === 'string') {
file = path.resolve(opts.basedir || this._options.basedir, file)
this.pipeline.write({ file: file })
} else {
this.pipeline.write(mix({ basedir: this._options.basedir }, file, opts))
}
return this
}

Depsify.prototype.processor = function(p) {
this.pipeline.write({ processor: p })
return this
}

Depsify.prototype.reset = function() {
this.pipeline = this._createPipeline(this._options)
this._bundled = false
this.emit('reset')
}

Depsify.prototype.plugin = function(p, opts) {
if (Array.isArray(p)) {
opts = p[1]
p = p[0]
}
var basedir = opts && opts.basedir || this._options.basedir
if (typeof p === 'function') {
p(this, opts)
} else {
var pfile = resolve.sync(p, { basedir: basedir })
var f = require(pfile)
f(this, opts)
}
return this
}

Depsify.prototype.bundle = function() {
if (this._bundled) {
var recorded = this._recorded
this.reset()
recorded.forEach(function (x) {
this.pipeline.write(x)
}, this)
}
var output = readonly(this.pipeline)
this.emit('bundle', output)
this.pipeline.end()
this._bundled = true

return output
}

0 comments on commit e52538b

Please sign in to comment.