Skip to content

Commit 0a8218c

Browse files
committed
rolling back that API change
1 parent 003d7cd commit 0a8218c

File tree

3 files changed

+26
-55
lines changed

3 files changed

+26
-55
lines changed

src/file-system-loader.js

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
import Core from './index.js'
22
import fs from 'fs'
3-
import pathUtils from 'path'
4-
5-
class LoaderState {
6-
constructor() {
7-
this.loaderVars = {}
8-
this.parserVars = {}
9-
}
10-
}
3+
import path from 'path'
114

125
export default class FileSystemLoader {
136
constructor( root ) {
@@ -16,28 +9,20 @@ export default class FileSystemLoader {
169
this.seenPaths = new Set()
1710
}
1811

19-
fetch( path, relativeTo ) {
20-
let state = new LoaderState(), vars = state.loaderVars
21-
vars.path = path.replace( /^["']|["']$/g, "" )
12+
fetch( _newPath, relativeTo ) {
13+
let newPath = _newPath.replace( /^["']|["']$/g, "" )
2214
return new Promise( ( resolve, reject ) => {
23-
vars.rootRelativePath = pathUtils.resolve( pathUtils.dirname( relativeTo ), vars.path )
24-
vars.fileRelativePath = this.root + rootRelativePath
15+
let rootRelativePath = path.resolve( path.dirname( relativeTo ), newPath ),
16+
fileRelativePath = this.root + rootRelativePath
2517

2618
fs.readFile( fileRelativePath, "utf-8", ( err, source ) => {
27-
vars.source = source
28-
err ? reject( err ) : resolve( state )
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 )
2925
} )
3026
} )
3127
}
32-
33-
load( state ) {
34-
let { source, rootRelativePath } = state.loaderVars
35-
console.log("LOADING " + rootRelativePath)
36-
return Core.load( source, rootRelativePath, this )
37-
.then( state => {
38-
console.log("LOADED " + rootRelativePath)
39-
this.sources.push( state.parserVars.injectableSource )
40-
return state
41-
} )
42-
}
4328
}

src/parser.js

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default class Parser {
99
}
1010

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

@@ -20,13 +20,7 @@ export default class Parser {
2020
imports.push( this.fetchImport( node, css.source.input.from ) )
2121
}
2222
} )
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-
} )
23+
return imports
3024
}
3125

3226
extractExports( css ) {
@@ -38,8 +32,8 @@ export default class Parser {
3832
handleExport( exportNode ) {
3933
exportNode.each( decl => {
4034
if ( decl.type == 'decl' ) {
41-
Object.keys( this.translations ).forEach( translation => {
42-
decl.value = decl.value.replace( translation, this.translations[translation] )
35+
Object.keys(this.translations).forEach( translation => {
36+
decl.value = decl.value.replace(translation, this.translations[translation])
4337
} )
4438
this.exportTokens[decl.prop] = decl.value
4539
}
@@ -49,19 +43,13 @@ export default class Parser {
4943

5044
fetchImport( importNode, relativeTo ) {
5145
let file = importNode.selector.match( importRegexp )[1]
52-
console.log("FETCHING " + file)
53-
return this.pathFetcher.fetch( file, relativeTo )
54-
.then( state => {
55-
state.propResolver = exports => {
56-
console.log("RESOLVING " + file)
57-
importNode.each( decl => {
58-
if ( decl.type == 'decl' ) {
59-
this.translations[decl.value] = exports[decl.prop]
60-
}
61-
} )
62-
importNode.removeSelf()
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]
6350
}
64-
return state
65-
})
51+
} )
52+
importNode.removeSelf()
53+
}, err => console.log( err ) )
6654
}
6755
}

test/test-cases.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@ 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`, "/" )
21-
.then( state => loader.load( state ) )
22-
.then( tokens => {
23-
assert.equal( loader.sources.join( "" ), expected )
24-
assert.equal( JSON.stringify( tokens ), JSON.stringify( expectedTokens ) )
25-
} ).then( done, done )
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 )
2624
} );
2725
}
2826
} );

0 commit comments

Comments
 (0)