Skip to content
This repository has been archived by the owner on Oct 8, 2023. It is now read-only.

Commit

Permalink
feat: 支持监视模式
Browse files Browse the repository at this point in the history
  • Loading branch information
rxliuli committed Jan 30, 2022
1 parent 821a8b7 commit 68ee0eb
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 44 deletions.
72 changes: 36 additions & 36 deletions builder/BuilderProgram.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/rxliuli/saki/utils/fsExtra"
"github.com/rxliuli/saki/utils/object"
"github.com/swaggest/assertjson/json5"
"gopkg.in/ffmt.v1"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -92,13 +91,16 @@ func (receiver Program) getPlatform() api.Platform {
return api.PlatformNeutral
}

func getPlugins(platform api.Platform) []api.Plugin {
func (receiver Program) getPlugins(platform api.Platform) []api.Plugin {
var plugins []api.Plugin
if platform == api.PlatformBrowser {
}
if platform == api.PlatformNode {
plugins = append(plugins, NodeExternals())
}
if receiver.Watch {
plugins = append(plugins, Logger(receiver.Cwd))
}
return plugins
}

Expand All @@ -111,19 +113,20 @@ func (receiver Program) getBaseOptions() api.BuildOptions {
if receiver.Watch {
watch = api.WatchMode{}
}
platform := receiver.getPlatform()
return api.BuildOptions{
Sourcemap: api.SourceMapExternal,
Bundle: true,
Watch: &watch,
External: globalExternal,
Platform: receiver.getPlatform(),
Platform: platform,
MinifyWhitespace: !receiver.Watch,
MinifyIdentifiers: !receiver.Watch,
MinifySyntax: !receiver.Watch,
Incremental: receiver.Watch,
Metafile: receiver.Watch,
Write: true,
Plugins: getPlugins(receiver.getPlatform()),
Plugins: receiver.getPlugins(platform),
}
}

Expand Down Expand Up @@ -165,29 +168,43 @@ func (receiver Program) getCliOptions() api.BuildOptions {
return options
}

type Target = string

const (
esm = "esm"
cjs = "cjs"
iife = "iife"
cli = "cli"
TargetEsm Target = "esm"
TargetCjs Target = "cjs"
TargetIife Target = "iife"
TargetCli Target = "cli"
)

func (receiver Program) GetOptionsByTarget(target string) api.BuildOptions {
func (receiver Program) GetOptionsByTarget(target Target) api.BuildOptions {
switch target {
case esm:
case TargetEsm:
return receiver.getEsmOptions()
case cjs:
case TargetCjs:
return receiver.getCjsOptions()
case iife:
case TargetIife:
return receiver.getIifeOptions()
case cli:
case TargetCli:
return receiver.getCliOptions()
default:
panic("无法识别的目标")
}
}

func (receiver Program) BuildToTargets(targets []string) []api.BuildResult {
func resolveResultError(results []api.BuildResult) error {
for _, result := range results {
if len(result.Errors) != 0 {
message := result.Errors[0]
location := message.Location
return fmt.Errorf("构建失败: %s %s:%d:%d\n", message.Text, location.File, location.Line, location.Column)
}
}
return nil
}

func (receiver Program) BuildToTargets(targets []Target) error {
start := time.Now()
if !receiver.Watch {
err := os.RemoveAll(filepath.Join(receiver.Cwd, "dist"))
if err != nil {
Expand All @@ -204,27 +221,10 @@ func (receiver Program) BuildToTargets(targets []string) []api.BuildResult {
}(i, target)
}
wg.Wait()
return res
}

func (receiver Program) BuildLib() {
start := time.Now()
targets := receiver.BuildToTargets([]string{esm, cjs})
for _, target := range targets {
if len(target.Errors) != 0 {
_, _ = ffmt.Puts(target.Errors)
}
}
fmt.Printf("构建完成: %s", time.Now().Sub(start).String())
}

func (receiver Program) BuildCli() {
start := time.Now()
targets := receiver.BuildToTargets([]string{esm, cjs, cli})
for _, target := range targets {
if len(target.Errors) != 0 {
_, _ = ffmt.Puts(target.Errors)
}
if receiver.Watch {
<-make(chan bool)
} else {
fmt.Printf("构建完成: %s\n", time.Now().Sub(start).String())
}
fmt.Printf("构建完成: %s", time.Now().Sub(start).String())
return resolveResultError(res)
}
17 changes: 15 additions & 2 deletions builder/BuilderProgram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ func beforeEach() {
}

func TestBuilderProgram_BuildLib(t *testing.T) {
builder.BuildLib()
assert.NoError(t, builder.BuildToTargets([]Target{TargetEsm, TargetCjs}))
}

func TestBuilderProgram_BuildCli(t *testing.T) {
builder.BuildCli()
assert.NoError(t, builder.BuildToTargets([]Target{TargetCli, TargetEsm, TargetCjs}))
}

func TestGetPlatformOfNode(t *testing.T) {
Expand Down Expand Up @@ -61,3 +61,16 @@ func TestGetPlatformOfNeutral(t *testing.T) {
beforeEach()
assert.Equal(t, builder.getPlatform(), api.PlatformNeutral)
}

func TestProgram_BuildLibWatch(t *testing.T) {
beforeEach()
assert.NoError(t, os.MkdirAll(filepath.Join(tempPath, "src"), fs.ModeDir))
assert.NoError(t, fsExtra.WriteStringFile(filepath.Join(tempPath, "package.json"), `{}`))
assert.NoError(t, fsExtra.WriteStringFile(filepath.Join(tempPath, "src/index.ts"), `export function hello(name: string) {
return 'hello ' + name;
}`))
builder.Watch = true
builder.Cwd = tempPath
assert.NoError(t, builder.BuildToTargets([]Target{TargetEsm, TargetCjs}))
<-make(chan bool)
}
25 changes: 25 additions & 0 deletions builder/esbuildPlugins.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package builder

import (
"fmt"
"github.com/evanw/esbuild/pkg/api"
"path/filepath"
"regexp"
"strings"
"time"
)

// NodeExternals 排除和替换 node 内置模块
Expand Down Expand Up @@ -49,3 +51,26 @@ func AutoExternal() api.Plugin {
},
}
}

func Logger(cwd string) api.Plugin {
return api.Plugin{
Name: "esbuild-plugin-logger",
Setup: func(build api.PluginBuild) {
start := time.Now()
build.OnStart(func() (api.OnStartResult, error) {
start = time.Now()
return api.OnStartResult{}, nil
})
build.OnEnd(func(result *api.BuildResult) {
rel, _ := filepath.Rel(cwd, build.InitialOptions.Outfile)
if len(result.Errors) != 0 {
message := result.Errors[0]
location := message.Location
fmt.Printf("构建失败 %s: %s %s:%d:%d\n", rel, message.Text, location.File, location.Line, location.Column)
return
}
fmt.Printf("构建成功 %s: %v\n", strings.ReplaceAll(rel, "\\", "/"), time.Since(start))
})
},
}
}
20 changes: 14 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,29 @@ func main() {
{
Name: "build",
Usage: "构建命令",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "watch",
Usage: "监听文件变化,自动编译",
Value: false,
Aliases: []string{"w"},
},
},
Subcommands: cli.Commands{
{
Name: "lib",
Usage: "构建 lib",
Action: func(context *cli.Context) error {
program.BuildLib()
return nil
program.Watch = context.Bool("watch")
return program.BuildToTargets([]builder.Target{builder.TargetEsm, builder.TargetCjs})
},
},
{
Name: "cli",
Usage: "构建 cli",
Action: func(context *cli.Context) error {
program.BuildCli()
return nil
program.Watch = context.Bool("watch")
return program.BuildToTargets([]builder.Target{builder.TargetCli, builder.TargetEsm, builder.TargetCjs})
},
},
{
Expand All @@ -50,10 +58,10 @@ func main() {
},
},
Action: func(context *cli.Context) error {
program.BuildToTargets(array.StringFlatMap(context.StringSlice("target"), func(s string) []string {
program.Watch = context.Bool("watch")
return program.BuildToTargets(array.StringFlatMap(context.StringSlice("target"), func(s string) []string {
return strings.Split(s, ",")
}))
return nil
},
},
},
Expand Down

0 comments on commit 68ee0eb

Please sign in to comment.