feat(webpack): simplify inferred webpack-cli command (#21340)

This commit is contained in:
Jack Hsu 2024-01-26 12:00:18 -05:00 committed by GitHub
parent c577f48cea
commit 89c5188df3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 43 additions and 20 deletions

View File

@ -8,7 +8,7 @@ exports[`@nx/webpack/plugin should create nodes 1`] = `
"targets": { "targets": {
"build-something": { "build-something": {
"cache": true, "cache": true,
"command": "webpack-cli -c webpack.config.js --node-env=production", "command": "webpack-cli build",
"dependsOn": [ "dependsOn": [
"^build-something", "^build-something",
], ],
@ -22,21 +22,30 @@ exports[`@nx/webpack/plugin should create nodes 1`] = `
}, },
], ],
"options": { "options": {
"args": [
"--node-env=production",
],
"cwd": "my-app", "cwd": "my-app",
}, },
"outputs": [ "outputs": [
"dist/foo", "{projectRoot}/dist/foo",
], ],
}, },
"my-serve": { "my-serve": {
"command": "webpack-cli serve -c webpack.config.js --node-env=development", "command": "webpack-cli serve",
"options": { "options": {
"args": [
"--node-env=development",
],
"cwd": "my-app", "cwd": "my-app",
}, },
}, },
"preview-site": { "preview-site": {
"command": "webpack-cli serve -c webpack.config.js --node-env=production", "command": "webpack-cli serve",
"options": { "options": {
"args": [
"--node-env=production",
],
"cwd": "my-app", "cwd": "my-app",
}, },
}, },

View File

@ -8,7 +8,7 @@ import {
workspaceRoot, workspaceRoot,
writeJsonFile, writeJsonFile,
} from '@nx/devkit'; } from '@nx/devkit';
import { basename, dirname, isAbsolute, join, relative } from 'path'; import { dirname, isAbsolute, join, relative } from 'path';
import { getNamedInputs } from '@nx/devkit/src/utils/get-named-inputs'; import { getNamedInputs } from '@nx/devkit/src/utils/get-named-inputs';
import { WebpackExecutorOptions } from '../executors/webpack/schema'; import { WebpackExecutorOptions } from '../executors/webpack/schema';
import { WebDevServerOptions } from '../executors/dev-server/schema'; import { WebDevServerOptions } from '../executors/dev-server/schema';
@ -53,7 +53,7 @@ export const createDependencies: CreateDependencies = () => {
}; };
export const createNodes: CreateNodes<WebpackPluginOptions> = [ export const createNodes: CreateNodes<WebpackPluginOptions> = [
'**/webpack.config.{js,ts,mjs,mts,cjs,cts}', '**/webpack.config.{js,ts,mjs,cjs}',
async (configFilePath, options, context) => { async (configFilePath, options, context) => {
options ??= {}; options ??= {};
options.buildTargetName ??= 'build'; options.buildTargetName ??= 'build';
@ -118,17 +118,16 @@ async function createWebpackTargets(
const webpackOptions = await readWebpackOptions(webpackConfig); const webpackOptions = await readWebpackOptions(webpackConfig);
const outputPath = const outputPath = normalizeOutputPath(
normalizeOutputPath(webpackOptions.output?.path) ?? webpackOptions.output?.path,
'{workspaceRoot}/dist/{projectRoot}'; projectRoot
);
const targets = {}; const targets = {};
const configBasename = basename(configFilePath);
targets[options.buildTargetName] = { targets[options.buildTargetName] = {
command: `webpack-cli -c ${configBasename} --node-env=production`, command: `webpack-cli build`,
options: { cwd: projectRoot }, options: { cwd: projectRoot, args: ['--node-env=production'] },
cache: true, cache: true,
dependsOn: [`^${options.buildTargetName}`], dependsOn: [`^${options.buildTargetName}`],
inputs: inputs:
@ -151,16 +150,18 @@ async function createWebpackTargets(
}; };
targets[options.serveTargetName] = { targets[options.serveTargetName] = {
command: `webpack-cli serve -c ${configBasename} --node-env=development`, command: `webpack-cli serve`,
options: { options: {
cwd: projectRoot, cwd: projectRoot,
args: ['--node-env=development'],
}, },
}; };
targets[options.previewTargetName] = { targets[options.previewTargetName] = {
command: `webpack-cli serve -c ${configBasename} --node-env=production`, command: `webpack-cli serve`,
options: { options: {
cwd: projectRoot, cwd: projectRoot,
args: ['--node-env=production'],
}, },
}; };
@ -175,12 +176,25 @@ async function createWebpackTargets(
} }
function normalizeOutputPath( function normalizeOutputPath(
outputPath: string | undefined outputPath: string | undefined,
projectRoot: string
): string | undefined { ): string | undefined {
if (!outputPath) return undefined; if (!outputPath) {
if (isAbsolute(outputPath)) { // If outputPath is undefined, use webpack's default `dist` directory.
return `{workspaceRoot}/${relative(workspaceRoot, outputPath)}`; if (projectRoot === '.') {
return `{projectRoot}/dist}`;
} else {
return `{workspaceRoot}/dist/{projectRoot}`;
}
} else { } else {
return outputPath; if (isAbsolute(outputPath)) {
return `{workspaceRoot}/${relative(workspaceRoot, outputPath)}`;
} else {
if (outputPath.startsWith('..')) {
return join('{workspaceRoot}', join(projectRoot, outputPath));
} else {
return join('{projectRoot}', outputPath);
}
}
} }
} }