Skip to content

Commit

Permalink
fix: resolve output filename relative to source root (#733)
Browse files Browse the repository at this point in the history
  • Loading branch information
huang-xiao-jian authored Jan 31, 2021
1 parent 34ca3e5 commit c606b3f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
4 changes: 3 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"description": "Blazing fast zero-runtime CSS in JS library",
"main": "lib/cli.js",
"module": "esm/cli.js",
"bin": "bin/linaria.js",
"bin": {
"linaria": "bin/linaria.js"
},
"files": [
"bin/",
"types/",
Expand Down
19 changes: 14 additions & 5 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const { argv } = yargs
alias: 'r',
type: 'string',
description: 'Directory containing the source JS files',
demandOption: true,
requiresArg: true,
})
.option('insert-css-requires', {
Expand Down Expand Up @@ -67,7 +68,7 @@ processFiles(argv._, {
type Options = {
outDir: string;
sourceMaps?: boolean;
sourceRoot?: string;
sourceRoot: string;
insertCssRequires?: string;
configFile?: string;
ignore?: string;
Expand All @@ -85,7 +86,11 @@ function processFiles(files: string[], options: Options) {
);

resolvedFiles.forEach((filename) => {
const outputFilename = resolveOutputFilename(filename, options.outDir);
const outputFilename = resolveOutputFilename(
filename,
options.outDir,
options.sourceRoot
);

const { cssText, sourceMap, cssSourceMapText } = transform(
fs.readFileSync(filename).toString(),
Expand Down Expand Up @@ -156,11 +161,15 @@ function resolveRequireInsertionFilename(filename: string) {
return filename.replace(/\.tsx?/, '.js');
}

function resolveOutputFilename(filename: string, outDir: string) {
const folderStructure = path.relative(process.cwd(), path.dirname(filename));
function resolveOutputFilename(
filename: string,
outDir: string,
sourceRoot: string
) {
const outputFolder = path.relative(sourceRoot, path.dirname(filename));
const outputBasename = path
.basename(filename)
.replace(path.extname(filename), '.css');

return path.join(outDir, folderStructure, outputBasename);
return path.join(outDir, outputFolder, outputBasename);
}

0 comments on commit c606b3f

Please sign in to comment.