fix(vite): provide correct root directory when building a root project (#19298)

This commit is contained in:
Jack Hsu 2023-09-22 13:08:34 -04:00 committed by GitHub
parent ca85d2683e
commit 94d8356e51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 6 deletions

View File

@ -5,6 +5,7 @@ import {
directoryExists, directoryExists,
exists, exists,
fileExists, fileExists,
getPackageManagerCommand,
killPorts, killPorts,
listFiles, listFiles,
newProject, newProject,
@ -14,12 +15,14 @@ import {
removeFile, removeFile,
rmDist, rmDist,
runCLI, runCLI,
runCommand,
runCLIAsync, runCLIAsync,
runCommandUntil, runCommandUntil,
tmpProjPath, tmpProjPath,
uniq, uniq,
updateFile, updateFile,
updateJson, updateJson,
checkFilesExist,
} from '@nx/e2e/utils'; } from '@nx/e2e/utils';
import { join } from 'path'; import { join } from 'path';
@ -282,7 +285,7 @@ export function App() {
<${buildableLibCmp} /> <${buildableLibCmp} />
<${nonBuildableLibCmp} /> <${nonBuildableLibCmp} />
<p>{${buildableJsLibFn}()}</p> <p>{${buildableJsLibFn}()}</p>
<NxWelcome title="${app}" /> <NxWelcome title='${app}' />
</div> </div>
); );
} }
@ -354,7 +357,7 @@ export default App;
it('should collect coverage', () => { it('should collect coverage', () => {
runCLI(`generate @nx/react:lib ${lib} --unitTestRunner=vitest`); runCLI(`generate @nx/react:lib ${lib} --unitTestRunner=vitest`);
updateFile(`libs/${lib}/vite.config.ts`, () => { updateFile(`libs/${lib}/vite.config.ts`, () => {
return `/// <reference types="vitest" /> return `/// <reference types='vitest' />
import { defineConfig } from 'vite'; import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react'; import react from '@vitejs/plugin-react';
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin'; import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
@ -475,4 +478,71 @@ export default defineConfig({
expect(result.combinedOutput).toContain(`1 passed`); expect(result.combinedOutput).toContain(`1 passed`);
}, 100_000); }, 100_000);
}); });
describe('ESM-only apps', () => {
beforeAll(() => {
newProject({ unsetProjectNameAndRootFormat: false });
});
it('should support ESM-only plugins in vite.config.ts for root apps (#NXP-168)', () => {
// ESM-only plugin to test with
updateFile(
'foo/package.json',
JSON.stringify({
name: '@acme/foo',
type: 'module',
version: '1.0.0',
main: 'index.js',
})
);
updateFile(
'foo/index.js',
`
export default function fooPlugin() {
return {
name: 'foo-plugin',
configResolved() {
console.log('Foo plugin');
}
}
}`
);
updateJson('package.json', (json) => {
json.devDependencies['@acme/foo'] = 'file:./foo';
return json;
});
runCommand(getPackageManagerCommand().install);
const rootApp = uniq('root');
runCLI(
`generate @nx/react:app ${rootApp} --rootProject --bundler=vite --unitTestRunner=none --e2eTestRunner=none --style=css --no-interactive`
);
updateJson(`package.json`, (json) => {
// This allows us to use ESM-only packages in vite.config.ts.
json.type = 'module';
return json;
});
updateFile(
`vite.config.ts`,
`
import fooPlugin from '@acme/foo';
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
export default defineConfig({
cacheDir: './node_modules/.vite/root-app',
server: {
port: 4200,
host: 'localhost',
},
plugins: [react(), nxViteTsPaths(), fooPlugin()],
});`
);
runCLI(`build ${rootApp}`);
checkFilesExist(`dist/${rootApp}/index.html`);
});
});
}); });

View File

@ -89,10 +89,10 @@ export function getViteSharedConfig(
const projectRoot = const projectRoot =
context.projectsConfigurations.projects[context.projectName].root; context.projectsConfigurations.projects[context.projectName].root;
const root = relative( const root =
context.cwd, projectRoot === '.'
joinPathFragments(context.root, projectRoot) ? process.cwd()
); : relative(context.cwd, joinPathFragments(context.root, projectRoot));
return { return {
mode: options.mode, mode: options.mode,