Skip to content

Commit

Permalink
feat: etag and gzip by compiler option (#21)
Browse files Browse the repository at this point in the history
* feat: etag compiler option
* feat: gzip compiler option
* feat: order of directives
* feat: created and version info
* fix: separated demo env
* feat: create output directory
* feat: 2x9 build and test system
* feat: detect precompressed files
* feat: reduce .h file size
* feat: skip gz under 1024
* fix: pipeline node version
* fix: proper svelte demo app
* feat: warning if not usable directive used
* feat: colored console
  • Loading branch information
BCsabaEngine authored Aug 18, 2024
1 parent a0314dc commit d562214
Show file tree
Hide file tree
Showing 44 changed files with 1,260 additions and 11,474 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20.13.1'
node-version: '20.16.0'

- run: npm ci
- run: npm run format:check
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci-full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20.13.1'
node-version: '20.16.0'

- run: npm ci
- run: npm run format:check
Expand Down Expand Up @@ -47,7 +47,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20.13.1'
node-version: '20.16.0'
registry-url: 'https://registry.npmjs.org'
- run: npm ci
- run: npm run build
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ dist

# dotenv environment variables file
.env

demo/esp32/include/**/*.h
42 changes: 42 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Change log

## 1.4

### 1.4.0

## 1.3

### 1.3.1

- feat: filename directives are uppercased
- feat: filecount by extension
- fix: readme

### 1.3.0

- C++ defines can be used
- C++ defines group by filetypes: Should we be able to check if a given file name or number of files is correct? For this, we should find C++ defines in the generated code.

## 1.2

### 1.2.4

- Deps update

### 1.2.2

- Necessary C++ includes

### 1.2.1

- Windows paths

### 1.2.0

- ESP8266/ESP8285 is also supported

## 1.1

### 1.1.0

- ETag available
73 changes: 40 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# `svelteesp32` ![image](https://badges.github.io/stability-badges/dist/stable.svg)

[Changelog](CHANGELOG.md)

# Convert Svelte (or React/Angular/Vue) JS application to serve it from ESP32/ESP8266 webserver

### Forget SPIFFS and LittleFS now
Expand All @@ -10,11 +12,13 @@ In order to be able to easily update OTA, it is important - from the users' poin

This npm package provides a solution for **inserting any JS client application into the ESP web server** (PsychicHttp and also ESPAsyncWebServer available, PsychicHttp is the default). For this, JS, html, css, font, assets, etc. files must be converted to binary byte array. Npm mode is easy to use and easy to **integrate into your CI/CD pipeline**.

> Starting with version v1.1.0, the ETag header is also supported.
> Version v1.4.0 has a breaking change! --no-gzip changed to --gzip. Starting with this version c++ compiler directives are available to setup operation in project level.
> Starting with version v1.3.0, c++ defines can be used.
> Starting with version v1.2.0, ESP8266/ESP8285 is also supported.
> Starting with version v1.3.0, c++ defines can be used.
> Starting with version v1.1.0, the ETag header is also supported.
### Usage

Expand All @@ -28,29 +32,20 @@ After a successful Svelte build (rollup/webpack/vite) **create an includeable c+

```bash
// for PsychicHttpServer
npx svelteesp32 -e psychic -s ../svelteapp/dist -o ../esp32project/svelteesp32.h --etag
npx svelteesp32 -e psychic -s ../svelteapp/dist -o ../esp32project/svelteesp32.h --etag=true

// for ESPAsyncWebServer
npx svelteesp32 -e async -s ../svelteapp/dist -o ../esp32project/svelteesp32.h --etag
npx svelteesp32 -e async -s ../svelteapp/dist -o ../esp32project/svelteesp32.h --etag=true
```

During the **translation process**, the processed file details are visible, and at the end, the result shows the ESP's memory allocation (gzip size)

```
[assets/index-KwubEIf-.js]
✓ gzip used (38850 -> 12547)
[assets/index-Soe6cpLA.css]
✓ gzip used (32494 -> 5368)
[favicon.png]
x gzip unused (33249 -> 33282)
[index.html]
✓ gzip used (472 -> 308)
[roboto_regular.json]
✓ gzip used (363757 -> 93567)
[assets/index-KwubEIf-.js] ✓ gzip used (38850 -> 12547)
[assets/index-Soe6cpLA.css] ✓ gzip used (32494 -> 5368)
[favicon.png] x gzip unused (33249 -> 33282)
[index.html] x gzip unused (too small) (472 -> 308)
[roboto_regular.json] ✓ gzip used (363757 -> 93567)
5 files, 458kB original size, 142kB gzip size
../../../Arduino/EspSvelte/svelteesp32.h 842kB size
Expand Down Expand Up @@ -158,17 +153,21 @@ If you **only work on ESP32**, I recommend using PsychicHttpServer, which uses t

All modern browsers have been able to handle gzip-compressed content for years. For this reason, there is no question that the easily compressed JS and CSS files are stored compressed in the ESP32/ESP8266 and sent to the browser.

During the translation process, data in gzip format is generated and will be used if the **size is greater than 100 bytes** and we experience a **reduction of at least 15%**. In such a case, the compressed data is unconditionally sent to the browser with the appropriate **Content-Encoding** header information.
During the translation process, data in gzip format is generated and will be used if the **size is greater than 1024 bytes** and we experience a **reduction of at least 15%**. In such a case, the compressed data is unconditionally sent to the browser with the appropriate **Content-Encoding** header information.

Automatic compression can be turned off with the `--gzip=false` option.

Automatic compression can be turned off with the `--no-gzip` option.
> This setting has three states: yes, no, and compiler mode is available. In compiler mode, you can disable/enable Gzip by setting the `SVELTEESP32_ENABLE_GZIP` c++ compiler directive. For example, if using platformio, just write `-D SVELTEESP32_ENABLE_GZIP`.
### ETag

The ETag HTTP header can be used to significantly reduce network traffic. If the server sends ETag information, the client can check the integrity of the file by sending back this ETag (in `If-None-Match` header) without sending the data back again. All browsers use this function, the 304 HTTP response code is clearly visible in the network traffic.

Since microcontroller data traffic is moderately expensive, it is an individual decision whether to use the ETag or not. We **recommend using ETag**, which adds a bit more code (about 1-3%) but results in a much cleaner operation.

The use of ETag is **not enabled by default**, this can be achieved with the `--etag` switch.
The use of ETag is **not enabled by default**, this can be achieved with the `--etag=true` switch.

> This setting has three states: yes, no, and compiler mode is available. In compiler mode, you can disable/enable ETag by setting the `SVELTEESP32_ENABLE_ETAG` c++ compiler directive. For example, if using platformio, just type `-D SVELTEESP32_ENABLE_ETAG`.
### Main entry point - index.html

Expand Down Expand Up @@ -214,27 +213,35 @@ You can include a blocker error if a named file accidentally missing from the bu
...
```

You can use the following c++ directives at the project level if you want to configure the usage there: `SVELTEESP32_ENABLE_ETAG` and `SVELTEESP32_ENABLE_GZIP`. (Do not forget `--etag=compiler` or `--gzip=compiler` command line arg!)

### Command line options

| Option | Description | default |
| ------------- | --------------------------------------------------------- | ----------------------- | ------- |
| `-s` | **Source dist folder contains compiled web files** | |
| `-e` | The engine for which the include file is created [psychic | async] | psychic |
| `-o` | Generated output file with path | `svelteesp32.h` |
| `--etag` | Use ETag header for cache | false |
| `--no-gzip` | Do not compress content with gzip | false -> gzip used |
| `--espmethod` | Name of generated method | `initSvelteStaticFiles` |
| `--define` | Prefix of c++ defines | `SVELTEESP32` |
| `-h` | Show help | |
| Option | Description | default |
| ------------- | ---------------------------------------------------------------- | ----------------------- |
| `-s` | **Source dist folder contains compiled web files** | |
| `-e` | The engine for which the include file is created (psychic/async) | psychic |
| `-o` | Generated output file with path | `svelteesp32.h` |
| `--etag` | Use ETag header for cache (true/false/compiler) | false |
| `--gzip` | Compress content with gzip (true/false/compiler) | true |
| `--created` | Include creation time | false |
| `--version` | Include a version string, `--version=v$npm_package_version` | '' |
| `--espmethod` | Name of generated method | `initSvelteStaticFiles` |
| `--define` | Prefix of c++ defines | `SVELTEESP32` |
| `-h` | Show help | |

### Q&A

- **How big a frontend application can be placed?** If you compress the content with gzip, even a 3-4Mb assets directory can be placed. This is a serious enough amount to serve a complete application.

- **How fast is cpp file compilation?** The cpp file can be large, but it can be compiled in a few seconds on any machine. If you don't modify your svelte/react app, it will use the already compiled cpp file (not recompile). This does not increase the speed of ESP32/ESP8266 development.
- **How fast is cpp file compilation?** The cpp (.h) file can be large, but it can be compiled in a few seconds on any machine. If you don't modify your svelte/react app, it will use the already compiled cpp file (not recompile). This does not increase the speed of ESP32/ESP8266 development.

- **Does the solution use PROGMEM?** No and yes. ESP32 no longer has PROGMEM. (Exists, but does not affect the translation). Instead, if we use a const array in the global namespace, its content will be placed in the code area, i.e. it will not be used from the heap or the stack, so the content of the files to be served will be placed next to the code. When working on ESP8266, PROGMEM will actually be used.

- **Is this safe to use in production?** I suggest you give it a try! If you find it useful and safe in several different situations, feel free to use it, just like any other free library.
- **Why is the .h file so big?** The source files are always larger than the binary compiled from them. Does the size information in the header (SVELTEESP32_SIZE, SVELTEESP32_SIZE_GZIP) show the actual memory allocation.

- **Is collaboration between groups supported?** Yes, the Frontend team produces the application, the use of svelteesp32 is part of the build process. Then, provided with a version number, the .h file is placed in git, which the ESP team translates into the platformio application.

- **Will you develop it further?** Since I use it myself, I will do my best to make the solution better and better.

- **Is this safe to use in production?** I suggest you give it a try! If you find it useful and safe in several different situations, feel free to use it, just like any other free library.
1 change: 1 addition & 0 deletions demo/esp32/include/placeholder.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Generated header files
97 changes: 0 additions & 97 deletions demo/esp32/include/svelteesp32async.h

This file was deleted.

107 changes: 0 additions & 107 deletions demo/esp32/include/svelteesp32psychic.h

This file was deleted.

120 changes: 118 additions & 2 deletions demo/esp32/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,126 @@ platform = espressif32
board = wemos_d1_mini32
framework = arduino



[env:async]
build_flags = -D ASYNC
build_flags =
-D ASYNC
-I include/_
lib_deps = https://github.com/me-no-dev/ESPAsyncWebServer

[env:async_E]
build_flags =
-D ASYNC
-I include/e
lib_deps = https://github.com/me-no-dev/ESPAsyncWebServer

[env:async_EC]
build_flags =
-D ASYNC
-I include/ec
-D SVELTEESP32_ENABLE_ETAG
lib_deps = https://github.com/me-no-dev/ESPAsyncWebServer

[env:async_ECG]
build_flags =
-D ASYNC
-I include/ecg
-D SVELTEESP32_ENABLE_ETAG
lib_deps = https://github.com/me-no-dev/ESPAsyncWebServer

[env:async_ECGC]
build_flags =
-D ASYNC
-I include/ecgc
-D SVELTEESP32_ENABLE_ETAG
-D SVELTEESP32_ENABLE_GZIP
lib_deps = https://github.com/me-no-dev/ESPAsyncWebServer

[env:async_EG]
build_flags =
-D ASYNC
-I include/eg
lib_deps = https://github.com/me-no-dev/ESPAsyncWebServer

[env:async_EGC]
build_flags =
-D ASYNC
-I include/egc
-D SVELTEESP32_ENABLE_GZIP
lib_deps = https://github.com/me-no-dev/ESPAsyncWebServer

[env:async_G]
build_flags =
-D ASYNC
-I include/g
lib_deps = https://github.com/me-no-dev/ESPAsyncWebServer

[env:async_GC]
build_flags =
-D ASYNC
-I include/gc
-D SVELTEESP32_ENABLE_GZIP
lib_deps = https://github.com/me-no-dev/ESPAsyncWebServer



[env:psychic]
build_flags = -D PSYCHIC
build_flags =
-D PSYCHIC
-I include/_
lib_deps = hoeken/PsychicHttp

[env:psychic_E]
build_flags =
-D PSYCHIC
-I include/e
lib_deps = hoeken/PsychicHttp

[env:psychic_EC]
build_flags =
-D PSYCHIC
-I include/ec
-D SVELTEESP32_ENABLE_ETAG
lib_deps = hoeken/PsychicHttp

[env:psychic_ECG]
build_flags =
-D PSYCHIC
-I include/ecg
-D SVELTEESP32_ENABLE_ETAG
lib_deps = hoeken/PsychicHttp

[env:psychic_ECGC]
build_flags =
-D PSYCHIC
-I include/ecgc
-D SVELTEESP32_ENABLE_ETAG
-D SVELTEESP32_ENABLE_GZIP
lib_deps = hoeken/PsychicHttp

[env:psychic_EG]
build_flags =
-D PSYCHIC
-I include/eg
lib_deps = hoeken/PsychicHttp

[env:psychic_EGC]
build_flags =
-D PSYCHIC
-I include/egc
-D SVELTEESP32_ENABLE_GZIP
lib_deps = hoeken/PsychicHttp

[env:psychic_G]
build_flags =
-D PSYCHIC
-I include/g
lib_deps = hoeken/PsychicHttp

[env:psychic_GC]
build_flags =
-D PSYCHIC
-I include/gc
-D SVELTEESP32_ENABLE_GZIP
lib_deps = hoeken/PsychicHttp
4 changes: 2 additions & 2 deletions demo/esp32/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <ESPAsyncWebServer.h>
#include "svelteesp32async.h"

#if SVELTEESP32_COUNT != 5
#if SVELTEESP32_COUNT != 10
#error Invalid file count
#endif

Expand All @@ -30,7 +30,7 @@ void loop() {}
#include <PsychicHttp.h>
#include "svelteesp32psychic.h"

#if SVELTEESP32_COUNT != 5
#if SVELTEESP32_COUNT != 10
#error Invalid file count
#endif

Expand Down
12 changes: 0 additions & 12 deletions demo/svelte/.eslintignore

This file was deleted.

Loading

0 comments on commit d562214

Please sign in to comment.