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

falling demo #25

Open
wants to merge 3 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.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
"dependencies": {
"postcss": "^4.1.11",
"postcss-modules-extract-imports": "^0.0.5",
"postcss-modules-local-by-default": "^0.0.9",
"postcss-modules-scope": "^0.0.8"
"postcss-modules-local-by-default": "^0.0.9"
},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should put the proper version of the scope plugin, when it will be updated.
css-modules/postcss-modules-scope#15

"devDependencies": {
"babel": "^5.5.4",
Expand All @@ -21,6 +20,8 @@
"mocha": "^2.2.5"
},
"scripts": {
"preinstall": "rm -rf node_modules/postcss-modules-scope",
"postinstall": "git clone https://github.com/sullenor/postcss-modules-scope.git node_modules/postcss-modules-scope && cd node_modules/postcss-modules-scope && git checkout absolute-paths && npm i",
"lint": "eslint src",
"build": "babel --out-dir lib src",
"autotest": "chokidar src test -c 'npm test'",
Expand Down
27 changes: 15 additions & 12 deletions src/file-system-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,34 @@ export default class FileSystemLoader {
this.tokensByFile = {};
}

fetch( _newPath, relativeTo, _trace ) {
fetch( _newPath, sourcePath, _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 )
let filename = path.resolve( path.dirname( sourcePath ), newPath );

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

const tokens = this.tokensByFile[fileRelativePath]
const tokens = this.tokensByFile[filename]
if (tokens) { return resolve(tokens) }

fs.readFile( fileRelativePath, "utf-8", ( err, source ) => {
if ( err ) reject( err )
this.core.load( source, rootRelativePath, trace, this.fetch.bind( this ) )
fs.readFile( filename, "utf-8", ( err, source ) => {
if ( err ) {
return void reject( err )
}

this.core.load( source, filename, 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
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,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
6 changes: 3 additions & 3 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ export default class Parser {
exportNode.removeSelf()
}

fetchImport( importNode, relativeTo, depNr ) {
fetchImport( importNode, sourcePath, depNr ) {
let file = importNode.selector.match( importRegexp )[1],
depTrace = this.trace + String.fromCharCode(depNr)
return this.pathFetcher( file, relativeTo, depTrace ).then( exports => {
return this.pathFetcher( file, sourcePath, depTrace ).then( exports => {
importNode.each( decl => {
if ( decl.type == 'decl' ) {
this.translations[decl.prop] = exports[decl.value]
}
} )
importNode.removeSelf()
}, err => console.log( err ) )
} )
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are handling error at the file-system-loader module, so no need to do it here.

}
}
22 changes: 17 additions & 5 deletions test/test-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import fs from "fs"
import path from "path"
import FileSystemLoader from "../src/file-system-loader"

import PostcssLocal from "postcss-modules-local-by-default"
import PostcssExtract from "postcss-modules-extract-imports"
import PostcssScope from "postcss-modules-scope"

let normalize = ( str ) => {
return str.replace( /\r\n?/g, "\n" );
}
Expand All @@ -14,16 +18,24 @@ const pipelines = {
"cssi": []
}

const getPlugins = rootDir => {
return [
PostcssLocal,
PostcssExtract,
new PostcssScope({rootDir: rootDir})
];
}

Object.keys( pipelines ).forEach( dirname => {
describe( dirname, () => {
let testDir = path.join( __dirname, dirname )
fs.readdirSync( testDir ).forEach( testCase => {
if ( fs.existsSync( path.join( testDir, testCase, "source.css" ) ) ) {
it( "should " + testCase.replace( /-/g, " " ), done => {
let expected = normalize( fs.readFileSync( path.join( testDir, testCase, "expected.css" ), "utf-8" ) )
let loader = new FileSystemLoader( testDir, pipelines[dirname] )
let loader = new FileSystemLoader( testDir, pipelines[dirname] || getPlugins(testDir) )
let expectedTokens = JSON.parse( fs.readFileSync( path.join( testDir, testCase, "expected.json" ), "utf-8" ) )
loader.fetch( `${testCase}/source.css`, "/" ).then( tokens => {
loader.fetch( path.join( testDir, testCase, "source.css" ) ).then( tokens => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

building absolute paths

assert.equal( loader.finalSource, expected )
assert.equal( JSON.stringify( tokens ), JSON.stringify( expectedTokens ) )
} ).then( done, done )
Expand All @@ -41,10 +53,10 @@ describe( 'multiple sources', () => {
if ( fs.existsSync( path.join( testDir, testCase, "source1.css" ) ) ) {
it( "should " + testCase.replace( /-/g, " " ), done => {
let expected = normalize( fs.readFileSync( path.join( testDir, testCase, "expected.css" ), "utf-8" ) )
let loader = new FileSystemLoader( testDir, pipelines[dirname] )
let loader = new FileSystemLoader( testDir, pipelines[dirname] || getPlugins(testDir) )
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 => {
loader.fetch( path.join( testDir, testCase, "source1.css" ) ).then( tokens1 => {
loader.fetch( path.join( testDir, testCase, "source2.css" ) ).then( tokens2 => {
assert.equal( loader.finalSource, expected )
const tokens = Object.assign({}, tokens1, tokens2);
assert.equal( JSON.stringify( tokens ), JSON.stringify( expectedTokens ) )
Expand Down
6 changes: 5 additions & 1 deletion test/test-cases/compose-node-module/expected.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
._compose_node_module_cool_styles_foo__example {
._node_modules_cool_styles_bar__example
{
background: #369;
}
._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_bar__example"
}