Skip to content

Commit

Permalink
new api
Browse files Browse the repository at this point in the history
  • Loading branch information
Falldot committed Nov 13, 2021
1 parent 162fdab commit eab36f9
Show file tree
Hide file tree
Showing 47 changed files with 3,453 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
dist
example/public/js
example/node_modules
example/yarn.lock
lib/node_modules

npm/lib/
npm/esbuild-dev-server/lib/
npm/esbuild-dev-server-darwin-x64/devserver
npm/esbuild-dev-server-darwin-arm64/devserver
npm/esbuild-dev-server-linux-x32/devserver
npm/esbuild-dev-server-linux-x64/devserver
npm/esbuild-dev-server-linux-arm/devserver
npm/esbuild-dev-server-linux-arm64/devserver
npm/esbuild-dev-server-win32-x32/devserver.exe
npm/esbuild-dev-server-win32-x64/devserver.exe
npm/esbuild-dev-server-win32-arm64/devserver.exe

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Vladislav Fedotov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
109 changes: 109 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# esbuild-dev-server

This plugin allows you to start a local server with hot reloading for [Esbuild](https://esbuild.github.io/)

More community [plugins](https://github.com/esbuild/community-plugins)

## Installation
`npm`
```
npm i esbuild-dev-server -D
```
`yarn`
```
yarn add esbuild-dev-server -D
```
`go`
```
go get github.com/Falldot/esbuild-dev-server
```
## Configuration

- `options.port`, `string`: local server start port.
- `options.index`, `string`: path to index html file.
- `options.staticDir`, `string`: path to static files (js, css, img ...).
- `options.watchDir`, `string`: path to working directory.
- `options.onBeforeRebuild`, `() => void`: event before rebuild.
- `options.onAfterRebuild`, `() => void`: event after rebuild.

## How to use?
### Node.js
`esbuild.config.js`
```js
const {build} = require("esbuild")
const esBuildDevServer = require("esbuild-dev-server")

esBuildDevServer.start(
build({
entryPoints: ["src/index.js"],
outdir: "dist",
incremental: true,
// and more options ...
}),
{
port: "8080", // optional, default: 8080
watchDir: "src", // optional, default: "src"
index: "dist/index.html", // optional
staticDir: "dist", // optional
onBeforeRebuild: {}, // optional
onAfterRebuild: {}, // optional
}
)
```
`package.json`
```json
"scripts": {
"dev": "node esbuild.config.js",
},
```
`dist/index.html`
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="root"></div>
<script src="index.js"></script>
</body>
</html>
```
### Golang
`esbuild.config.go`
```go
package main

import (
devserver "github.com/Falldot/esbuild-dev-server"
"github.com/evanw/esbuild/pkg/api"
)

func main() {
devserver.Start(
api.Build(api.BuildOptions{
EntryPoints: []string{"src/index.js"},
Outdir: "dist",
Incremental: true,
// and more options ...
}),
devserver.Options{
Port: "8080", // optional, default: 8080
WatchDir: "src", // optional, default: "src"
Index: "dist/index.html", // optional
StaticDir: "dist", // optional
OnBeforeRebuild: func() {}, // optional
OnAfterRebuild: func() {}, // optional
},
)
}
```
`package.json`
```json
"scripts": {
"dev": "go esbuild.config.go",
},
```
25 changes: 25 additions & 0 deletions cmd/build/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"log"

"github.com/evanw/esbuild/pkg/api"
)

func main() {
result := api.Build(api.BuildOptions{
EntryPoints: []string{"lib/src/esbuild-dev-server.ts"},
Bundle: true,
Platform: api.PlatformNode,
Engines: []api.Engine{
{api.EngineNode, "14.18"},
},
Tsconfig: "lib/tsconfig.json",
Write: true,
Outdir: "npm/esbuild-dev-server/lib",
})

if len(result.Errors) > 0 {
log.Fatalln(result.Errors)
}
}
31 changes: 31 additions & 0 deletions cmd/devserver/devserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"flag"
"fmt"
"log"

"github.com/Falldot/esbuild-dev-server/internal/api"
)

func main() {

port := flag.String("p", "", "local server start port")
idnex := flag.String("i", "", "path to index html file")
staticDir := flag.String("s", "", "path to static files (js, css, img ...)")
watchDir := flag.String("w", "", "path to working directory")
flag.Parse()

server := api.DevServer{
Port: *port,
Index: *idnex,
StaticDir: *staticDir,
WatchDir: *watchDir,
OnReload: func() {
fmt.Print("Reload")
},
}
if err := server.Start(); err != nil {
log.Fatalln(err)
}
}
64 changes: 64 additions & 0 deletions esbuild-dev-server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package devserver

import (
"log"

plugin "github.com/Falldot/esbuild-dev-server/internal/api"
"github.com/evanw/esbuild/pkg/api"
)

type Options struct {
Port string
Index string
StaticDir string
WatchDir string
OnBeforeRebuild func()
OnAfterRebuild func()
}

func Start(build api.BuildResult, options Options) {
if !errorHandler(build.Errors, nil) {
log.Fatalln("esbuild error!")
}

var server plugin.DevServer
server = plugin.DevServer{
Port: options.Port,
Index: options.Index,
StaticDir: options.StaticDir,
WatchDir: options.WatchDir,
OnReload: func() {
if options.OnBeforeRebuild != nil {
options.OnBeforeRebuild()
}
result := build.Rebuild()
if errorHandler(result.Errors, server.SendError) {
server.SendReload()
}
if options.OnAfterRebuild != nil {
options.OnAfterRebuild()
}
},
}

if err := server.Start(); err != nil {
log.Fatalln(err)
}
}

func errorHandler(errors []api.Message, callback func(string)) bool {
if len(errors) > 0 {
str := api.FormatMessages(errors, api.FormatMessagesOptions{
Kind: api.ErrorMessage,
Color: true,
})
for _, err := range str {
log.Println(err)
if callback != nil {
callback(err)
}
}
return false
}
return true
}
25 changes: 25 additions & 0 deletions example/esbuild.config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
devserver "github.com/Falldot/esbuild-dev-server"
"github.com/evanw/esbuild/pkg/api"
)

func main() {
devserver.Start(
api.Build(api.BuildOptions{
EntryPoints: []string{"src/index.js"},
Outdir: "dist",
Incremental: true,
// and more options ...
}),
devserver.Options{
Port: "8080", // optional, default: 8080
WatchDir: "src", // optional, default: "src"
Index: "dist/index.html", // optional
StaticDir: "dist", // optional
OnBeforeRebuild: func() {}, // optional
OnAfterRebuild: func() {}, // optional
},
)
}
19 changes: 19 additions & 0 deletions example/esbuild.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const {build} = require("esbuild")
const esBuildDevServer = require("esbuild-dev-server")

esBuildDevServer.start(
build({
entryPoints: ["src/index.js"],
outdir: "dist/js",
incremental: true,
// and more options ...
}),
{
port: "8080", // optional, default: 8080
watchDir: "src", // optional, default: "src"
index: "dist/index.html", // optional
staticDir: "dist", // optional
onBeforeRebuild: {}, // optional
onAfterRebuild: {}, // optional
}
)
17 changes: 17 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "example",
"version": "1.0.0",
"main": "index.js",
"author": "Vladislav Fedotov",
"license": "MIT",
"private": true,
"scripts": {
"js-start": "node esbuild.config.js",
"go-start": "go run esbuild.config.go"
},
"devDependencies": {
"esbuild": "^0.13.2",
"esbuild-dev-server": "../npm/esbuild-dev-server",
"esbuild-dev-server-win32-x64": "../npm/esbuild-dev-server-win32-x64"
}
}
3 changes: 3 additions & 0 deletions example/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
console.log("Hello world!dsa!!!");

console.log("hi!!!");
Loading

0 comments on commit eab36f9

Please sign in to comment.