farm-plugin-selective
v0.2.1
Published
Selective compilation plugin for FarmFE — conditionally include/exclude code at function, class, statement, and page level
Maintainers
Readme
farm-plugin-selective
Selective compilation plugin for FarmFE — conditionally include/exclude code at function, class, statement, and page level.
Install
npm install farm-plugin-selectiveDependencies: This plugin uses core-ast-ts internally for AST parsing. Installed automatically.
Concept
In a multi-app monorepo, different platforms (mobile, web, desktop) share code but also have platform-specific logic. This plugin lets you mark code with conditions so that only matching branches are compiled in — at function, class, variable, statement, and even page level.
Usage
import selective from 'farm-plugin-selective'
export default {
plugins: [
selective({
conditions: {
platform: 'mobile',
features: ['premium', 'analytics'],
debug: false,
},
}),
],
}Or set conditions via environment variable:
SELECTIVE_CONDITIONS='{"platform":"mobile","features":["premium"]}' farm buildOr key=value format:
SELECTIVE_CONDITIONS='platform=mobile,features=premium|analytics,debug=false' farm buildAPI Surface
@selective('condition') — Function/Class/Variable level
Decorate a declaration to include it only when the condition matches.
@selective('platform:mobile')
function mobileLayout() { /* only in mobile builds */ }
@selective('feature:premium')
class PremiumDashboard { /* only when premium feature enabled */ }
@selective('!debug:true')
const API_URL = '/api' /* excluded in debug builds */When the condition doesn't match:
- Functions → empty stub
function name() {} - Classes → empty stub
class name {} - Variables →
const name = undefined
$selective('condition', include, exclude?) — Expression level
const layout = $selective('platform:mobile', () => MobileLayout, () => WebLayout)
// → MobileLayout (when platform=mobile)
// → WebLayout (when platform!=mobile)
const color = $selective('feature:premium', () => 'gold')
// → 'gold' (when feature includes premium)
// → undefined (otherwise)$selective.block('condition', () => { ... }) — Statement level
$selective.block('debug:true', () => {
console.log('debug info')
setupDevtools()
})
// → keeps the statements when debug=true
// → removes entirely when debug=false@runtime — Compile-time code generator
A function decorated with @runtime executes at compile time. Its return value (a code string) replaces all call sites.
@runtime
function createHandler(env) {
if (env.platform === 'mobile') {
return `(url) => fetch('/m' + url)`
}
return `(url) => fetch('/api' + url)`
}
const handler = createHandler()
// → const handler = ((url) => fetch('/m' + url)) (when platform=mobile)The function receives the current conditions object as its first argument, enabling condition-aware code generation.
Page-level selective compilation
selective({
conditions: { platform: 'mobile' },
pagePatterns: ['src/pages/**/*.vue', 'src/pages/**/*.tsx'],
})When a page file contains @selective('condition') and the condition doesn't match, the entire page is replaced with an empty component stub. This means the page still exists in the route tree but renders nothing — no dead routes.
Custom empty page template:
selective({
conditions: { platform: 'mobile' },
pagePatterns: ['src/pages/**/*.vue'],
emptyPageTemplate: (name, ext) => {
if (ext === '.vue') return `<template><div></div></template>`
return `export default function ${name}() { return null }`
},
})Condition Syntax
| Syntax | Meaning |
|--------|---------|
| platform:mobile | platform equals "mobile" |
| !platform:mobile | platform is NOT "mobile" |
| feature:premium,basic | feature is "premium" OR "basic" |
| feature:* | feature key exists (any value) |
| !feature:* | feature key does NOT exist |
| platform:mobile && feature:premium | both conditions |
| platform:mobile || platform:web | either condition |
HMR
When source files change, conditions are re-resolved and affected modules are re-transformed.
License
MIT
