-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
47 changed files
with
3,453 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
console.log("Hello world!dsa!!!"); | ||
|
||
console.log("hi!!!"); |
Oops, something went wrong.