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

Fix class naming, path resolution, and broken tests on Windows #49

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"dependencies": {
"icss-replace-symbols": "1.0.2",
"path-is-absolute": "^1.0.0",
"postcss": "5.0.10",
"postcss-modules-values": "1.1.1",
"postcss-modules-extract-imports": "1.0.0",
Expand Down
12 changes: 9 additions & 3 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 pathIsAbsolute from 'path-is-absolute';

// Sorts dependencies in the following way:
// AAA comes before AA and A
Expand All @@ -26,15 +27,20 @@ export default class FileSystemLoader {
this.importNr = 0
this.core = new Core(plugins)
this.tokensByFile = {};

Core.scope.generateScopedName = function (exportedName, unsanitizedPath) {
let sanitizedPath = path.relative(root, unsanitizedPath).replace(/\.[^\.\/\\]+$/, '').replace(/[\W_]+/g, '_').replace(/^_|_$/g, '');
return `_${sanitizedPath}__${exportedName}`;
};

}

fetch( _newPath, relativeTo, _trace ) {
let newPath = _newPath.replace( /^["']|["']$/g, "" ),
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 )
fileRelativePath = path.resolve(pathIsAbsolute(relativeDir) ? relativeDir : path.join( this.root, relativeDir ), newPath )

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

fs.readFile( fileRelativePath, "utf-8", ( err, source ) => {
if ( err ) reject( err )
this.core.load( source, rootRelativePath, trace, this.fetch.bind( this ) )
this.core.load( source, fileRelativePath, trace, this.fetch.bind( this ) )
.then( ( { injectableSource, exportTokens } ) => {
this.sources[trace] = injectableSource
this.tokensByFile[fileRelativePath] = exportTokens
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class Core {
let parser = new Parser( pathFetcher, trace )

return postcss( this.plugins.concat( [parser.plugin] ) )
.process( sourceString, { from: "/" + sourcePath } )
.process( sourceString, { from: sourcePath } )
.then( result => {
return { injectableSource: result.css, exportTokens: parser.exportTokens }
} )
Expand Down
10 changes: 5 additions & 5 deletions test/test-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ Object.keys( pipelines ).forEach( dirname => {
let expected = normalize( fs.readFileSync( path.join( testDir, testCase, "expected.css" ), "utf-8" ) )
let loader = new FileSystemLoader( testDir, pipelines[dirname] )
let expectedTokens = JSON.parse( fs.readFileSync( path.join( testDir, testCase, "expected.json" ), "utf-8" ) )
loader.fetch( `${testCase}/source.css`, "/" ).then( tokens => {
assert.equal( loader.finalSource, expected )
loader.fetch( `${testCase}/source.css`, "./" ).then( tokens => {
assert.equal( normalize(loader.finalSource), expected )
assert.equal( JSON.stringify( tokens ), JSON.stringify( expectedTokens ) )
} ).then( done, done )
} );
Expand All @@ -43,9 +43,9 @@ describe( 'multiple sources', () => {
let expected = normalize( fs.readFileSync( path.join( testDir, testCase, "expected.css" ), "utf-8" ) )
let loader = new FileSystemLoader( testDir, pipelines[dirname] )
let expectedTokens = JSON.parse( fs.readFileSync( path.join( testDir, testCase, "expected.json" ), "utf-8" ) )
loader.fetch( `${testCase}/source1.css`, "/" ).then( tokens1 => {
loader.fetch( `${testCase}/source2.css`, "/" ).then( tokens2 => {
assert.equal( loader.finalSource, expected )
loader.fetch( `${testCase}/source1.css`, "./" ).then( tokens1 => {
loader.fetch( `${testCase}/source2.css`, "./" ).then( tokens2 => {
assert.equal( normalize(loader.finalSource), expected )
const tokens = Object.assign({}, tokens1, tokens2);
assert.equal( JSON.stringify( tokens ), JSON.stringify( expectedTokens ) )
} ).then( done, done )
Expand Down
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"
}