Skip to content

Commit

Permalink
fix: insert relative imports for CSS files and support absolute path …
Browse files Browse the repository at this point in the history
…in outDir (#180)

* fix: insert relative imports for CSS files, #178

* fix: support absolute path in output directory
  • Loading branch information
satya164 authored and zamotany committed Nov 27, 2017
1 parent 8c94761 commit 7e92e95
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
38 changes: 24 additions & 14 deletions src/babel/preval-extract/extractStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ const withPreamble = data =>
* Get output filename with directory structure from source preserved inside
* custom outDir.
*/
function getOutputFilename(relativeFilename: string, outDir: string): string {
function getOutputFilename(
relativeFilename: string,
absOutDir: string
): string {
const basename = /(.+)\..+$/.exec(path.basename(relativeFilename))[1];
const relativeOutputFilename = path.join(
path.dirname(relativeFilename),
`${basename}.css`
);
return path.join(process.cwd(), outDir, relativeOutputFilename);
return path.join(absOutDir, relativeOutputFilename);
}

let stylesCache = {};
Expand Down Expand Up @@ -95,21 +98,28 @@ export default function extractStyles(
const relativeCurrentFilename = relativeToCwd(currentFilename);
const absCurrentFilename = makeAbsolute(currentFilename);

const { single, cache, extract, outDir, filename: basename } = {
cache: true,
extract: true,
single: false,
outDir: '.linaria-cache',
filename: 'styles.css',
...options,
};
const {
single = false,
cache = true,
extract = true,
outDir = '.linaria-cache',
filename: basename = 'styles.css',
} = options;

const absOutDir = path.isAbsolute(outDir)
? outDir
: path.join(process.cwd(), outDir);

// If single === true, we compute filename from outDir and filename options,
// since there will be only one file, otherwise we need to reconstruct directory
// structure inside outDir. In that case filename option is discard.
const filename = single
? path.join(process.cwd(), outDir, basename)
: getOutputFilename(relativeCurrentFilename, outDir);
? path.join(absOutDir, basename)
: getOutputFilename(relativeCurrentFilename, absOutDir);
const importPath = `./${path.relative(
path.dirname(absCurrentFilename),
filename
)}`;

if (!extract) {
return;
Expand All @@ -132,7 +142,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;
Expand All @@ -146,7 +156,7 @@ export default function extractStyles(
);
} else {
outputStylesToFile(filename, withPreamble(data));
addRequireForCss(types, program, filename);
addRequireForCss(types, program, importPath);
}
}

Expand Down

0 comments on commit 7e92e95

Please sign in to comment.