Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Katochimoto committed Jan 14, 2017
1 parent e05c454 commit f3dd4ce
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 29 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "less-plugin-css-modules",
"version": "0.0.1",
"version": "0.0.2",
"description": "A less plugin for css modules",
"main": "less-plugin-css-modules.js",
"scripts": {
Expand Down Expand Up @@ -33,7 +33,6 @@
"postcss": "5.2.10",
"postcss-modules-local-by-default": "1.1.1",
"postcss-modules-scope": "1.0.2",
"string-hash": "1.1.1",
"yargs": "6.6.0"
},
"devDependencies": {
Expand Down
63 changes: 36 additions & 27 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
import yargs from 'yargs';
import stringHash from 'string-hash';
import genericNames from 'generic-names';
import postcss from 'postcss';
import PostcssModulesLocalByDefault from 'postcss-modules-local-by-default';
import PostcssModulesScope from 'postcss-modules-scope';

const OPTIONS = {
mode: {
describe: '',
example: 'lessc index.less --css-modules="--mode=global"',
choices: [ 'global', 'local', 'pure' ]
},
hashPrefix: {
describe: '',
example: 'lessc index.less --css-modules="--hashPrefix=project_name"',
type: 'string'
},
generateScopedName: {
describe: '',
example: 'lessc index.less --css-modules="--generateScopedName=[name]__[local]___[hash:base64:5]"',
type: 'string'
}
};

/**
* @class CSSModules
* @param {Object} [options]
* @param {string} [options.mode]
* @param {string} [options.hashPrefix]
* @param {string|function} [options.generateScopedName]
* @param {string|function} [options.generateScopedName='[name]__[local]___[hash:base64:5]']
*/
function CSSModules (options = {}) {
this.options = options;
Expand All @@ -29,12 +46,11 @@ CSSModules.prototype.printUsage = function () {
console.log('');
};

/**
* @param {string} options
*/
CSSModules.prototype.setOptions = function (options) {
this.options = yargs.option('mode', {
describe: '',
example: 'lessc index.less --css-modules="--mode=global"',
choices: [ 'global', 'local', 'pure' ]
}).parse(options);
this.options = yargs.options(OPTIONS).parse(options);
};

export default CSSModules;
Expand All @@ -50,6 +66,11 @@ function CSSProcessor (options = {}) {
this.options = options;
}

/**
* [process description]
* @param {string} css
* @returns {string}
*/
CSSProcessor.prototype.process = function (css) {
const processor = postcss();

Expand All @@ -59,20 +80,16 @@ CSSProcessor.prototype.process = function (css) {
}));
}

const typeGenerateScopedName = typeof this.options.generateScopedName;
const pattern = typeGenerateScopedName === 'string' ? this.options.generateScopedName : '[name]__[local]___[hash:base64:5]';
const scopedName = do {
if (this.options.generateScopedName) {
if (typeof this.options.generateScopedName === 'function') {
this.options.generateScopedName;

} else {
genericNames(this.options.generateScopedName, {
context: process.cwd(),
hashPrefix: this.options.hashPrefix
});
}

if (typeGenerateScopedName === 'function') {
this.options.generateScopedName;
} else {
generateScopedName;
genericNames(pattern, {
context: process.cwd(),
hashPrefix: this.options.hashPrefix
});
}
};

Expand All @@ -83,11 +100,3 @@ CSSProcessor.prototype.process = function (css) {
const result = processor.process(css);
return result.toString();
};

function generateScopedName (name, filename, css) {
const i = css.indexOf(`.${name}`);
const lineNumber = css.substr(0, i).split(/[\r\n]/).length;
const hash = stringHash(css).toString(36).substr(0, 5);

return `_${name}_${hash}_${lineNumber}`;
}

0 comments on commit f3dd4ce

Please sign in to comment.