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): handle both nx and nrwl scoped executors when migrating config #20714

Merged
merged 1 commit into from
Dec 12, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,27 @@ describe('17.2.1 migration (setup webpack.config file)', () => {
tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
});

it('should create webpack.config.js for projects that did not set webpackConfig', async () => {
addProjectConfiguration(tree, 'myapp', {
root: 'apps/myapp',
targets: {
build: {
executor: '@nrwl/webpack:webpack',
options: {},
it.each`
executor
${'@nx/webpack:webpack'}
${'@nrwl/webpack:webpack'}
`(
'should create webpack.config.js for projects that did not set webpackConfig',
async ({ executor }) => {
addProjectConfiguration(tree, 'myapp', {
root: 'apps/myapp',
targets: {
build: {
executor,
options: {},
},
},
},
});
});

await webpackConfigSetup(tree);
await webpackConfigSetup(tree);

expect(tree.read('apps/myapp/webpack.config.js', 'utf-8'))
.toEqual(`const { composePlugins, withNx } = require('@nx/webpack');
expect(tree.read('apps/myapp/webpack.config.js', 'utf-8'))
.toEqual(`const { composePlugins, withNx } = require('@nx/webpack');

// Nx plugins for webpack.
module.exports = composePlugins(withNx(), (config) => {
Expand All @@ -32,7 +38,8 @@ module.exports = composePlugins(withNx(), (config) => {
return config;
});
`);
});
}
);

it('should not create webpack.config.js when webpackConfig is already set', async () => {
tree.write(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,22 @@ import { forEachExecutorOptions } from '@nx/devkit/src/generators/executor-optio
import { WebpackExecutorOptions } from '../../executors/webpack/schema';

export default async function (tree: Tree) {
forEachExecutorOptions<WebpackExecutorOptions>(
tree,
'@nrwl/webpack:webpack',
(
options: WebpackExecutorOptions,
projectName,
targetName,
configurationName
) => {
// Only handle webpack config for default configuration
if (configurationName) return;
const update = (
options: WebpackExecutorOptions,
projectName: string,
targetName: string,
configurationName: string
) => {
// Only handle webpack config for default configuration
if (configurationName) return;

const projectConfiguration = readProjectConfiguration(tree, projectName);
const projectConfiguration = readProjectConfiguration(tree, projectName);

if (!options.webpackConfig && options.isolatedConfig !== false) {
options.webpackConfig = `${projectConfiguration.root}/webpack.config.js`;
tree.write(
options.webpackConfig,
`
if (!options.webpackConfig && options.isolatedConfig !== false) {
options.webpackConfig = `${projectConfiguration.root}/webpack.config.js`;
tree.write(
options.webpackConfig,
`
const { composePlugins, withNx } = require('@nx/webpack');

// Nx plugins for webpack.
Expand All @@ -36,12 +33,22 @@ export default async function (tree: Tree) {
return config;
});
`
);
);

projectConfiguration.targets[targetName].options = options;
updateProjectConfiguration(tree, projectName, projectConfiguration);
}
projectConfiguration.targets[targetName].options = options;
updateProjectConfiguration(tree, projectName, projectConfiguration);
}
};

forEachExecutorOptions<WebpackExecutorOptions>(
tree,
'@nx/webpack:webpack',
update
);
forEachExecutorOptions<WebpackExecutorOptions>(
tree,
'@nrwl/webpack:webpack',
update
);

await formatFiles(tree);
Expand Down