11import './guard' ;
22import hook from './hook' ;
33import postcss from 'postcss' ;
4- import { dirname , join , relative , resolve } from 'path' ;
4+ import { dirname , join , parse , relative , resolve , sep } from 'path' ;
55import { readFileSync } from 'fs' ;
66
77import ExtractImports from 'postcss-modules-extract-imports' ;
88import LocalByDefault from 'postcss-modules-local-by-default' ;
99import Scope from 'postcss-modules-scope' ;
1010import Parser from './parser' ;
1111
12+ const escapedSeparator = sep . replace ( / ( .) / g, '\\$1' ) ;
13+ const relativePathPattern = new RegExp ( `^.{1,2}$|^.{1,2}${ escapedSeparator } ` ) ;
14+
1215const defaultRoot = process . cwd ( ) ;
1316const tokensByFile = { } ;
1417let plugins = [ LocalByDefault , ExtractImports , Scope ] ;
1518let root = defaultRoot ;
19+ let importNr = 0 ;
20+
21+ /**
22+ * @param {string } pathname
23+ * @return {boolean }
24+ */
25+ function isModule ( pathname ) {
26+ const parsed = parse ( pathname ) ;
27+ return ! parsed . root && ! relativePathPattern . test ( parsed . dir ) ;
28+ }
1629
1730/**
1831 * @param {string } sourceString The file content
@@ -29,32 +42,38 @@ function load(sourceString, sourcePath, trace, pathFetcher) {
2942 return result . tokens ;
3043}
3144
32- hook ( filename => {
33- let importNr = 0 ;
45+ /**
46+ * @param {string } _newPath
47+ * @param {string } _relativeTo
48+ * @param {string } _trace
49+ * @return {object }
50+ */
51+ function fetch ( _newPath , _relativeTo , _trace ) {
52+ const newPath = _newPath . replace ( / ^ [ " ' ] | [ " ' ] $ / g, '' ) ;
53+ const trace = _trace || String . fromCharCode ( importNr ++ ) ;
3454
35- const fetch = ( _newPath , _relativeTo , _trace ) => {
36- const newPath = _newPath . replace ( / ^ [ " ' ] | [ " ' ] $ / g , '' ) ;
37- const trace = _trace || String . fromCharCode ( importNr ++ ) ;
55+ const relativeDir = dirname ( _relativeTo ) ;
56+ const rootRelativePath = resolve ( relativeDir , newPath ) ;
57+ let fileRelativePath = resolve ( join ( root , relativeDir ) , newPath ) ;
3858
39- const relativeDir = dirname ( _relativeTo ) ;
40- const rootRelativePath = resolve ( relativeDir , newPath ) ;
41- const fileRelativePath = resolve ( join ( root , relativeDir ) , newPath ) ;
59+ if ( isModule ( newPath ) ) {
60+ fileRelativePath = require . resolve ( newPath ) ;
61+ }
4262
43- const tokens = tokensByFile [ fileRelativePath ] ;
44- if ( tokens ) {
45- return tokens ;
46- }
63+ const tokens = tokensByFile [ fileRelativePath ] ;
64+ if ( tokens ) {
65+ return tokens ;
66+ }
4767
48- const source = readFileSync ( fileRelativePath , 'utf-8' ) ;
49- const exportTokens = load ( source , rootRelativePath , trace , fetch ) ;
68+ const source = readFileSync ( fileRelativePath , 'utf-8' ) ;
69+ const exportTokens = load ( source , rootRelativePath , trace , fetch ) ;
5070
51- tokensByFile [ fileRelativePath ] = exportTokens ;
71+ tokensByFile [ fileRelativePath ] = exportTokens ;
5272
53- return exportTokens ;
54- } ;
73+ return exportTokens ;
74+ }
5575
56- return fetch ( relative ( root , filename ) , '/' ) ;
57- } ) ;
76+ hook ( filename => fetch ( `.${ sep } ${ relative ( root , filename ) } ` , '/' ) ) ;
5877
5978/**
6079 * @param {object } opts
0 commit comments