diff --git a/e2e/react/src/react-ts-solution.test.ts b/e2e/react/src/react-ts-solution.test.ts index d75642e019..1f9b87b1a6 100644 --- a/e2e/react/src/react-ts-solution.test.ts +++ b/e2e/react/src/react-ts-solution.test.ts @@ -1,9 +1,15 @@ import { + checkFilesExist, cleanupProject, + getPackageManagerCommand, newProject, + readFile, readJson, runCLI, + runCommand, uniq, + updateFile, + updateJson, } from '@nx/e2e/utils'; describe('React (TS solution)', () => { @@ -38,4 +44,47 @@ describe('React (TS solution)', () => { `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); }); diff --git a/e2e/react/src/react-webpack.test.ts b/e2e/react/src/react-webpack.test.ts index 9fb22144aa..b3576f209d 100644 --- a/e2e/react/src/react-webpack.test.ts +++ b/e2e/react/src/react-webpack.test.ts @@ -10,7 +10,7 @@ import { updateFile, } from '@nx/e2e/utils'; -describe('Build React applications and libraries with Vite', () => { +describe('Build React applications and libraries with Webpack', () => { beforeAll(() => { newProject({ packages: ['@nx/react'], diff --git a/packages/webpack/src/plugins/nx-webpack-plugin/lib/apply-base-config.ts b/packages/webpack/src/plugins/nx-webpack-plugin/lib/apply-base-config.ts index 8378c6ac9a..7b93076ba6 100644 --- a/packages/webpack/src/plugins/nx-webpack-plugin/lib/apply-base-config.ts +++ b/packages/webpack/src/plugins/nx-webpack-plugin/lib/apply-base-config.ts @@ -26,6 +26,10 @@ const IGNORED_WEBPACK_WARNINGS = [ /could not find any license/i, ]; +const extensionAlias = { + '.js': ['.ts', '.js'], + '.mjs': ['.mts', '.mjs'], +}; const extensions = ['.ts', '.tsx', '.mjs', '.js', '.jsx']; const mainFields = ['module', 'main']; @@ -356,6 +360,10 @@ function applyNxDependentConfig( config.resolve = { ...config.resolve, extensions: [...(config?.resolve?.extensions ?? []), ...extensions], + extensionAlias: { + ...(config.resolve?.extensionAlias ?? {}), + ...extensionAlias, + }, alias: { ...(config.resolve?.alias ?? {}), ...(options.fileReplacements?.reduce(