Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more accurate way for resolving files in node_modules #89

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
"dependencies": {
"icss-replace-symbols": "1.0.2",
"postcss": "5.0.10",
"postcss-modules-values": "1.1.1",
"postcss-modules-extract-imports": "1.0.0",
"postcss-modules-local-by-default": "1.0.1",
"postcss-modules-scope": "1.0.0"
"postcss-modules-scope": "1.0.0",
"postcss-modules-values": "1.1.1",
"resolve": "^1.1.7"
},
"devDependencies": {
"babel": "5.8.29",
Expand Down
8 changes: 6 additions & 2 deletions src/file-system-loader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Core from './index.js'
import fs from 'fs'
import path from 'path'
import nodeResolve from 'resolve'

// Sorts dependencies in the following way:
// AAA comes before AA and A
Expand Down Expand Up @@ -34,12 +35,15 @@ export default class FileSystemLoader {
return new Promise( ( resolve, reject ) => {
let relativeDir = path.dirname( relativeTo ),
rootRelativePath = path.resolve( relativeDir, newPath ),
fileRelativePath = path.resolve( path.join( this.root, relativeDir ), newPath )
rootRelativeDir = path.join( this.root, relativeDir ),
fileRelativePath = path.resolve( rootRelativeDir, newPath )

// 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 = nodeResolve.sync(newPath, { basedir: rootRelativeDir });
// in this case we need to actualize rootRelativePath too
rootRelativePath = path.relative(this.root, fileRelativePath);
}
catch (e) {}
}
Expand Down
5 changes: 5 additions & 0 deletions test/test-cases/compose-local-node-module/expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
._compose_local_node_module_node_modules_cool_local_styles_foo__example {
color: #F00;
}
._compose_local_node_module_source__foo {
}
3 changes: 3 additions & 0 deletions test/test-cases/compose-local-node-module/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"foo": "_compose_local_node_module_source__foo _compose_local_node_module_node_modules_cool_local_styles_foo__example"
}

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

3 changes: 3 additions & 0 deletions test/test-cases/compose-local-node-module/source.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.foo {
composes: example from "cool-local-styles/foo.css";
}
2 changes: 1 addition & 1 deletion test/test-cases/compose-node-module/expected.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
._compose_node_module_cool_styles_foo__example {
._node_modules_cool_styles_foo__example {
color: #F00;
}
._compose_node_module_source__foo {
Expand Down
2 changes: 1 addition & 1 deletion test/test-cases/compose-node-module/expected.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"foo": "_compose_node_module_source__foo _compose_node_module_cool_styles_foo__example"
"foo": "_compose_node_module_source__foo _node_modules_cool_styles_foo__example"
}