Skip to content

Commit 368f86b

Browse files
committed
improving docs, and test UX (errors/toasts)
1 parent 8daa269 commit 368f86b

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed

BUNDLERS.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
This project has non-ESM dependencies, which unfortunately cannot be *bundled* in with your other app code. Modern bundlers unfortunately don't out-of-the-box support configurations that can handle such a situation.
44

5-
As such, this project provides plugins for webpack and vite, to take care of the various steps needed to get these non-ESM dependencies into an otherwise bundled web app built by those tools.
5+
As such, this project provides plugins for Vite and Webpack, to take care of the various steps needed to get these non-ESM dependencies into an otherwise bundled web app built by those tools.
66

77
## Bundler Plugins
88

9-
The plugins for vite and webpack are included in the `bundler-plugins/` directory. They should handle all necessary steps to load the dependencies.
9+
The plugins for Vite and Webpack are included in the `bundler-plugins/` directory. They should handle all necessary steps to load the dependencies.
1010

1111
**Note:** You should not need to manually copy any files out of the `dist/bundlers/` directory, as the plugins access the `qr-data-sync` dependency (in `node_modules`) directly to pull the files needed. But for reference, the files these plugins access are:
1212

@@ -24,7 +24,7 @@ The plugins for vite and webpack are included in the `bundler-plugins/` director
2424

2525
### Vite Plugin
2626

27-
If using Vite 5+, it's strongly suggested to import this library's vite-plugin to manage the loading of its non-ESM dependencies. Add something like the following to your `vite.config.js` file:
27+
If using Vite 5+, it's strongly suggested to import this library's Vite-plugin to manage the loading of its non-ESM dependencies. Add something like the following to your `vite.config.js` file:
2828

2929
```js
3030
import { defineConfig } from "vite";
@@ -39,19 +39,25 @@ export default defineConfig({
3939
});
4040
```
4141

42-
This plugin works for both the `vite dev` (dev-server) mode and the `vite build` mode. In both cases, it copies the `dist/bundlers/qrds-external-bundle.js` file into the `public/` directory of your project root. It also injects a `<script src="qrds-external-bundle.js"></script>` tag into the markup of the `index.html` file that vite produces for your app.
42+
This plugin works for the `vite dev` (dev-server), `vite preview` (also dev-server), and `vite build` modes. In all cases, it copies the `dist/bundlers/qrds-external-bundle.js` file into the `public/` directory of your project root. It also injects a `<script src="/qrds-external-bundle.js"></script>` tag into the markup of the `index.html` file that Vite produces for your app.
4343

44-
**Note:** At present, this plugin is not configurable in any way (i.e., calling `QRDS()` above with no arguments). If something about its behavior is not compatible with your vite project setup -- which can vary widely and be quite complex to predict or support by a basic plugin -- it's recommended you simply copy over the `qr-data-sync/bundler-plugins/vite.mjs` plugin and make necessary changes.
44+
**Note:** At present, this plugin is not configurable in any way (i.e., calling `QRDS()` above with no arguments). If something about its behavior is not compatible with your Vite project setup -- which can vary widely and be quite complex to predict or support by a basic plugin -- it's recommended you simply copy over the `qr-data-sync/bundler-plugins/vite.mjs` plugin and make necessary changes.
45+
46+
#### SSR Breakage
47+
48+
An unfortunate gotcha of tools that wrap Vite (e.g., Astro, Nuxt, etc) and do SSR (server-side rendering) is that they *break* a key assumption/behavior of this module's Vite plugin: the HTML injection of `<script src="/qrds-external-bundle.js"></script>`.
49+
50+
As such, you'll likely need to manually add that `<script>` tag to your HTML pages/templates. The Vite plugin still copies that file into the `public/` folder for you, so it should load once the tag is added to your HTML.
4551

4652
### Webpack Plugin
4753

4854
If using Webpack 5+, make sure you're already using the [HTML Webpack Plugin](https://github.com/jantimon/html-webpack-plugin/) to manage building your `index.html` (and/or other HTML pages).
4955

50-
Then import this library's webpack-plugin to manage the loading of its non-ESM dependencies. Add something like the following to your `webpack.config.js`:
56+
Then import this library's Webpack-plugin to manage the loading of its non-ESM dependencies. Add something like the following to your `webpack.config.js`:
5157

5258
```js
5359
// 'HtmlWebpackPlugin' is a required dependency of the
54-
// qr-data-sync webpack plugin
60+
// qr-data-sync Webpack plugin
5561
import HtmlWebpackPlugin from "html-webpack-plugin";
5662
import QRDS from "qr-data-sync/bundlers/webpack";
5763

@@ -71,9 +77,9 @@ export default {
7177
};
7278
```
7379

74-
This plugin copies the `dist/bundlers/qrds-external-bundle.js` file into the build root (default `dist/`), along with the other bundled files. It also injects a `<script src="qrds-external-bundle.js"></script>` tag into the markup of the `index.html` file (and any other HTML files) that webpack produces for your app.
80+
This plugin copies the `dist/bundlers/qrds-external-bundle.js` file into the build root (default `dist/`), along with the other bundled files. It also injects a `<script src="qrds-external-bundle.js"></script>` tag into the markup of the `index.html` file (and any other HTML files) that Webpack produces for your app.
7581

