Configuring Farm
Config File Specâ
By default, Farm reads the configuration from the farm.config.ts|js|mjs
file in the project root directory, an example configuration file:
import { defineConfig } from "@farmfe/core";
export default defineConfig({
root: process.cwd(), // compiled root directory
// compile options
compilation: {
//...
},
// Dev Server options
server: {
hmr: true,
//...
},
// plugin configuration
plugins: [],
});
For config options details, refer to:
Compiler Options
: Configuring compiler options(compilation
field), likeinput
,output
,css compilation
,bundling rules
and so on.Dev Server Options
: Configuring dev server options(server
field), likeport
,host
,protocol
and so on.Shared Options
: Configuring shared options betweencompiler options
anddev server options
, likeroot
,env
and so on.
You can also use farm start/build -c my-config.ts
to use a custom file as config file.
Loading Ts Config Fileâ
Farm support load ts config file like farm.config.ts
out of box. Farm will bundle farm.config.ts
and it's local ts dependencies into farm-config.xxx.mjs
file first and load it from disk. Because Farm compiles the farm.config.ts
into mjs
file, you CAN NOT use __dirname
or __filename
in your farm.config.ts
, use import.meta.url
instead.
Or you can use farm.config.mjs
or farm.config.cjs
with @type
to support types avoid bundling farm.config.ts
:
/**
* @type {import('@farmfe/core').UserConfig}
*/
export default {
// ...
}
Examplesâ
Input and Outputâ
import { defineConfig } from "@farmfe/core";
export default defineConfig({
// compile options
compilation: {
input: {
index: './src/index.html',
about: './src/about.html',
},
output: {
path: 'build',
publicPath: process.env.NODE_ENV === 'production' ? 'https://my-cdn.com' : '/'
}
},
});
In above example, we configured ./src/index.html
and ./src/about.html
as input, then output the compiled resources to build
dir.
Dev Server Portâ
import { defineConfig } from "@farmfe/core";
export default defineConfig({
server: {
port: 9801
}
});
Disable Default Optimizationsâ
import { defineConfig } from "@farmfe/core";
export default defineConfig({
// compile options
compilation: {
lazyCompilation: false,
persistentCache: false,
minify: false,
treeShaking: false
},
});