diff --git a/README.md b/README.md index 1fda4cfdbb..0e43fea55a 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,81 @@ -# rspack +

Nx - Smart, Fast and Extensible Build System

-This library was generated with [Nx](https://nx.dev). +
-## Building +# Nx: Smart, Fast and Extensible Build System -Run `nx build rspack` to build the library. +Nx is a next generation build system with first class monorepo support and powerful integrations. -## Running unit tests +This package is a Rspack plugin for Nx. -Run `nx test rspack` to execute the unit tests via [Jest](https://jestjs.io). +## Getting Started + +Use `--preset=@nrwl/rspack` when creating new workspace. + +e.g. + +```bash +npx create-nx-workspace@latest rspack-demo --preset=@nrwl/rspack +``` + +Now, you can go into the `rspack-demo` folder and start development. + +```bash +cd rspack-demo +npm start +``` + +You can also run lint, test, and e2e scripts for the project. + +```bash +npm run lint +npm run test +npm run e2e +``` + +## Existing workspaces + +You can add Rspack to any existing Nx workspace. + +First, install the plugin: + +```bash +npm install --save-dev @nrwl/rspack +``` + +Then, run the `rspack-project` generator: + +```bash +npx nx g @nrwl/rspack:rspack-project --skipValidation +``` + +**Note:** The `--skipValidation` option allows you to overwrite existing build targets. + +## Workspace libraries + +The `@nrwl/rspack` executor support importing workspace libs into the app. + +```bash +npx nx g @nrwl/react:lib mylib +``` + +Import the new library in your app. + +```typescript jsx +// src/app/app.tsx +import { Mylib } from '@rspack-demo/mylib'; + +// ... + +export default function App() { + return ; +} +``` + +Now, run the dev server again to see the new library in action. + +```bash +npm start +``` + +**Note:** You must restart the server if you make any changes to your library. diff --git a/generators.json b/generators.json index b263978a59..94d24a47ff 100644 --- a/generators.json +++ b/generators.json @@ -6,19 +6,26 @@ "rspack-project": { "factory": "./src/generators/rspack-project/rspack-project", "schema": "./src/generators/rspack-project/schema.json", - "description": "rspack generator" + "description": "Rspack project generator." }, "init": { "factory": "./src/generators/init/init", "schema": "./src/generators/init/schema.json", - "description": "init generator", + "description": "Rspack init generator.", "hidden": true }, "preset": { "factory": "./src/generators/preset/preset", "schema": "./src/generators/preset/schema.json", - "description": "preset generator", + "description": "React preset generator.", "hidden": true + }, + "application": { + "factory": "./src/generators/application/application", + "schema": "./src/generators/application/schema.json", + "aliases": ["app"], + "x-type": "application", + "description": "React application generator." } } } diff --git a/package.json b/package.json index 692e5e27ac..de4bf5c09b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nrwl/rspack", - "version": "15.7.0-beta.1", + "version": "0.0.1", "type": "commonjs", "repository": { "type": "git", @@ -19,7 +19,11 @@ "generators": "./generators.json", "executors": "./executors.json", "dependencies": { - "@nrwl/devkit": "^15.7.2", - "copy-webpack-plugin": "^10.2.4" + "@nrwl/linter": "^15.8.1", + "@nrwl/devkit": "^15.8.1", + "ajv": "^8.12.0", + "sass-loader": "^12.2.0", + "stylus-loader": "^7.1.0", + "less-loader": "11.1.0" } } diff --git a/src/generators/application/application.ts b/src/generators/application/application.ts new file mode 100644 index 0000000000..00c86601eb --- /dev/null +++ b/src/generators/application/application.ts @@ -0,0 +1,110 @@ +import { + addDependenciesToPackageJson, + addProjectConfiguration, + ensurePackage, + formatFiles, + generateFiles, + joinPathFragments, + names, + offsetFromRoot as _offsetFromRoot, + Tree, +} from '@nrwl/devkit'; +import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial'; +import { version as nxVersion } from 'nx/package.json'; +import * as path from 'path'; +import { + lessVersion, + reactDomVersion, + reactVersion, + sassVersion, + stylusVersion, + typesReactDomVersion, + typesReactVersion, +} from '../../utils/versions'; +import projectGenerator from '../rspack-project/rspack-project'; +import { addCypress } from './lib/add-cypress'; +import { addJest } from './lib/add-jest'; +import { addLinting } from './lib/add-linting'; +import { createTsConfig } from './lib/create-ts-config'; +import { normalizeOptions } from './lib/normalize-options'; +import { ApplicationGeneratorSchema, NormalizedSchema } from './schema'; + +export default async function ( + tree: Tree, + _options: ApplicationGeneratorSchema +) { + await ensurePackage(tree, '@nrwl/react', nxVersion); + // eslint-disable-next-line @typescript-eslint/no-var-requires + const { reactInitGenerator } = require('@nrwl/react'); + + const tasks = []; + const options = normalizeOptions(tree, _options); + + options.style ??= 'css'; + + addProjectConfiguration(tree, options.name, { + root: options.appProjectRoot, + projectType: 'application', + sourceRoot: `${options.appProjectRoot}/src`, + targets: {}, + }); + + const offsetFromRoot = _offsetFromRoot(options.appProjectRoot); + generateFiles(tree, path.join(__dirname, 'files'), options.appProjectRoot, { + ...options, + ...names(options.name), + offsetFromRoot, + template: '', + }); + + createTsConfig( + tree, + options, + joinPathFragments(offsetFromRoot, 'tsconfig.base.json') + ); + + const projectTask = await projectGenerator(tree, { + project: options.name, + devServer: true, + tsConfig: joinPathFragments(options.appProjectRoot, 'tsconfig.app.json'), + uiFramework: 'react', + target: 'web', + main: joinPathFragments(options.appProjectRoot, 'src/main.tsx'), + }); + tasks.push(projectTask); + + const jestTask = await addJest(tree, options); + tasks.push(jestTask); + + const cypressTask = await addCypress(tree, options); + tasks.push(cypressTask); + + const lintTask = await addLinting(tree, options); + tasks.push(lintTask); + + const installTask = addDependenciesToPackageJson( + tree, + { react: reactVersion, 'react-dom': reactDomVersion }, + { + ...getStyleDependency(options), + '@nrwl/react': nxVersion, + '@types/react': typesReactVersion, + '@types/react-dom': typesReactDomVersion, + } + ); + tasks.push(installTask); + + const reactInitTask = await reactInitGenerator(tree, options); + tasks.push(reactInitTask); + + await formatFiles(tree); + + return runTasksInSerial(...tasks); +} + +function getStyleDependency(options: NormalizedSchema): Record { + if (options.style === 'scss') return { sass: sassVersion }; + if (options.style === 'less') return { less: lessVersion }; + if (options.style === 'styl') return { stylus: stylusVersion }; + return {}; +} diff --git a/src/generators/preset/files/src/app/app.module.__style__ b/src/generators/application/files/src/app/app.module.__style__ similarity index 100% rename from src/generators/preset/files/src/app/app.module.__style__ rename to src/generators/application/files/src/app/app.module.__style__ diff --git a/src/generators/preset/files/src/app/app.spec.tsx__template__ b/src/generators/application/files/src/app/app.spec.tsx__template__ similarity index 63% rename from src/generators/preset/files/src/app/app.spec.tsx__template__ rename to src/generators/application/files/src/app/app.spec.tsx__template__ index d229ddcff5..b67b70fea4 100644 --- a/src/generators/preset/files/src/app/app.spec.tsx__template__ +++ b/src/generators/application/files/src/app/app.spec.tsx__template__ @@ -2,10 +2,10 @@ import { render } from '@testing-library/react'; import { App } from './app'; describe('App component', () => { - it('should render a header', () => { + it('should render a welcome message', () => { const { getByTestId } = render(); - expect(getByTestId('header')).toBeTruthy(); + expect(getByTestId('welcome')).toBeTruthy(); }); }); diff --git a/src/generators/application/files/src/app/app.tsx__template__ b/src/generators/application/files/src/app/app.tsx__template__ new file mode 100644 index 0000000000..54cc8cf2e4 --- /dev/null +++ b/src/generators/application/files/src/app/app.tsx__template__ @@ -0,0 +1,10 @@ +import styles from './app.module.<%= style %>'; +import { NxWelcome } from './nx-welcome'; + +export function App() { + return ( +
+ +
+ ); +} diff --git a/src/generators/application/files/src/app/nx-welcome.tsx__template__ b/src/generators/application/files/src/app/nx-welcome.tsx__template__ new file mode 100644 index 0000000000..761a47a0e2 --- /dev/null +++ b/src/generators/application/files/src/app/nx-welcome.tsx__template__ @@ -0,0 +1,820 @@ +/* + * * * * * * * * * * * * * * * * * * * * * * * * * * * * + This is a starter component and can be deleted. + * * * * * * * * * * * * * * * * * * * * * * * * * * * * + Delete this file and get started with your project! + * * * * * * * * * * * * * * * * * * * * * * * * * * * * + */ +export function NxWelcome({ title }: { title: string }) { + return ( + <> +