Skip to content

Add LESS support #21

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

Merged
merged 7 commits into from
May 25, 2019
Merged
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
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/helpers/__tests__/fixtures/test.module.less
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@
},
"dependencies": {
"icss-utils": "^4.1.0",
"less": "^3.9.0",
"lodash": "^4.17.11",
"postcss": "^7.0.16",
"postcss-icss-selectors": "^2.0.3",
"sass": "^1.20.1"
},
"devDependencies": {
"@types/jest": "^24.0.13",
"@types/less": "^3.0.0",
"@types/lodash": "^4.14.132",
"@types/node": "^10.12.18",
"@types/sass": "^1.16.0",
Expand Down
74 changes: 58 additions & 16 deletions src/helpers/__tests__/__snapshots__/cssSnapshots.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`utils / cssSnapshots with file 'empty.module.less' createExports should create an exports file 1`] = `
"declare const classes: {

};
export default classes;
"
`;

exports[`utils / cssSnapshots with file 'empty.module.less' getClasses should return an object matching expected CSS 1`] = `Object {}`;

exports[`utils / cssSnapshots with file 'empty.module.scss' createExports should create an exports file 1`] = `
"declare const classes: {

Expand Down Expand Up @@ -36,6 +46,38 @@ Object {
}
`;

exports[`utils / cssSnapshots with file 'test.module.less' createExports should create an exports file 1`] = `
"declare const classes: {
'nested-class-parent': string;
'child-class': string;
'selector-blue': string;
'selector-green': string;
'selector-red': string;
'column-1': string;
'column-2': string;
'column-3': string;
'column-4': string;
'color-set': string;
};
export default classes;
"
`;

exports[`utils / cssSnapshots with file 'test.module.less' getClasses should return an object matching expected CSS 1`] = `
Object {
"child-class": "file__child-class---1mwoB",
"color-set": "file__color-set---9sHH_",
"column-1": "file__column-1---vHRb_",
"column-2": "file__column-2---28y1r",
"column-3": "file__column-3---1PsZw",
"column-4": "file__column-4---2qaaI",
"nested-class-parent": "file__nested-class-parent---_ft7G",
"selector-blue": "file__selector-blue---3mslq",
"selector-green": "file__selector-green---143xX",
"selector-red": "file__selector-red---Gckob",
}
`;

