@flareapp/vite 1.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) Spatie bvba <info@spatie.be>
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
13
+ all 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
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # Vite plugin for sending sourcemaps to Flare
2
+
3
+ The Flare Vite plugin helps you send sourcemaps of your compiled JavaScript code to Flare. This way, reports sent using the `@flareapp/js` will be formatted correctly.
4
+
5
+ Additionally, it automatically passes the Flare API key to `@flareapp/js`. This way, `flare.light()` works without any additional configuration.
6
+
7
+ Check the JavaScript error tracking section in [the Flare documentation](https://flareapp.io/docs/javascript-error-tracking/installation) for more information.
8
+
9
+ ## Installation
10
+
11
+ Install the plugin using NPM or Yarn:
12
+
13
+ ```bash
14
+ yarn add @flareapp/vite
15
+ # or
16
+ npm install @flareapp/vite
17
+ ```
18
+ Next, add the plugin to your `vite.config.js` file:
19
+
20
+ ```js
21
+ import { defineConfig } from 'vite';
22
+ import flareSourcemapUploader from '@flareapp/vite';
23
+
24
+ export default defineConfig({
25
+ plugins: [
26
+ flareSourcemapUploader({
27
+ key: 'YOUR API KEY HERE'
28
+ }),
29
+ ],
30
+ });
31
+ ```
32
+
33
+ Run the `vite build` command to make sure the sourcemaps are generated. You should see the following lines in the output:
34
+
35
+ ```bash
36
+ @flareapp/flare-vite-plugin-sourcemaps: Uploading 12 sourcemap files to Flare.
37
+ @flareapp/flare-vite-plugin-sourcemaps: Successfully uploaded sourcemaps to Flare.
38
+ ```
39
+
40
+ ## Configuration
41
+
42
+ - `key: string` **(required)**: the Flare API key
43
+ - `base: string`: the base path of built output (defaults to Vite's base path)
44
+ - `runInDevelopment: boolean`: whether to upload sourcemaps when `NODE_ENV=development` or when running the dev server (defaults to `false`)
45
+ - `version: string`: the sourcemap version (defaults to a fresh `uuid` per build)
46
+ - `removeSourcemaps: boolean`: whether to remove the sourcemaps after uploading them (defaults to `false`). Comes in handy when you want to upload sourcemaps to Flare but don't want them published in your build.
47
+
48
+ ## development
49
+
50
+ Publish a new release:
51
+
52
+ ```bash
53
+ npm version patch
54
+ npm publish
55
+ ```
56
+
57
+ Tag the release:
58
+
59
+ <pre>
60
+ git tag <var>VERSION</var>
61
+ git push origin <var>VERSION</var>
62
+ </pre>
63
+
64
+ Replace <var>VERSION</var> with `v` + the version from `package.json` — for example, `v1.0.2`
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@flareapp/vite",
3
+ "version": "1.0.0-beta.1",
4
+ "description": "Vite plugin for flareapp.io",
5
+ "homepage": "https://flareapp.io",
6
+ "bugs": "https://github.com/facade/flare-client-js/issues",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/facade/flare-client-js.git"
10
+ },
11
+ "license": "MIT",
12
+ "author": "adriaan@spatie.be",
13
+ "main": "./dist/index.js",
14
+ "module": "./dist/index.mjs",
15
+ "types": "./dist/index.d.ts",
16
+ "scripts": {
17
+ "prepublishOnly": "npm run build",
18
+ "build": "tsup src/index.ts --format cjs,esm --dts --clean",
19
+ "test": "exit 0",
20
+ "typescript": "tsc"
21
+ },
22
+ "dependencies": {
23
+ "@flareapp/js": "^1.0.0",
24
+ "axios": "^1.2.1",
25
+ "fast-glob": "^3.2.12"
26
+ },
27
+ "devDependencies": {
28
+ "@flareapp/js": "file:../js",
29
+ "@types/node": "^18.11.17",
30
+ "typescript": "^5.3.3",
31
+ "vite": "^4.0.0||^5.0.0"
32
+ },
33
+ "peerDependencies": {
34
+ "vite": "^4.0.0||^5.0.0"
35
+ },
36
+ "publishConfig": {
37
+ "access": "public"
38
+ }
39
+ }
@@ -0,0 +1,39 @@
1
+ import { deflateRawSync } from 'zlib';
2
+ import axios from 'axios';
3
+ import { Sourcemap } from './index';
4
+
5
+ export default class FlareApi {
6
+ endpoint: string;
7
+ key: string;
8
+ version: string;
9
+
10
+ constructor(endpoint: string, key: string, version: string) {
11
+ this.endpoint = endpoint;
12
+ this.key = key;
13
+ this.version = version;
14
+ }
15
+
16
+ uploadSourcemap(sourcemap: Sourcemap) {
17
+ return new Promise((resolve, reject) => {
18
+ const base64GzipSourcemap = deflateRawSync(
19
+ sourcemap.content,
20
+ ).toString('base64');
21
+
22
+ axios
23
+ .post(this.endpoint, {
24
+ key: this.key,
25
+ version_id: this.version,
26
+ relative_filename: sourcemap.original_file,
27
+ sourcemap: base64GzipSourcemap,
28
+ })
29
+ .then(resolve)
30
+ .catch((error) => {
31
+ return reject(
32
+ `${error.response.status}: ${JSON.stringify(
33
+ error.response.data,
34
+ )}`,
35
+ );
36
+ });
37
+ });
38
+ }
39
+ }
package/src/index.ts ADDED
@@ -0,0 +1,173 @@
1
+ import { resolve } from 'path';
2
+ import { existsSync, readFileSync, unlinkSync } from 'fs';
3
+ import glob from 'fast-glob';
4
+ import { Plugin, ResolvedConfig, UserConfig } from 'vite';
5
+ import { OutputOptions } from 'rollup';
6
+ import { uuid } from './util';
7
+ import FlareApi from './flareApi';
8
+
9
+ export type PluginConfig = {
10
+ key: string;
11
+ base?: string;
12
+ apiEndpoint?: string;
13
+ runInDevelopment?: boolean;
14
+ version?: string;
15
+ removeSourcemaps?: boolean;
16
+ };
17
+
18
+ export type Sourcemap = {
19
+ original_file: string;
20
+ content: string;
21
+ sourcemap_url: string;
22
+ };
23
+
24
+ export default function flareSourcemapUploader({
25
+ key,
26
+ base,
27
+ apiEndpoint = 'https://flareapp.io/api/sourcemaps',
28
+ runInDevelopment = false,
29
+ version = uuid(),
30
+ removeSourcemaps = false,
31
+ }: PluginConfig): Plugin {
32
+ if (!key) {
33
+ flareLog(
34
+ 'No Flare API key was provided, not uploading sourcemaps to Flare.',
35
+ );
36
+ }
37
+
38
+ const flare = new FlareApi(apiEndpoint, key, version);
39
+
40
+ const enableUploadingSourcemaps =
41
+ key &&
42
+ (process.env.NODE_ENV !== 'development' || runInDevelopment) &&
43
+ process.env.SKIP_SOURCEMAPS !== 'true';
44
+
45
+ return {
46
+ name: 'flare-vite-plugin',
47
+ apply: 'build',
48
+
49
+ config({ build }: UserConfig, { mode }: { mode: string }) {
50
+ return {
51
+ // Set FLARE_SOURCEMAP_VERSION and API key so the Flare JS client can read it
52
+ define: {
53
+ FLARE_SOURCEMAP_VERSION: `'${version}'`,
54
+ FLARE_JS_KEY: `'${key}'`,
55
+ },
56
+ build: {
57
+ sourcemap: (() => {
58
+ if (build?.sourcemap !== undefined)
59
+ return build.sourcemap;
60
+ const enableSourcemaps =
61
+ enableUploadingSourcemaps && mode !== 'development';
62
+ if (enableSourcemaps) return 'hidden';
63
+ return false;
64
+ })(),
65
+ },
66
+ };
67
+ },
68
+
69
+ configResolved(config: ResolvedConfig) {
70
+ base = base || config.base;
71
+ base += base.endsWith('/') ? '' : '/';
72
+ },
73
+
74
+ async writeBundle(outputConfig: OutputOptions) {
75
+ if (!enableUploadingSourcemaps) {
76
+ return;
77
+ }
78
+
79
+ const outputDir = outputConfig.dir || '';
80
+
81
+ const files = await glob('./**/*.map', { cwd: outputDir });
82
+ const sourcemaps = files
83
+ .map((file): Sourcemap | null => {
84
+ const sourcePath = file.replace(/\.map$/, '');
85
+ const sourceFilename = resolve(outputDir, sourcePath);
86
+
87
+ if (!existsSync(sourceFilename)) {
88
+ flareLog(
89
+ `no corresponding source found for "${file}"`,
90
+ true,
91
+ );
92
+ return null;
93
+ }
94
+
95
+ const sourcemapLocation = resolve(outputDir, file);
96
+
97
+ try {
98
+ return {
99
+ content: readFileSync(sourcemapLocation, 'utf8'),
100
+ sourcemap_url: sourcemapLocation,
101
+ original_file: `${base}${sourcePath}`,
102
+ };
103
+ } catch (error) {
104
+ flareLog(
105
+ 'Error reading sourcemap file ' +
106
+ sourcemapLocation +
107
+ ': ' +
108
+ error,
109
+ true,
110
+ );
111
+ return null;
112
+ }
113
+ })
114
+ .filter((sourcemap) => sourcemap !== null) as Sourcemap[];
115
+
116
+ if (!sourcemaps.length) {
117
+ return;
118
+ }
119
+
120
+ flareLog(
121
+ `Uploading ${sourcemaps.length} sourcemap files to Flare.`,
122
+ );
123
+
124
+ const pendingUploads = sourcemaps.map(
125
+ (sourcemap) => () => flare.uploadSourcemap(sourcemap),
126
+ );
127
+
128
+ try {
129
+ while (pendingUploads.length) {
130
+ // Maximum 10 at once https://stackoverflow.com/a/58686835
131
+ await Promise.all(
132
+ pendingUploads.splice(0, 10).map((f) => f()),
133
+ );
134
+ }
135
+
136
+ flareLog('Successfully uploaded sourcemaps to Flare.');
137
+ } catch (error) {
138
+ flareLog(
139
+ `Something went wrong while uploading the sourcemaps to Flare: ${error}`,
140
+ true,
141
+ );
142
+ }
143
+
144
+ if (removeSourcemaps) {
145
+ sourcemaps.forEach(({ sourcemap_url }) => {
146
+ try {
147
+ unlinkSync(sourcemap_url);
148
+ } catch (error) {
149
+ console.error(
150
+ 'Error removing sourcemap file',
151
+ sourcemap_url,
152
+ ': ',
153
+ error,
154
+ );
155
+ }
156
+ });
157
+
158
+ flareLog('Successfully removed sourcemaps.');
159
+ }
160
+ },
161
+ };
162
+ }
163
+
164
+ function flareLog(message: string, isError = false) {
165
+ const formattedMessage = '@flareapp/vite: ' + message;
166
+
167
+ if (isError) {
168
+ console.error(formattedMessage);
169
+ return;
170
+ }
171
+
172
+ console.log(formattedMessage);
173
+ }
package/src/util.ts ADDED
@@ -0,0 +1,11 @@
1
+ export function uuid() {
2
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(
3
+ /[xy]/g,
4
+ function (c) {
5
+ const r = (Math.random() * 16) | 0;
6
+ const v = c == 'x' ? r : (r & 0x3) | 0x8;
7
+
8
+ return v.toString(16);
9
+ },
10
+ );
11
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "include": ["src"],
4
+ }