Skip to content

Commit eab36f9

Browse files
committed
new api
1 parent 162fdab commit eab36f9

File tree

47 files changed

+3453
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+3453
-0
lines changed

.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
dist
2+
example/public/js
3+
example/node_modules
4+
example/yarn.lock
5+
lib/node_modules
6+
7+
npm/lib/
8+
npm/esbuild-dev-server/lib/
9+
npm/esbuild-dev-server-darwin-x64/devserver
10+
npm/esbuild-dev-server-darwin-arm64/devserver
11+
npm/esbuild-dev-server-linux-x32/devserver
12+
npm/esbuild-dev-server-linux-x64/devserver
13+
npm/esbuild-dev-server-linux-arm/devserver
14+
npm/esbuild-dev-server-linux-arm64/devserver
15+
npm/esbuild-dev-server-win32-x32/devserver.exe
16+
npm/esbuild-dev-server-win32-x64/devserver.exe
17+
npm/esbuild-dev-server-win32-arm64/devserver.exe
18+

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Vladislav Fedotov
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# esbuild-dev-server
2+
3+
This plugin allows you to start a local server with hot reloading for [Esbuild](https://esbuild.github.io/)
4+
5+
More community [plugins](https://github.com/esbuild/community-plugins)
6+
7+
## Installation
8+
`npm`
9+
```
10+
npm i esbuild-dev-server -D
11+
```
12+
`yarn`
13+
```
14+
yarn add esbuild-dev-server -D
15+
```
16+
`go`
17+
```
18+
go get github.com/Falldot/esbuild-dev-server
19+
```
20+
## Configuration
21+
22+
- `options.port`, `string`: local server start port.
23+
- `options.index`, `string`: path to index html file.
24+
- `options.staticDir`, `string`: path to static files (js, css, img ...).
25+
- `options.watchDir`, `string`: path to working directory.
26+
- `options.onBeforeRebuild`, `() => void`: event before rebuild.
27+
- `options.onAfterRebuild`, `() => void`: event after rebuild.
28+
29+
## How to use?
30+
### Node.js
31+
`esbuild.config.js`
32+
```js
33+
const {build} = require("esbuild")
34+
const esBuildDevServer = require("esbuild-dev-server")
35+
36+
esBuildDevServer.start(
37+
build({
38+
entryPoints: ["src/index.js"],
39+
outdir: "dist",
40+
incremental: true,
41+
// and more options ...
42+
}),
43+
{
44+
port: "8080", // optional, default: 8080
45+
watchDir: "src", // optional, default: "src"
46+
index: "dist/index.html", // optional
47+
staticDir: "dist", // optional
48+
onBeforeRebuild: {}, // optional
49+
onAfterRebuild: {}, // optional
50+
}
51+
)
52+
```
53+
`package.json`
54+
```json
55+
"scripts": {
56+
"dev": "node esbuild.config.js",
57+
},
58+
```
59+
`dist/index.html`
60+
```html
61+
<!DOCTYPE html>
62+
<html lang="en">
63+
<head>
64+
<meta charset="UTF-8">
65+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
66+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
67+
<title>Document</title>
68+
</head>
69+
<body>
70+
<div id="root"></div>
71+
<script src="index.js"></script>
72+
</body>
73+
</html>
74+
```
75+
### Golang
76+
`esbuild.config.go`
77+
```go
78+
package main
79+
80+
import (
81+
devserver "github.com/Falldot/esbuild-dev-server"
82+
"github.com/evanw/esbuild/pkg/api"
83+
)
84+
85+
func main() {
86+
devserver.Start(
87+
api.Build(api.BuildOptions{
88+
EntryPoints: []string{"src/index.js"},
89+
Outdir: "dist",
90+
Incremental: true,
91+
// and more options ...
92+
}),
93+
devserver.Options{
94+
Port: "8080", // optional, default: 8080
95+
WatchDir: "src", // optional, default: "src"
96+
Index: "dist/index.html", // optional
97+
StaticDir: "dist", // optional
98+
OnBeforeRebuild: func() {}, // optional
99+
OnAfterRebuild: func() {}, // optional
100+
},
101+
)
102+
}
103+
```
104+
`package.json`
105+
```json
106+
"scripts": {
107+
"dev": "go esbuild.config.go",
108+
},
109+
```

cmd/build/build.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"log"
5+
6+
"github.com/evanw/esbuild/pkg/api"
7+
)
8+
9+
func main() {
10+
result := api.Build(api.BuildOptions{
11+
EntryPoints: []string{"lib/src/esbuild-dev-server.ts"},
12+
Bundle: true,
13+
Platform: api.PlatformNode,
14+
Engines: []api.Engine{
15+
{api.EngineNode, "14.18"},
16+
},
17+
Tsconfig: "lib/tsconfig.json",
18+
Write: true,
19+
Outdir: "npm/esbuild-dev-server/lib",
20+
})
21+
22+
if len(result.Errors) > 0 {
23+
log.Fatalln(result.Errors)
24+
}
25+
}

cmd/devserver/devserver.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"log"
7+
8+
"github.com/Falldot/esbuild-dev-server/internal/api"
9+
)
10+
11+
func main() {
12+
13+
port := flag.String("p", "", "local server start port")
14+
idnex := flag.String("i", "", "path to index html file")
15+
staticDir := flag.String("s", "", "path to static files (js, css, img ...)")
16+
watchDir := flag.String("w", "", "path to working directory")
17+
flag.Parse()
18+
19+
server := api.DevServer{
20+
Port: *port,
21+
Index: *idnex,
22+
StaticDir: *staticDir,
23+
WatchDir: *watchDir,
24+
OnReload: func() {
25+
fmt.Print("Reload")
26+
},
27+
}
28+
if err := server.Start(); err != nil {
29+
log.Fatalln(err)
30+
}
31+
}

