@@ -16,24 +16,25 @@ import {
16
16
} from "../src/importing" ;
17
17
18
18
const FIXTURES = path . resolve ( __dirname , ".." , ".." , "test" , "fixtures" ) ;
19
- const FSI_FIXTURES = path . resolve ( FIXTURES , "filesystemImporter" ) ;
20
- const ALIAS_FIXTURES = path . resolve ( FIXTURES , "pathAliasImporter" ) ;
19
+ const FSI_FIXTURES = path . join ( FIXTURES , "filesystemImporter" ) ;
20
+ const ALIAS_FIXTURES = path . join ( FIXTURES , "pathAliasImporter" ) ;
21
+ const NODE_MODULE_FIXTURES = path . join ( FIXTURES , "nodeModuleImporter" ) ;
21
22
22
- function getConfiguration ( options ?: Options ) : ResolvedConfiguration {
23
- return resolveConfiguration ( options , { rootDir : path . join ( FSI_FIXTURES ) } ) ;
23
+ function getConfiguration ( rootDir : string , options ?: Options ) : ResolvedConfiguration {
24
+ return resolveConfiguration ( options , { rootDir } ) ;
24
25
}
25
26
26
27
function testFSImporter ( name : string , importer : Importer ) {
27
28
describe ( name , ( ) => {
28
29
it ( "handles an absolute path without a from identifier" , ( ) => {
29
- let config = getConfiguration ( ) ;
30
+ let config = getConfiguration ( FSI_FIXTURES ) ;
30
31
let filename = path . resolve ( FSI_FIXTURES , "a.block.css" ) ;
31
32
let ident = importer . identifier ( null , filename , config ) ;
32
33
let resolvedFilename = importer . filesystemPath ( ident , config ) ;
33
34
assert . equal ( resolvedFilename , filename ) ;
34
35
} ) ;
35
36
it ( "handles an absolute path with a from identifier" , ( ) => {
36
- let config = getConfiguration ( ) ;
37
+ let config = getConfiguration ( FSI_FIXTURES ) ;
37
38
let relativeFilename = path . resolve ( FSI_FIXTURES , "a.block.css" ) ;
38
39
let filename = path . resolve ( FSI_FIXTURES , "b.block.css" ) ;
39
40
let relativeIdent = importer . identifier ( null , relativeFilename , config ) ;
@@ -42,29 +43,29 @@ function testFSImporter(name: string, importer: Importer) {
42
43
assert . equal ( resolvedFilename , filename ) ;
43
44
} ) ;
44
45
it ( "handles a relative path with a from identifier" , ( ) => {
45
- let options = getConfiguration ( ) ;
46
+ let options = getConfiguration ( FSI_FIXTURES ) ;
46
47
let filename = path . resolve ( FSI_FIXTURES , "a.block.css" ) ;
47
48
let ident = importer . identifier ( null , filename , options ) ;
48
49
let relativeIdent = importer . identifier ( ident , "b.block.css" , options ) ;
49
50
let resolvedFilename = importer . filesystemPath ( relativeIdent , options ) ;
50
51
assert . equal ( resolvedFilename , path . resolve ( FSI_FIXTURES , "b.block.css" ) ) ;
51
52
} ) ;
52
53
it ( "resolves a relative path without a from identifier against the root directory" , ( ) => {
53
- let options = getConfiguration ( ) ;
54
+ let options = getConfiguration ( FSI_FIXTURES ) ;
54
55
assert . equal ( options . rootDir , FSI_FIXTURES ) ;
55
56
let ident = importer . identifier ( null , "a.block.css" , options ) ;
56
57
let resolvedFilename = importer . filesystemPath ( ident , options ) ;
57
58
assert . equal ( resolvedFilename , path . resolve ( FSI_FIXTURES , "a.block.css" ) ) ;
58
59
} ) ;
59
60
it ( "inspects relative to the root directory" , ( ) => {
60
- let options = getConfiguration ( ) ;
61
+ let options = getConfiguration ( FSI_FIXTURES ) ;
61
62
let filename = path . resolve ( FSI_FIXTURES , "a.block.css" ) ;
62
63
let ident = importer . identifier ( null , filename , options ) ;
63
64
let inspected = importer . debugIdentifier ( ident , options ) ;
64
65
assert . equal ( inspected , "a.block.css" ) ;
65
66
} ) ;
66
67
it ( "decides syntax based on extension" , ( ) => {
67
- let options = getConfiguration ( ) ;
68
+ let options = getConfiguration ( FSI_FIXTURES ) ;
68
69
let cssIdent = importer . identifier ( null , "a.block.css" , options ) ;
69
70
assert . equal ( importer . syntax ( cssIdent , options ) , Syntax . css ) ;
70
71
let scssIdent = importer . identifier ( null , "scss.block.scss" , options ) ;
@@ -79,7 +80,7 @@ function testFSImporter(name: string, importer: Importer) {
79
80
assert . equal ( importer . syntax ( otherIdent , options ) , Syntax . other ) ;
80
81
} ) ;
81
82
it ( "imports a file" , async ( ) => {
82
- let options = getConfiguration ( ) ;
83
+ let options = getConfiguration ( FSI_FIXTURES ) ;
83
84
let ident = importer . identifier ( null , "a.block.css" , options ) ;
84
85
let importedFile = await importer . import ( ident , options ) ;
85
86
assert . deepEqual ( importedFile . contents , fs . readFileSync ( path . join ( FSI_FIXTURES , "a.block.css" ) , "utf-8" ) ) ;
@@ -94,6 +95,34 @@ testFSImporter("FilesystemImporter", defaultImporter);
94
95
testFSImporter ( "Default PathAliasImporter" , new NodeJsImporter ( { } ) ) ;
95
96
testFSImporter ( "Configured PathAliasImporter" , new NodeJsImporter ( { alias : ALIAS_FIXTURES } ) ) ;
96
97
98
+ describe ( "Node Module Importer" , ( ) => {
99
+ before ( function ( this : IHookCallbackContext ) {
100
+ this . importer = new NodeJsImporter ( ) ;
101
+ this . config = getConfiguration ( NODE_MODULE_FIXTURES ) ;
102
+ } ) ;
103
+ it ( "handles un-scoped packages' fully qualified paths" , function ( ) {
104
+ let filename = "package/blocks/styles.block.css" ;
105
+ let ident = this . importer . identifier ( null , filename , this . config ) ;
106
+ let resolvedFilename = this . importer . filesystemPath ( ident , this . config ) ;
107
+ assert . equal ( ident , path . join ( NODE_MODULE_FIXTURES , "node_modules" , filename ) ) ;
108
+ assert . equal ( resolvedFilename , path . join ( NODE_MODULE_FIXTURES , "node_modules" , filename ) ) ;
109
+ } ) ;
110
+ it ( "handles scoped packages' fully qualified paths" , function ( ) {
111
+ let filename = "@scoped/package/blocks/styles.block.css" ;
112
+ let ident = this . importer . identifier ( null , filename , this . config ) ;
113
+ let resolvedFilename = this . importer . filesystemPath ( ident , this . config ) ;
114
+ assert . equal ( ident , path . join ( NODE_MODULE_FIXTURES , "node_modules" , filename ) ) ;
115
+ assert . equal ( resolvedFilename , path . join ( NODE_MODULE_FIXTURES , "node_modules" , filename ) ) ;
116
+ } ) ;
117
+ it ( "gracefully degrades back to relative lookup for undiscoverable fully qualified paths" , function ( ) {
118
+ let filename = "@scoped/package/blocks/not-here.block.css" ;
119
+ let ident = this . importer . identifier ( null , filename , this . config ) ;
120
+ let resolvedFilename = this . importer . filesystemPath ( ident , this . config ) ;
121
+ assert . equal ( ident , path . join ( NODE_MODULE_FIXTURES , filename ) ) ;
122
+ assert . equal ( resolvedFilename , null ) ;
123
+ } ) ;
124
+ } ) ;
125
+
97
126
describe ( "PathAliasImporter" , ( ) => {
98
127
before ( function ( this : IHookCallbackContext ) {
99
128
let aliases = {
@@ -103,7 +132,7 @@ describe("PathAliasImporter", () => {
103
132
this . importer = new NodeJsImporter ( aliases ) ;
104
133
} ) ;
105
134
it ( "identifies relative to an alias" , function ( ) {
106
- let options = getConfiguration ( ) ;
135
+ let options = getConfiguration ( FSI_FIXTURES ) ;
107
136
let importer : Importer = this . importer ;
108
137
let ident = importer . identifier ( null , "pai/alias1.block.css" , options ) ;
109
138
let actualFilename = importer . filesystemPath ( ident , options ) ;
@@ -113,7 +142,7 @@ describe("PathAliasImporter", () => {
113
142
assert . equal ( "pai/alias1.block.css" , inspected ) ;
114
143
} ) ;
115
144
it ( "produces the same identifier via different aliases" , function ( ) {
116
- let options = getConfiguration ( ) ;
145
+ let options = getConfiguration ( FSI_FIXTURES ) ;
117
146
let importer : Importer = this . importer ;
118
147
let actualFilename = path . resolve ( ALIAS_FIXTURES , "alias_subdirectory" , "sub.block.css" ) ;
119
148
let ident1 = importer . identifier ( null , "pai/alias_subdirectory/sub.block.css" , options ) ;
@@ -129,7 +158,7 @@ describe("PathAliasImporter", () => {
129
158
assert . equal ( inspected2 , "sub/sub.block.css" ) ;
130
159
} ) ;
131
160
it ( "imports an aliased file" , async function ( ) {
132
- let options = getConfiguration ( ) ;
161
+ let options = getConfiguration ( FSI_FIXTURES ) ;
133
162
let importer : Importer = this . importer ;
134
163
let ident = importer . identifier ( null , "sub/sub.block.css" , options ) ;
135
164
let importedFile = await importer . import ( ident , options ) ;
0 commit comments