-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support @compose in CSS files directly
- Loading branch information
1 parent
fac3c3b
commit 402d836
Showing
18 changed files
with
190 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const postcss = require('postcss'); | ||
const Filter = require('broccoli-persistent-filter'); | ||
|
||
CSSStripCompose.prototype = Object.create(Filter.prototype); | ||
CSSStripCompose.prototype.constructor = CSSStripCompose; | ||
CSSStripCompose.prototype.extensions = ['css']; | ||
CSSStripCompose.prototype.targetExtension = 'css'; | ||
|
||
function CSSStripCompose(inputTree, options = {}) { | ||
if (!(this instanceof CSSStripCompose)) { | ||
return new CSSStripCompose(inputTree, options); | ||
} | ||
|
||
Filter.call(this, inputTree, options); | ||
} | ||
|
||
CSSStripCompose.prototype.processString = function(content) { | ||
return removeComposesDecls(content); | ||
}; | ||
|
||
function removeComposesDecls(content) { | ||
let ast = postcss.parse(content); | ||
|
||
ast.walkAtRules((node) => { | ||
if (node.name === 'composer') { | ||
node.remove(); | ||
} | ||
}); | ||
|
||
return ast.toResult().css; | ||
} | ||
|
||
module.exports = CSSStripCompose; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
@composer { | ||
@test-1 { | ||
composes: 'class-a', 'class-b', 'class-c'; | ||
} | ||
|
||
@test-2 { | ||
composes: 'class-d','class-e'; | ||
} | ||
} | ||
|
||
.class-a { | ||
background: red; | ||
} | ||
|
||
.class-b { | ||
background: green; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.class-a { | ||
background: red; | ||
} | ||
|
||
.class-b { | ||
background: green; | ||
} |
13 changes: 13 additions & 0 deletions
13
node-test/fixtures/css-clean-plugin/test-2/input/app-2.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
.class-e { | ||
font-size: 12px; | ||
} | ||
|
||
@composer { | ||
@test-2 { | ||
composes: 'class-d','class-e'; | ||
} | ||
} | ||
|
||
.other-styles { | ||
color: blue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
@composer { | ||
@test-1 { | ||
composes: 'class-a', 'class-b', 'class-c'; | ||
} | ||
} | ||
|
||
.class-a { | ||
background: red; | ||
} | ||
|
||
.class-b { | ||
background: green; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.class-e { | ||
font-size: 12px; | ||
} | ||
|
||
.other-styles { | ||
color: blue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.class-a { | ||
background: red; | ||
} | ||
|
||
.class-b { | ||
background: green; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,4 @@ | |
@test-2 { | ||
composes: 'class-d','class-e'; | ||
} | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const broccoli = require('broccoli'); | ||
const plugin = require('../../lib/css-plugin/clean-composer'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const filesPath = 'node-test/fixtures/css-clean-plugin'; | ||
const outputFile = 'some/file/tmp.js'; | ||
|
||
module.exports = { | ||
transform(treePath) { | ||
let tree = plugin(`${filesPath}/${treePath}/input`, { outputFile }); | ||
let builder = new broccoli.Builder(tree); | ||
|
||
return builder.build().then(() => { | ||
return filesMap(builder.outputPath); | ||
}); | ||
}, | ||
getExpectedResult(path) { | ||
let fullPath = `${filesPath}/${path}/output`; | ||
return filesMap(fullPath); | ||
} | ||
} | ||
|
||
function filesMap(folder) { | ||
return fs.readdirSync(folder).reduce((hash, file) => { | ||
hash[file] = fs.readFileSync(path.join(folder, file), 'utf8'); | ||
return hash; | ||
}, {}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const { transform, getExpectedResult } = require('../../helpers/clean-compose-css-file'); | ||
const should = require('should'); | ||
|
||
describe('css plugin', function() { | ||
it('works with a single file', function() { | ||
// Make sure it strips out any @compose declarations. | ||
return transform('test-1').then((result) => { | ||
let expected = getExpectedResult('test-1'); | ||
assertKeys(expected, result); | ||
}); | ||
}); | ||
|
||
it('works with multiple files', function() { | ||
// Make sure it strips out any @compose declarations. | ||
return transform('test-2').then((result) => { | ||
let expected = getExpectedResult('test-2'); | ||
assertKeys(expected, result); | ||
}); | ||
}); | ||
}); | ||
|
||
function assertKeys(expectedHash, resultHash) { | ||
let expectedKeys = Object.keys(expectedHash); | ||
let resultKeys = Object.keys(resultHash); | ||
|
||
// Assert the same keys | ||
expectedKeys.should.deepEqual(resultKeys); | ||
|
||
// Assert each of the values are the same | ||
expectedKeys.forEach((key) => { | ||
expectedHash[key].should.equal(resultHash[key]); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,4 @@ describe('css plugin', function() { | |
result.should.equal(expected); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
@composer { | ||
@testing-basic { | ||
composes: 'tb-1', 'tb-2'; | ||
} | ||
|
||
@testing-basic__open { | ||
composes: 'tb-3', 'tb-4'; | ||
} | ||
|
||
@testing-basic__closed { | ||
composes: 'tb-5'; | ||
} | ||
|
||
@testing-basic__inner { | ||
composes: 'tb-6', 'tb-7', 'tb-8'; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.