Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jun 10, 2019
1 parent 11a8a79 commit 681f98c
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 92 deletions.
11 changes: 3 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,9 @@ export default function loader(content, map, meta) {
const callback = this.async();
const sourceMap = options.sourceMap || false;

if (sourceMap && map) {
// eslint-disable-next-line no-param-reassign
map = normalizeSourceMap(map);
} else {
// Some loaders (example `"postcss-loader": "1.x.x"`) always generates source map, we should remove it
// eslint-disable-next-line no-param-reassign
map = null;
}
// Some loaders (example `"postcss-loader": "1.x.x"`) always generates source map, we should remove it
// eslint-disable-next-line no-param-reassign
map = sourceMap && map ? normalizeSourceMap(map) : null;

// Reuse CSS AST (PostCSS AST e.g 'postcss-loader') to avoid reparsing
if (meta) {
Expand Down
57 changes: 2 additions & 55 deletions src/plugins/postcss-icss-parser.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import postcss from 'postcss';
import { extractICSS, replaceValueSymbols, replaceSymbols } from 'icss-utils';
import loaderUtils from 'loader-utils';
import cc from 'camelcase';

import { getImportItemCode } from '../utils';
import { getExportItemCode, getImportItemCode } from '../utils';

const pluginName = 'postcss-icss-parser';

Expand All @@ -17,56 +16,6 @@ function hasImportMessage(messages, url) {
);
}

function camelCase(str) {
return cc(str);
}

function dashesCamelCase(str) {
return str.replace(/-+(\w)/g, (match, firstLetter) =>
firstLetter.toUpperCase()
);
}

function getExportItem(key, value, localsStyle) {
let targetKey;
const items = [];

function addEntry(k) {
items.push(`\t${JSON.stringify(k)}: ${JSON.stringify(value)}`);
}

switch (localsStyle) {
case 'camelCase':
addEntry(key);
targetKey = camelCase(key);

if (targetKey !== key) {
addEntry(targetKey);
}
break;
case 'camelCaseOnly':
addEntry(camelCase(key));
break;
case 'dashes':
addEntry(key);
targetKey = dashesCamelCase(key);

if (targetKey !== key) {
addEntry(targetKey);
}
break;
case 'dashesOnly':
addEntry(dashesCamelCase(key));
break;
case 'asIs':
default:
addEntry(key);
break;
}

return items;
}

export default postcss.plugin(
pluginName,
(options = {}) =>
Expand Down Expand Up @@ -118,9 +67,7 @@ export default postcss.plugin(

result.messages.push({
pluginName,
export: getExportItem(name, value, options.exportLocalsStyle).join(
',\n'
),
export: getExportItemCode(name, value, options.exportLocalsStyle),
type: 'export',
item: { name, value },
});
Expand Down
17 changes: 5 additions & 12 deletions src/plugins/postcss-import-parser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import postcss from 'postcss';
import valueParser from 'postcss-value-parser';

import { getImportItemCode } from '../utils';
import { uniqWith, getImportItemCode } from '../utils';

const pluginName = 'postcss-import-parser';

Expand Down Expand Up @@ -86,22 +86,15 @@ function walkAtRules(css, result, filter) {
return items;
}

function uniq(array) {
return array.reduce(
(acc, d) =>
!acc.find((el) => el.url === d.url && el.media === d.media)
? [...acc, d]
: acc,
[]
);
}

export default postcss.plugin(
pluginName,
(options = {}) =>
function process(css, result) {
const traversed = walkAtRules(css, result, options.filter);
const paths = uniq(traversed);
const paths = uniqWith(
traversed,
(value, other) => value.url === other.url && value.media === other.media
);

paths.forEach((item) => {
result.messages.push({
Expand Down
20 changes: 3 additions & 17 deletions src/plugins/postcss-url-parser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import postcss from 'postcss';
import valueParser from 'postcss-value-parser';

import { getUrlHelperCode, getUrlItemCode } from '../utils';
import { uniqWith, flatten, getUrlHelperCode, getUrlItemCode } from '../utils';

const pluginName = 'postcss-url-parser';

Expand Down Expand Up @@ -104,29 +104,15 @@ function walkDeclsWithUrl(css, result, filter) {
return items;
}

function uniqWith(array, comparator) {
return array.reduce(
(acc, d) => (!acc.some((item) => comparator(d, item)) ? [...acc, d] : acc),
[]
);
}

function flatten(array) {
return array.reduce((a, b) => a.concat(b), []);
}

function isEqual(value, other) {
return value.url === other.url && value.needQuotes === other.needQuotes;
}

export default postcss.plugin(
pluginName,
(options = {}) =>
function process(css, result) {
const traversed = walkDeclsWithUrl(css, result, options.filter);
const paths = uniqWith(
flatten(traversed.map((item) => item.urls)),
isEqual
(value, other) =>
value.url === other.url && value.needQuotes === other.needQuotes
);

if (paths.length === 0) {
Expand Down
62 changes: 62 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@ import modulesValues from 'postcss-modules-values';
import localByDefault from 'postcss-modules-local-by-default';
import extractImports from 'postcss-modules-extract-imports';
import modulesScope from 'postcss-modules-scope';
import camelCase from 'camelcase';

function uniqWith(array, comparator) {
return array.reduce(
(acc, d) => (!acc.some((item) => comparator(d, item)) ? [...acc, d] : acc),
[]
);
}

function flatten(array) {
return array.reduce((a, b) => a.concat(b), []);
}

function dashesCamelCase(str) {
return str.replace(/-+(\w)/g, (match, firstLetter) =>
firstLetter.toUpperCase()
);
}

function getImportPrefix(loaderContext, importLoaders) {
if (importLoaders === false) {
Expand Down Expand Up @@ -236,6 +254,46 @@ function getModuleCode(result, sourceMap, onlyLocals) {
)}, ""${sourceMap && result.map ? `,${result.map}` : ''}]);\n`;
}

function getExportItemCode(key, value, localsStyle) {
let targetKey;
const items = [];

function addEntry(k) {
items.push(`\t${JSON.stringify(k)}: ${JSON.stringify(value)}`);
}

switch (localsStyle) {
case 'camelCase':
addEntry(key);
targetKey = camelCase(key);

if (targetKey !== key) {
addEntry(targetKey);
}
break;
case 'camelCaseOnly':
addEntry(camelCase(key));
break;
case 'dashes':
addEntry(key);
targetKey = dashesCamelCase(key);

if (targetKey !== key) {
addEntry(targetKey);
}
break;
case 'dashesOnly':
addEntry(dashesCamelCase(key));
break;
case 'asIs':
default:
addEntry(key);
break;
}

return items.join(',\n');
}

function getExportCode(exportItems, onlyLocals) {
if (exportItems.length === 0) {
return '';
Expand Down Expand Up @@ -317,6 +375,9 @@ function prepareCode(file, messages, loaderContext, importPrefix, onlyLocals) {
}

export {
uniqWith,
flatten,
dashesCamelCase,
getImportPrefix,
getLocalIdent,
getFilter,
Expand All @@ -328,6 +389,7 @@ export {
getApiCode,
getImportCode,
getModuleCode,
getExportItemCode,
getExportCode,
prepareCode,
};

0 comments on commit 681f98c

Please sign in to comment.