Skip to content

Commit d2ba3cc

Browse files
committed
feat: support for rfw.json, the rfw manifest file
1 parent 700a3d9 commit d2ba3cc

File tree

7 files changed

+75
-26
lines changed

7 files changed

+75
-26
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,15 @@ rfw dev
2525

2626
Read the [documentation](./docs/index.md) for a complete guide to the framework.
2727

28-
## Plugins
28+
## Build-level Plugins
2929

3030
`rfw` exposes a simple plugin system for build-time tasks. Plugins can register
3131
build steps and file-watcher triggers to extend the CLI without relying on
3232
external tooling.
3333

34-
## Tailwind CSS
34+
### Tailwind CSS
3535

3636
`rfw` includes a build step for [Tailwind CSS](https://tailwindcss.com/) using the official standalone CLI.
37-
Place an `input.css` file containing the `@tailwind` directives and optionally a
38-
`tailwind.config.js` in your project root. During development the server watches
37+
Place an `input.css` file containing the `@tailwind` directives in your project. During development the server watches
3938
template, stylesheet and configuration files and emits a trimmed `tailwind.css`
4039
containing only the classes you use, without requiring Node or a CDN.

cmd/rfw/build/build.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package build
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"io"
67
"os"
@@ -29,7 +30,13 @@ func Build() error {
2930
return fmt.Errorf("failed to build project: %s: %w", output, err)
3031
}
3132

32-
if err := plugins.BuildAll(); err != nil {
33+
var manifest struct {
34+
Plugins map[string]json.RawMessage `json:"plugins"`
35+
}
36+
if data, err := os.ReadFile("rfw.json"); err == nil {
37+
_ = json.Unmarshal(data, &manifest)
38+
}
39+
if err := plugins.BuildFromConfig(manifest.Plugins); err != nil {
3340
return fmt.Errorf("failed to run plugins: %w", err)
3441
}
3542

cmd/rfw/plugins/plugins.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,44 @@
11
package plugins
22

3-
import "fmt"
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
)
47

58
// Plugin defines the interface that build plugins must implement.
69
type Plugin interface {
710
Name() string
8-
Build() error
11+
Build(cfg json.RawMessage) error
912
ShouldRebuild(path string) bool
1013
}
1114

12-
var registered []Plugin
15+
var (
16+
registry = map[string]Plugin{}
17+
active []Plugin
18+
)
1319

1420
// Register adds a plugin to the registry.
1521
func Register(p Plugin) {
16-
registered = append(registered, p)
22+
registry[p.Name()] = p
1723
}
1824

19-
// BuildAll executes the Build method for all registered plugins.
20-
func BuildAll() error {
21-
for _, p := range registered {
22-
if err := p.Build(); err != nil {
23-
return fmt.Errorf("%s build failed: %w", p.Name(), err)
25+
// BuildFromConfig executes the Build method for plugins listed in the manifest.
26+
func BuildFromConfig(cfg map[string]json.RawMessage) error {
27+
active = active[:0]
28+
for name, raw := range cfg {
29+
if p, ok := registry[name]; ok {
30+
if err := p.Build(raw); err != nil {
31+
return fmt.Errorf("%s build failed: %w", p.Name(), err)
32+
}
33+
active = append(active, p)
2434
}
2535
}
2636
return nil
2737
}
2838

29-
// NeedsRebuild reports whether any plugin requires a rebuild for the given path.
39+
// NeedsRebuild reports whether any active plugin requires a rebuild for the given path.
3040
func NeedsRebuild(path string) bool {
31-
for _, p := range registered {
41+
for _, p := range active {
3242
if p.ShouldRebuild(path) {
3343
return true
3444
}

cmd/rfw/plugins/tailwind/tailwind.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tailwind
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"log"
67
"os/exec"
@@ -9,28 +10,45 @@ import (
910
"github.com/rfwlab/rfw/cmd/rfw/plugins"
1011
)
1112

12-
type plugin struct{}
13+
type plugin struct {
14+
output string
15+
}
1316

1417
func init() {
1518
plugins.Register(&plugin{})
1619
}
1720

1821
func (p *plugin) Name() string { return "tailwind" }
1922

20-
func (p *plugin) Build() error {
23+
func (p *plugin) Build(raw json.RawMessage) error {
2124
log.Printf("tailwind: starting build")
2225
bin, err := exec.LookPath("tailwindcss")
2326
if err != nil {
2427
log.Printf("tailwind: tailwindcss not found, please install it manually")
2528
return err
2629
}
2730

28-
// FIXME: in future an rfw project should have a root manifest file with plugins configurations and so on,
29-
// for the moment we will look for an input.css file in the project root
30-
args := []string{
31-
"-i", "input.css",
32-
"-o", "tailwind.css",
33-
"--minify",
31+
cfg := struct {
32+
Input string `json:"input"`
33+
Output string `json:"output"`
34+
Minify bool `json:"minify"`
35+
Args []string `json:"args"`
36+
}{
37+
Input: "index.css",
38+
Output: "tailwind.css",
39+
Minify: true,
40+
}
41+
if len(raw) > 0 {
42+
_ = json.Unmarshal(raw, &cfg)
43+
}
44+
p.output = cfg.Output
45+
46+
args := []string{"-i", cfg.Input, "-o", cfg.Output}
47+
if cfg.Minify {
48+
args = append(args, "--minify")
49+
}
50+
if len(cfg.Args) > 0 {
51+
args = append(args, cfg.Args...)
3452
}
3553

3654
log.Printf("tailwind: running %s %s", bin, strings.Join(args, " "))
@@ -43,7 +61,7 @@ func (p *plugin) Build() error {
4361
}
4462

4563
func (p *plugin) ShouldRebuild(path string) bool {
46-
if strings.HasSuffix(path, ".css") && !strings.HasSuffix(path, "tailwind.css") {
64+
if strings.HasSuffix(path, ".css") && !strings.HasSuffix(path, p.output) {
4765
log.Printf("tailwind: rebuild triggered by %s", path)
4866
return true
4967
}

cmd/rfw/server/server.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/fsnotify/fsnotify"
1414
"github.com/rfwlab/rfw/cmd/rfw/build"
1515
"github.com/rfwlab/rfw/cmd/rfw/plugins"
16-
_ "github.com/rfwlab/rfw/cmd/rfw/plugins/tailwind"
1716
"github.com/rfwlab/rfw/cmd/rfw/utils"
1817
)
1918

docs/ui/rfw.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"plugins": {
3+
"tailwind": {
4+
"input": "input.css",
5+
"output": "tailwind.css"
6+
}
7+
}
8+
}

example/rfw.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"plugins": {
3+
"tailwind": {
4+
"input": "input.css",
5+
"output": "tailwind.css"
6+
}
7+
}
8+
}

0 commit comments

Comments
 (0)