Skip to content

Fix lint errors and enforced consistency #19

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

Open
wants to merge 1 commit 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
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"jsx": true
},
"rules": {
"quotes": "single"
"quotes": [2, "single"],
"space-in-parens": [2, "always"]
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"lint": "eslint src",
"build": "babel --out-dir lib src",
"autotest": "chokidar src test -c 'npm test'",
"test": "mocha --compilers js:babel/register",
"test": "npm run lint && mocha --compilers js:babel/register",
"prepublish": "npm run build"
},
"repository": {
Expand Down
60 changes: 31 additions & 29 deletions src/file-system-loader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Core from './index.js'
import fs from 'fs'
import path from 'path'
import Core from './index.js';
import fs from 'fs';
import path from 'path';

// Sorts dependencies in the following way:
// AAA comes before AA and A
Expand All @@ -11,56 +11,58 @@ import path from 'path'
// - After all their dependencies
const traceKeySorter = ( a, b ) => {
if ( a.length < b.length ) {
return a < b.substring( 0, a.length ) ? -1 : 1
return a < b.substring( 0, a.length ) ? -1 : 1;
} else if ( a.length > b.length ) {
return a.substring( 0, b.length ) <= b ? -1 : 1
return a.substring( 0, b.length ) <= b ? -1 : 1;
} else {
return a < b ? -1 : 1
return a < b ? -1 : 1;
}
};

export default class FileSystemLoader {
constructor( root, plugins ) {
this.root = root
this.sources = {}
this.importNr = 0
this.core = new Core(plugins)
this.root = root;
this.sources = {};
this.importNr = 0;
this.core = new Core( plugins );
this.tokensByFile = {};
}

fetch( _newPath, relativeTo, _trace ) {
let newPath = _newPath.replace( /^["']|["']$/g, "" ),
trace = _trace || String.fromCharCode( this.importNr++ )
let newPath = _newPath.replace( /^["']|["']$/g, '' );
let 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 relativeDir = path.dirname( relativeTo );
let rootRelativePath = path.resolve( relativeDir, newPath );
let fileRelativePath = path.resolve( 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] !== '/') {
if ( newPath[0] !== '.' && newPath[0] !== '/' ) {
try {
fileRelativePath = require.resolve(newPath);
fileRelativePath = require.resolve( newPath );
}
catch ( e ) {
console.error( e );
}
catch (e) {}
}

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

fs.readFile( fileRelativePath, "utf-8", ( err, source ) => {
if ( err ) reject( err )
fs.readFile( fileRelativePath, 'utf-8', ( err, source ) => {
if ( err ) { reject( err ); }
this.core.load( source, rootRelativePath, trace, this.fetch.bind( this ) )
.then( ( { injectableSource, exportTokens } ) => {
this.sources[trace] = injectableSource
this.tokensByFile[fileRelativePath] = exportTokens
resolve( exportTokens )
}, reject )
} )
} )
this.sources[trace] = injectableSource;
this.tokensByFile[fileRelativePath] = exportTokens;
resolve( exportTokens );
}, reject );
} );
} );
}

get finalSource() {
return Object.keys( this.sources ).sort( traceKeySorter ).map( s => this.sources[s] )
.join( "" )
.join( '' );
}
}
29 changes: 14 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
import postcss from 'postcss'
import localByDefault from 'postcss-modules-local-by-default'
import extractImports from 'postcss-modules-extract-imports'
import scope from 'postcss-modules-scope'
import postcss from 'postcss';
import localByDefault from 'postcss-modules-local-by-default';
import extractImports from 'postcss-modules-extract-imports';
import scope from 'postcss-modules-scope';

import Parser from './parser'
import Parser from './parser';

export default class Core {
constructor( plugins ) {
this.plugins = plugins || Core.defaultPlugins
this.plugins = plugins || Core.defaultPlugins;
}

load( sourceString, sourcePath, trace, pathFetcher ) {
let parser = new Parser( pathFetcher, trace )
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 }
} )
return { injectableSource: result.css, exportTokens: parser.exportTokens };
} );
}
}


// These three plugins are aliased under this package for simplicity.
Core.localByDefault = localByDefault
Core.extractImports = extractImports
Core.scope = scope
Core.defaultPlugins = [localByDefault, extractImports, scope]
Core.localByDefault = localByDefault;
Core.extractImports = extractImports;
Core.scope = scope;
Core.defaultPlugins = [localByDefault, extractImports, scope];
68 changes: 35 additions & 33 deletions src/parser.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,68 @@
const importRegexp = /^:import\((.+)\)$/
const importRegexp = /^:import\((.+)\)$/;

export default class Parser {
constructor( pathFetcher, trace ) {
this.pathFetcher = pathFetcher
this.plugin = this.plugin.bind( this )
this.exportTokens = {}
this.translations = {}
this.trace = trace
this.pathFetcher = pathFetcher;
this.plugin = this.plugin.bind( this );
this.exportTokens = {};
this.translations = {};
this.trace = trace;
}

plugin( css, result ) {
plugin( css ) {
return Promise.all( this.fetchAllImports( css ) )
.then( _ => this.linkImportedSymbols( css ) )
.then( _ => this.extractExports( css ) )
.then( () => this.linkImportedSymbols( css ) )
.then( () => this.extractExports( css ) );
}

fetchAllImports( css ) {
let imports = []
let imports = [];
css.each( node => {
if ( node.type == "rule" && node.selector.match( importRegexp ) ) {
imports.push( this.fetchImport( node, css.source.input.from, imports.length ) )
if ( node.type === 'rule' && node.selector.match( importRegexp ) ) {
imports.push( this.fetchImport( node, css.source.input.from, imports.length ) );
}
} )
return imports
} );
return imports;
}

linkImportedSymbols( css ) {
css.eachDecl( decl => {
Object.keys(this.translations).forEach( translation => {
decl.value = decl.value.replace(translation, this.translations[translation])
} )
})
Object.keys( this.translations ).forEach( translation => {
decl.value = decl.value.replace( translation, this.translations[translation] );
} );
} );
}

extractExports( css ) {
css.each( node => {
if ( node.type == "rule" && node.selector == ":export" ) this.handleExport( node )
} )
if ( node.type === 'rule' && node.selector === ':export' ) {
this.handleExport( node );
}
} );
}

handleExport( exportNode ) {
exportNode.each( decl => {
if ( decl.type == 'decl' ) {
Object.keys(this.translations).forEach( translation => {
decl.value = decl.value.replace(translation, this.translations[translation])
} )
this.exportTokens[decl.prop] = decl.value
if ( decl.type === 'decl' ) {
Object.keys( this.translations ).forEach( translation => {
decl.value = decl.value.replace( translation, this.translations[translation] );
} );
this.exportTokens[decl.prop] = decl.value;
}
} )
exportNode.removeSelf()
} );
exportNode.removeSelf();
}

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