Skip to content
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

fix: Append classnames when extracting to single file #115

Merged
merged 3 commits into from
Sep 22, 2017
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
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 @@ -148,6 +148,37 @@ describe('preval-extract/extractStyles module', () => {
expect(program.node.body.length).toBe(1);
});

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}'
);
});

it('should overwrite if the file has changed', () => {
getCachedModule.mockImplementationOnce(
getCachedModuleImpl('.classname{color: #ffffff}')
Expand Down
29 changes: 16 additions & 13 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 @@ -90,16 +90,19 @@ export default function extractStyles(
return;
}

if (cache && hasCachedStyles(filename, data)) {
if (!single) {
addRequireForCss(types, program, filename);
}

return;
}

if (cache) {
stylesCache[filename] = data;
if (single) {
if (hasCachedStyles(currentFilename, data)) {
return;
}
stylesCache[currentFilename] = data;
Copy link
Member Author

@thymikee thymikee Sep 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's the issue, filename is always the same when { single: true, filename: styles.css } and get's overwritten

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it be currentFilePath?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, this got me thinking, that if this path is not absolute, we need to process it through parseCurrentFilename, otherwise we can overwrite cache of 2 unrelated files which have the same names. I'll post a followup later.

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

if (single) {
Expand Down