1
1
import './guard' ;
2
2
import hook from './hook' ;
3
3
import postcss from 'postcss' ;
4
- import { dirname , join , relative , resolve } from 'path' ;
4
+ import { dirname , join , parse , relative , resolve , sep } from 'path' ;
5
5
import { readFileSync } from 'fs' ;
6
6
7
7
import ExtractImports from 'postcss-modules-extract-imports' ;
8
8
import LocalByDefault from 'postcss-modules-local-by-default' ;
9
9
import Scope from 'postcss-modules-scope' ;
10
10
import Parser from './parser' ;
11
11
12
+ const escapedSeparator = sep . replace ( / ( .) / g, '\\$1' ) ;
13
+ const relativePathPattern = new RegExp ( `^.{1,2}$|^.{1,2}${ escapedSeparator } ` ) ;
14
+
12
15
const defaultRoot = process . cwd ( ) ;
13
16
const tokensByFile = { } ;
14
17
let plugins = [ LocalByDefault , ExtractImports , Scope ] ;
15
18
let 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
+ }
16
29
17
30
/**
18
31
* @param {string } sourceString The file content
@@ -29,32 +42,38 @@ function load(sourceString, sourcePath, trace, pathFetcher) {
29
42
return result . tokens ;
30
43
}
31
44
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 ++ ) ;
34
54
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 ) ;
38
58
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
+ }
42
62
43
- const tokens = tokensByFile [ fileRelativePath ] ;
44
- if ( tokens ) {
45
- return tokens ;
46
- }
63
+ const tokens = tokensByFile [ fileRelativePath ] ;
64
+ if ( tokens ) {
65
+ return tokens ;
66
+ }
47
67
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 ) ;
50
70
51
- tokensByFile [ fileRelativePath ] = exportTokens ;
71
+ tokensByFile [ fileRelativePath ] = exportTokens ;
52
72
53
- return exportTokens ;
54
- } ;
73
+ return exportTokens ;
74
+ }
55
75
56
- return fetch ( relative ( root , filename ) , '/' ) ;
57
- } ) ;
76
+ hook ( filename => fetch ( `.${ sep } ${ relative ( root , filename ) } ` , '/' ) ) ;
58
77
59
78
/**
60
79
* @param {object } opts
0 commit comments