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: insert relative imports for CSS files and support absolute path in outDir #180

Merged
merged 2 commits into from
Nov 27, 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
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