@nx/next
Version:
94 lines (93 loc) • 4.73 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.cypressComponentConfiguration = cypressComponentConfiguration;
exports.cypressComponentConfigurationInternal = cypressComponentConfigurationInternal;
const devkit_1 = require("@nx/devkit");
const internal_1 = require("@nx/js/internal");
const react_1 = require("@nx/react");
const internal_2 = require("@nx/react/internal");
const path_1 = require("path");
const versions_1 = require("../../utils/versions");
const assert_supported_next_version_1 = require("../../utils/assert-supported-next-version");
function cypressComponentConfiguration(tree, options) {
return cypressComponentConfigurationInternal(tree, {
addPlugin: false,
...options,
});
}
async function cypressComponentConfigurationInternal(tree, options) {
(0, assert_supported_next_version_1.assertSupportedNextVersion)(tree);
const tasks = [];
const { componentConfigurationGenerator: baseCyCtConfig } = (0, devkit_1.ensurePackage)('@nx/cypress', versions_1.nxVersion);
tasks.push(await baseCyCtConfig(tree, {
project: options.project,
skipFormat: true,
framework: 'next',
jsx: true,
addPlugin: options.addPlugin,
}));
const { webpackInitGenerator } = (0, devkit_1.ensurePackage)('@nx/webpack', versions_1.nxVersion);
tasks.push(await webpackInitGenerator(tree, {
skipFormat: true,
addPlugin: options.addPlugin,
}));
const { ensureDependencies, } = require('@nx/webpack/internal');
tasks.push(ensureDependencies(tree, { compiler: 'swc', uiFramework: 'react' }));
const projectConfig = (0, devkit_1.readProjectConfiguration)(tree, options.project);
if (projectConfig.targets?.['component-test']?.executor ===
'@nx/cypress:cypress') {
projectConfig.targets['component-test'].options = {
...projectConfig.targets['component-test'].options,
skipServe: true,
};
(0, devkit_1.updateProjectConfiguration)(tree, options.project, projectConfig);
}
await addFiles(tree, projectConfig, options);
if (!options.skipFormat) {
await (0, devkit_1.formatFiles)(tree);
}
return (0, devkit_1.runTasksInSerial)(...tasks);
}
async function addFiles(tree, projectConfig, opts) {
const { addMountDefinition, addDefaultCTConfig } = await import('@nx/cypress/internal');
const { getInstalledCypressMajorVersion } = await import('@nx/cypress/internal');
const installedCypressMajorVersion = getInstalledCypressMajorVersion(tree);
const ctFile = (0, devkit_1.joinPathFragments)(projectConfig.root, 'cypress', 'support', 'component.ts');
const updatedCommandFile = await addMountDefinition(tree.read(ctFile, 'utf-8'));
const moduleSpecifier = installedCypressMajorVersion >= 14 ? 'cypress/react' : 'cypress/react18';
tree.write(ctFile, `import { mount } from '${moduleSpecifier}';\nimport './styles.ct.css';\n${updatedCommandFile}`);
const cyFile = (0, devkit_1.joinPathFragments)(projectConfig.root, 'cypress.config.ts');
const updatedCyConfig = await addDefaultCTConfig(tree.read(cyFile, 'utf-8'), undefined, '@nx/next/plugins/component-testing', installedCypressMajorVersion);
tree.write(cyFile, updatedCyConfig);
const isUsingTailwind = ['js', 'cjs'].some((ext) => tree.exists((0, devkit_1.joinPathFragments)(projectConfig.root, `tailwind.config.${ext}`)));
tree.write((0, devkit_1.joinPathFragments)(projectConfig.root, 'cypress', 'support', 'styles.ct.css'), `/* This is where you can load global styles to apply to all components. */
${isUsingTailwind
? `@tailwind base;
@tailwind components;
@tailwind utilities;`
: ''}
`);
if (opts.generateTests) {
const filePaths = [];
const sourceRoot = (0, internal_1.getProjectSourceRoot)(projectConfig, tree);
(0, devkit_1.visitNotIgnoredFiles)(tree, sourceRoot, (filePath) => {
const fromProjectRootPath = (0, path_1.relative)(projectConfig.root, filePath);
// we don't generate tests for pages/server-side/appDir components
if (fromProjectRootPath.includes('pages') ||
fromProjectRootPath.includes('server') ||
fromProjectRootPath.includes('app')) {
return;
}
if ((0, internal_2.isComponent)(tree, filePath)) {
filePaths.push(filePath);
}
});
for (const filePath of filePaths) {
await (0, react_1.componentTestGenerator)(tree, {
project: opts.project,
componentPath: filePath,
});
}
}
}
exports.default = cypressComponentConfiguration;