Skip to content

Commit

Permalink
fix(webpack): resolve assets from executor options as relative to wor…
Browse files Browse the repository at this point in the history
…kspace root (#22544)
  • Loading branch information
jaysoo authored Mar 28, 2024
1 parent 5381742 commit 7d2a420
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
29 changes: 29 additions & 0 deletions e2e/webpack/src/webpack.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
checkFilesExist,
cleanupProject,
fileExists,
listFiles,
Expand Down Expand Up @@ -262,6 +263,34 @@ describe('Webpack Plugin', () => {

fileExists(`dist/apps/${appName}/package.json`);
});

it('should resolve assets from executors as relative to workspace root', () => {
const appName = uniq('app');
runCLI(`generate @nx/web:app ${appName} --bundler webpack`);
updateFile('shared/docs/TEST.md', 'TEST');
updateJson(`apps/${appName}/project.json`, (json) => {
json.targets.build = {
executor: '@nx/webpack:webpack',
outputs: ['{options.outputPath}'],
options: {
assets: [
{
input: 'shared/docs',
glob: 'TEST.md',
output: '.',
},
],
outputPath: `dist/apps/${appName}`,
webpackConfig: `apps/${appName}/webpack.config.js`,
},
};
return json;
});

runCLI(`build ${appName}`);

checkFilesExist(`dist/apps/${appName}/TEST.md`);
});
});

function readMainFile(dir: string): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export function normalizeOptions(
options.assets,
root,
sourceRoot,
projectRoot
projectRoot,
false // executor assets are relative to workspace root for consistency
);
}
return normalizedOptions as NormalizedWebpackExecutorOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@ export function normalizeOptions(
}
Object.assign(
combinedPluginAndMaybeExecutorOptions,
buildTargetOptions,
options // plugin options take precedence
options,
// executor options take precedence (especially for overriding with CLI args)
buildTargetOptions
);
} else {
Object.assign(
combinedPluginAndMaybeExecutorOptions,
originalTargetOptions,
options // plugin options take precedence
options,
// executor options take precedence (especially for overriding with CLI args)
originalTargetOptions
);
}

Expand Down Expand Up @@ -121,7 +123,8 @@ export function normalizeAssets(
assets: any[],
root: string,
sourceRoot: string,
projectRoot: string
projectRoot: string,
resolveRelativePathsToProjectRoot = true
): AssetGlobPattern[] {
return assets.map((asset) => {
if (typeof asset === 'string') {
Expand Down Expand Up @@ -155,7 +158,7 @@ export function normalizeAssets(

const assetPath = normalizePath(asset.input);
let resolvedAssetPath = resolve(root, assetPath);
if (asset.input.startsWith('.')) {
if (resolveRelativePathsToProjectRoot && asset.input.startsWith('.')) {
const resolvedProjectRoot = resolve(root, projectRoot);
resolvedAssetPath = resolve(resolvedProjectRoot, assetPath);
}
Expand Down

0 comments on commit 7d2a420

Please sign in to comment.