exports[`utils / cssSnapshots with file 'test.module.scss' createExports should create an exports file 1`] = `
"declare const classes: {
'local-class-inside-global': string;
Expand All @@ -61,21 +103,21 @@ export default classes;

exports[`utils / cssSnapshots with file 'test.module.scss' getClasses should return an object matching expected CSS 1`] = `
Object {
"child-class": "file__child-class---1mwoB",
"local-class": "file__local-class---3KegX",
"local-class-2": "file__local-class-2---2h6qz",
"local-class-inside-global": "file__local-class-inside-global---2xH_Y",
"local-class-inside-local": "file__local-class-inside-local---QdL6b",
"nested-class-parent": "file__nested-class-parent---_ft7G",
"nested-class-parent--extended": "file__nested-class-parent--extended---1642l",
"section-1": "file__section-1---2EiKX",
"section-2": "file__section-2---2f4aZ",
"section-3": "file__section-3---R_Ilj",
"section-4": "file__section-4---3EjYO",
"section-5": "file__section-5---1DSe8",
"section-6": "file__section-6---1RoVP",
"section-7": "file__section-7---l5yMj",
"section-8": "file__section-8---3FEWv",
"section-9": "file__section-9---1TFYE",
"child-class": "file__child-class---1QWYM",
"local-class": "file__local-class---3SW3k",
"local-class-2": "file__local-class-2----c5z7",
"local-class-inside-global": "file__local-class-inside-global---1T0um",
"local-class-inside-local": "file__local-class-inside-local---1Z9pB",
"nested-class-parent": "file__nested-class-parent---3qXdF",
"nested-class-parent--extended": "file__nested-class-parent--extended---qsVau",
"section-1": "file__section-1---1IHCS",
"section-2": "file__section-2---cLFhf",
"section-3": "file__section-3---1ldKa",
"section-4": "file__section-4---2u0CG",
"section-5": "file__section-5---1lAYL",
"section-6": "file__section-6---2YZ9I",
"section-7": "file__section-7---3w-OF",
"section-8": "file__section-8---3RB8g",
"section-9": "file__section-9---3_Mtj",
}
`;
10 changes: 7 additions & 3 deletions src/helpers/__tests__/cssSnapshots.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import { readFileSync } from 'fs';
import { IICSSExports } from 'icss-utils';
import { join } from 'path';
import { createExports, getClasses } from '../cssSnapshots';
import { createExports, getClasses, FileTypes } from '../cssSnapshots';

const testFileNames = [
'test.module.css',
'test.module.less',
'test.module.scss',
'empty.module.less',
'empty.module.scss',
];

describe('utils / cssSnapshots', () => {
testFileNames.map((filename) => {
testFileNames.forEach((filename) => {
let classes: IICSSExports;
const isLess = filename.includes('less');
const fileType = isLess ? FileTypes.less : FileTypes.css;
const testFile = readFileSync(
join(__dirname, 'fixtures', filename),
'utf8',
);

beforeAll(() => {
classes = getClasses(testFile);
classes = getClasses(testFile, fileType);
});

describe(`with file '${filename}'`, () => {
Expand Down
Empty file.
33 changes: 33 additions & 0 deletions src/helpers/__tests__/fixtures/test.module.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.nested-class-parent {
color: black;

.child-class {
font-size: 12px;
}
}

@selectors: blue, green, red;

each(@selectors, {
.selector-@{value} {
color: b;
}
});

each(range(4), {
.column-@{value} {
height: (@value * 50px);
}
})

.color-set () {
one: blue;
two: green;
three: red;
}

.color-set {
each(.color-set (), .(@v, @k, @i) {
@{k}-@{i}: @v;
});
}
23 changes: 19 additions & 4 deletions src/helpers/cssSnapshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as postcss from 'postcss';
import * as postcssIcssSelectors from 'postcss-icss-selectors';
import * as ts_module from 'typescript/lib/tsserverlibrary';
import * as sass from 'sass';
import * as less from 'less';
import { transformClasses } from './classTransforms';
import { Options } from '../options';

Expand All @@ -15,15 +16,29 @@ const flattenClassNames = (
currentValue: string[],
) => previousValue.concat(currentValue);

export const getClasses = (css: string, isLess: boolean = false) => {
export const enum FileTypes {
css = 'css',
sass = 'sass',
less = 'less',
}

export const getClasses = (
css: string,
fileType: FileTypes = FileTypes.css,
) => {
try {
let transformedCss: string;
if (isLess) {
transformedCss = '';
let transformedCss = '';

if (fileType === FileTypes.less) {
less.render(css, { asyncImport: true } as any, (err, output) => {
transformedCss = output.css.toString();
});
} else {
transformedCss = sass.renderSync({ data: css }).css.toString();
}

const processedCss = processor.process(transformedCss);

return extractICSS(processedCss.root).icssExports;
} catch (e) {
return {};
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"declaration": true,
"forceConsistentCasingInFileNames": true,
"lib": ["esnext"],
"lib": ["esnext", "dom"],
"module": "commonjs",
"moduleResolution": "node",
"noEmitOnError": true,
Expand Down
64 changes: 62 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,11 @@
dependencies:
"@types/jest-diff" "*"

"@types/less@^3.0.0":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@types/less/-/less-3.0.0.tgz#2b76e6a2fecf7108c1c2b701ad846b605d8f2c7e"
integrity sha512-AUO7jdGrDi7x+7w2vYHNCom3NRjkspKSvamYp013Jyd/VYxpm0xtCupRZCdSX2IPx/W9C81Cj7nPXqQUj6MlQQ==

"@types/lodash@^4.14.132":
version "4.14.132"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.132.tgz#8ce45ca8745ff2e004fac0de0ab46f61e390ffa0"
Expand Down Expand Up @@ -532,6 +537,11 @@ arrify@^1.0.1:
resolved "https://registry.yarnpkg.com/arrify/-/arrify-1.0.1.tgz#898508da2226f380df904728456849c1501a4b0d"
integrity sha1-iYUI2iIm84DfkEcoRWhJwVAaSw0=

asap@~2.0.3:
version "2.0.6"
resolved "https://registry.yarnpkg.com/asap/-/asap-2.0.6.tgz#e50347611d7e690943208bbdafebcbc2fb866d46"
integrity sha1-5QNHYR1+aQlDIIu9r+vLwvuGbUY=

asn1@~0.2.3:
version "0.2.4"
resolved "https://registry.yarnpkg.com/asn1/-/asn1-0.2.4.tgz#8d2475dfab553bb33e77b54e59e880bb8ce23136"
Expand Down Expand Up @@ -843,6 +853,11 @@ cliui@^4.0.0:
strip-ansi "^4.0.0"
wrap-ansi "^2.0.0"

clone@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/clone/-/clone-2.1.2.tgz#1b7f4b9f591f1e8f83670401600345a02887435f"
integrity sha1-G39Ln1kfHo+DZwQBYANFoCiHQ18=

co@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/co/-/co-4.6.0.tgz#6ea6bdf3d853ae54ccb8e47bfa0bf3f9031fb184"
Expand Down Expand Up @@ -1114,6 +1129,13 @@ end-of-stream@^1.1.0:
dependencies:
once "^1.4.0"

errno@^0.1.1:
version "0.1.7"
resolved "https://registry.yarnpkg.com/errno/-/errno-0.1.7.tgz#4684d71779ad39af177e3f007996f7c67c852618"
integrity sha512-MfrRBDWzIWifgq6tJj60gkAwtLNb6sQPlcFrSOflcP1aFmmruKQ2wRnze/8V6kgyz7H3FF8Npzv78mZ7XLLflg==
dependencies:
prr "~1.0.1"

error-ex@^1.3.1:
version "1.3.2"
resolved "https://registry.yarnpkg.com/error-ex/-/error-ex-1.3.2.tgz#b4ac40648107fdcdcfae242f428bea8a14d4f1bf"
Expand Down Expand Up @@ -1641,6 +1663,11 @@ ignore@^3.3.7:
resolved "https://registry.yarnpkg.com/ignore/-/ignore-3.3.10.tgz#0a97fb876986e8081c631160f8f9f389157f0043"
integrity sha512-Pgs951kaMm5GXP7MOvxERINe3gsaVjUWFm+UZPSq9xYriQAksyhg0csnS0KXSNRD5NmNdapXEpjxG49+AKh/ug==

image-size@~0.5.0:
version "0.5.5"
resolved "https://registry.yarnpkg.com/image-size/-/image-size-0.5.5.tgz#09dfd4ab9d20e29eb1c3e80b8990378df9e3cb9c"
integrity sha1-Cd/Uq50g4p6xw+gLiZA3jfnjy5w=

import-fresh@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-2.0.0.tgz#d81355c15612d386c61f9ddd3922d4304822a546"
Expand Down Expand Up @@ -2442,6 +2469,22 @@ left-pad@^1.3.0:
resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.3.0.tgz#5b8a3a7765dfe001261dde915589e782f8c94d1e"
integrity sha512-XI5MPzVNApjAyhQzphX8BkmKsKUxD4LdyK24iZeQGinBN9yTQT3bFlCBy/aVx2HrNcqQGsdot8ghrjyrvMCoEA==

less@^3.9.0:
version "3.9.0"
resolved "https://registry.yarnpkg.com/less/-/less-3.9.0.tgz#b7511c43f37cf57dc87dffd9883ec121289b1474"
integrity sha512-31CmtPEZraNUtuUREYjSqRkeETFdyEHSEPAGq4erDlUXtda7pzNmctdljdIagSb589d/qXGWiiP31R5JVf+v0w==
dependencies:
clone "^2.1.2"
optionalDependencies:
errno "^0.1.1"
graceful-fs "^4.1.2"
image-size "~0.5.0"
mime "^1.4.1"
mkdirp "^0.5.0"
promise "^7.1.1"
request "^2.83.0"
source-map "~0.6.0"

leven@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/leven/-/leven-2.1.0.tgz#c2e7a9f772094dee9d34202ae8acce4687875580"
Expand Down Expand Up @@ -2609,6 +2652,11 @@ mime-types@^2.1.12, mime-types@~2.1.19:
dependencies:
mime-db "~1.37.0"

mime@^1.4.1:
version "1.6.0"
resolved "https://registry.yarnpkg.com/mime/-/mime-1.6.0.tgz#32cd9e5c64553bd58d19a568af452acff04981b1"
integrity sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==

mimic-fn@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-2.1.0.tgz#7ed2c2ccccaf84d3ffcb7a69b57711fc2083401b"
Expand Down Expand Up @@ -3201,6 +3249,13 @@ process-nextick-args@~2.0.0:
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.0.tgz#a37d732f4271b4ab1ad070d35508e8290788ffaa"
integrity sha512-MtEC1TqN0EU5nephaJ4rAtThHtC86dNN9qCuEhtshvpVBkAW5ZO7BASN9REnF9eoXGcRub+pFuKEpOHE+HbEMw==

promise@^7.1.1:
version "7.3.1"
resolved "https://registry.yarnpkg.com/promise/-/promise-7.3.1.tgz#064b72602b18f90f29192b8b1bc418ffd1ebd3bf"
integrity sha512-nolQXZ/4L+bP/UGlkfaIujX9BKxGwmQ9OT4mOt5yvy8iK1h3wqTEJCijzGANTCCl9nWjY41juyAn2K3Q1hLLTg==
dependencies:
asap "~2.0.3"

prompts@^2.0.1:
version "2.1.0"
resolved "https://registry.yarnpkg.com/prompts/-/prompts-2.1.0.tgz#bf90bc71f6065d255ea2bdc0fe6520485c1b45db"
Expand All @@ -3209,6 +3264,11 @@ prompts@^2.0.1:
kleur "^3.0.2"
sisteransi "^1.0.0"

prr@~1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/prr/-/prr-1.0.1.tgz#d3fc114ba06995a45ec6893f484ceb1d78f5f476"
integrity sha1-0/wRS6BplaRexok/SEzrHXj19HY=

pseudomap@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3"
Expand Down Expand Up @@ -3378,7 +3438,7 @@ request-promise-native@^1.0.5:
stealthy-require "^1.1.0"
tough-cookie ">=2.3.3"

request@^2.87.0:
request@^2.83.0, request@^2.87.0:
version "2.88.0"
resolved "https://registry.yarnpkg.com/request/-/request-2.88.0.tgz#9c2fca4f7d35b592efe57c7f0a55e81052124fef"
integrity sha512-NAqBSrijGLZdM0WZNsInLJpkJokL72XYjUpnB0iwsRgxh7dB6COrHnTBNwN0E+lHDAJzu7kLAkDeY08z2/A0hg==
Expand Down Expand Up @@ -3669,7 +3729,7 @@ source-map@^0.5.0, source-map@^0.5.6:
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.7.tgz#8a039d2d1021d22d1ea14c80d8ea468ba2ef3fcc"
integrity sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w=

source-map@^0.6.0, source-map@^0.6.1, source-map@~0.6.1:
source-map@^0.6.0, source-map@^0.6.1, source-map@~0.6.0, source-map@~0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
Expand Down