From b8f976df7ecda182ede0c810f7d763b5dc0d0a40 Mon Sep 17 00:00:00 2001 From: Nathan Reid Date: Tue, 10 Nov 2015 22:34:49 -0600 Subject: [PATCH 1/4] Fixed Windows issues: broken class naming (_absolute_path_to_file instead of _relative_path_to_file) and broken path resolution ("composes" didn't work) --- src/file-system-loader.js | 11 ++++++++--- src/index.js | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/file-system-loader.js b/src/file-system-loader.js index 2a3f47d..5369c49 100644 --- a/src/file-system-loader.js +++ b/src/file-system-loader.js @@ -26,6 +26,12 @@ 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 ) { @@ -33,8 +39,7 @@ 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 ) + fileRelativePath = path.resolve(path.isAbsolute(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] !== '/') { @@ -49,7 +54,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 diff --git a/src/index.js b/src/index.js index 92795bd..418409d 100644 --- a/src/index.js +++ b/src/index.js @@ -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 } } ) From 04576456cb4fdd50b3257e109873ccc42830b918 Mon Sep 17 00:00:00 2001 From: Nathan Reid Date: Tue, 10 Nov 2015 22:39:47 -0600 Subject: [PATCH 2/4] All tests except for "test-cases should compose node module" are working on Windows and Mac. --- test/test-cases.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/test-cases.js b/test/test-cases.js index c6adcff..dd238fa 100644 --- a/test/test-cases.js +++ b/test/test-cases.js @@ -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 ) } ); @@ -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 ) From 82476702e7fdf4f4c365cb9b1834264889174bf2 Mon Sep 17 00:00:00 2001 From: Nathan Reid Date: Tue, 10 Nov 2015 22:41:38 -0600 Subject: [PATCH 3/4] updated expected css & tokens for test "should compose node module" to better reflect the path to the imported style --- test/test-cases/compose-node-module/expected.css | 2 +- test/test-cases/compose-node-module/expected.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test-cases/compose-node-module/expected.css b/test/test-cases/compose-node-module/expected.css index 0667b94..f304ae0 100644 --- a/test/test-cases/compose-node-module/expected.css +++ b/test/test-cases/compose-node-module/expected.css @@ -1,4 +1,4 @@ -._compose_node_module_cool_styles_foo__example { +._node_modules_cool_styles_foo__example { color: #F00; } ._compose_node_module_source__foo { diff --git a/test/test-cases/compose-node-module/expected.json b/test/test-cases/compose-node-module/expected.json index a57448c..ce71efb 100644 --- a/test/test-cases/compose-node-module/expected.json +++ b/test/test-cases/compose-node-module/expected.json @@ -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" } From 2ef5e570f703c89ed8b73a9b3126b9ad6129eddf Mon Sep 17 00:00:00 2001 From: Nathan Reid Date: Tue, 10 Nov 2015 23:03:31 -0600 Subject: [PATCH 4/4] replace path.isAbsolute with sindresorhus/path-is-absolute ponyfill for old versions of Node --- package.json | 1 + src/file-system-loader.js | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 2443f9a..10060c7 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/file-system-loader.js b/src/file-system-loader.js index 5369c49..1076ca5 100644 --- a/src/file-system-loader.js +++ b/src/file-system-loader.js @@ -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 @@ -39,7 +40,7 @@ export default class FileSystemLoader { trace = _trace || String.fromCharCode( this.importNr++ ) return new Promise( ( resolve, reject ) => { let relativeDir = path.dirname( relativeTo ), - fileRelativePath = path.resolve(path.isAbsolute(relativeDir) ? relativeDir : 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] !== '/') {