esbuild-dev-server.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package devserver
2+
3+
import (
4+
"log"
5+
6+
plugin "github.com/Falldot/esbuild-dev-server/internal/api"
7+
"github.com/evanw/esbuild/pkg/api"
8+
)
9+
10+
type Options struct {
11+
Port string
12+
Index string
13+
StaticDir string
14+
WatchDir string
15+
OnBeforeRebuild func()
16+
OnAfterRebuild func()
17+
}
18+
19+
func Start(build api.BuildResult, options Options) {
20+
if !errorHandler(build.Errors, nil) {
21+
log.Fatalln("esbuild error!")
22+
}
23+
24+
var server plugin.DevServer
25+
server = plugin.DevServer{
26+
Port: options.Port,
27+
Index: options.Index,
28+
StaticDir: options.StaticDir,
29+
WatchDir: options.WatchDir,
30+
OnReload: func() {
31+
if options.OnBeforeRebuild != nil {
32+
options.OnBeforeRebuild()
33+
}
34+
result := build.Rebuild()
35+
if errorHandler(result.Errors, server.SendError) {
36+
server.SendReload()
37+
}
38+
if options.OnAfterRebuild != nil {
39+
options.OnAfterRebuild()
40+
}
41+
},
42+
}
43+
44+
if err := server.Start(); err != nil {
45+
log.Fatalln(err)
46+
}
47+
}
48+
49+
func errorHandler(errors []api.Message, callback func(string)) bool {
50+
if len(errors) > 0 {
51+
str := api.FormatMessages(errors, api.FormatMessagesOptions{
52+
Kind: api.ErrorMessage,
53+
Color: true,
54+
})
55+
for _, err := range str {
56+
log.Println(err)
57+
if callback != nil {
58+
callback(err)
59+
}
60+
}
61+
return false
62+
}
63+
return true
64+
}

example/esbuild.config.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
devserver "github.com/Falldot/esbuild-dev-server"
5+
"github.com/evanw/esbuild/pkg/api"
6+
)
7+
8+
func main() {
9+
devserver.Start(
10+
api.Build(api.BuildOptions{
11+
EntryPoints: []string{"src/index.js"},
12+
Outdir: "dist",
13+
Incremental: true,
14+
// and more options ...
15+
}),
16+
devserver.Options{
17+
Port: "8080", // optional, default: 8080
18+
WatchDir: "src", // optional, default: "src"
19+
Index: "dist/index.html", // optional
20+
StaticDir: "dist", // optional
21+
OnBeforeRebuild: func() {}, // optional
22+
OnAfterRebuild: func() {}, // optional
23+
},
24+
)
25+
}

example/esbuild.config.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const {build} = require("esbuild")
2+
const esBuildDevServer = require("esbuild-dev-server")
3+
4+
esBuildDevServer.start(
5+
build({
6+
entryPoints: ["src/index.js"],
7+
outdir: "dist/js",
8+
incremental: true,
9+
// and more options ...
10+
}),
11+
{
12+
port: "8080", // optional, default: 8080
13+
watchDir: "src", // optional, default: "src"
14+
index: "dist/index.html", // optional
15+
staticDir: "dist", // optional
16+
onBeforeRebuild: {}, // optional
17+
onAfterRebuild: {}, // optional
18+
}
19+
)

example/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "example",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"author": "Vladislav Fedotov",
6+
"license": "MIT",
7+
"private": true,
8+
"scripts": {
9+
"js-start": "node esbuild.config.js",
10+
"go-start": "go run esbuild.config.go"
11+
},
12+
"devDependencies": {
13+
"esbuild": "^0.13.2",
14+
"esbuild-dev-server": "../npm/esbuild-dev-server",
15+
"esbuild-dev-server-win32-x64": "../npm/esbuild-dev-server-win32-x64"
16+
}
17+
}

example/src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
console.log("Hello world!dsa!!!");
2+
3+
console.log("hi!!!");

0 commit comments

Comments
 (0)