nx/e2e/react/src/react-ts-solution.test.ts
Nicholas Cunningham bf8848da95
fix(webpack): add extension alias support for handling ESM libs (#30513)
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->
Currently, if you have a webpack application that uses out
NxWebpackAppPlugin and has a non-buildable lib that has exports with
extension enabled for example:`export * from './lib/lib8446520.js';`.
The app fails to build.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
When using webpack and including libraries that contain extension it
should resolve.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #30492
2025-03-28 11:51:20 -06:00

91 lines
2.5 KiB
TypeScript

import {
checkFilesExist,
cleanupProject,
getPackageManagerCommand,
newProject,
readFile,
readJson,
runCLI,
runCommand,
uniq,
updateFile,
updateJson,
} from '@nx/e2e/utils';
describe('React (TS solution)', () => {
let workspaceName: string;
beforeAll(() => {
workspaceName = newProject({ preset: 'ts', packages: ['@nx/react'] });
});
afterAll(() => cleanupProject());
it('should respect and support generating libraries with a name different than the import path', async () => {
const lib = uniq('lib');
runCLI(
`generate @nx/react:library packages/${lib} --name=${lib} --bundler=vite --linter=eslint --unitTestRunner=vitest`
);
const packageJson = readJson(`packages/${lib}/package.json`);
expect(packageJson.nx.name).toBe(lib);
expect(runCLI(`build ${lib}`)).toContain(
`Successfully ran target build for project ${lib}`
);
expect(runCLI(`typecheck ${lib}`)).toContain(
`Successfully ran target typecheck for project ${lib}`
);
expect(runCLI(`lint ${lib}`)).toContain(
`Successfully ran target lint for project ${lib}`
);
expect(runCLI(`test ${lib}`)).toContain(
`Successfully ran target test for project ${lib}`
);
}, 90000);
it('should be able to use Webpack to build apps with an imported lib', async () => {
const appName = uniq('app');
const libName = uniq('lib');
runCLI(
`generate @nx/react:app packages/${appName} --bundler=webpack --no-interactive --skipFormat --linter=eslint --unitTestRunner=none`
);
runCLI(
`generate @nx/js:lib libs/${libName} --bundler=none --no-interactive --unit-test-runner=none --skipFormat --linter=eslint`
);
const mainPath = `packages/${appName}/src/main.tsx`;
updateFile(
mainPath,
`
import {${libName}} from '@${workspaceName}/${libName}';
${readFile(mainPath)}
console.log(${libName}());
`
);
runCLI('sync');
// Add library to package.json to make sure it is linked (not needed for npm package manager)
updateJson(`packages/${appName}/package.json`, (json) => {
return {
...json,
devDependencies: {
...(json.devDependencies || {}),
[`@${workspaceName}/${libName}`]: 'workspace:*',
},
};
});
runCommand(
`cd packages/${appName} && ${getPackageManagerCommand().install}`
);
runCLI(`build ${appName}`);
checkFilesExist(`packages/${appName}/dist/index.html`);
}, 90_000);
});