CRACO (Create React App Configuration Override)

repository·main·Indexed 27 days ago

https://github.com/dilanx/craco

CRACO provides a configuration layer for Create React App, allowing developers to customize settings for Babel, ESLint, PostCSS, Webpack, Jest, and TypeScript without the need to 'eject'. It includes a CLI to replace react-scripts and official TypeScript type definitions via @craco/types for type-safe configurations.

Tokens
22.1K
Snippets
73
Records
154
Agent score
91%

What's inside CRACO

  1. Understand CRACO (Create React App Configuration Override)

    main

    CRACO allows you to customize the configuration of a Create React App project without having to eject. You can override settings for tools like ESLint, Babel, PostCSS, and more using a single configuration file located at the root of your project.

    Warning: Using CRACO breaks the configuration guarantees provided by Create React App. By using it, you take ownership of your configurations, and the project does not provide support for the underlying CRA settings.

  2. Explore community maintained CRACO plugins

    main
    A variety of community-maintained plugins are available to extend CRACO functionality, covering loaders (Babel, Less, Sass, PureScript), styling (Ant Design, CSS Modules, Styled JSX, Linaria), and build optimizations (Esbuild, Image Optimizer, Workbox).
  3. Add TypeScript typings for HTML imports

    main

    If you are using TypeScript and encounter a "Cannot find module" error when importing HTML files (e.g., import foo from './foo.html';), add the following declaration to your typings.d.ts file to allow HTML modules to be treated as strings.

    /**
     * To resolve "Cannot find module error on importing html file in webpack" if you use Typescript
     * Usage: import foo from './foo.html';
     */
    declare module '*.html' {
      const value: string;
      export default value;
    }
  4. Use less-loader with CRACO

    main

    To use less-loader in a Create React App project managed by CRACO, install the craco-less plugin and add it to the plugins array in your craco.config.js file. You can pass configuration options to the plugin, such as noIeCompat.

    // Official documentation available at: https://github.com/FormAPI/craco-less
    
    module.exports = {
      plugins: [
        {
          plugin: require('craco-less'),
          options: {
            noIeCompat: true,
          },
        },
      ],
    };
  5. Create a CRACO configuration file

    main

    CRACO looks for configuration files in a specific order of precedence. You can use any of the following filenames:

    1. craco.config.ts
    2. craco.config.js
    3. craco.config.cjs
    4. .cracorc.ts
    5. .cracorc.js
    6. .cracorc

    If multiple files exist, the one highest on this list is used. You can also override this behavior by specifying a path in your package.json or via the CLI.

  6. Deploy the website

    main

    The website can be deployed using different methods depending on your hosting setup.

    Deploy via SSH

    Use the USE_SSH=true environment variable to deploy via SSH:

    USE_SSH=true yarn deploy

    Deploy to GitHub Pages

    To deploy to GitHub Pages (which pushes the build to the gh-pages branch), provide your GitHub username via the GIT_USER environment variable:

    GIT_USER=<Your GitHub username> yarn deploy
  7. Configure babel-plugin-react-css-modules with CRACO

    main

    To use babel-plugin-react-css-modules in a Create React App project via CRACO, you must synchronize the localIdentName in the style.modules configuration with the generateScopedName option in the babel.plugins configuration. This ensures that the class names generated by CSS Modules match the names expected by the Babel plugin when transforming component attributes.

    Example configuration for craco.config.js:

    const CSS_MODULE_LOCAL_IDENT_NAME = '[local]___[hash:base64:5]';
    
    module.exports = {
      style: {
        modules: {
          localIdentName: CSS_MODULE_LOCAL_IDENT_NAME,
        },
      },
      babel: {
        plugins: [
          [
            'babel-plugin-react-css-modules',
            {
              generateScopedName: CSS_MODULE_LOCAL_IDENT_NAME,
              attributeNames: { activeStyleName: 'activeClassName' },
            },
          ],
        ],
      },
    };