76-
**Note:** At present, this plugin is not configurable in any way (i.e., calling `QRDS()` above with no arguments). If something about its behavior is not compatible with your webpack project setup -- which can vary widely and be quite complex to predict or support by a basic plugin -- it's recommended you simply copy over the `qr-data-sync/bundler-plugins/webpack.mjs` plugin and make necessary changes.
82+
**Note:** At present, this plugin is not configurable in any way (i.e., calling `QRDS()` above with no arguments). If something about its behavior is not compatible with your Webpack project setup -- which can vary widely and be quite complex to predict or support by a basic plugin -- it's recommended you simply copy over the `qr-data-sync/bundler-plugins/webpack.mjs` plugin and make necessary changes.
7783

7884
## Import/Usage
7985

@@ -83,4 +89,4 @@ To import and use **qr-data-sync** in a *bundled* browser app:
8389
import { todo } from "qr-data-sync";
8490
```
8591

86-
When `import`ed like this, both vite and webpack should (via these plugins) properly find and bundle the `dist/bundlers/qrds.js` ESM library module with the rest of your app code, without any further steps necessary.
92+
When `import`ed like this, both Vite and Webpack should (via these plugins) properly find and bundle the `dist/bundlers/qrds.js` ESM library module with the rest of your app code, hopefully without any further steps necessary.

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# QR Data Sync
22

3-
[![npm Module](https://badge.fury.io/mylofi/qr-data-sync.svg)](https://www.npmjs.org/package/qr-data-sync)
3+
[![npm Module](https://badge.fury.io/@lofi%2fqr-data-sync.svg)](https://www.npmjs.org/package/@lofi/qr-data-sync)
44
[![License](https://img.shields.io/badge/license-MIT-a1356a)](LICENSE.txt)
55

66
**QR Data Sync** is a browser library with utils for sharing/synchronizing data via "animated" QR codes.
@@ -13,11 +13,15 @@
1313

1414
## Deployment / Import
1515

16-
The [**qr-data-sync** npm package](https://npmjs.com/package/webauthn-local-client) includes a `dist/` directory with all files you need to deploy **qr-data-sync** (and its dependencies) into your application/project.
16+
```cmd
17+
npm install @lofi/qr-data-sync
18+
```
19+
20+
The [**qr-data-sync** npm package](https://npmjs.com/package/@lofi/webauthn-local-client) includes a `dist/` directory with all files you need to deploy **qr-data-sync** (and its dependencies) into your application/project.
1721

1822
If you obtain this library via git instead of npm, you'll need to [build `dist/` manually](#re-building-dist) before deployment.
1923

20-
* **USING A WEB BUNDLER?** (vite, webpack, etc) Use the `dist/bundlers/*` files and see [Bundler Deployment](BUNDLERS.md) for instructions.
24+
* **USING A WEB BUNDLER?** (Vite, Webpack, etc) Use the `dist/bundlers/*` files and see [Bundler Deployment](BUNDLERS.md) for instructions.
2125

2226
* Otherwise, use the `dist/auto/*` files and see [Non-Bundler Deployment](NON-BUNDLERS.md) for instructions.
2327

test/test.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ async function showQRCodes(name,note,randomString) {
153153
}
154154
catch (err) {
155155
if (!closing) {
156-
Swal.showValidationMessage(err.toString());
157-
console.log(err);
156+
console.error(err);
157+
await showError(`Generating QR codes failed: ${err.toString()}`);
158158
}
159159
}
160160
},
@@ -229,11 +229,13 @@ async function promptReceiveData() {
229229

230230
// render received data
231231
renderReceivedData(data);
232+
233+
return showToast("Data received successfully.");
232234
}
233235
catch (err) {
234236
if (!closing) {
235-
Swal.showValidationMessage(err.toString());
236-
console.log(err);
237+
console.error(err);
238+
await showError(`Reading QR codes failed: ${err.toString()}`);
237239
}
238240
}
239241
},
@@ -285,3 +287,26 @@ function randomString(length) {
285287
var rv = crypto.getRandomValues(new Uint8Array(length));
286288
return btoa(String.fromCharCode.apply(null,rv));
287289
}
290+
291+
function showError(errMsg) {
292+
return Swal.fire({
293+
title: "Error!",
294+
text: errMsg,
295+
icon: "error",
296+
confirmButtonText: "ok",
297+
});
298+
}
299+
300+
function showToast(toastMsg) {
301+
return Swal.fire({
302+
text: toastMsg,
303+
showConfirmButton: false,
304+
showCloseButton: true,
305+
timer: 5000,
306+
toast: true,
307+
position: "top-end",
308+
customClass: {
309+
popup: "toast-popup",
310+
},
311+
});
312+
}

0 commit comments

Comments
 (0)