Skip to main content

Develop With Farm

In this chapter, we will introduce commonly used configuration and plugins to help you build complex production-ready web project with Farm.

note

This chapter reuse the project we created in chapter 1

We'll setup our project step by step:

  1. Introduce popular component library antd, and configure necessary plugins for it
  2. Introduce commonly used plugins like postcss, svgr, less and so on.
  3. Configure proxies and other useful dev server options

Introduce Component Library

A component library is often necessary when develop a web project, in this section, we will use ant-design as a demo to show How to add component libraries in Farm.

We use ant design here only for illustration, you can introduce any component library. Farm does not have objection.

First we need to install ant-design into our project:

Ant Design needs Sass, so we also need to install plugins for compiling scss. We can use @farmfe/plugin-sass which is a Rust Plugin officially provided by Farm:

Then add this plugin to plugins:

farm.config.ts

Now Antd is ready, add it to our project:

Then execute npm start and open http://localhost:9000 in browser:

Styling the Project

Now we have successfully introduced a component library into our project. Next we'll learn how to styling.

Create a Basic Admin Site Layout

First we create a new app.tsx next to index.tsx:

Content of app.tsx(It's demo code from official site of Antd):

app.tsx

Then modify index.tsx as:

index.tsx

Then we get a Basic admin layout:

Styling With Css Modules

Farm supports css modules out of box, by default, Farm will treat any .module.(css|scss|less) as css modules. Firstly we create a app.module.scss:

Content of app.module.scss:

app.module.scss

Then import app.module.scss in app.tsx and save it:

Then your page should be updated like below:

Using Css Preprocessor

Farm provided official js plugins for postcss(@farmfe/js-plugin-postcss) and less(@farmfe/js-plugin-less) (We have already installed rust plugin sass(@farmfe/plugin-sass) above).

To use postcss, First we need to install the plugin:

then configure it in plugins of farm.config.ts:

farm.config.ts

Now postcss is fully supported in Farm, you can use popular postcss plugins tailwind, px2rem, etc. We won't cover postcss details here, refer to postcss docs for more details.

tip

Refer to Farm Plugins to learn more about Farm plugins.

Using Public Directory

For assets that don't need compilation, you can put them under public directory. Add a favicon.ico under public:

Then the favicon is available for your website. And you can also put some static assets that can be directly fetched, for example, images:

then you can use /images/background.png to fetch the image, for example, <img src="/images/background.png">.

note

Using config option publicDir to custom your public directory.

Configuring publicPath

Use compilation.output.publicPath to configuring url prefix for dynamic resources loading and when inject <script> and <link> tags into html. We add following config in farm.config.ts

farm.config.ts

When building, the injected resources url would be https://cdn.com/index-s2f3.s14dqwa.js. For example, in your output html, all <script> and <link> would be:

and when loading dynamic scripts and css, the dynamic fetched resources url would also be: https://cdn.com/<asset-path>

Configuring Alias And Externals

Alias and externals are also most useful configurations, we can use compilation.resolve.alias and compilation.externals in Farm:

farm.config.ts

Configuring DevServer

You can find server configuration in Farm Dev Server Options.

Useful Configuration

Example configuration:

For above examples, we used following options:

  • open: open the browser with specified port automatically
  • port: set the dev sever port to 9001
  • hmr: set the hmr port and watched files, we ignores file changes under auto_generated directory.

Setup Proxy

Configure server proxy. farm uses http-proxy as a proxy for the development server. Based on http-proxy implementation, specific options refer to its documentation, example:

Configuring root and envDir

Use root and envDir to specify your project root and the directory to load env variables. Add following options in farm.config.ts:

farm.config.ts
note

For details about envDir, see Environment Variables and Modes

Extremely Fast Web Build Tool Written in Rust

Copyright © 2024 Farm Community. Built with Docusaurus.