diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..a2c757f --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/node_modules/cool-styles/bar.css b/node_modules/cool-styles/bar.css new file mode 100644 index 0000000..9ed16a9 --- /dev/null +++ b/node_modules/cool-styles/bar.css @@ -0,0 +1,3 @@ +.example2 { + color: black; +} diff --git a/node_modules/cool-styles/foo.css b/node_modules/cool-styles/foo.css index 65f95db..12c0b50 100644 --- a/node_modules/cool-styles/foo.css +++ b/node_modules/cool-styles/foo.css @@ -1,3 +1,6 @@ .example { color: #F00; } +.example3 { + composes: example2 from "./bar.css"; +} diff --git a/src/file-system-loader.js b/src/file-system-loader.js index e26c2c0..b73e950 100644 --- a/src/file-system-loader.js +++ b/src/file-system-loader.js @@ -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 ) { @@ -40,9 +41,16 @@ 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) + // Record the resolved path since this might be inside node_modules + this.resolvedPaths[rootRelativePath] = path.dirname(fileRelativePath) } 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] diff --git a/test/test-cases/compose-node-module/expected.css b/test/test-cases/compose-node-module/expected.css index 0667b94..88350d8 100644 --- a/test/test-cases/compose-node-module/expected.css +++ b/test/test-cases/compose-node-module/expected.css @@ -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 { +} diff --git a/test/test-cases/compose-node-module/expected.json b/test/test-cases/compose-node-module/expected.json index a57448c..29dde54 100644 --- a/test/test-cases/compose-node-module/expected.json +++ b/test/test-cases/compose-node-module/expected.json @@ -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" } diff --git a/test/test-cases/compose-node-module/source.css b/test/test-cases/compose-node-module/source.css index 6477b1d..285c269 100644 --- a/test/test-cases/compose-node-module/source.css +++ b/test/test-cases/compose-node-module/source.css @@ -1,3 +1,6 @@ .foo { composes: example from "cool-styles/foo.css"; } +.bar { + composes: example3 from "cool-styles/foo.css"; +}