Skip to content

Commit 86feb2a

Browse files
committed
Merged with nathantreid fork
2 parents e985720 + 2ef5e57 commit 86feb2a

File tree

6 files changed

+18
-11
lines changed

6 files changed

+18
-11
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
},
99
"dependencies": {
1010
"icss-replace-symbols": "1.0.2",
11+
"path-is-absolute": "^1.0.0",
1112
"postcss": "5.2.9",
1213
"postcss-modules-values": "1.2.2",
1314
"postcss-modules-extract-imports": "1.0.1",

src/file-system-loader.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Core from './index.js'
22
import fs from 'fs'
33
import path from 'path'
4+
import pathIsAbsolute from 'path-is-absolute';
45

56
// Sorts dependencies in the following way:
67
// AAA comes before AA and A
@@ -27,15 +28,20 @@ export default class FileSystemLoader {
2728
this.importNr = 0
2829
this.core = new Core(plugins)
2930
this.tokensByFile = {};
31+
32+
Core.scope.generateScopedName = function (exportedName, unsanitizedPath) {
33+
let sanitizedPath = path.relative(root, unsanitizedPath).replace(/\.[^\.\/\\]+$/, '').replace(/[\W_]+/g, '_').replace(/^_|_$/g, '');
34+
return `_${sanitizedPath}__${exportedName}`;
35+
};
36+
3037
}
3138

3239
fetch( _newPath, relativeTo, _trace ) {
3340
let newPath = _newPath.replace( /^["']|["']$/g, "" ),
3441
trace = _trace || String.fromCharCode( this.importNr++ )
3542
return new Promise( ( resolve, reject ) => {
3643
let relativeDir = path.dirname( relativeTo ),
37-
rootRelativePath = path.resolve( relativeDir, newPath ),
38-
fileRelativePath = path.resolve( path.join( this.root, relativeDir ), newPath )
44+
fileRelativePath = path.resolve(pathIsAbsolute(relativeDir) ? relativeDir : path.join( this.root, relativeDir ), newPath )
3945

4046
// if the path is not relative or absolute, try to resolve it in node_modules
4147
if (newPath[0] !== '.' && newPath[0] !== '/') {
@@ -50,7 +56,7 @@ export default class FileSystemLoader {
5056

5157
fs.readFile( fileRelativePath, "utf-8", ( err, source ) => {
5258
if ( err ) reject( err )
53-
this.core.load( source, rootRelativePath, trace, this.fetch.bind( this ) )
59+
this.core.load( source, fileRelativePath, trace, this.fetch.bind( this ) )
5460
.then( ( { injectableSource, exportTokens } ) => {
5561
this.sources[fileRelativePath] = injectableSource
5662
this.traces[trace] = fileRelativePath

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default class Core {
1515
let parser = new Parser( pathFetcher, trace )
1616

1717
return postcss( this.plugins.concat( [parser.plugin] ) )
18-
.process( sourceString, { from: "/" + sourcePath } )
18+
.process( sourceString, { from: sourcePath } )
1919
.then( result => {
2020
return { injectableSource: result.css, exportTokens: parser.exportTokens }
2121
} )

test/test-cases.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ Object.keys( pipelines ).forEach( dirname => {
2323
let expected = normalize( fs.readFileSync( path.join( testDir, testCase, "expected.css" ), "utf-8" ) )
2424
let loader = new FileSystemLoader( testDir, pipelines[dirname] )
2525
let expectedTokens = JSON.parse( fs.readFileSync( path.join( testDir, testCase, "expected.json" ), "utf-8" ) )
26-
loader.fetch( `${testCase}/source.css`, "/" ).then( tokens => {
27-
assert.equal( loader.finalSource, expected )
26+
loader.fetch( `${testCase}/source.css`, "./" ).then( tokens => {
27+
assert.equal( normalize(loader.finalSource), expected )
2828
assert.equal( JSON.stringify( tokens ), JSON.stringify( expectedTokens ) )
2929
} ).then( done, done )
3030
} );
@@ -43,9 +43,9 @@ describe( 'multiple sources', () => {
4343
let expected = normalize( fs.readFileSync( path.join( testDir, testCase, "expected.css" ), "utf-8" ) )
4444
let loader = new FileSystemLoader( testDir, pipelines[dirname] )
4545
let expectedTokens = JSON.parse( fs.readFileSync( path.join( testDir, testCase, "expected.json" ), "utf-8" ) )
46-
loader.fetch( `${testCase}/source1.css`, "/" ).then( tokens1 => {
47-
loader.fetch( `${testCase}/source2.css`, "/" ).then( tokens2 => {
48-
assert.equal( loader.finalSource, expected )
46+
loader.fetch( `${testCase}/source1.css`, "./" ).then( tokens1 => {
47+
loader.fetch( `${testCase}/source2.css`, "./" ).then( tokens2 => {
48+
assert.equal( normalize(loader.finalSource), expected )
4949
const tokens = Object.assign({}, tokens1, tokens2);
5050
assert.equal( JSON.stringify( tokens ), JSON.stringify( expectedTokens ) )
5151
} ).then( done, done )

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
._compose_node_module_cool_styles_foo__example {
1+
._node_modules_cool_styles_foo__example {
22
color: #F00;
33
}
44
._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"
33
}

0 commit comments

Comments
 (0)