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

using path.parse() method to check if the provided path is relative o… #17

Open
wants to merge 4 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
4 changes: 4 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.

1 change: 1 addition & 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.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"directories": {
"test": "test"
},
"engines": {
"node": ">=0.12"
},
"dependencies": {
"postcss": "^4.1.11",
"postcss-modules-extract-imports": "^0.0.5",
Expand Down
16 changes: 8 additions & 8 deletions src/file-system-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,26 @@ export default class FileSystemLoader {
trace = _trace || String.fromCharCode( this.importNr++ )
return new Promise( ( resolve, reject ) => {
let relativeDir = path.dirname( relativeTo ),
rootRelativePath = path.resolve( relativeDir, newPath ),
fileRelativePath = path.resolve( path.join( this.root, relativeDir ), newPath )
filename = path.resolve( this.root + relativeDir, newPath )

// if the path is not relative or absolute, try to resolve it in node_modules
if (newPath[0] !== '.' && newPath[0] !== '/') {
if ( newPath[0] !== '.' && newPath[0] !== path.sep ) {
try {
fileRelativePath = require.resolve(newPath);
filename = require.resolve( newPath );
}
catch (e) {}
}

const tokens = this.tokensByFile[fileRelativePath]
if (tokens) { return resolve(tokens) }
const rootRelativePath = path.sep + path.relative( this.root, filename )
const tokens = this.tokensByFile[filename]
if ( tokens ) { return resolve( tokens ) }

fs.readFile( fileRelativePath, "utf-8", ( err, source ) => {
fs.readFile( filename, "utf-8", ( err, source ) => {
if ( err ) reject( err )
this.core.load( source, rootRelativePath, trace, this.fetch.bind( this ) )
.then( ( { injectableSource, exportTokens } ) => {
this.sources[trace] = injectableSource
this.tokensByFile[fileRelativePath] = exportTokens
this.tokensByFile[filename] = exportTokens
resolve( exportTokens )
}, reject )
} )
Expand Down
5 changes: 4 additions & 1 deletion test/test-cases/compose-node-module/expected.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
._compose_node_module_cool_styles_foo__example {
:import("./bar.css") {
i__imported_example_0: 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 _node_modules_cool_styles_foo__example i__imported_example_0"
}