Permalink
Browse files

added ability to import css dependencies multiple times in the same b…

…uild target; closes #23
  • Loading branch information...
1 parent d8f3281 commit 9f47c830af2a612333fdf95ed6a8d2bd6c2fdf52 @popeindustries committed Feb 19, 2013
View
@@ -1,2 +1,3 @@
.DS_Store
node_modules
+.buddy-filelog
View
@@ -176,7 +176,7 @@ Target.prototype._parse = function(fn) {
, self = this
, file;
// Inner function
- var parse = function(file, fn) {
+ var parse = function(file, dependant, fn) {
var dep;
// Parse file content, if necessary
outstanding++;
@@ -187,23 +187,26 @@ Target.prototype._parse = function(fn) {
if (self.options.concat && file.dependencies.length) {
file.dependencies.forEach(function(dependency, idx) {
dep = self.options.source.byModule[dependency]
- || self.options.source.byModule["" + dependency + "/index"];
+ || self.options.source.byModule["" + dependency + "/index"];
// Resolve dependency
if (dep) {
- // Protect against circular references and duplicates
- if (!dep.isDependency) {
+ // Protect against circular references
+ // but allow duplicate css dependencies
+ if (dep != dependant && (self.type == 'css' ? true : !dep.isDependency)) {
// Store dependency references
- self._modified.push(dep);
dep.isDependency = true;
+ if (!self._modified.indexOf(dep) == -1) self._modified.push(dep);
+ // Replace string ref with instance
file.dependencies[idx] = dep;
debug("added dependency "
+ (strong(dep.moduleID))
+ " to "
+ (strong(file.moduleID)), 3);
// Parse
- parse(dep, fn);
+ parse(dep, file, fn);
}
- } else {
+ // Don't warn if dependency is already resolved
+ } else if ('String' == typeof dependency) {
warn("dependency "
+ (strong(dependency))
+ " for "
@@ -232,7 +235,7 @@ Target.prototype._parse = function(fn) {
if (!self.hasSource(file)) {
self.sources.push(file);
self._modified.push(file);
- parse(file, function(err) {
+ parse(file, null, function(err) {
if (err) return fn(err);
// Filter out all files that are dependants
self.sources = self.sources.filter(function(file) {
@@ -261,7 +264,7 @@ Target.prototype._parse = function(fn) {
if (!this.hasSource(file)) {
this.sources.push(file);
this._modified.push(file);
- parse(file, fn);
+ parse(file, null, fn);
} else {
return fn();
}
View
@@ -215,3 +215,21 @@ describe 'Builder', ->
@builder.build 'buddy_empty.js', false, false, false, false, false, (err) =>
gatherFiles(@builder.targets.js[0].output).should.have.length(2)
done()
+ describe 'css project', ->
+ before ->
+ process.chdir(path.resolve(__dirname, 'fixtures/builder/build/project-css'))
+ describe 'with 2 stylus files referencing a shared dependency', ->
+ it 'should build 2 css files', (done) ->
+ @builder.build 'buddy.js', false, false, false, false, false, (err) =>
+ fs.existsSync(path.resolve(@builder.targets.css[0].output, 'one.css')).should.be.true
+ fs.existsSync(path.resolve(@builder.targets.css[0].output, 'two.css')).should.be.true
+ fs.existsSync(path.resolve(@builder.targets.css[0].output, 'three.css')).should.be.false
+ done()
+ it 'should import the dependency into both files', (done) ->
+ @builder.build 'buddy.js', false, false, false, false, false, (err) =>
+ contents1 = fs.readFileSync(path.resolve(@builder.targets.css[0].output, 'one.css'), 'utf8')
+ contents2 = fs.readFileSync(path.resolve(@builder.targets.css[0].output, 'two.css'), 'utf8')
+ contents1.should.eql(contents2)
+ contents1.should.include("colour: '#ffffff';")
+ contents2.should.include("colour: '#ffffff';")
+ done()
View
@@ -50,9 +50,9 @@ describe 'file', ->
file 'js', path.resolve('src/package/Class.coffee'), path.resolve('src'), {processors: processors.js}, (err, instance) ->
instance.qualifiedName.should.equal('package' + path.sep + 'Class')
done()
- it 'should match it`s directory when filename is \`index\`', (done) ->
- file 'js', path.resolve('src/package/index.coffee'), path.resolve('src'), {processors: processors.js}, (err, instance) ->
- instance.qualifiedName.should.equal('package')
+ it 'should prepend it`s directory when a root filename is \`index\`', (done) ->
+ file 'js', path.resolve('src/index.coffee'), path.resolve('src'), {processors: processors.js}, (err, instance) ->
+ instance.qualifiedName.should.equal('src/index')
done()
describe '"compile" property', ->
it 'should be true for compileable file types', (done) ->
@@ -0,0 +1,11 @@
+exports.build = {
+ "css": {
+ "sources": ["src/stylus"],
+ "targets": [
+ {
+ "input": "src/stylus",
+ "output": "output/css"
+ }
+ ]
+ }
+}
@@ -0,0 +1,4 @@
+@import 'three'
+
+body
+ colour: RED
@@ -0,0 +1 @@
+RED = '#ffffff'
@@ -0,0 +1,4 @@
+@import 'three'
+
+body
+ colour: RED
@@ -1,3 +0,0 @@
-p {
- padding: 0;
-}
@@ -1,2 +1 @@
-@import "two.css";
@import "three.css";
@@ -1,5 +1,3 @@
-@import "four/four.css";
-
body {
padding: 0;
}
@@ -1,3 +1,5 @@
+@import 'three.css';
+
html {
margin: 0;
}
View
@@ -57,7 +57,7 @@ describe 'target', ->
instance.options.concat.should.be.true
done()
- describe 'parsing sources', ->
+ describe 'parsing js sources', ->
before (done) ->
@tgt = null
@src = new Source('js', ['src'], {processors:processors.js})
@@ -149,6 +149,25 @@ describe 'target', ->
@tgt.sources.should.have.length(3)
done()
+ describe 'parsing css sources', ->
+ before (done) ->
+ @tgt = null
+ @src = new Source('css', ['src-css'], {processors:processors.css})
+ @src.parse (err) =>
+ target 'css', {input: 'src-css', output: 'temp', source: @src, processors: processors.css}, (err, instance) =>
+ @tgt = instance
+ done()
+ describe 'with a directory of 2 files and 1 shared dependency', ->
+ it 'should increase \'sources\' by 2', (done) ->
+ @tgt.input = path.resolve('src-css')
+ @tgt.options.concat = true
+ @tgt.isDir = true
+ @tgt._parse (err) =>
+ @tgt.sources.should.have.length(2)
+ done()
+ it 'should add the shared dependency to both files', ->
+ @tgt.sources[0].dependencies.should.eql(@tgt.sources[1].dependencies)
+
describe 'outputing sources', ->
before (done) ->
@tgt = null

0 comments on commit 9f47c83

Please sign in to comment.