diff --git a/src/babel/preval-extract/__tests__/extractStyles.spec.js b/src/babel/preval-extract/__tests__/extractStyles.spec.js index 9599f34b8..d2c3b49d7 100644 --- a/src/babel/preval-extract/__tests__/extractStyles.spec.js +++ b/src/babel/preval-extract/__tests__/extractStyles.spec.js @@ -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}') diff --git a/src/babel/preval-extract/extractStyles.js b/src/babel/preval-extract/extractStyles.js index 0298ac2eb..fe685422d 100644 --- a/src/babel/preval-extract/extractStyles.js +++ b/src/babel/preval-extract/extractStyles.js @@ -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); @@ -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 @@ -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) { @@ -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; + } else { + if (hasCachedStyles(filename, data)) { + addRequireForCss(types, program, filename); + return; + } + stylesCache[filename] = data; + } } if (single) {