Skip to content

Commit

Permalink
fix: Append classnames when extracting to single file
Browse files Browse the repository at this point in the history
  • Loading branch information
thymikee committed Sep 22, 2017
1 parent 1c61aa4 commit fd5ce29
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
31 changes: 31 additions & 0 deletions src/babel/preval-extract/__tests__/extractStyles.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,37 @@ describe('preval-extract/extractStyles module', () => {
expect(writeFileSync).not.toHaveBeenCalled();
});

it('should append classnames when writing to single file', () => {
const writeFileSync = jest.fn();
const opts = { single: true, filename: 'A.css' };

getCachedModule.mockImplementationOnce(
getCachedModuleImpl('.classname{color: #ffffff}')
);

extractStyles(types, null, 'file1.js', opts, { writeFileSync });

getCachedModule.mockImplementationOnce(
getCachedModuleImpl('.other{color: #000000}')
);

extractStyles(types, null, 'file2.js', opts, { writeFileSync });

expect(fs.writeFileSync).toHaveBeenCalledTimes(2);
expect(fs.writeFileSync.mock.calls[0][0]).toEqual(
path.join(process.cwd(), 'A.css')
);
expect(fs.writeFileSync.mock.calls[0][1]).toMatch(
'.classname{color: #ffffff}'
);
expect(fs.writeFileSync.mock.calls[1][0]).toEqual(
path.join(process.cwd(), 'A.css')
);
expect(fs.writeFileSync.mock.calls[1][1]).toMatch(
'.classname{color: #ffffff}\n.other{color: #000000}'
);
});

describe('with cache disabled', () => {
beforeEach(clearCache);

Expand Down
21 changes: 16 additions & 5 deletions src/babel/preval-extract/extractStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { getCachedModule } from '../lib/moduleSystem';
const preamble =
'/* THIS FILE IS AUTOGENERATED. DO NOT EDIT IT DIRECTLY, NOR COMMIT IT TO VERSION CONTROL. */';

function parseCurrentFilename(filename: string, outDir: string) {
function parseCurrentFilename(filename: string, outDir: string = '') {
const dirname = path.isAbsolute(filename)
? path.dirname(filename)
: path.join(process.cwd(), outDir);
Expand All @@ -21,7 +21,7 @@ function parseCurrentFilename(filename: string, outDir: string) {
function parseFilename(
filename: string,
currentFilename: string,
outDir: string
outDir: string = ''
) {
const dirname = path.isAbsolute(outDir)
? outDir
Expand Down Expand Up @@ -75,8 +75,8 @@ export default function extractStyles(
single: false,
...options,
filename: options.filename
? parseFilename(options.filename, currentFilename, options.outDir || '')
: parseCurrentFilename(currentFilename, options.outDir || ''),
? parseFilename(options.filename, currentFilename, options.outDir)
: parseCurrentFilename(currentFilename, options.outDir),
};

if (!extract) {
Expand All @@ -99,7 +99,18 @@ export default function extractStyles(
}

if (cache) {
stylesCache[filename] = data;
if (single) {
if (hasCachedStyles(currentFilename, data)) {
return;
}
stylesCache[currentFilename] = data;
} else {
if (hasCachedStyles(filename, data)) {
addRequireForCss(types, program, filename);
return;
}
stylesCache[filename] = data;
}
}

if (single) {
Expand Down

0 comments on commit fd5ce29

Please sign in to comment.