Skip to content

Commit cf698b7

Browse files
committed
OH GOD non-deterministic order-of-execution-dependant side-effects ARE THE WORST
1 parent 381e4d1 commit cf698b7

File tree

10 files changed

+75
-23
lines changed

10 files changed

+75
-23
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"dependencies": {
1010
"postcss": "^4.1.11",
11-
"postcss-modules-extract-imports": "0.0.1",
11+
"postcss-modules-extract-imports": "0.0.2",
1212
"postcss-modules-local-by-default": "0.0.7",
1313
"postcss-modules-scope": "0.0.3"
1414
},
@@ -17,7 +17,7 @@
1717
"babel-eslint": "^3.1.9",
1818
"babelify": "^6.1.2",
1919
"chokidar-cli": "^0.2.1",
20-
"eslint": "^0.21.2",
20+
"eslint": "^0.22.1",
2121
"mocha": "^2.2.5"
2222
},
2323
"scripts": {

src/file-system-loader.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ export default class FileSystemLoader {
1616
fileRelativePath = this.root + rootRelativePath
1717

1818
fs.readFile( fileRelativePath, "utf-8", ( err, source ) => {
19-
if ( err ) reject( err )
20-
Core.load( source, rootRelativePath, this.fetch.bind( this ) )
21-
.then( ( { injectableSource, exportTokens } ) => {
22-
this.sources.push( injectableSource )
23-
resolve( exportTokens )
24-
}, reject )
19+
err ? reject( err ) : resolve( { source, rootRelativePath } )
2520
} )
2621
} )
2722
}
23+
24+
load( {source, rootRelativePath} ) {
25+
console.log("LOADING " + rootRelativePath)
26+
return Core.load( source, rootRelativePath, this )
27+
.then( ( { injectableSource, exportTokens } ) => {
28+
console.log("LOADED " + rootRelativePath)
29+
this.sources.push( injectableSource )
30+
return exportTokens
31+
} )
32+
}
2833
}

src/parser.js

+23-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default class Parser {
99
}
1010

1111
plugin( css, result ) {
12-
return Promise.all( this.fetchAllImports( css ) )
12+
return this.fetchAllImports( css )
1313
.then( _ => this.extractExports( css ) )
1414
}
1515

@@ -20,7 +20,13 @@ export default class Parser {
2020
imports.push( this.fetchImport( node, css.source.input.from ) )
2121
}
2222
} )
23-
return imports
23+
return Promise.all( imports )
24+
.then( fetchedState => {
25+
return Promise.all( fetchedState.map(
26+
( {loaderData, propResolver} ) =>
27+
this.pathFetcher.load( loaderData ).then( propResolver )
28+
) )
29+
} )
2430
}
2531

2632
extractExports( css ) {
@@ -32,8 +38,8 @@ export default class Parser {
3238
handleExport( exportNode ) {
3339
exportNode.each( decl => {
3440
if ( decl.type == 'decl' ) {
35-
Object.keys(this.translations).forEach( translation => {
36-
decl.value = decl.value.replace(translation, this.translations[translation])
41+
Object.keys( this.translations ).forEach( translation => {
42+
decl.value = decl.value.replace( translation, this.translations[translation] )
3743
} )
3844
this.exportTokens[decl.prop] = decl.value
3945
}
@@ -43,13 +49,19 @@ export default class Parser {
4349

4450
fetchImport( importNode, relativeTo ) {
4551
let file = importNode.selector.match( importRegexp )[1]
46-
return this.pathFetcher( file, relativeTo ).then( exports => {
47-
importNode.each( decl => {
48-
if ( decl.type == 'decl' ) {
49-
this.translations[decl.value] = exports[decl.prop]
52+
console.log("FETCHING " + file)
53+
return this.pathFetcher.fetch( file, relativeTo )
54+
.then( loaderData => ({
55+
loaderData,
56+
propResolver: exports => {
57+
console.log("RESOLVING " + file)
58+
importNode.each( decl => {
59+
if ( decl.type == 'decl' ) {
60+
this.translations[decl.value] = exports[decl.prop]
61+
}
62+
} )
63+
importNode.removeSelf()
5064
}
51-
} )
52-
importNode.removeSelf()
53-
}, err => console.log( err ) )
65+
}) )
5466
}
5567
}

test/test-cases.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ describe( "test-cases", () => {
1717
let expected = normalize( fs.readFileSync( path.join( testDir, testCase, "expected.css" ), "utf-8" ) )
1818
let loader = new FileSystemLoader( testDir )
1919
let expectedTokens = JSON.parse( fs.readFileSync( path.join( testDir, testCase, "expected.json" ), "utf-8" ) )
20-
loader.fetch( `${testCase}/source.css`, "/" ).then( tokens => {
21-
assert.equal( loader.sources.join( "" ), expected )
22-
assert.equal( JSON.stringify( tokens ), JSON.stringify( expectedTokens ) )
23-
} ).then( done, done )
20+
loader.fetch( `${testCase}/source.css`, "/" )
21+
.then( fetched => loader.load( fetched ) )
22+
.then( tokens => {
23+
assert.equal( loader.sources.join( "" ), expected )
24+
assert.equal( JSON.stringify( tokens ), JSON.stringify( expectedTokens ) )
25+
} ).then( done, done )
2426
} );
2527
}
2628
} );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.b {
2+
extends: d from "./d.css";
3+
color: #bbb;
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.c {
2+
color: #ccc;
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,16 @@
1+
2+
._multiple_dependencies_d__d {
3+
color: #ddd;
4+
}
5+
6+
._multiple_dependencies_b__b {
7+
color: #bbb;
8+
}
9+
10+
._multiple_dependencies_c__c {
11+
color: #ccc;
12+
}
13+
14+
._multiple_dependencies_source__a {
15+
color: #aaa;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.a {
2+
extends: b from "./b.css";
3+
extends: c from "./c.css";
4+
color: #aaa;
5+
}
6+

0 commit comments

Comments
 (0)