Broadleaf Microservices
  • v1.0.0-latest-prod

Upgrading the Starter to AdminWeb 1.6.0

Table of Contents

We’ve migrated from Create React App to Vite to speed up development time, reduce time to first paint, and cut down heavily on 3rd-party dependencies. This will require some migration to complete the upgrade to AdminWeb 1.6.0 from 1.5.x or lower. Below are the list of changes and migration steps.

Migration Steps

  1. Move src/styles out of src/ into the root

  2. Rename environment properties prefixed with REACT_APP_ to be prefixed with VITE_

  3. index.html

    1. Move public/index.html to the root

    2. Add <link rel="stylesheet" href="/styles/global.css" /> to the <head>

    3. Add window.global window; to index.html in the <script> before the try-catch

    4. Remove references to %PUBLIC_URL%

    5. Add <script type="module" src="/src/index.tsx"></script> under the root <div> in the <body>

  4. Add jest.config.ts to the root of the project

    /*
     * For a detailed explanation regarding each configuration property and type check, visit:
     * https://jestjs.io/docs/en/configuration.html
     */
    export default {
      // The test environment that will be used for testing
      testEnvironment: "jsdom",
      // Automatically clear mock calls and instances between every test
      clearMocks: true,
      // The directory where Jest should output its coverage files
      coverageDirectory: "coverage",
      // The paths to modules that run some code to configure or set up the testing environment before each test
      "setupFiles": [
        "./test/jest.js"
      ],
      // A list of paths to snapshot serializer modules Jest should use for snapshot testing
      "snapshotSerializers": [
        "enzyme-to-json/serializer"
      ],
      // A map from regular expressions to paths to transformers
      transform: {
        '^.+\\.(js|jsx|ts|tsx)$': 'ts-jest',
      },
      // Indicates which provider should be used to instrument code for coverage
      coverageProvider: "v8",
      // All imported modules in your tests should be mocked automatically
      // automock: false,
      // Stop running tests after `n` failures
      // bail: 0,
      // The directory where Jest should store its cached dependency information
      // cacheDirectory: "/private/var/folders/3r/1g2b5mkd5n5dsw5y4vh667b40000gp/T/jest_dy",
      // Indicates whether the coverage information should be collected while executing the test
      // collectCoverage: false,
      // An array of glob patterns indicating a set of files for which coverage information should be collected
      // collectCoverageFrom: undefined,
      // An array of regexp pattern strings used to skip coverage collection
      // coveragePathIgnorePatterns: [
      //   "/node_modules/"
      // ],
      // A list of reporter names that Jest uses when writing coverage reports
      // coverageReporters: [
      //   "json",
      //   "text",
      //   "lcov",
      //   "clover"
      // ],
      // An object that configures minimum threshold enforcement for coverage results
      // coverageThreshold: undefined,
      // A path to a custom dependency extractor
      // dependencyExtractor: undefined,
      // Make calling deprecated APIs throw helpful error messages
      // errorOnDeprecated: false,
      // Force coverage collection from ignored files using an array of glob patterns
      // forceCoverageMatch: [],
      // A path to a module which exports an async function that is triggered once before all test suites
      // globalSetup: undefined,
      // A path to a module which exports an async function that is triggered once after all test suites
      // globalTeardown: undefined,
      // A set of global variables that need to be available in all test environments
      // globals: {},
      // The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
      // maxWorkers: "50%",
      // An array of directory names to be searched recursively up from the requiring module's location
      // moduleDirectories: [
      //   "node_modules"
      // ],
      // An array of file extensions your modules use
      // moduleFileExtensions: [
      //   "js",
      //   "json",
      //   "jsx",
      //   "ts",
      //   "tsx",
      //   "node"
      // ],
      // A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
      // moduleNameMapper: {},
      // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
      // modulePathIgnorePatterns: [],
      // Activates notifications for test results
      // notify: false,
      // An enum that specifies notification mode. Requires { notify: true }
      // notifyMode: "failure-change",
      // A preset that is used as a base for Jest's configuration
      // preset: undefined,
      // Run tests from one or more projects
      // projects: undefined,
      // Use this configuration option to add custom reporters to Jest
      // reporters: undefined,
      // Automatically reset mock state between every test
      // resetMocks: false,
      // Reset the module registry before running each individual test
      // resetModules: false,
      // A path to a custom resolver
      // resolver: undefined,
      // Automatically restore mock state between every test
      // restoreMocks: false,
      // The root directory that Jest should scan for tests and modules within
      // rootDir: undefined,
      // A list of paths to directories that Jest should use to search for files in
      // roots: [
      //   "<rootDir>"
      // ],
      // Allows you to use a custom runner instead of Jest's default test runner
      // runner: "jest-runner",
      // A list of paths to modules that run some code to configure or set up the testing framework before each test
      // setupFilesAfterEnv: [],
      // The number of seconds after which a test is considered as slow and reported as such in the results.
      // slowTestThreshold: 5,
      // Options that will be passed to the testEnvironment
      // testEnvironmentOptions: {},
      // Adds a location field to test results
      // testLocationInResults: false,
      // The glob patterns Jest uses to detect test files
      // testMatch: [
      //   "**/__tests__/**/*.[jt]s?(x)",
      //   "**/?(*.)+(spec|test).[tj]s?(x)"
      // ],
      // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
      // testPathIgnorePatterns: [
      //   "/node_modules/"
      // ],
      // The regexp pattern or array of patterns that Jest uses to detect test files
      // testRegex: [],
      // This option allows the use of a custom results processor
      // testResultsProcessor: undefined,
      // This option allows use of a custom test runner
      // testRunner: "jasmine2",
      // This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
      // testURL: "http://localhost",
      // Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
      // timers: "real",
      // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
      // transformIgnorePatterns: [
      //   "/node_modules/",
      //   "\\.pnp\\.[^\\/]+$"
      // ],
      // An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
      // unmockedModulePathPatterns: undefined,
      // Indicates whether each individual test should be reported during the run
      // verbose: undefined,
      // An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
      // watchPathIgnorePatterns: [],
      // Whether to use watchman for file crawling
      // watchman: true,
    };
  5. Apply this patch to your package.json

    Index: package.json
    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
    ===================================================================
    diff --git a/package.json b/package.json
    --- a/package.json  (revision c9039c1da7bd43850621050192404dc18e691fc8)
    +++ b/package.json  (date 1626296436729)
    @@ -31,38 +31,47 @@
         "react-dom": "^16.13.1",
         "react-intl": "^5.8.3",
         "react-router-dom": "^5.2.0",
    -    "react-scripts": "3.4.1",
         "tailwindcss": "^1.8.10",
         "turbocolor": "^2.6.1",
         "yup": "^0.32.8"
       },
       "devDependencies": {
    -    "@fullhuman/postcss-purgecss": "^1.2.0",
    +    "@rollup/plugin-inject": "^4.0.2",
    +    "@vitejs/plugin-react-refresh": "^1.3.5",
         "autoprefixer": "^9.6.1",
    +    "babel-eslint": "^10.1.0",
         "babel-polyfill": "^6.26.0",
         "cpr": "^3.0.1",
         "cross-env": "^6.0.3",
    -    "cssnano": "^4.1.10",
         "enzyme": "^3.10.0",
         "enzyme-adapter-react-16": "^1.15.1",
         "enzyme-to-json": "^3.4.3",
    +    "eslint": "^7.30.0",
    +    "eslint-config-react-app": "^6.0.0",
    +    "eslint-plugin-flowtype": "^5.8.0",
    +    "eslint-plugin-import": "^2.23.4",
    +    "eslint-plugin-jsx-a11y": "^6.4.1",
         "eslint-plugin-notice": "^0.8.9",
    +    "eslint-plugin-react": "^7.24.0",
    +    "eslint-plugin-react-hooks": "^4.2.0",
         "husky": "3.0.4",
    -    "jest": "24.9.0",
    +    "jest": "26.6.0",
         "jest-sonar-reporter": "^2.0.0",
         "lint-staged": "^9.5.0",
         "mock-local-storage": "^1.1.7",
         "node-sass-chokidar": "^1.3.5",
    -    "postcss-cli": "^6.1.3",
         "prettier": "^1.15.2",
         "sinon": "^7.5.0",
         "source-map-explorer": "^2.1.1",
    -    "watch": "^1.0.2"
    +    "ts-jest": "26.5.6",
    +    "ts-node": "^10.0.0",
    +    "typescript": "^4.3.5",
    +    "vite": "^2.4.1"
       },
       "scripts": {
         "analyze:dependencies": "source-map-explorer build/static/js/1.*",
         "analyze:main": "source-map-explorer build/static/js/main.*",
    -    "build": "react-scripts build",
    +    "build": "NODE_ENV=production tsc && vite build",
         "build:langs": "node scripts/build-langs.js",
         "precommit:langs": "yarn build:langs && git add ./src/messages/",
         "postbuild": "yarn sample:build",
    @@ -75,8 +84,9 @@
         "sample:docker": "yarn --cwd ./samples/express docker",
         "sample:install": "yarn --cwd ./samples/express install",
         "sample:start": "yarn --cwd ./samples/express start",
    -    "start": "cross-env HTTPS=true BROWSER=none react-scripts start",
    -    "test": "cross-env CI=true react-scripts test --env=jsdom --passWithNoTests",
    +    "start": "cross-env vite preview --port 3000",
    +    "test": "jest",
    +    "watch": "cross-env vite",
         "test:coverage": "yarn test --coverage --watchAll=false --testResultsProcessor jest-sonar-reporter"
       },
       "browserslist": [
    @@ -98,18 +108,13 @@
           "prettier --single-quote --write",
           "eslint --fix --max-warnings 0",
           "git add",
    -      "react-scripts --bail --findRelatedTests"
    +      "jest --bail --findRelatedTests"
         ],
         "src/**/*.{json,css,scss}": [
           "prettier --single-quote --write",
           "git add"
         ]
       },
    -  "jest": {
    -    "snapshotSerializers": [
    -      "enzyme-to-json/serializer"
    -    ]
    -  },
       "jestSonar": {
         "reportPath": "coverage"
       }
  6. Remove postcss.config.js

  7. Apply this patch to you samples/express/package.json

    Index: samples/express/package.json
    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
    ===================================================================
    diff --git a/samples/express/package.json b/samples/express/package.json
    --- a/samples/express/package.json  (revision c9039c1da7bd43850621050192404dc18e691fc8)
    +++ b/samples/express/package.json  (date 1626296545502)
    @@ -34,7 +34,7 @@
       },
       "scripts": {
         "clean": "rimraf ./build",
    -    "build": "yarn clean && cpr ../../build ./build",
    +    "build": "yarn clean && cpr ../../dist ./build",
         "prestart": "yarn build",
         "start": "node ./index.js",
         "docker": "docker build .",
  8. Remove samples/express/yarn.lock

  9. In index.js, replace the import for tailwind.css and index.scss with import '../styles/index.css'

  10. Remove src/setupTests.js

  11. Move src/index.scss to styles/

    1. Also update the import of any fonts to not be prefixed with styles/ as this is not necessary: @import url('styles/font/inter.css'); becomes @import url('font/inter.css');

  12. Add test/jest.js

    import 'babel-polyfill';
  13. Add vite.config.js

{
  "compilerOptions": {
    "baseUrl": "./src",
    "target": "esnext",
    "module": "esnext",
    "lib": ["dom", "dom.Iterable", "esnext"],
    "moduleResolution": "node",
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "allowSyntheticDefaultImports": true,
    "strict": false,
    "forceConsistentCasingInFileNames": false,
    "noEmit": true,
    "jsx": "react-jsx",
    "outDir": "./dist",
    "isolatedModules": true,
    "sourceMap": true
  }
}

+ . Run yarn install --force . To start up for local dev, use yarn watch instead of yarn start . To start up for production, use yarn sample:start after doing a yarn build