Skip to content

Commit 20e11a5

Browse files
committed
possible solution
1 parent 02759ea commit 20e11a5

File tree

5 files changed

+18
-19
lines changed

5 files changed

+18
-19
lines changed

node_modules/cool-styles/bar.css

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/cool-styles/foo.css

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/file-system-loader.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@ import Core from './index.js'
22
import fs from 'fs'
33
import path from 'path'
44

5-
const escapedSeparator = path.sep.replace(/(.)/g, '\\$1');
6-
const relativePathPattern = new RegExp(`^.{1,2}$|^.{1,2}${escapedSeparator}`);
7-
8-
// Checks if the path to the external module given
9-
const isModule = pathname => {
10-
const parsed = path.parse( pathname );
11-
return !parsed.root && !relativePathPattern.test( parsed.dir );
12-
}
13-
145
// Sorts dependencies in the following way:
156
// AAA comes before AA and A
167
// AB comes after AA and before A
@@ -42,26 +33,26 @@ export default class FileSystemLoader {
4233
trace = _trace || String.fromCharCode( this.importNr++ )
4334
return new Promise( ( resolve, reject ) => {
4435
let relativeDir = path.dirname( relativeTo ),
45-
rootRelativePath = path.resolve( relativeDir, newPath ),
46-
fileRelativePath = path.resolve( path.join( this.root, relativeDir ), newPath )
36+
filename = path.resolve( this.root + relativeDir, newPath )
4737

4838
// if the path is not relative or absolute, try to resolve it in node_modules
49-
if (isModule(newPath)) {
39+
if ( newPath[0] !== '.' && newPath[0] !== path.sep ) {
5040
try {
51-
fileRelativePath = require.resolve( newPath );
41+
filename = require.resolve( newPath );
5242
}
5343
catch (e) {}
5444
}
5545

56-
const tokens = this.tokensByFile[fileRelativePath]
57-
if (tokens) { return resolve(tokens) }
46+
const rootRelativePath = path.sep + path.relative( this.root, filename )
47+
const tokens = this.tokensByFile[filename]
48+
if ( tokens ) { return resolve( tokens ) }
5849

59-
fs.readFile( fileRelativePath, "utf-8", ( err, source ) => {
50+
fs.readFile( filename, "utf-8", ( err, source ) => {
6051
if ( err ) reject( err )
6152
this.core.load( source, rootRelativePath, trace, this.fetch.bind( this ) )
6253
.then( ( { injectableSource, exportTokens } ) => {
6354
this.sources[trace] = injectableSource
64-
this.tokensByFile[fileRelativePath] = exportTokens
55+
this.tokensByFile[filename] = exportTokens
6556
resolve( exportTokens )
6657
}, reject )
6758
} )

test/test-cases/compose-node-module/expected.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
._compose_node_module_cool_styles_foo__example {
1+
:import("./bar.css") {
2+
i__imported_example_0: example;
3+
}
4+
._node_modules_cool_styles_foo__example {
25
color: #F00;
36
}
47
._compose_node_module_source__foo {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"foo": "_compose_node_module_source__foo _compose_node_module_cool_styles_foo__example"
2+
"foo": "_compose_node_module_source__foo _node_modules_cool_styles_foo__example _node_modules_cool_styles_foo__example i__imported_example_0"
33
}

0 commit comments

Comments
 (0)