33import Core from 'css-modules-loader-core'
44import * as fs from 'fs'
55import * as path from 'path'
6+ import * as util from 'util'
67import { Plugin } from "postcss" ;
78
89
910type Dictionary < T > = {
1011 [ key : string ] : T | undefined ;
1112} ;
1213
14+ const readFile = util . promisify ( fs . readFile ) ;
15+
16+
1317export default class FileSystemLoader {
1418 private root : string ;
1519 private sources : Dictionary < string > ;
@@ -18,55 +22,54 @@ export default class FileSystemLoader {
1822 public tokensByFile : Dictionary < Core . ExportTokens > ;
1923
2024 constructor ( root : string , plugins ?: Array < Plugin < any > > ) {
21- this . root = root
22- this . sources = { }
23- this . importNr = 0
24- this . core = new Core ( plugins )
25+ this . root = root ;
26+ this . sources = { } ;
27+ this . importNr = 0 ;
28+ this . core = new Core ( plugins ) ;
2529 this . tokensByFile = { } ;
2630 }
2731
28- public fetch ( _newPath : string , relativeTo : string , _trace ?: string , initialContents ?: string ) : Promise < Core . ExportTokens > {
29- let newPath = _newPath . replace ( / ^ [ " ' ] | [ " ' ] $ / g, "" ) ,
30- trace = _trace || String . fromCharCode ( this . importNr ++ )
31- return new Promise ( ( resolve , reject ) => {
32- let relativeDir = path . dirname ( relativeTo ) ,
33- rootRelativePath = path . resolve ( relativeDir , newPath ) ,
34- fileRelativePath = path . resolve ( path . join ( this . root , relativeDir ) , newPath )
32+ public async fetch ( _newPath : string , relativeTo : string , _trace ?: string , initialContents ?: string ) : Promise < Core . ExportTokens > {
33+ let newPath = _newPath . replace ( / ^ [ " ' ] | [ " ' ] $ / g, "" ) ;
34+ let trace = _trace || String . fromCharCode ( this . importNr ++ ) ;
3535
36- // if the path is not relative or absolute, try to resolve it in node_modules
37- if ( newPath [ 0 ] !== '.' && newPath [ 0 ] !== '/' ) {
38- try {
39- fileRelativePath = require . resolve ( newPath ) ;
40- }
41- catch ( e ) { }
36+ let relativeDir = path . dirname ( relativeTo ) ;
37+ let rootRelativePath = path . resolve ( relativeDir , newPath ) ;
38+ let fileRelativePath = path . resolve ( path . join ( this . root , relativeDir ) , newPath ) ;
39+
40+ // if the path is not relative or absolute, try to resolve it in node_modules
41+ if ( newPath [ 0 ] !== '.' && newPath [ 0 ] !== '/' ) {
42+ try {
43+ fileRelativePath = require . resolve ( newPath ) ;
44+ }
45+ catch ( e ) { }
46+ }
47+
48+ let source : string ;
49+
50+ if ( ! initialContents ) {
51+ const tokens = this . tokensByFile [ fileRelativePath ]
52+ if ( tokens ) {
53+ return tokens ;
4254 }
4355
44- if ( ! initialContents ) {
45- const tokens = this . tokensByFile [ fileRelativePath ]
46- if ( tokens ) { return resolve ( tokens ) }
56+ try {
57+ source = await readFile ( fileRelativePath , "utf-8" ) ;
58+ }
59+ catch ( error ) {
60+ if ( relativeTo && relativeTo !== '/' ) {
61+ return { } ;
62+ }
4763
48- fs . readFile ( fileRelativePath , "utf-8" , ( err , source ) => {
49- if ( err && relativeTo && relativeTo !== '/' ) {
50- resolve ( { } ) ;
51- } else if ( err && ( ! relativeTo || relativeTo === '/' ) ) {
52- reject ( err ) ;
53- } else {
54- this . core . load ( source , rootRelativePath , trace , this . fetch . bind ( this ) )
55- . then ( ( { injectableSource, exportTokens } ) => {
56- this . sources [ trace ] = injectableSource
57- this . tokensByFile [ fileRelativePath ] = exportTokens
58- resolve ( exportTokens )
59- } , reject )
60- }
61- } )
62- } else {
63- this . core . load ( initialContents , rootRelativePath , trace , this . fetch . bind ( this ) )
64- . then ( ( { injectableSource, exportTokens } ) => {
65- this . sources [ trace ] = injectableSource
66- this . tokensByFile [ fileRelativePath ] = exportTokens
67- resolve ( exportTokens )
68- } , reject )
64+ throw error ;
6965 }
70- } )
66+ } else {
67+ source = initialContents ;
68+ }
69+
70+ const { injectableSource, exportTokens } = await this . core . load ( source , rootRelativePath , trace , this . fetch . bind ( this ) ) ;
71+ this . sources [ trace ] = injectableSource ;
72+ this . tokensByFile [ fileRelativePath ] = exportTokens ;
73+ return exportTokens ;
7174 }
7275}
0 commit comments