Skip to content

Commit eb9c09b

Browse files
author
Long Ho
committed
handle relative path in node_modules case, fix css-modules#161
1 parent b27907f commit eb9c09b

File tree

7 files changed

+43
-3
lines changed

7 files changed

+43
-3
lines changed

.editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_style = space
6+
indent_size = 2
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false
13+
14+
[package.json]
15+
indent_size = 2

node_modules/cool-styles/bar.css

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/cool-styles/foo.css

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/file-system-loader.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ export default class FileSystemLoader {
2626
this.traces = {}
2727
this.importNr = 0
2828
this.core = new Core(plugins)
29-
this.tokensByFile = {};
29+
this.tokensByFile = {}
30+
this.resolvedPaths = {}
3031
}
3132

3233
fetch( _newPath, relativeTo, _trace ) {
@@ -40,9 +41,16 @@ export default class FileSystemLoader {
4041
// if the path is not relative or absolute, try to resolve it in node_modules
4142
if (newPath[0] !== '.' && newPath[0] !== '/') {
4243
try {
43-
fileRelativePath = require.resolve(newPath);
44+
fileRelativePath = require.resolve(newPath)
45+
// Record the resolved path since this might be inside node_modules
46+
this.resolvedPaths[rootRelativePath] = path.dirname(fileRelativePath)
4447
}
4548
catch (e) {}
49+
} else {
50+
// Relative path but might be relative to a node_modules module
51+
if (this.resolvedPaths[relativeTo]) {
52+
fileRelativePath = path.resolve(this.resolvedPaths[relativeTo], newPath)
53+
}
4654
}
4755

4856
const tokens = this.tokensByFile[fileRelativePath]
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
._compose_node_module_cool_styles_bar__example2 {
2+
color: black;
3+
}
14
._compose_node_module_cool_styles_foo__example {
25
color: #F00;
36
}
7+
._compose_node_module_cool_styles_foo__example3 {
8+
}
49
._compose_node_module_source__foo {
510
}
11+
._compose_node_module_source__bar {
12+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"foo": "_compose_node_module_source__foo _compose_node_module_cool_styles_foo__example"
2+
"foo": "_compose_node_module_source__foo _compose_node_module_cool_styles_foo__example",
3+
"bar": "_compose_node_module_source__bar _compose_node_module_cool_styles_foo__example3 _compose_node_module_cool_styles_bar__example2"
34
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
.foo {
22
composes: example from "cool-styles/foo.css";
33
}
4+
.bar {
5+
composes: example3 from "cool-styles/foo.css";
6+
}

0 commit comments

Comments
 (0)