zoukankan      html  css  js  c++  java
  • [Tools] Nx workspace with React

    Nx

    Create a new empty Nx workspace

    npx create-nx-workspace <workspace name>
    

    Choose

    Empty workspace
    Nx Cloud
    Nx CLI

    Useful Commands

    yarn nx list
    

    Shows all the available packages for nx

    yarn nx list @nrwl/react
    

    Generate a new React app with Nx

    bash yarn add @nrwl/react yarn nx g @nrwl/react:application --name store

    Useful Commands

    bash yarn nx g @nrwl/react:application --help

    Running the application

    yarn nx run store:serve
    

    You can modify the port in workspace.json:

      "serve": {
        "executor": "@nrwl/web:dev-server",
        "options": {
          "buildTarget": "store:build",
    +     "port": 3000
      },
    

    Generate a shared react lib for store application

    yarn nx g @nrwl/react:lib ui-shared --directory=store
    

    Generate a component inside lib

    yarn nx g @nrwl/react:component header --project=store-ui-shared
    

    Choose Y to export the component.

    Using the generated component inside application

    You can find the component import path from tsconfig.base.json:

        "paths": {
          "@egghead/store/ui-shared": ["libs/store/ui-shared/src/index.ts"]
        }
    

    app.tsx:

    import {Header} from '@egghead/store/ui-shared'
    

    Genearte a Typescript lib

    yarn nx g @nrwl/workspace:lib util-formatters --directory=store
    

    Generate a Lib for application by --appProject

     yarn nx g @nrwl/react:lib feature-game-detail --directory=store --appProject=store
    

    This will add some routing config into application

    Add a backend server

    yarn add -D @nrwl/express
    
    yarn nx g @nrwl/express:application api --frontendProject=store
    

    Added --frontendProject will also generate a proxy.conf.json file in store application.

    yarn nx run api:serve
    

    Useful commands

    Run Frontend and backend in one command

    yarn nx run-many --target=serve --projects=api,store --parallel=true
    

    Modify workspace.json to run multi applications

            "serve": {...},
            "serveAppAndApi": {
              "builder": "@nrwl/workspace:run-commands",
              "options": {
                "commands": [
                  {
                    "command": "nx run api:serve"
                  },
                  {
                    "command": "nx run store:serve"
                  }
                ]
              }
            },
    

    Run:

    yarn nx run store:serveAppAndApi
    

    Generate a lib which share between backend and frontend

    yarn nx g @nrwl/workspace:lib util-interface --directory=api
    

    It will generate under libs/api/util-interface.

    Use storybook

    yarn add @nrwl/storybook -D
    yarn nx list @nrwl/react
    

    You should be able to see storybook-configuration.

    yarn nx generate @nrwl/react:stroybook-configruation store-ui-shared --configureCypress --generateStories
    

    It will generate Storybook under libs/ui-shared/.storybook & Cypress under store-ui-shared-e2e folder

    Run storybook

    yarn nx run store-ui-shared:storybook
    

    Run Cypress

    yarn nx run store-ui-shared-e2e:e2e --watch
    

    Run JEST

    yarn nx run store:test
    

    Build application

    yarn nx run store:build --configuration=production
    

    or

    yarn nx build store --configuration=production
    

    Will generate a dist folder

    Lint application

    yarn nx run store:lint
    

    Run the applications/libs which affected

    Nx use git history to detect which applications or libs have been changed.

    And then run the affected libs and applications to speed up testing.

    yarn nx affected:test --base=master
    yarn nx affected:lint --base=master
    yarn nx affected:dep-graph --base=master
    

    Run unit testings based on master branch.

    yarn nx affected:test --all
    yarn nx affected:test --all --skip-nx-cache
    

    Nx will cache the running affected result into node_modules/.cache to speed up next runtime.

    You can --skip-nx-cache or delete cache.

    Migrations

    yarn nx migrate latest
    

    then install the new packages.

    yarn
    

    If there is migrations.json created:

    yarn nx migrate --run-migrations=migrations.json
    
  • 相关阅读:
    HDU 5222 ——Exploration——————【并查集+拓扑排序判有向环】
    nyoj 600——花儿朵朵——【离散化、线段树插线问点】
    Poj 3667——hotel——————【线段树区间合并】
    BNU 4260 ——Trick or Treat——————【三分求抛物线顶点】
    编写自己的Arduino库
    怎样看懂电路板?电路板短路检查方法是什么?
    Intel HEX格式
    关于2的补码
    sysfs是什么??
    Arduino 串口的一些高级用法
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14311094.html
Copyright © 2011-2022 走看看