Skip to content

Commit

Permalink
fix(webpack): handle relative paths for additionalEntryPath (#27885)
Browse files Browse the repository at this point in the history
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.

<!-- 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
Build will fail.
## Expected Behavior
Build should work.

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

Fixes #
  • Loading branch information
jaysoo authored Sep 11, 2024
1 parent 43eaa5a commit d8cb932
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
24 changes: 18 additions & 6 deletions e2e/webpack/src/webpack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand All @@ -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,
})
Expand All @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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])) {
Expand Down

0 comments on commit d8cb932

Please sign in to comment.