From d8cb9324222cdf49982e583edd9a35db1cb5c7e7 Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Wed, 11 Sep 2024 16:37:25 -0400 Subject: [PATCH] fix(webpack): handle relative paths for additionalEntryPath (#27885) The `NxAppWebpackPlugin` does not support relative paths in `additionalEntryPoints`. So this will fail: ```js new NxAppWebpackPlugin({ ... additionalEntryPoints: ['.src/foo.ts'] ``` The resolved path is relative to workspace root when it should be project root. ## Current Behavior Build will fail. ## Expected Behavior Build should work. ## Related Issue(s) Fixes # --- e2e/webpack/src/webpack.test.ts | 24 ++++++++++++++----- .../lib/normalize-options.ts | 14 ++++++++++- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/e2e/webpack/src/webpack.test.ts b/e2e/webpack/src/webpack.test.ts index 5c3d224b10aa5..ed83de42e2de3 100644 --- a/e2e/webpack/src/webpack.test.ts +++ b/e2e/webpack/src/webpack.test.ts @@ -132,8 +132,12 @@ describe('Webpack Plugin', () => { it('should be able to build with NxWebpackPlugin and a standard webpack config file', () => { const appName = uniq('app'); - runCLI(`generate @nx/web:app ${appName} --bundler webpack`); + runCLI( + `generate @nx/web:app ${appName} --bundler webpack --directory=apps/${appName} --projectNameAndRootFormat=as-provided` + ); updateFile(`apps/${appName}/src/main.ts`, `console.log('Hello');\n`); + updateFile(`apps/${appName}/src/foo.ts`, `console.log('Foo');\n`); + updateFile(`apps/${appName}/src/bar.ts`, `console.log('Bar');\n`); updateFile( `apps/${appName}/webpack.config.js`, @@ -144,13 +148,20 @@ describe('Webpack Plugin', () => { module.exports = { target: 'node', output: { - path: path.join(__dirname, '../../dist/${appName}') + path: path.join(__dirname, '../../dist/apps/${appName}') }, plugins: [ new NxAppWebpackPlugin({ compiler: 'tsc', - main: 'apps/${appName}/src/main.ts', - tsConfig: 'apps/${appName}/tsconfig.app.json', + main: './src/main.ts', + additionalEntryPoints: [ + './src/foo.ts', + { + entryName: 'bar', + entryPath: './src/bar.ts', + } + ], + tsConfig: './tsconfig.app.json', outputHashing: 'none', optimization: false, }) @@ -160,8 +171,9 @@ describe('Webpack Plugin', () => { runCLI(`build ${appName}`); - let output = runCommand(`node dist/${appName}/main.js`); - expect(output).toMatch(/Hello/); + expect(runCommand(`node dist/apps/${appName}/main.js`)).toMatch(/Hello/); + expect(runCommand(`node dist/apps/${appName}/foo.js`)).toMatch(/Foo/); + expect(runCommand(`node dist/apps/${appName}/bar.js`)).toMatch(/Bar/); }, 500_000); it('should bundle in NX_PUBLIC_ environment variables', () => { diff --git a/packages/webpack/src/plugins/nx-webpack-plugin/lib/normalize-options.ts b/packages/webpack/src/plugins/nx-webpack-plugin/lib/normalize-options.ts index 8bf4f04ea7183..21c880be1e14b 100644 --- a/packages/webpack/src/plugins/nx-webpack-plugin/lib/normalize-options.ts +++ b/packages/webpack/src/plugins/nx-webpack-plugin/lib/normalize-options.ts @@ -1,4 +1,4 @@ -import { basename, dirname, join, relative, resolve } from 'path'; +import { basename, dirname, join, parse, relative, resolve } from 'path'; import { statSync } from 'fs'; import { normalizePath, @@ -204,6 +204,18 @@ function normalizeRelativePaths( for (const [fieldName, fieldValue] of Object.entries(options)) { if (isRelativePath(fieldValue)) { options[fieldName] = join(projectRoot, fieldValue); + } else if (fieldName === 'additionalEntryPoints') { + for (let i = 0; i < fieldValue.length; i++) { + const v = fieldValue[i]; + if (isRelativePath(v)) { + fieldValue[i] = { + entryName: parse(v).name, + entryPath: join(projectRoot, v), + }; + } else if (isRelativePath(v.entryPath)) { + v.entryPath = join(projectRoot, v.entryPath); + } + } } else if (Array.isArray(fieldValue)) { for (let i = 0; i < fieldValue.length; i++) { if (isRelativePath(fieldValue[i])) {