Skip to content

Commit

Permalink
fix(vite): dist and coverage paths for root projects (nrwl#20878)
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini authored Dec 21, 2023
1 parent 7578d00 commit 84c9533
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ exports[`@nx/nuxt/plugin root project should create nodes 1`] = `
"cwd": ".",
},
"outputs": [
"{workspaceRoot}/coverage/{projectRoot}",
"{projectRoot}/coverage",
],
},
},
Expand Down
34 changes: 21 additions & 13 deletions packages/nuxt/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,11 @@ function getOutputs(
buildOutputs: string[];
testOutputs: string[];
} {
const reportsDirectory =
normalizeOutputPath(
viteConfig?.['test']?.coverage?.reportsDirectory,
projectRoot
) ?? '{workspaceRoot}/coverage/{projectRoot}';
const reportsDirectory = normalizeOutputPath(
viteConfig?.['test']?.coverage?.reportsDirectory,
projectRoot,
'coverage'
);

let nuxtBuildDir = nuxtConfig?.buildDir;
if (nuxtConfig?.buildDir && basename(nuxtConfig?.buildDir) === '.nuxt') {
Expand All @@ -248,7 +248,7 @@ function getOutputs(
);
}
const buildOutputPath =
normalizeOutputPath(nuxtBuildDir, projectRoot) ??
normalizeOutputPath(nuxtBuildDir, projectRoot, 'dist') ??
'{workspaceRoot}/dist/{projectRoot}';

return {
Expand All @@ -259,16 +259,24 @@ function getOutputs(

function normalizeOutputPath(
outputPath: string | undefined,
projectRoot: string
projectRoot: string,
path: 'coverage' | 'dist'
): string | undefined {
if (!outputPath) return undefined;
if (isAbsolute(outputPath)) {
return `{workspaceRoot}/${relative(workspaceRoot, outputPath)}`;
if (!outputPath) {
if (projectRoot === '.') {
return `{projectRoot}/${path}`;
} else {
return `{workspaceRoot}/${path}/{projectRoot}`;
}
} else {
if (outputPath.startsWith('..')) {
return join('{workspaceRoot}', join(projectRoot, outputPath));
if (isAbsolute(outputPath)) {
return `{workspaceRoot}/${relative(workspaceRoot, outputPath)}`;
} else {
return outputPath;
if (outputPath.startsWith('..')) {
return join('{workspaceRoot}', join(projectRoot, outputPath));
} else {
return join('{projectRoot}', outputPath);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ export default defineConfig({
reporters: ['default'],
coverage: {
reportsDirectory: './coverage/.',
reportsDirectory: './coverage/test',
provider: 'v8',
},
},
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/plugins/__snapshots__/plugin.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ exports[`@nx/vite/plugin root project should create nodes 1`] = `
"cwd": ".",
},
"outputs": [
"{workspaceRoot}/dist/{projectRoot}",
"{projectRoot}/dist",
],
},
"preview": {
Expand Down Expand Up @@ -133,7 +133,7 @@ exports[`@nx/vite/plugin root project should create nodes 1`] = `
"cwd": ".",
},
"outputs": [
"{workspaceRoot}/coverage/{projectRoot}",
"{projectRoot}/coverage",
],
},
},
Expand Down
38 changes: 25 additions & 13 deletions packages/vite/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,17 @@ function getOutputs(
} {
const { build, test } = viteConfig;

const buildOutputPath =
normalizeOutputPath(build?.outDir, projectRoot) ??
'{workspaceRoot}/dist/{projectRoot}';
const buildOutputPath = normalizeOutputPath(
build?.outDir,
projectRoot,
'dist'
);

const reportsDirectoryPath =
normalizeOutputPath(test?.coverage?.reportsDirectory, projectRoot) ??
'{workspaceRoot}/coverage/{projectRoot}';
const reportsDirectoryPath = normalizeOutputPath(
test?.coverage?.reportsDirectory,
projectRoot,
'coverage'
);

return {
buildOutputs: [buildOutputPath],
Expand All @@ -237,16 +241,24 @@ function getOutputs(

function normalizeOutputPath(
outputPath: string | undefined,
projectRoot: string
projectRoot: string,
path: 'coverage' | 'dist'
): string | undefined {
if (!outputPath) return undefined;
if (isAbsolute(outputPath)) {
return `{workspaceRoot}/${relative(workspaceRoot, outputPath)}`;
if (!outputPath) {
if (projectRoot === '.') {
return `{projectRoot}/${path}`;
} else {
return `{workspaceRoot}/${path}/{projectRoot}`;
}
} else {
if (outputPath.startsWith('..')) {
return join('{workspaceRoot}', join(projectRoot, outputPath));
if (isAbsolute(outputPath)) {
return `{workspaceRoot}/${relative(workspaceRoot, outputPath)}`;
} else {
return outputPath;
if (outputPath.startsWith('..')) {
return join('{workspaceRoot}', join(projectRoot, outputPath));
} else {
return join('{projectRoot}', outputPath);
}
}
}
}
Expand Down
28 changes: 20 additions & 8 deletions packages/vite/src/utils/generator-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,14 +496,19 @@ export function createOrEditViteConfig(
? `${projectRoot}/vitest.config.ts`
: `${projectRoot}/vite.config.ts`;

const buildOutDir =
projectRoot === '.'
? `./dist/${options.project}`
: `${offsetFromRoot(projectRoot)}dist/${projectRoot}`;

const buildOption = onlyVitest
? ''
: options.includeLib
? `
// Configuration for building your library.
// See: https://vitejs.dev/guide/build.html#library-mode
build: {
outDir: '${offsetFromRoot(projectRoot)}dist/${projectRoot}',
outDir: '${buildOutDir}',
reportCompressedSize: true,
commonjsOptions: {
transformMixedEsModules: true,
Expand All @@ -524,7 +529,7 @@ export function createOrEditViteConfig(
},`
: `
build: {
outDir: '${offsetFromRoot(projectRoot)}dist/${projectRoot}',
outDir: '${buildOutDir}',
reportCompressedSize: true,
commonjsOptions: {
transformMixedEsModules: true,
Expand Down Expand Up @@ -553,6 +558,11 @@ export function createOrEditViteConfig(
);
}

const reportsDirectory =
projectRoot === '.'
? `./coverage/${options.project}`
: `${offsetFromRoot(projectRoot)}coverage/${projectRoot}`;

const testOption = options.includeVitest
? `test: {
globals: true,
Expand All @@ -568,7 +578,7 @@ export function createOrEditViteConfig(
}
reporters: ['default'],
coverage: {
reportsDirectory: '${offsetFromRoot(projectRoot)}coverage/${projectRoot}',
reportsDirectory: '${reportsDirectory}',
provider: ${
options.coverageProvider ? `'${options.coverageProvider}'` : `'v8'`
},
Expand Down Expand Up @@ -618,12 +628,13 @@ export function createOrEditViteConfig(
viteConfigPath,
options,
buildOption,
buildOutDir,
imports,
plugins,
testOption,
reportsDirectory,
cacheDir,
offsetFromRoot(projectRoot),
projectRoot,
projectAlreadyHasViteTargets
);
return;
Expand Down Expand Up @@ -788,12 +799,13 @@ function handleViteConfigFileExists(
viteConfigPath: string,
options: ViteConfigFileOptions,
buildOption: string,
buildOutDir: string,
imports: string[],
plugins: string[],
testOption: string,
reportsDirectory: string,
cacheDir: string,
offsetFromRoot: string,
projectRoot: string,
projectAlreadyHasViteTargets?: TargetFlags
) {
if (
Expand All @@ -820,14 +832,14 @@ function handleViteConfigFileExists(
rollupOptions: {
external: options.rollupOptionsExternal ?? [],
},
outDir: `${offsetFromRoot}dist/${projectRoot}`,
outDir: buildOutDir,
reportCompressedSize: true,
commonjsOptions: {
transformMixedEsModules: true,
},
}
: {
outDir: `${offsetFromRoot}dist/${projectRoot}`,
outDir: buildOutDir,
reportCompressedSize: true,
commonjsOptions: {
transformMixedEsModules: true,
Expand All @@ -843,7 +855,7 @@ function handleViteConfigFileExists(
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
reporters: ['default'],
coverage: {
reportsDirectory: `${offsetFromRoot}coverage/${projectRoot}`,
reportsDirectory: reportsDirectory,
provider: `${options.coverageProvider ?? 'v8'}`,
},
};
Expand Down

0 comments on commit 84c9533

Please sign in to comment.