1
1
import { ObjectDictionary } from "@opticss/util" ;
2
2
3
+ import * as debugGenerator from "debug" ;
3
4
import { existsSync , readFile , readFileSync } from "fs-extra" ;
4
5
import * as path from "path" ;
5
6
@@ -8,6 +9,8 @@ import { ResolvedConfiguration } from "../configuration";
8
9
9
10
import { FileIdentifier , ImportedFile , Importer } from "./Importer" ;
10
11
12
+ const debug = debugGenerator ( "css-blocks:importer" ) ;
13
+
11
14
const DEFAULT_MAIN = "blocks/index.block.css" ;
12
15
13
16
/**
@@ -48,27 +51,33 @@ export class NodeJsImporter implements Importer {
48
51
let fromDir = from ? path . dirname ( from ) : config . rootDir ;
49
52
let resolvedPath = path . resolve ( fromDir , importPath ) ;
50
53
if ( existsSync ( resolvedPath ) ) { return resolvedPath ; }
54
+ debug ( `No relative or absolute Block file discovered for ${ importPath } .` ) ;
51
55
52
56
// If not a real file, attempt to resolve to an aliased path instead.
53
57
let alias = this . aliases . find ( a => importPath . startsWith ( a . alias + path . sep ) ) ;
54
58
if ( alias ) {
55
59
return path . resolve ( alias . path , importPath . substring ( alias . alias . length + 1 ) ) ;
56
60
}
61
+ debug ( `No file path alias discovered for ${ importPath } .` ) ;
57
62
58
63
// If no alias found, test for a node_module resolution as a file path.
59
64
try {
60
65
return require . resolve ( importPath , { paths : [ config . rootDir ] } ) ;
61
- } catch ( err ) { }
66
+ } catch ( err ) {
67
+ debug ( `Could not resolve ${ importPath } as a file. Resolution failed with ${ err . message } .` ) ;
68
+ }
62
69
63
- // If no alias found, test for a node_module resolution as a package name.
70
+ // If no file found, test for a node_module resolution as a package name.
64
71
try {
65
72
const pjsonPath = require . resolve ( path . join ( importPath , "package.json" ) , { paths : [ config . rootDir ] } ) ;
66
73
const pjson = JSON . parse ( readFileSync ( pjsonPath , "utf-8" ) ) ;
67
74
const main : string | undefined = pjson [ "css-blocks" ] && pjson [ "css-blocks" ] . main ;
68
75
return path . resolve ( pjsonPath , ".." , main || DEFAULT_MAIN ) ;
69
- } catch ( err ) { console . log ( err ) ; }
76
+ } catch ( err ) {
77
+ debug ( `Could not resolve ${ importPath } as a module. Resolution failed with ${ err . message } .` ) ;
78
+ }
70
79
71
- // If no backup alias or node_module fount , return the previously calculated
80
+ // If no backup alias or node_module found , return the previously calculated
72
81
// absolute path where we expect it should be.
73
82
return resolvedPath ;
74
83
}
0 commit comments