Skip to content

Commit 31dc01a

Browse files
committedJun 11, 2015
Merge pull request #8 from joshwnj/dedupe
Simple caching to prevent duplicate styles
2 parents b61a4f8 + a4fe796 commit 31dc01a

File tree

9 files changed

+69
-0
lines changed

9 files changed

+69
-0
lines changed
 

‎src/file-system-loader.js

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default class FileSystemLoader {
2525
this.sources = {}
2626
this.importNr = 0
2727
this.core = new Core(plugins)
28+
this.tokensByFile = {};
2829
}
2930

3031
fetch( _newPath, relativeTo, _trace ) {
@@ -35,11 +36,15 @@ export default class FileSystemLoader {
3536
rootRelativePath = path.resolve( relativeDir, newPath ),
3637
fileRelativePath = path.resolve( path.join( this.root, relativeDir ), newPath )
3738

39+
const tokens = this.tokensByFile[fileRelativePath]
40+
if (tokens) { return resolve(tokens) }
41+
3842
fs.readFile( fileRelativePath, "utf-8", ( err, source ) => {
3943
if ( err ) reject( err )
4044
this.core.load( source, rootRelativePath, trace, this.fetch.bind( this ) )
4145
.then( ( { injectableSource, exportTokens } ) => {
4246
this.sources[trace] = injectableSource
47+
this.tokensByFile[fileRelativePath] = exportTokens
4348
resolve( exportTokens )
4449
}, reject )
4550
} )

‎test/test-cases.js

+21
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,24 @@ Object.keys( pipelines ).forEach( dirname => {
3232
} );
3333
} );
3434
} )
35+
36+
// special case for testing multiple sources
37+
describe( 'multiple sources', () => {
38+
let testDir = path.join( __dirname, 'test-cases' )
39+
let testCase = 'multiple-sources';
40+
let dirname = 'test-cases';
41+
if ( fs.existsSync( path.join( testDir, testCase, "source1.css" ) ) ) {
42+
it( "should " + testCase.replace( /-/g, " " ), done => {
43+
let expected = normalize( fs.readFileSync( path.join( testDir, testCase, "expected.css" ), "utf-8" ) )
44+
let loader = new FileSystemLoader( testDir, pipelines[dirname] )
45+
let expectedTokens = JSON.parse( fs.readFileSync( path.join( testDir, testCase, "expected.json" ), "utf-8" ) )
46+
loader.fetch( `${testCase}/source1.css`, "/" ).then( tokens1 => {
47+
loader.fetch( `${testCase}/source2.css`, "/" ).then( tokens2 => {
48+
assert.equal( loader.finalSource, expected )
49+
const tokens = Object.assign({}, tokens1, tokens2);
50+
assert.equal( JSON.stringify( tokens ), JSON.stringify( expectedTokens ) )
51+
} ).then( done, done )
52+
})
53+
} );
54+
}
55+
} );
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.b {
2+
composes: d from "./d.css";
3+
color: #bbb;
4+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.c {
2+
color: #ccc;
3+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.d {
2+
color: #ddd;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
._multiple_sources_d__d {
3+
color: #ddd;
4+
}
5+
6+
._multiple_sources_b__b {
7+
color: #bbb;
8+
}
9+
10+
._multiple_sources_c__c {
11+
color: #ccc;
12+
}
13+
14+
._multiple_sources_source1__a {
15+
color: #aaa;
16+
}
17+
18+
._multiple_sources_source2__foo {
19+
color: #f00;
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"a": "_multiple_sources_source1__a _multiple_sources_b__b _multiple_sources_d__d _multiple_sources_c__c",
3+
"foo": "_multiple_sources_source2__foo _multiple_sources_b__b _multiple_sources_d__d"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.a {
2+
composes: b from "./b.css";
3+
composes: c from "./c.css";
4+
color: #aaa;
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.foo {
2+
composes: b from "./b.css";
3+
color: #f00;
4+
}

0 commit comments

Comments
 (0)