fix(react): clean-up after dependency removal from @nrwl/react (#13563)

This commit is contained in:
Jack Hsu 2022-12-02 11:58:07 -05:00 committed by GitHub
parent edad5ef48a
commit 5b7dba1cb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 97 additions and 51 deletions

View File

@ -1184,10 +1184,10 @@ When running with --dryRun, the function will throw when dependencies are missin
#### Parameters #### Parameters
| Name | Type | Description | | Name | Type | Description |
| :------------------------ | :-------------------------------- | :------------------------------------- | | :------------------------ | :-------------------------------- | :----------------------------------------------------------------- |
| `tree` | [`Tree`](../../devkit/index#tree) | the file system tree | | `tree` | [`Tree`](../../devkit/index#tree) | the file system tree |
| `pkg` | `string` | the package to check (e.g. @nrwl/jest) | | `pkg` | `string` | the package to check (e.g. @nrwl/jest) |
| `requiredVersion` | `string` | the version to check | | `requiredVersion` | `string` | the version or semver range to check (e.g. ~1.0.0, >=1.0.0 <2.0.0) |
| `options` | `Object` | | | `options` | `Object` | |
| `options.dev?` | `boolean` | - | | `options.dev?` | `boolean` | - |
| `options.throwOnMissing?` | `boolean` | - | | `options.throwOnMissing?` | `boolean` | - |

File diff suppressed because one or more lines are too long

View File

@ -399,7 +399,8 @@
"buildable": { "buildable": {
"type": "boolean", "type": "boolean",
"default": false, "default": false,
"description": "Generate a buildable library. If a bundler is set then the library is buildable by default." "description": "Generate a buildable library that uses rollup to bundle. Use 'bundler' option instead for greater control (none, vite, rollup).",
"x-deprecated": true
}, },
"importPath": { "importPath": {
"type": "string", "type": "string",
@ -438,13 +439,14 @@
"type": "string", "type": "string",
"description": "The bundler to use. Choosing 'none' means this library is not buildable.", "description": "The bundler to use. Choosing 'none' means this library is not buildable.",
"enum": ["none", "vite", "rollup"], "enum": ["none", "vite", "rollup"],
"default": "none",
"x-prompt": "Which bundler would you like to use to build the library? Choose 'none' to skip build setup." "x-prompt": "Which bundler would you like to use to build the library? Choose 'none' to skip build setup."
}, },
"compiler": { "compiler": {
"type": "string", "type": "string",
"enum": ["babel", "swc"], "enum": ["babel", "swc"],
"default": "babel", "default": "babel",
"description": "Which compiler to use. Does not apply if bundler is set to Vite." "description": "Which compiler to use. Only applies to `bundler: 'rollup'`."
}, },
"skipPackageJson": { "skipPackageJson": {
"description": "Do not add dependencies to `package.json`.", "description": "Do not add dependencies to `package.json`.",

View File

@ -20,7 +20,7 @@
"properties": { "properties": {
"uiFramework": { "uiFramework": {
"type": "string", "type": "string",
"description": "UI Framework to use for Vite.", "description": "UI Framework to use for Webpack.",
"enum": ["react", "none"], "enum": ["react", "none"],
"x-prompt": "What UI framework plugin should Webpack use?" "x-prompt": "What UI framework plugin should Webpack use?"
}, },

View File

@ -178,7 +178,7 @@ export async function h() { return 'c'; }
// Setup // Setup
const myLib = uniq('my-lib'); const myLib = uniq('my-lib');
runCLI( runCLI(
`generate @nrwl/react:library ${myLib} --publishable --importPath="@mproj/${myLib}" --no-interactive` `generate @nrwl/react:library ${myLib} --bundler=rollup --publishable --importPath="@mproj/${myLib}" --no-interactive`
); );
/** /**

View File

@ -3,7 +3,6 @@ import {
checkFilesExist, checkFilesExist,
cleanupProject, cleanupProject,
createFile, createFile,
expectJestTestsToPass,
killPorts, killPorts,
newProject, newProject,
readFile, readFile,
@ -96,6 +95,34 @@ describe('React Applications', () => {
}); });
}, 250_000); }, 250_000);
it('should be able to use Vite to build and test apps', async () => {
const appName = uniq('app');
const libName = uniq('lib');
runCLI(
`generate @nrwl/react:app ${appName} --bundler=vite --no-interactive`
);
runCLI(
`generate @nrwl/react:lib ${libName} --bundler=none --no-interactive`
);
// Library generated with Vite
checkFilesExist(`libs/${libName}/vite.config.ts`);
const mainPath = `apps/${appName}/src/main.tsx`;
updateFile(
mainPath,
`
import '@${proj}/${libName}';
${readFile(mainPath)}
`
);
runCLI(`build ${appName}`);
checkFilesExist(`dist/apps/${appName}/index.html`);
}, 250_000);
async function testGeneratedApp( async function testGeneratedApp(
appName, appName,
opts: { opts: {
@ -243,9 +270,4 @@ describe('React Applications and Libs with PostCSS', () => {
expect(buildResults.combinedOutput).toMatch(/HELLO FROM APP/); expect(buildResults.combinedOutput).toMatch(/HELLO FROM APP/);
expect(buildResults.combinedOutput).not.toMatch(/HELLO FROM LIB/); expect(buildResults.combinedOutput).not.toMatch(/HELLO FROM LIB/);
}, 250_000); }, 250_000);
it('should run default jest tests', async () => {
await expectJestTestsToPass('@nrwl/react:lib');
await expectJestTestsToPass('@nrwl/react:app');
}, 200000);
}); });

View File

@ -60,7 +60,7 @@ describe('Web Components Applications with bundler set as vite', () => {
runCLI(`generate @nrwl/web:app ${appName} --bundler=vite --no-interactive`); runCLI(`generate @nrwl/web:app ${appName} --bundler=vite --no-interactive`);
runCLI( runCLI(
`generate @nrwl/react:lib ${libName} --buildable --no-interactive --compiler swc` `generate @nrwl/react:lib ${libName} --bundler=vite --no-interactive`
); );
createFile(`dist/apps/${appName}/_should_remove.txt`); createFile(`dist/apps/${appName}/_should_remove.txt`);

View File

@ -80,7 +80,7 @@ describe('Web Components Applications', () => {
`generate @nrwl/web:app ${appName} --bundler=webpack --no-interactive --compiler swc` `generate @nrwl/web:app ${appName} --bundler=webpack --no-interactive --compiler swc`
); );
runCLI( runCLI(
`generate @nrwl/react:lib ${libName} --buildable --no-interactive --compiler swc` `generate @nrwl/react:lib ${libName} --bundler=rollup --no-interactive --compiler swc`
); );
createFile(`dist/apps/${appName}/_should_remove.txt`); createFile(`dist/apps/${appName}/_should_remove.txt`);

View File

@ -281,7 +281,7 @@ function requiresRemovingOfPackages(
* @typedef EnsurePackageOptions * @typedef EnsurePackageOptions
* @type {object} * @type {object}
* @property {boolean} dev indicate if the package is a dev dependency * @property {boolean} dev indicate if the package is a dev dependency
* @property {throwOnMissing} boolean throws an error when the packag is missing * @property {throwOnMissing} boolean throws an error when the package is missing
*/ */
/** /**
@ -297,7 +297,7 @@ function requiresRemovingOfPackages(
* *
* @param tree the file system tree * @param tree the file system tree
* @param pkg the package to check (e.g. @nrwl/jest) * @param pkg the package to check (e.g. @nrwl/jest)
* @param requiredVersion the version to check * @param requiredVersion the version or semver range to check (e.g. ~1.0.0, >=1.0.0 <2.0.0)
* @param {EnsurePackageOptions} options * @param {EnsurePackageOptions} options
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */

View File

@ -1,13 +1,16 @@
import { import {
ensurePackage,
formatFiles, formatFiles,
normalizePath, normalizePath,
Tree, Tree,
visitNotIgnoredFiles, visitNotIgnoredFiles,
} from '@nrwl/devkit'; } from '@nrwl/devkit';
import { tsquery } from '@phenomnomnominal/tsquery'; import { tsquery } from '@phenomnomnominal/tsquery';
import { nxVersion } from '../../utils/versions';
export default async function eslint8Updates(tree: Tree) { export default async function eslint8Updates(tree: Tree) {
try { try {
await ensurePackage(tree, '@nrwl/jest/', nxVersion);
const { addPropertyToJestConfig } = await import('@nrwl/jest'); const { addPropertyToJestConfig } = await import('@nrwl/jest');
const existingJestConfigPath = normalizePath( const existingJestConfigPath = normalizePath(
'tools/eslint-rules/jest.config.js' 'tools/eslint-rules/jest.config.js'

View File

@ -1,3 +1,4 @@
import * as devkit from '@nrwl/devkit';
import { import {
addProjectConfiguration, addProjectConfiguration,
joinPathFragments, joinPathFragments,
@ -9,12 +10,6 @@ import {
import { createTreeWithEmptyV1Workspace } from '@nrwl/devkit/testing'; import { createTreeWithEmptyV1Workspace } from '@nrwl/devkit/testing';
import { exampleRootTslintJson } from '@nrwl/linter'; import { exampleRootTslintJson } from '@nrwl/linter';
import { conversionGenerator } from './convert-tslint-to-eslint'; import { conversionGenerator } from './convert-tslint-to-eslint';
import * as devkit from '@nrwl/devkit';
/**
* Don't run actual child_process implementation of installPackagesTask()
*/
// jest.mock('child_process');
const appProjectName = 'nest-app-1'; const appProjectName = 'nest-app-1';
const appProjectRoot = `apps/${appProjectName}`; const appProjectRoot = `apps/${appProjectName}`;

View File

@ -34,6 +34,7 @@
"@nrwl/devkit": "file:../devkit", "@nrwl/devkit": "file:../devkit",
"@nrwl/linter": "file:../linter", "@nrwl/linter": "file:../linter",
"@nrwl/workspace": "file:../workspace", "@nrwl/workspace": "file:../workspace",
"@phenomnomnominal/tsquery": "4.1.1",
"chalk": "4.1.0", "chalk": "4.1.0",
"minimatch": "3.0.5", "minimatch": "3.0.5",
"semver": "7.3.4" "semver": "7.3.4"

View File

@ -1021,4 +1021,21 @@ describe('app', () => {
).toBe(false); ).toBe(false);
}); });
}); });
describe('setting generator defaults', () => {
it('should set libraries to use vitest when app uses vite bundler', async () => {
await applicationGenerator(appTree, {
...schema,
name: 'my-app',
bundler: 'vite',
});
const workspace = readWorkspaceConfiguration(appTree);
expect(workspace.generators['@nrwl/react']).toMatchObject({
library: {
unitTestRunner: 'vitest',
},
});
});
});
}); });

View File

@ -22,25 +22,33 @@ export function setDefaults(host: Tree, options: NormalizedSchema) {
const prev = { ...workspace.generators['@nrwl/react'] }; const prev = { ...workspace.generators['@nrwl/react'] };
workspace.generators = { const appDefaults = {
...workspace.generators,
'@nrwl/react': {
...prev,
application: {
style: options.style, style: options.style,
linter: options.linter, linter: options.linter,
bundler: options.bundler, bundler: options.bundler,
...prev.application, ...prev.application,
}, };
component: { const componentDefaults = {
style: options.style, style: options.style,
...prev.component, ...prev.component,
}, };
library: { const libDefaults = {
style: options.style, style: options.style,
linter: options.linter, linter: options.linter,
...prev.library, ...prev.library,
}, };
// Future react libs should use same test runner as the app.
if (options.unitTestRunner === 'vitest') {
// Note: We don't set bundler: 'vite' for libraries because that means they are buildable.
libDefaults.unitTestRunner ??= 'vitest';
}
workspace.generators = {
...workspace.generators,
'@nrwl/react': {
...prev,
application: appDefaults,
component: componentDefaults,
library: libDefaults,
}, },
}; };

View File

@ -125,7 +125,8 @@
"buildable": { "buildable": {
"type": "boolean", "type": "boolean",
"default": false, "default": false,
"description": "Generate a buildable library. If a bundler is set then the library is buildable by default." "description": "Generate a buildable library that uses rollup to bundle. Use 'bundler' option instead for greater control (none, vite, rollup).",
"x-deprecated": true
}, },
"importPath": { "importPath": {
"type": "string", "type": "string",
@ -164,13 +165,14 @@
"type": "string", "type": "string",
"description": "The bundler to use. Choosing 'none' means this library is not buildable.", "description": "The bundler to use. Choosing 'none' means this library is not buildable.",
"enum": ["none", "vite", "rollup"], "enum": ["none", "vite", "rollup"],
"default": "none",
"x-prompt": "Which bundler would you like to use to build the library? Choose 'none' to skip build setup." "x-prompt": "Which bundler would you like to use to build the library? Choose 'none' to skip build setup."
}, },
"compiler": { "compiler": {
"type": "string", "type": "string",
"enum": ["babel", "swc"], "enum": ["babel", "swc"],
"default": "babel", "default": "babel",
"description": "Which compiler to use. Does not apply if bundler is set to Vite." "description": "Which compiler to use. Only applies to `bundler: 'rollup'`."
}, },
"skipPackageJson": { "skipPackageJson": {
"description": "Do not add dependencies to `package.json`.", "description": "Do not add dependencies to `package.json`.",

View File

@ -24,7 +24,6 @@ export function installWebpackRollupDependencies(tree: Tree) {
{ {
'@babel/preset-react': '^7.14.5', '@babel/preset-react': '^7.14.5',
'@pmmmwh/react-refresh-webpack-plugin': '^0.5.7', '@pmmmwh/react-refresh-webpack-plugin': '^0.5.7',
'@phenomnomnominal/tsquery': '4.1.1',
'@svgr/webpack': '^6.1.2', '@svgr/webpack': '^6.1.2',
'css-loader': '^6.4.0', 'css-loader': '^6.4.0',
'react-refresh': '^0.10.0', 'react-refresh': '^0.10.0',

View File

@ -16,7 +16,6 @@ import {
swcHelpersVersion, swcHelpersVersion,
swcLoaderVersion, swcLoaderVersion,
tsLibVersion, tsLibVersion,
tsQueryVersion,
urlLoaderVersion, urlLoaderVersion,
} from '../../utils/versions'; } from '../../utils/versions';
import { addBabelInputs } from '@nrwl/js/src/utils/add-babel-inputs'; import { addBabelInputs } from '@nrwl/js/src/utils/add-babel-inputs';
@ -57,7 +56,6 @@ export async function webpackInitGenerator(tree: Tree, schema: Schema) {
{ {
'@pmmmwh/react-refresh-webpack-plugin': '@pmmmwh/react-refresh-webpack-plugin':
reactRefreshWebpackPluginVersion, reactRefreshWebpackPluginVersion,
'@phenomnomnominal/tsquery': tsQueryVersion,
'@svgr/webpack': svgrWebpackVersion, '@svgr/webpack': svgrWebpackVersion,
'react-refresh': reactRefreshVersion, 'react-refresh': reactRefreshVersion,
'url-loader': urlLoaderVersion, 'url-loader': urlLoaderVersion,

View File

@ -8,7 +8,7 @@
"properties": { "properties": {
"uiFramework": { "uiFramework": {
"type": "string", "type": "string",
"description": "UI Framework to use for Vite.", "description": "UI Framework to use for Webpack.",
"enum": ["react", "none"], "enum": ["react", "none"],
"x-prompt": "What UI framework plugin should Webpack use?" "x-prompt": "What UI framework plugin should Webpack use?"
}, },

View File

@ -6,7 +6,6 @@ export const tsLibVersion = '^2.3.0';
// React apps // React apps
export const reactRefreshWebpackPluginVersion = '^0.5.7'; export const reactRefreshWebpackPluginVersion = '^0.5.7';
export const tsQueryVersion = '4.1.1';
export const svgrWebpackVersion = '^6.1.2'; export const svgrWebpackVersion = '^6.1.2';
export const reactRefreshVersion = '^0.10.0'; export const reactRefreshVersion = '^0.10.0';
export const urlLoaderVersion = '^4.1.1'; export const urlLoaderVersion = '^4.1.1';