Javascript Api
Farm
provides a comprehensive set of APIs for development servers, compilers, watchers, etc. Developers can use these APIs by importing the @farmfe/core
package.
Install the @farmfe/core
package:
Start
The start
method is used to quickly launch the development server.
After calling the start
method, you can see the log information of the available ip
address in the current console. By default, it compiles the index.html
file in the current directory.
Type:
Basic example:
another way is use more deep api to start server:
Build
The build method is used to build for the production environment.
After calling the build method, it defaults to building browser artifacts and generates a dist folder in the current directory. If you need to build artifacts for different environments and versions, such as node, node-next, browser, browser-es2017, etc., you can configure it by checking output targetEnv.
Type:
Basic example:
Watch
The watch method provides real-time updates for the compilation of the current project, equivalent to npx farm build --watch. Generally used in the node environment.
Type:
Basic example:
Preview
The preview method starts a preview server for previewing production artifacts. Make sure to have built the artifacts using the build method and have the correct production artifacts.
Type:
Basic example:
Clean
The clean method clears the cache generated by the farm incremental build. If you have issues with the incremental build causing crashes due to unforeseen or undiscovered problems, clearing the cache might help.
If there are problems with the incremental build causing crashes that are not resolved by clearing the cache, please submit an issue on GitHub.
Type:
Basic example:
loadEnv
Load environment variables from the .env file.
mode
isdevelopment
,production
or any string.loadEnv
will try load[``.env``, ``.env.local``, ``.env.${mode}``, ``.env.${mode}.local``]
forenvDir
.envDir
is the directory where the.env
file is located.prefixes
is the prefix of the environment variable. The default value is['FARM_', 'VITE_']
. Env variables with these prefixes will be injected intodefine
automatically.
createDevServer
The createDevServer method is used to start a local development server. You need to instantiate the Server object first and pass the compiler as a parameter.
Type:
Basic example:
createPreviewServer
Create a preview server for previewing production artifacts.
Type:
Basic example:
getCompiler
Get the current development server's compiler instance. Pass the compiler as a parameter when instantiating the Server.
Type:
Basic example:
close
Close all servers and WebSocket services opened by createDevServer.
Basic example:
Compiler
The Compiler provides a set of compiler APIs. You can create a compiler instance by instantiating Compiler.
Basic example:
compile
Asynchronously start the compilation process. Returns a Promise.
If specific environment variables are set (process.env.FARM_PROFILE), it performs a synchronous compilation.
Type:
Basic example:
compileSync
Synchronously start the compilation process.
Type:
Basic example:
traceDependencies
Trace dependencies between files. Returns an array of all dependencies for the provided input in the compiler configuration. Useful for restarting compilation based on file dependencies.
Type:
Basic example:
Returns an array of paths representing all dependencies.
update
Update compilation based on the provided paths. Returns a Promise resolving to a JsUpdateResult. If compilation is already in progress, it waits for completion and updates. If ignoreCompilingCheck is set to true, it won't check the compilation status.
Basic example:
hasModule
Pass a path to determine if the current path is within the modules compiled by the compiler.
Type:
Basic example:
getParentFiles
Retrieve the current file that a module import with the module import name (id) or resolved path identifier (resolvedPath) imports.
Type:
Basic example:
resources
Return all resources compiled by the compiler.
Type:
Basic example:
Resource
Return the buffer of the current artifact based on the given file.
Type:
Basic example:
writeResourcesToDisk
Write resources to disk based on the configured output path.
Type:
Basic example:
removeOutputPathDir
Remove the output path directory.
Type:
Basic example:
resolvedWatchPaths
Return resolved watch paths.
Type:
Basic example:
resolvedModulePaths
Return resolved module paths relative to the provided root path.
Type:
Basic example:
onUpdateFinish
Add a callback to be executed after the update process is complete.
Type:
Basic example:
outputPath
Return the resolved output path.
Type:
Basic example:
addExtraWatchFile
Add extra watch files for the compiler.
Type:
Basic example:
modules
Return an array of objects representing file module resolutions.
Type:
Basic example: