Skip to content

Update handling of nested Sass #15

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 1 commit into from
Jan 7, 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
22 changes: 12 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,24 @@
"trailingComma": "all"
},
"dependencies": {
"@types/lodash": "^4.14.118",
"icss-utils": "^4.0.0",
"lodash": "^4.17.11",
"postcss": "^7.0.5",
"postcss-icss-selectors": "^2.0.3"
"postcss": "^7.0.7",
"postcss-icss-selectors": "^2.0.3",
"postcss-nested": "^4.1.1",
"strip-css-singleline-comments": "^1.1.0"
},
"devDependencies": {
"@types/jest": "^23.3.9",
"@types/node": "^10.12.6",
"husky": "^1.1.3",
"@types/jest": "^23.3.12",
"@types/lodash": "^4.14.119",
"@types/node": "^10.12.18",
"husky": "^1.3.1",
"jest": "^23.6.0",
"prettier": "^1.15.2",
"prettier": "^1.15.3",
"pretty-quick": "^1.8.0",
"ts-jest": "^23.10.4",
"tslint": "^5.11.0",
"typescript": "^3.1.6"
"ts-jest": "^23.10.5",
"tslint": "^5.12.0",
"typescript": "^3.2.2"
},
"peerDependencies": {
"typescript": "^3.0.0"
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions src/@types/postcss-nested.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare module 'postcss-nested' {
import { Plugin } from 'postcss';
const plugin: Plugin<any>;
export = plugin;
}
4 changes: 4 additions & 0 deletions src/@types/strip-css-singleline-comments.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module 'strip-css-singleline-comments/sync' {
const strip: (s: string) => string;
export = strip;
}
41 changes: 39 additions & 2 deletions src/helpers/__tests__/__snapshots__/cssSnapshots.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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

};
export default classes;
"
`;

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

exports[`utils / cssSnapshots with file 'test.module.css' createExports should create an exports file 1`] = `
"declare const classes: {
'classA': string;
'ClassB': string;
Expand All @@ -14,13 +24,40 @@ export default classes;
"
`;

exports[`utils / cssSnapshots createExports should create an exports file 2`] = `
exports[`utils / cssSnapshots with file 'test.module.css' getClasses should return an object matching expected CSS 1`] = `
Object {
"ClassB": "file__ClassB---2bPVi",
"childA": "file__childA---1hjQD",
"childB": "file__childB---pq4Ks",
"class-c": "file__class-c---DZ1TD",
"classA": "file__classA---2xcnJ",
"nestedChild": "file__nestedChild---2d15b",
"parent": "file__parent---1ATMj",
}
`;

exports[`utils / cssSnapshots with file 'test.module.scss' createExports should create an exports file 1`] = `
"declare const classes: {
'local-class-inside-global': string;
'local-class': string;
'local-class-2': string;
'local-class-inside-local': string;
'nested-class-parent': string;
'child-class': string;
'nested-class-parent--extended': string;
};
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",
}
`;
62 changes: 25 additions & 37 deletions src/helpers/__tests__/cssSnapshots.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,37 @@ import { IICSSExports } from 'icss-utils';
import { join } from 'path';
import { createExports, getClasses } from '../cssSnapshots';

describe('utils / cssSnapshots', () => {
let classesA: IICSSExports;
let classesB: IICSSExports;
const testFileNames = [
'test.module.css',
'test.module.scss',
'empty.module.scss',
];

beforeAll(() => {
const testFileA = readFileSync(
join(__dirname, 'fixtures/testA.module.css'),
'utf8',
);
const testFileB = readFileSync(
join(__dirname, 'fixtures/testB.module.scss'),
describe('utils / cssSnapshots', () => {
testFileNames.map((filename) => {
let classes: IICSSExports;
const testFile = readFileSync(
join(__dirname, 'fixtures', filename),
'utf8',
);
classesA = getClasses(testFileA);
classesB = getClasses(testFileB);
});

describe('getClasses', () => {
it('should return an object matching expected CSS classes', () => {
expect(classesA).toEqual({
ClassB: 'file__ClassB---2bPVi',
childA: 'file__childA---1hjQD',
childB: 'file__childB---pq4Ks',
'class-c': 'file__class-c---DZ1TD',
classA: 'file__classA---2xcnJ',
nestedChild: 'file__nestedChild---2d15b',
parent: 'file__parent---1ATMj',
});
expect(classesB).toEqual({
'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',
});
beforeAll(() => {
classes = getClasses(testFile);
});
});

describe('createExports', () => {
it('should create an exports file', () => {
const exportsA = createExports(classesA, {});
const exportsB = createExports(classesB, {});
expect(exportsA).toMatchSnapshot();
expect(exportsB).toMatchSnapshot();
describe(`with file '${filename}'`, () => {
describe('getClasses', () => {
it('should return an object matching expected CSS', () => {
expect(classes).toMatchSnapshot();
});
});

describe('createExports', () => {
it('should create an exports file', () => {
const exports = createExports(classes, {});
expect(exports).toMatchSnapshot();
});
});
});
});
});
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,14 @@
.local-class-inside-local {
}
}

.nested-class-parent {
.child-class {
}
&--extended {
}
}

// .commented-parent-class {
// .commented-child-class
// }
20 changes: 16 additions & 4 deletions src/helpers/cssSnapshots.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import { extractICSS, IICSSExports } from 'icss-utils';
import * as postcss from 'postcss';
import * as postcssIcssSelectors from 'postcss-icss-selectors';
import * as postcssNested from 'postcss-nested';
import * as strip from 'strip-css-singleline-comments/sync';
import * as ts_module from 'typescript/lib/tsserverlibrary';
import { transformClasses } from './classTransforms';

const processor = postcss(postcssIcssSelectors({ mode: 'local' }));
const processor = postcss(
postcssNested,
postcssIcssSelectors({ mode: 'local' }),
);

export const getClasses = (css: string) =>
extractICSS(processor.process(css).root).icssExports;
export const getClasses = (css: string) => {
try {
const cleanCss = strip(css);
const processedCss = processor.process(cleanCss);
return extractICSS(processedCss.root).icssExports;
} catch (e) {
return {};
}
};
const classNameToProperty = (className: string) => `'${className}': string;`;
const flattenClassNames = (
previousValue: string[] = [],
Expand All @@ -18,7 +30,7 @@ export const createExports = (classes: IICSSExports, options: IOptions) => `\
declare const classes: {
${Object.keys(classes)
.map(transformClasses(options.camelCase))
.reduce(flattenClassNames)
.reduce(flattenClassNames, [])
.map(classNameToProperty)
.join('\n ')}
};
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function init({ typescript: ts }: { typescript: typeof ts_module }) {
containingFile,
reusedNames,
) => {
const resolvedModules: ts_module.ResolvedModuleFull[] = _resolveModuleNames(
const resolvedModules = _resolveModuleNames(
moduleNames,
containingFile,
reusedNames,
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
"strictNullChecks": true,
"target": "es5"
},
"include": ["src"]
"include": ["src"],
"exclude": ["node_modules", "**/__tests__/**"]
}
Loading