From 908dcd7aabe44da1e389164228e5a907713be97d Mon Sep 17 00:00:00 2001 From: Satyajit Sahoo Date: Mon, 27 Nov 2017 00:12:41 +0530 Subject: [PATCH] fix: insert relative imports for CSS files, #178 --- .../preval-extract/exctraction.spec.js | 8 ++------ src/babel/preval-extract/extractStyles.js | 8 ++++++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/babel/__integration-tests__/preval-extract/exctraction.spec.js b/src/babel/__integration-tests__/preval-extract/exctraction.spec.js index a427fba2b..8905e82d2 100644 --- a/src/babel/__integration-tests__/preval-extract/exctraction.spec.js +++ b/src/babel/__integration-tests__/preval-extract/exctraction.spec.js @@ -65,12 +65,8 @@ describe('preval-extract babel plugin with extraction enabled', () => { { filename: filename2 } ); - expect(transpiled1).toMatch( - `require('${path.join(process.cwd(), '.linaria-cache/test1.css')}')` - ); - expect(transpiled2).toMatch( - `require('${path.join(process.cwd(), '.linaria-cache/test2.css')}')` - ); + expect(transpiled1).toMatch(`require('./.linaria-cache/test1.css')`); + expect(transpiled2).toMatch(`require('./.linaria-cache/test2.css')`); expect(data1).toMatchSnapshot(); expect(data2).toMatchSnapshot(); diff --git a/src/babel/preval-extract/extractStyles.js b/src/babel/preval-extract/extractStyles.js index 5587b974d..c107890c4 100644 --- a/src/babel/preval-extract/extractStyles.js +++ b/src/babel/preval-extract/extractStyles.js @@ -110,6 +110,10 @@ export default function extractStyles( const filename = single ? path.join(process.cwd(), outDir, basename) : getOutputFilename(relativeCurrentFilename, outDir); + const importPath = `./${path.relative( + path.dirname(absCurrentFilename), + filename + )}`; if (!extract) { return; @@ -132,7 +136,7 @@ export default function extractStyles( stylesCache[absCurrentFilename] = data; } else { if (hasCachedStyles(filename, data)) { - addRequireForCss(types, program, filename); + addRequireForCss(types, program, importPath); return; } stylesCache[filename] = data; @@ -146,7 +150,7 @@ export default function extractStyles( ); } else { outputStylesToFile(filename, withPreamble(data)); - addRequireForCss(types, program, filename); + addRequireForCss(types, program, importPath); } }