Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(webpack): pass options from executor to NxWebpackPlugin correctly #22529

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions e2e/webpack/src/webpack.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
cleanupProject,
fileExists,
listFiles,
newProject,
packageInstall,
Expand Down Expand Up @@ -218,6 +219,49 @@ describe('Webpack Plugin', () => {
runCLI(`build ${appName}`);
expect(readMainFile(`dist/apps/${appName}`)).not.toMatch(`await Promise`);
});

it('should allow options to be passed from the executor', async () => {
const appName = uniq('app');
runCLI(`generate @nx/web:app ${appName} --bundler webpack`);
updateJson(`apps/${appName}/project.json`, (json) => {
json.targets.build = {
executor: '@nx/webpack:webpack',
outputs: ['{options.outputPath}'],
options: {
generatePackageJson: true, // This should be passed to the plugin.
outputPath: `dist/apps/${appName}`,
webpackConfig: `apps/${appName}/webpack.config.js`,
},
};
return json;
});
updateFile(
`apps/${appName}/webpack.config.js`,
`
const { NxWebpackPlugin } = require('@nx/webpack');
const { join } = require('path');
module.exports = {
output: {
path: join(__dirname, '../../dist/apps/demo'),
},
plugins: [
new NxWebpackPlugin({
// NOTE: generatePackageJson is missing here, but executor passes it.
target: 'web',
compiler: 'swc',
main: './src/main.ts',
tsConfig: './tsconfig.app.json',
optimization: false,
outputHashing: 'none',
}),
],
};`
);

runCLI(`build ${appName}`);

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

function readMainFile(dir: string): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
export function normalizeOptions(
options: NxWebpackPluginOptions
): NormalizedNxWebpackPluginOptions {
const combinedOptions: Partial<NormalizedNxWebpackPluginOptions> = {};
const combinedPluginAndMaybeExecutorOptions: Partial<NormalizedNxWebpackPluginOptions> =
{};
const isProd = process.env.NODE_ENV === 'production';
const projectName = process.env.NX_TASK_TARGET_PROJECT;
const targetName = process.env.NX_TASK_TARGET_TARGET;
Expand All @@ -27,6 +28,8 @@ export function normalizeOptions(
const projectNode = projectGraph.nodes[projectName];
const targetConfig = projectNode.data.targets[targetName];

normalizeRelativePaths(projectNode.data.root, options);

// Merge options from `@nx/webpack:webpack` into plugin options.
// Options from `@nx/webpack:webpack` take precedence.
const originalTargetOptions = targetConfig.options;
Expand All @@ -46,13 +49,19 @@ export function normalizeOptions(
targetConfig.configurations?.[configurationName]
);
}
Object.assign(combinedOptions, buildTargetOptions);
Object.assign(
combinedPluginAndMaybeExecutorOptions,
buildTargetOptions,
options // plugin options take precedence
);
} else {
Object.assign(combinedOptions, originalTargetOptions, options);
Object.assign(
combinedPluginAndMaybeExecutorOptions,
originalTargetOptions,
options // plugin options take precedence
);
}

normalizeRelativePaths(projectNode.data.root, options);

const sourceRoot = projectNode.data.sourceRoot ?? projectNode.data.root;

if (!options.main) {
Expand All @@ -62,44 +71,49 @@ export function normalizeOptions(
}

return {
...options,
assets: options.assets
...combinedPluginAndMaybeExecutorOptions,
assets: combinedPluginAndMaybeExecutorOptions.assets
? normalizeAssets(
options.assets,
combinedPluginAndMaybeExecutorOptions.assets,
workspaceRoot,
sourceRoot,
projectNode.data.root
)
: [],
baseHref: options.baseHref ?? '/',
commonChunk: options.commonChunk ?? true,
compiler: options.compiler ?? 'babel',
baseHref: combinedPluginAndMaybeExecutorOptions.baseHref ?? '/',
commonChunk: combinedPluginAndMaybeExecutorOptions.commonChunk ?? true,
compiler: combinedPluginAndMaybeExecutorOptions.compiler ?? 'babel',
configurationName,
deleteOutputPath: options.deleteOutputPath ?? true,
extractCss: options.extractCss ?? true,
deleteOutputPath:
combinedPluginAndMaybeExecutorOptions.deleteOutputPath ?? true,
extractCss: combinedPluginAndMaybeExecutorOptions.extractCss ?? true,
fileReplacements: normalizeFileReplacements(
workspaceRoot,
options.fileReplacements
combinedPluginAndMaybeExecutorOptions.fileReplacements
),
generateIndexHtml: options.generateIndexHtml ?? true,
main: options.main,
namedChunks: options.namedChunks ?? !isProd,
optimization: options.optimization ?? isProd,
outputFileName: options.outputFileName ?? 'main.js',
outputHashing: options.outputHashing ?? (isProd ? 'all' : 'none'),
outputPath: options.outputPath,
generateIndexHtml:
combinedPluginAndMaybeExecutorOptions.generateIndexHtml ?? true,
main: combinedPluginAndMaybeExecutorOptions.main,
namedChunks: combinedPluginAndMaybeExecutorOptions.namedChunks ?? !isProd,
optimization: combinedPluginAndMaybeExecutorOptions.optimization ?? isProd,
outputFileName:
combinedPluginAndMaybeExecutorOptions.outputFileName ?? 'main.js',
outputHashing:
combinedPluginAndMaybeExecutorOptions.outputHashing ??
(isProd ? 'all' : 'none'),
outputPath: combinedPluginAndMaybeExecutorOptions.outputPath,
projectGraph,
projectName,
projectRoot: projectNode.data.root,
root: workspaceRoot,
runtimeChunk: options.runtimeChunk ?? true,
scripts: options.scripts ?? [],
sourceMap: options.sourceMap ?? !isProd,
runtimeChunk: combinedPluginAndMaybeExecutorOptions.runtimeChunk ?? true,
scripts: combinedPluginAndMaybeExecutorOptions.scripts ?? [],
sourceMap: combinedPluginAndMaybeExecutorOptions.sourceMap ?? !isProd,
sourceRoot,
styles: options.styles ?? [],
target: options.target,
styles: combinedPluginAndMaybeExecutorOptions.styles ?? [],
target: combinedPluginAndMaybeExecutorOptions.target,
targetName,
vendorChunk: options.vendorChunk ?? !isProd,
vendorChunk: combinedPluginAndMaybeExecutorOptions.vendorChunk ?? !isProd,
};
}

Expand Down