Skip to content

Commit

Permalink
handle relative path in node_modules case, fix #161
Browse files Browse the repository at this point in the history
  • Loading branch information
Long Ho committed Sep 25, 2016
1 parent b27907f commit e85ae1e
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 3 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# http://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

[package.json]
indent_size = 2
3 changes: 3 additions & 0 deletions node_modules/cool-styles/bar.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions node_modules/cool-styles/foo.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions src/file-system-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export default class FileSystemLoader {
this.traces = {}
this.importNr = 0
this.core = new Core(plugins)
this.tokensByFile = {};
this.tokensByFile = {}
this.resolvedPaths = {}
}

fetch( _newPath, relativeTo, _trace ) {
Expand All @@ -40,16 +41,23 @@ export default class FileSystemLoader {
// if the path is not relative or absolute, try to resolve it in node_modules
if (newPath[0] !== '.' && newPath[0] !== '/') {
try {
fileRelativePath = require.resolve(newPath);
fileRelativePath = require.resolve(newPath)
}
catch (e) {}
} else {
// Relative path but might be relative to a node_modules module
if (this.resolvedPaths[relativeTo]) {
fileRelativePath = path.resolve(this.resolvedPaths[relativeTo], newPath)
}
}

const tokens = this.tokensByFile[fileRelativePath]
if (tokens) { return resolve(tokens) }

fs.readFile( fileRelativePath, "utf-8", ( err, source ) => {
if ( err ) reject( err )
// Record the resolved path since this might be inside node_modules
this.resolvedPaths[rootRelativePath] = path.dirname(fileRelativePath)
this.core.load( source, rootRelativePath, trace, this.fetch.bind( this ) )
.then( ( { injectableSource, exportTokens } ) => {
this.sources[fileRelativePath] = injectableSource
Expand Down
7 changes: 7 additions & 0 deletions test/test-cases/compose-node-module/expected.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
._compose_node_module_cool_styles_bar__example2 {
color: black;
}
._compose_node_module_cool_styles_foo__example {
color: #F00;
}
._compose_node_module_cool_styles_foo__example3 {
}
._compose_node_module_source__foo {
}
._compose_node_module_source__bar {
}
3 changes: 2 additions & 1 deletion test/test-cases/compose-node-module/expected.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"foo": "_compose_node_module_source__foo _compose_node_module_cool_styles_foo__example"
"foo": "_compose_node_module_source__foo _compose_node_module_cool_styles_foo__example",
"bar": "_compose_node_module_source__bar _compose_node_module_cool_styles_foo__example3 _compose_node_module_cool_styles_bar__example2"
}
3 changes: 3 additions & 0 deletions test/test-cases/compose-node-module/source.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.foo {
composes: example from "cool-styles/foo.css";
}
.bar {
composes: example3 from "cool-styles/foo.css";
}

0 comments on commit e85ae1e

Please sign in to comment.