@flareapp/vite 1.2.0 → 2.0.0
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/.oxlintrc.json +7 -0
- package/CHANGELOG.md +22 -0
- package/README.md +15 -43
- package/dist/index.cjs +143 -0
- package/dist/index.d.cts +28 -0
- package/dist/index.d.mts +24 -14
- package/dist/index.mjs +137 -132
- package/package.json +35 -17
- package/.release-it.json +0 -16
- package/dist/index.d.ts +0 -18
- package/dist/index.js +0 -169
- package/src/flareApi.ts +0 -34
- package/src/index.ts +0 -145
- package/src/util.ts +0 -8
- package/tsconfig.json +0 -4
package/.oxlintrc.json
ADDED
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
## 2.0.0
|
|
2
|
+
|
|
3
|
+
### Breaking changes
|
|
4
|
+
|
|
5
|
+
- Renamed plugin function: `flareSourcemapUploader` -> `flareSourcemaps` (default export unchanged).
|
|
6
|
+
- Renamed option: `key` -> `apiKey`.
|
|
7
|
+
- Renamed type: `PluginConfig` -> `FlareVitePluginOptions`.
|
|
8
|
+
- Renamed `Sourcemap` fields: `original_file` -> `originalFile`, `sourcemap_url` -> `sourcemapPath`.
|
|
9
|
+
- Removed `fast-glob` dependency. Sourcemap discovery now uses Rollup's `bundle` parameter.
|
|
10
|
+
- Replaced hand-rolled UUID with `crypto.randomUUID()`. Requires Node >= 18.
|
|
11
|
+
- HTTP 429/5xx responses are now retried with exponential backoff. Non-retriable HTTP errors (4xx) throw immediately.
|
|
12
|
+
- Failed uploads no longer abort remaining uploads (`Promise.allSettled` replaces `Promise.all`).
|
|
13
|
+
- Sourcemaps are only deleted when `removeSourcemaps` is true AND the upload succeeded.
|
|
14
|
+
|
|
15
|
+
### New
|
|
16
|
+
|
|
17
|
+
- `enforce: 'post'` ensures the plugin runs after all other plugins.
|
|
18
|
+
- Uses Vite's logger instead of bare `console.log`/`console.error`.
|
|
19
|
+
- `define` values use `JSON.stringify` (fixes injection vulnerability with special characters in keys/versions).
|
|
20
|
+
- Upload enable/disable now uses Vite's `mode` parameter instead of `process.env.NODE_ENV`.
|
|
21
|
+
- `SKIP_SOURCEMAPS=true` env var disables uploads (useful for CI matrix jobs).
|
|
22
|
+
- Added `engines: { node: ">=18" }` to package.json.
|
package/README.md
CHANGED
|
@@ -1,64 +1,36 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @flareapp/vite
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Vite build plugin for [Flare](https://flareapp.io) that uploads sourcemaps after each build. With sourcemaps, error reports sent by `@flareapp/js` will show the original source code instead of minified output.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
Check the JavaScript error tracking section in [the Flare documentation](https://flareapp.io/docs/javascript-error-tracking/installation) for more information.
|
|
5
|
+
The plugin also injects the Flare API key and a sourcemap version identifier into your build, so `flare.light()` works without any additional configuration.
|
|
8
6
|
|
|
9
7
|
## Installation
|
|
10
8
|
|
|
11
|
-
Install the plugin using NPM or Yarn:
|
|
12
|
-
|
|
13
9
|
```bash
|
|
14
|
-
yarn add @flareapp/vite
|
|
15
|
-
# or
|
|
16
10
|
npm install @flareapp/vite
|
|
17
11
|
```
|
|
18
|
-
Next, add the plugin to your `vite.config.js` file:
|
|
19
12
|
|
|
20
|
-
|
|
13
|
+
## Quick start
|
|
14
|
+
|
|
15
|
+
Add the plugin to your `vite.config.ts`:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
21
18
|
import { defineConfig } from 'vite';
|
|
22
|
-
import
|
|
19
|
+
import flareSourcemaps from '@flareapp/vite';
|
|
23
20
|
|
|
24
21
|
export default defineConfig({
|
|
25
22
|
plugins: [
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
flareSourcemaps({
|
|
24
|
+
apiKey: 'YOUR_FLARE_API_KEY',
|
|
28
25
|
}),
|
|
29
26
|
],
|
|
30
27
|
});
|
|
31
28
|
```
|
|
32
29
|
|
|
33
|
-
|
|
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
|
-
```
|
|
30
|
+
## Documentation
|
|
56
31
|
|
|
57
|
-
|
|
32
|
+
Full documentation on configuration options, sourcemap resolution, and more is available at [flareapp.io/docs/javascript/general/resolving-bundled-code](https://flareapp.io/docs/javascript/general/resolving-bundled-code).
|
|
58
33
|
|
|
59
|
-
|
|
60
|
-
git tag <var>VERSION</var>
|
|
61
|
-
git push origin <var>VERSION</var>
|
|
62
|
-
</pre>
|
|
34
|
+
## License
|
|
63
35
|
|
|
64
|
-
|
|
36
|
+
The MIT License (MIT). Please see [License File](../../LICENSE.md) for more information.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
let node_crypto = require("node:crypto");
|
|
2
|
+
let node_fs = require("node:fs");
|
|
3
|
+
let node_path = require("node:path");
|
|
4
|
+
let node_zlib = require("node:zlib");
|
|
5
|
+
|
|
6
|
+
//#region src/flareApi.ts
|
|
7
|
+
var FlareApiError = class extends Error {
|
|
8
|
+
constructor(message) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = "FlareApiError";
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const RETRIABLE_STATUS_CODES = new Set([
|
|
14
|
+
429,
|
|
15
|
+
502,
|
|
16
|
+
503,
|
|
17
|
+
504
|
|
18
|
+
]);
|
|
19
|
+
var FlareApi = class {
|
|
20
|
+
constructor(endpoint, key, version) {
|
|
21
|
+
this.endpoint = endpoint;
|
|
22
|
+
this.key = key;
|
|
23
|
+
this.version = version;
|
|
24
|
+
}
|
|
25
|
+
uploadSourcemap(sourcemap) {
|
|
26
|
+
const base64GzipSourcemap = (0, node_zlib.deflateRawSync)(sourcemap.content).toString("base64");
|
|
27
|
+
return this.postWithRetry({
|
|
28
|
+
key: this.key,
|
|
29
|
+
version_id: this.version,
|
|
30
|
+
relative_filename: sourcemap.originalFile,
|
|
31
|
+
sourcemap: base64GzipSourcemap
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
async postWithRetry(data, maxRetries = 3) {
|
|
35
|
+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
36
|
+
try {
|
|
37
|
+
const response = await fetch(this.endpoint, {
|
|
38
|
+
method: "POST",
|
|
39
|
+
headers: { "Content-Type": "application/json" },
|
|
40
|
+
body: JSON.stringify(data)
|
|
41
|
+
});
|
|
42
|
+
if (response.ok) return;
|
|
43
|
+
if (!RETRIABLE_STATUS_CODES.has(response.status)) {
|
|
44
|
+
const body = await response.text();
|
|
45
|
+
throw new FlareApiError(`Flare API returned ${response.status}: ${body}`);
|
|
46
|
+
}
|
|
47
|
+
if (attempt === maxRetries) throw new FlareApiError(`Flare API returned ${response.status} after ${maxRetries} attempts`);
|
|
48
|
+
} catch (error) {
|
|
49
|
+
if (error instanceof FlareApiError) throw error;
|
|
50
|
+
if (attempt === maxRetries) {
|
|
51
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
52
|
+
throw new Error(`Network error after ${maxRetries} attempts: ${message}`, { cause: error });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
await this.delay(Math.pow(2, attempt - 1) * 1e3);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
delay(ms) {
|
|
59
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
//#endregion
|
|
64
|
+
//#region src/index.ts
|
|
65
|
+
function flareSourcemaps({ apiKey, base, apiEndpoint = "https://flareapp.io/api/sourcemaps", runInDevelopment = false, version = (0, node_crypto.randomUUID)(), removeSourcemaps = false }) {
|
|
66
|
+
let logger = null;
|
|
67
|
+
let resolvedBase = base ?? "/";
|
|
68
|
+
let enableUpload = false;
|
|
69
|
+
const flare = new FlareApi(apiEndpoint, apiKey, version);
|
|
70
|
+
function log(message, isError = false) {
|
|
71
|
+
const formatted = `@flareapp/vite: ${message}`;
|
|
72
|
+
if (isError) if (logger) logger.error(formatted);
|
|
73
|
+
else console.error(formatted);
|
|
74
|
+
else if (logger) logger.info(formatted);
|
|
75
|
+
else console.log(formatted);
|
|
76
|
+
}
|
|
77
|
+
if (!apiKey) console.warn("@flareapp/vite: No Flare API key provided, sourcemap upload disabled.");
|
|
78
|
+
return {
|
|
79
|
+
name: "flare-vite-plugin",
|
|
80
|
+
apply: "build",
|
|
81
|
+
enforce: "post",
|
|
82
|
+
config(_userConfig, { mode }) {
|
|
83
|
+
enableUpload = !!apiKey && (mode !== "development" || runInDevelopment) && process.env.SKIP_SOURCEMAPS !== "true";
|
|
84
|
+
return {
|
|
85
|
+
define: {
|
|
86
|
+
FLARE_SOURCEMAP_VERSION: JSON.stringify(version),
|
|
87
|
+
FLARE_JS_KEY: JSON.stringify(apiKey)
|
|
88
|
+
},
|
|
89
|
+
build: { sourcemap: enableUpload ? "hidden" : void 0 }
|
|
90
|
+
};
|
|
91
|
+
},
|
|
92
|
+
configResolved(config) {
|
|
93
|
+
logger = config.logger;
|
|
94
|
+
if (!base) resolvedBase = config.base;
|
|
95
|
+
if (!resolvedBase.endsWith("/")) resolvedBase += "/";
|
|
96
|
+
},
|
|
97
|
+
async writeBundle(outputOptions, bundle) {
|
|
98
|
+
if (!enableUpload) return;
|
|
99
|
+
const outputDir = outputOptions.dir || "";
|
|
100
|
+
const sourcemaps = [];
|
|
101
|
+
for (const fileName of Object.keys(bundle)) {
|
|
102
|
+
if (!fileName.endsWith(".map")) continue;
|
|
103
|
+
const sourceFileName = fileName.replace(/\.map$/, "");
|
|
104
|
+
if (!(0, node_fs.existsSync)((0, node_path.resolve)(outputDir, sourceFileName))) {
|
|
105
|
+
log(`No corresponding source found for "${fileName}"`, true);
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
const sourcemapPath = (0, node_path.resolve)(outputDir, fileName);
|
|
109
|
+
try {
|
|
110
|
+
sourcemaps.push({
|
|
111
|
+
content: (0, node_fs.readFileSync)(sourcemapPath, "utf8"),
|
|
112
|
+
sourcemapPath,
|
|
113
|
+
originalFile: `${resolvedBase}${sourceFileName}`
|
|
114
|
+
});
|
|
115
|
+
} catch (error) {
|
|
116
|
+
log(`Error reading sourcemap ${sourcemapPath}: ${error}`, true);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (!sourcemaps.length) return;
|
|
120
|
+
log(`Uploading ${sourcemaps.length} sourcemap(s) to Flare.`);
|
|
121
|
+
const results = await Promise.allSettled(sourcemaps.map((sourcemap) => flare.uploadSourcemap(sourcemap)));
|
|
122
|
+
const failed = results.filter((r) => r.status === "rejected");
|
|
123
|
+
if (failed.length > 0) {
|
|
124
|
+
for (const result of failed) log(`Upload failed: ${result.reason}`, true);
|
|
125
|
+
log(`${failed.length}/${sourcemaps.length} sourcemap upload(s) failed.`, true);
|
|
126
|
+
} else log("Successfully uploaded all sourcemaps to Flare.");
|
|
127
|
+
if (removeSourcemaps) {
|
|
128
|
+
for (let i = 0; i < sourcemaps.length; i++) {
|
|
129
|
+
if (results[i].status === "rejected") continue;
|
|
130
|
+
try {
|
|
131
|
+
(0, node_fs.unlinkSync)(sourcemaps[i].sourcemapPath);
|
|
132
|
+
} catch (error) {
|
|
133
|
+
log(`Error removing ${sourcemaps[i].sourcemapPath}: ${error}`, true);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
log("Removed sourcemap files from build output.");
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
//#endregion
|
|
143
|
+
module.exports = flareSourcemaps;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Plugin } from "vite";
|
|
2
|
+
|
|
3
|
+
//#region src/types.d.ts
|
|
4
|
+
type FlareVitePluginOptions = {
|
|
5
|
+
apiKey: string;
|
|
6
|
+
base?: string;
|
|
7
|
+
apiEndpoint?: string;
|
|
8
|
+
runInDevelopment?: boolean;
|
|
9
|
+
version?: string;
|
|
10
|
+
removeSourcemaps?: boolean;
|
|
11
|
+
};
|
|
12
|
+
type Sourcemap = {
|
|
13
|
+
originalFile: string;
|
|
14
|
+
content: string;
|
|
15
|
+
sourcemapPath: string;
|
|
16
|
+
};
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/index.d.ts
|
|
19
|
+
declare function flareSourcemaps({
|
|
20
|
+
apiKey,
|
|
21
|
+
base,
|
|
22
|
+
apiEndpoint,
|
|
23
|
+
runInDevelopment,
|
|
24
|
+
version,
|
|
25
|
+
removeSourcemaps
|
|
26
|
+
}: FlareVitePluginOptions): Plugin;
|
|
27
|
+
//#endregion
|
|
28
|
+
export { type FlareVitePluginOptions, type Sourcemap, flareSourcemaps as default };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,18 +1,28 @@
|
|
|
1
|
-
import { Plugin } from
|
|
1
|
+
import { Plugin } from "vite";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
//#region src/types.d.ts
|
|
4
|
+
type FlareVitePluginOptions = {
|
|
5
|
+
apiKey: string;
|
|
6
|
+
base?: string;
|
|
7
|
+
apiEndpoint?: string;
|
|
8
|
+
runInDevelopment?: boolean;
|
|
9
|
+
version?: string;
|
|
10
|
+
removeSourcemaps?: boolean;
|
|
10
11
|
};
|
|
11
12
|
type Sourcemap = {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
originalFile: string;
|
|
14
|
+
content: string;
|
|
15
|
+
sourcemapPath: string;
|
|
15
16
|
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/index.d.ts
|
|
19
|
+
declare function flareSourcemaps({
|
|
20
|
+
apiKey,
|
|
21
|
+
base,
|
|
22
|
+
apiEndpoint,
|
|
23
|
+
runInDevelopment,
|
|
24
|
+
version,
|
|
25
|
+
removeSourcemaps
|
|
26
|
+
}: FlareVitePluginOptions): Plugin;
|
|
27
|
+
//#endregion
|
|
28
|
+
export { type FlareVitePluginOptions, type Sourcemap, flareSourcemaps as default };
|
package/dist/index.mjs
CHANGED
|
@@ -1,138 +1,143 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { existsSync, readFileSync, unlinkSync } from "node:fs";
|
|
3
|
+
import { resolve } from "node:path";
|
|
4
|
+
import { deflateRawSync } from "node:zlib";
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
//#region src/flareApi.ts
|
|
7
|
+
var FlareApiError = class extends Error {
|
|
8
|
+
constructor(message) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = "FlareApiError";
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const RETRIABLE_STATUS_CODES = new Set([
|
|
14
|
+
429,
|
|
15
|
+
502,
|
|
16
|
+
503,
|
|
17
|
+
504
|
|
18
|
+
]);
|
|
9
19
|
var FlareApi = class {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
20
|
+
constructor(endpoint, key, version) {
|
|
21
|
+
this.endpoint = endpoint;
|
|
22
|
+
this.key = key;
|
|
23
|
+
this.version = version;
|
|
24
|
+
}
|
|
25
|
+
uploadSourcemap(sourcemap) {
|
|
26
|
+
const base64GzipSourcemap = deflateRawSync(sourcemap.content).toString("base64");
|
|
27
|
+
return this.postWithRetry({
|
|
28
|
+
key: this.key,
|
|
29
|
+
version_id: this.version,
|
|
30
|
+
relative_filename: sourcemap.originalFile,
|
|
31
|
+
sourcemap: base64GzipSourcemap
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
async postWithRetry(data, maxRetries = 3) {
|
|
35
|
+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
36
|
+
try {
|
|
37
|
+
const response = await fetch(this.endpoint, {
|
|
38
|
+
method: "POST",
|
|
39
|
+
headers: { "Content-Type": "application/json" },
|
|
40
|
+
body: JSON.stringify(data)
|
|
41
|
+
});
|
|
42
|
+
if (response.ok) return;
|
|
43
|
+
if (!RETRIABLE_STATUS_CODES.has(response.status)) {
|
|
44
|
+
const body = await response.text();
|
|
45
|
+
throw new FlareApiError(`Flare API returned ${response.status}: ${body}`);
|
|
46
|
+
}
|
|
47
|
+
if (attempt === maxRetries) throw new FlareApiError(`Flare API returned ${response.status} after ${maxRetries} attempts`);
|
|
48
|
+
} catch (error) {
|
|
49
|
+
if (error instanceof FlareApiError) throw error;
|
|
50
|
+
if (attempt === maxRetries) {
|
|
51
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
52
|
+
throw new Error(`Network error after ${maxRetries} attempts: ${message}`, { cause: error });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
await this.delay(Math.pow(2, attempt - 1) * 1e3);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
delay(ms) {
|
|
59
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
60
|
+
}
|
|
28
61
|
};
|
|
29
62
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
63
|
+
//#endregion
|
|
64
|
+
//#region src/index.ts
|
|
65
|
+
function flareSourcemaps({ apiKey, base, apiEndpoint = "https://flareapp.io/api/sourcemaps", runInDevelopment = false, version = randomUUID(), removeSourcemaps = false }) {
|
|
66
|
+
let logger = null;
|
|
67
|
+
let resolvedBase = base ?? "/";
|
|
68
|
+
let enableUpload = false;
|
|
69
|
+
const flare = new FlareApi(apiEndpoint, apiKey, version);
|
|
70
|
+
function log(message, isError = false) {
|
|
71
|
+
const formatted = `@flareapp/vite: ${message}`;
|
|
72
|
+
if (isError) if (logger) logger.error(formatted);
|
|
73
|
+
else console.error(formatted);
|
|
74
|
+
else if (logger) logger.info(formatted);
|
|
75
|
+
else console.log(formatted);
|
|
76
|
+
}
|
|
77
|
+
if (!apiKey) console.warn("@flareapp/vite: No Flare API key provided, sourcemap upload disabled.");
|
|
78
|
+
return {
|
|
79
|
+
name: "flare-vite-plugin",
|
|
80
|
+
apply: "build",
|
|
81
|
+
enforce: "post",
|
|
82
|
+
config(_userConfig, { mode }) {
|
|
83
|
+
enableUpload = !!apiKey && (mode !== "development" || runInDevelopment) && process.env.SKIP_SOURCEMAPS !== "true";
|
|
84
|
+
return {
|
|
85
|
+
define: {
|
|
86
|
+
FLARE_SOURCEMAP_VERSION: JSON.stringify(version),
|
|
87
|
+
FLARE_JS_KEY: JSON.stringify(apiKey)
|
|
88
|
+
},
|
|
89
|
+
build: { sourcemap: enableUpload ? "hidden" : void 0 }
|
|
90
|
+
};
|
|
91
|
+
},
|
|
92
|
+
configResolved(config) {
|
|
93
|
+
logger = config.logger;
|
|
94
|
+
if (!base) resolvedBase = config.base;
|
|
95
|
+
if (!resolvedBase.endsWith("/")) resolvedBase += "/";
|
|
96
|
+
},
|
|
97
|
+
async writeBundle(outputOptions, bundle) {
|
|
98
|
+
if (!enableUpload) return;
|
|
99
|
+
const outputDir = outputOptions.dir || "";
|
|
100
|
+
const sourcemaps = [];
|
|
101
|
+
for (const fileName of Object.keys(bundle)) {
|
|
102
|
+
if (!fileName.endsWith(".map")) continue;
|
|
103
|
+
const sourceFileName = fileName.replace(/\.map$/, "");
|
|
104
|
+
if (!existsSync(resolve(outputDir, sourceFileName))) {
|
|
105
|
+
log(`No corresponding source found for "${fileName}"`, true);
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
const sourcemapPath = resolve(outputDir, fileName);
|
|
109
|
+
try {
|
|
110
|
+
sourcemaps.push({
|
|
111
|
+
content: readFileSync(sourcemapPath, "utf8"),
|
|
112
|
+
sourcemapPath,
|
|
113
|
+
originalFile: `${resolvedBase}${sourceFileName}`
|
|
114
|
+
});
|
|
115
|
+
} catch (error) {
|
|
116
|
+
log(`Error reading sourcemap ${sourcemapPath}: ${error}`, true);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (!sourcemaps.length) return;
|
|
120
|
+
log(`Uploading ${sourcemaps.length} sourcemap(s) to Flare.`);
|
|
121
|
+
const results = await Promise.allSettled(sourcemaps.map((sourcemap) => flare.uploadSourcemap(sourcemap)));
|
|
122
|
+
const failed = results.filter((r) => r.status === "rejected");
|
|
123
|
+
if (failed.length > 0) {
|
|
124
|
+
for (const result of failed) log(`Upload failed: ${result.reason}`, true);
|
|
125
|
+
log(`${failed.length}/${sourcemaps.length} sourcemap upload(s) failed.`, true);
|
|
126
|
+
} else log("Successfully uploaded all sourcemaps to Flare.");
|
|
127
|
+
if (removeSourcemaps) {
|
|
128
|
+
for (let i = 0; i < sourcemaps.length; i++) {
|
|
129
|
+
if (results[i].status === "rejected") continue;
|
|
130
|
+
try {
|
|
131
|
+
unlinkSync(sourcemaps[i].sourcemapPath);
|
|
132
|
+
} catch (error) {
|
|
133
|
+
log(`Error removing ${sourcemaps[i].sourcemapPath}: ${error}`, true);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
log("Removed sourcemap files from build output.");
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
};
|
|
37
140
|
}
|
|
38
141
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
key,
|
|
42
|
-
base,
|
|
43
|
-
apiEndpoint = "https://flareapp.io/api/sourcemaps",
|
|
44
|
-
runInDevelopment = false,
|
|
45
|
-
version = uuid(),
|
|
46
|
-
removeSourcemaps = false
|
|
47
|
-
}) {
|
|
48
|
-
if (!key) {
|
|
49
|
-
flareLog("No Flare API key was provided, not uploading sourcemaps to Flare.");
|
|
50
|
-
}
|
|
51
|
-
const flare = new FlareApi(apiEndpoint, key, version);
|
|
52
|
-
const enableUploadingSourcemaps = key && (process.env.NODE_ENV !== "development" || runInDevelopment) && process.env.SKIP_SOURCEMAPS !== "true";
|
|
53
|
-
return {
|
|
54
|
-
name: "flare-vite-plugin",
|
|
55
|
-
apply: "build",
|
|
56
|
-
config({ build }, { mode }) {
|
|
57
|
-
return {
|
|
58
|
-
// Set FLARE_SOURCEMAP_VERSION and API key so the Flare JS client can read it
|
|
59
|
-
define: {
|
|
60
|
-
FLARE_SOURCEMAP_VERSION: `'${version}'`,
|
|
61
|
-
FLARE_JS_KEY: `'${key}'`
|
|
62
|
-
},
|
|
63
|
-
build: {
|
|
64
|
-
sourcemap: (() => {
|
|
65
|
-
if (build?.sourcemap !== void 0) return build.sourcemap;
|
|
66
|
-
const enableSourcemaps = enableUploadingSourcemaps && mode !== "development";
|
|
67
|
-
if (enableSourcemaps) return "hidden";
|
|
68
|
-
return false;
|
|
69
|
-
})()
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
},
|
|
73
|
-
configResolved(config) {
|
|
74
|
-
base = base || config.base;
|
|
75
|
-
base += base.endsWith("/") ? "" : "/";
|
|
76
|
-
},
|
|
77
|
-
async writeBundle(outputConfig) {
|
|
78
|
-
if (!enableUploadingSourcemaps) {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
const outputDir = outputConfig.dir || "";
|
|
82
|
-
const files = await glob("./**/*.map", { cwd: outputDir });
|
|
83
|
-
const sourcemaps = files.map((file) => {
|
|
84
|
-
const sourcePath = file.replace(/\.map$/, "");
|
|
85
|
-
const sourceFilename = resolve(outputDir, sourcePath);
|
|
86
|
-
if (!existsSync(sourceFilename)) {
|
|
87
|
-
flareLog(`no corresponding source found for "${file}"`, true);
|
|
88
|
-
return null;
|
|
89
|
-
}
|
|
90
|
-
const sourcemapLocation = resolve(outputDir, file);
|
|
91
|
-
try {
|
|
92
|
-
return {
|
|
93
|
-
content: readFileSync(sourcemapLocation, "utf8"),
|
|
94
|
-
sourcemap_url: sourcemapLocation,
|
|
95
|
-
original_file: `${base}${sourcePath}`
|
|
96
|
-
};
|
|
97
|
-
} catch (error) {
|
|
98
|
-
flareLog("Error reading sourcemap file " + sourcemapLocation + ": " + error, true);
|
|
99
|
-
return null;
|
|
100
|
-
}
|
|
101
|
-
}).filter((sourcemap) => sourcemap !== null);
|
|
102
|
-
if (!sourcemaps.length) {
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
flareLog(`Uploading ${sourcemaps.length} sourcemap files to Flare.`);
|
|
106
|
-
const pendingUploads = sourcemaps.map((sourcemap) => () => flare.uploadSourcemap(sourcemap));
|
|
107
|
-
try {
|
|
108
|
-
while (pendingUploads.length) {
|
|
109
|
-
await Promise.all(pendingUploads.splice(0, 10).map((f) => f()));
|
|
110
|
-
}
|
|
111
|
-
flareLog("Successfully uploaded sourcemaps to Flare.");
|
|
112
|
-
} catch (error) {
|
|
113
|
-
flareLog(`Something went wrong while uploading the sourcemaps to Flare: ${error}`, true);
|
|
114
|
-
}
|
|
115
|
-
if (removeSourcemaps) {
|
|
116
|
-
sourcemaps.forEach(({ sourcemap_url }) => {
|
|
117
|
-
try {
|
|
118
|
-
unlinkSync(sourcemap_url);
|
|
119
|
-
} catch (error) {
|
|
120
|
-
console.error("Error removing sourcemap file", sourcemap_url, ": ", error);
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
flareLog("Successfully removed sourcemaps.");
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
function flareLog(message, isError = false) {
|
|
129
|
-
const formattedMessage = "@flareapp/vite: " + message;
|
|
130
|
-
if (isError) {
|
|
131
|
-
console.error(formattedMessage);
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
console.log(formattedMessage);
|
|
135
|
-
}
|
|
136
|
-
export {
|
|
137
|
-
flareSourcemapUploader as default
|
|
138
|
-
};
|
|
142
|
+
//#endregion
|
|
143
|
+
export { flareSourcemaps as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flareapp/vite",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Vite plugin for flareapp.io",
|
|
5
5
|
"homepage": "https://flareapp.io",
|
|
6
6
|
"bugs": "https://github.com/spatie/flare-client-js/issues",
|
|
@@ -9,33 +9,51 @@
|
|
|
9
9
|
"url": "git+https://github.com/spatie/flare-client-js.git"
|
|
10
10
|
},
|
|
11
11
|
"license": "MIT",
|
|
12
|
-
"author":
|
|
13
|
-
|
|
12
|
+
"author": {
|
|
13
|
+
"name": "Spatie",
|
|
14
|
+
"email": "info@spatie.be"
|
|
15
|
+
},
|
|
16
|
+
"contributors": [
|
|
17
|
+
"Adriaan Marain <adriaan@spatie.be>",
|
|
18
|
+
"Alex Vanderbist <alex@spatie.be>",
|
|
19
|
+
"Dries Heyninck <dries@spatie.be>",
|
|
20
|
+
"Freek Van der Herten <freek@spatie.be>",
|
|
21
|
+
"Sebastian De Deyne <sebastian@spatie.be>",
|
|
22
|
+
"Sébastien Henau <seba@spatie.be>"
|
|
23
|
+
],
|
|
24
|
+
"main": "./dist/index.cjs",
|
|
14
25
|
"module": "./dist/index.mjs",
|
|
15
|
-
"types": "./dist/index.d.
|
|
26
|
+
"types": "./dist/index.d.cts",
|
|
16
27
|
"exports": {
|
|
17
28
|
".": {
|
|
18
|
-
"
|
|
19
|
-
|
|
29
|
+
"import": {
|
|
30
|
+
"types": "./dist/index.d.mts",
|
|
31
|
+
"default": "./dist/index.mjs"
|
|
32
|
+
},
|
|
33
|
+
"require": {
|
|
34
|
+
"types": "./dist/index.d.cts",
|
|
35
|
+
"default": "./dist/index.cjs"
|
|
36
|
+
}
|
|
20
37
|
}
|
|
21
38
|
},
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=18"
|
|
41
|
+
},
|
|
22
42
|
"scripts": {
|
|
23
43
|
"prepublishOnly": "npm run build",
|
|
24
|
-
"build": "
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
"dependencies": {
|
|
29
|
-
"axios": "^1.2.1",
|
|
30
|
-
"fast-glob": "^3.2.12"
|
|
44
|
+
"build": "tsdown src/index.ts --format cjs,esm --dts --clean",
|
|
45
|
+
"test": "vitest run",
|
|
46
|
+
"typescript": "tsc --noEmit",
|
|
47
|
+
"release": "release-it"
|
|
31
48
|
},
|
|
32
49
|
"devDependencies": {
|
|
33
|
-
"
|
|
34
|
-
"typescript": "^5.
|
|
35
|
-
"vite": "^
|
|
50
|
+
"tsdown": "^0.20.3",
|
|
51
|
+
"typescript": "^5.7.0",
|
|
52
|
+
"vite": "^5.0.0||^6.0.0||^7.0.0||^8.0.0",
|
|
53
|
+
"vitest": "^4.0.0"
|
|
36
54
|
},
|
|
37
55
|
"peerDependencies": {
|
|
38
|
-
"vite": "^
|
|
56
|
+
"vite": "^5.0.0||^6.0.0||^7.0.0||^8.0.0"
|
|
39
57
|
},
|
|
40
58
|
"publishConfig": {
|
|
41
59
|
"access": "public"
|
package/.release-it.json
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"git": {
|
|
3
|
-
"commitMessage": "Release @flareapp/vite v${version}",
|
|
4
|
-
"tagName": "@flareapp/vite@${version}",
|
|
5
|
-
"tagAnnotation": "Release @flareapp/vite v${version}"
|
|
6
|
-
},
|
|
7
|
-
"github": {
|
|
8
|
-
"release": false
|
|
9
|
-
},
|
|
10
|
-
"npm": {
|
|
11
|
-
"publish": true
|
|
12
|
-
},
|
|
13
|
-
"hooks": {
|
|
14
|
-
"after:bump": "npm run build"
|
|
15
|
-
}
|
|
16
|
-
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { Plugin } from 'vite';
|
|
2
|
-
|
|
3
|
-
type PluginConfig = {
|
|
4
|
-
key: string;
|
|
5
|
-
base?: string;
|
|
6
|
-
apiEndpoint?: string;
|
|
7
|
-
runInDevelopment?: boolean;
|
|
8
|
-
version?: string;
|
|
9
|
-
removeSourcemaps?: boolean;
|
|
10
|
-
};
|
|
11
|
-
type Sourcemap = {
|
|
12
|
-
original_file: string;
|
|
13
|
-
content: string;
|
|
14
|
-
sourcemap_url: string;
|
|
15
|
-
};
|
|
16
|
-
declare function flareSourcemapUploader({ key, base, apiEndpoint, runInDevelopment, version, removeSourcemaps, }: PluginConfig): Plugin;
|
|
17
|
-
|
|
18
|
-
export { type PluginConfig, type Sourcemap, flareSourcemapUploader as default };
|
package/dist/index.js
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
|
-
// src/index.ts
|
|
31
|
-
var index_exports = {};
|
|
32
|
-
__export(index_exports, {
|
|
33
|
-
default: () => flareSourcemapUploader
|
|
34
|
-
});
|
|
35
|
-
module.exports = __toCommonJS(index_exports);
|
|
36
|
-
var import_fast_glob = __toESM(require("fast-glob"));
|
|
37
|
-
var import_fs = require("fs");
|
|
38
|
-
var import_path = require("path");
|
|
39
|
-
|
|
40
|
-
// src/flareApi.ts
|
|
41
|
-
var import_axios = __toESM(require("axios"));
|
|
42
|
-
var import_zlib = require("zlib");
|
|
43
|
-
var FlareApi = class {
|
|
44
|
-
constructor(endpoint, key, version) {
|
|
45
|
-
this.endpoint = endpoint;
|
|
46
|
-
this.key = key;
|
|
47
|
-
this.version = version;
|
|
48
|
-
}
|
|
49
|
-
uploadSourcemap(sourcemap) {
|
|
50
|
-
return new Promise((resolve2, reject) => {
|
|
51
|
-
const base64GzipSourcemap = (0, import_zlib.deflateRawSync)(sourcemap.content).toString("base64");
|
|
52
|
-
import_axios.default.post(this.endpoint, {
|
|
53
|
-
key: this.key,
|
|
54
|
-
version_id: this.version,
|
|
55
|
-
relative_filename: sourcemap.original_file,
|
|
56
|
-
sourcemap: base64GzipSourcemap
|
|
57
|
-
}).then(resolve2).catch((error) => {
|
|
58
|
-
return reject(`${error.response.status}: ${JSON.stringify(error.response.data)}`);
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
// src/util.ts
|
|
65
|
-
function uuid() {
|
|
66
|
-
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
|
|
67
|
-
const r = Math.random() * 16 | 0;
|
|
68
|
-
const v = c == "x" ? r : r & 3 | 8;
|
|
69
|
-
return v.toString(16);
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// src/index.ts
|
|
74
|
-
function flareSourcemapUploader({
|
|
75
|
-
key,
|
|
76
|
-
base,
|
|
77
|
-
apiEndpoint = "https://flareapp.io/api/sourcemaps",
|
|
78
|
-
runInDevelopment = false,
|
|
79
|
-
version = uuid(),
|
|
80
|
-
removeSourcemaps = false
|
|
81
|
-
}) {
|
|
82
|
-
if (!key) {
|
|
83
|
-
flareLog("No Flare API key was provided, not uploading sourcemaps to Flare.");
|
|
84
|
-
}
|
|
85
|
-
const flare = new FlareApi(apiEndpoint, key, version);
|
|
86
|
-
const enableUploadingSourcemaps = key && (process.env.NODE_ENV !== "development" || runInDevelopment) && process.env.SKIP_SOURCEMAPS !== "true";
|
|
87
|
-
return {
|
|
88
|
-
name: "flare-vite-plugin",
|
|
89
|
-
apply: "build",
|
|
90
|
-
config({ build }, { mode }) {
|
|
91
|
-
return {
|
|
92
|
-
// Set FLARE_SOURCEMAP_VERSION and API key so the Flare JS client can read it
|
|
93
|
-
define: {
|
|
94
|
-
FLARE_SOURCEMAP_VERSION: `'${version}'`,
|
|
95
|
-
FLARE_JS_KEY: `'${key}'`
|
|
96
|
-
},
|
|
97
|
-
build: {
|
|
98
|
-
sourcemap: (() => {
|
|
99
|
-
if (build?.sourcemap !== void 0) return build.sourcemap;
|
|
100
|
-
const enableSourcemaps = enableUploadingSourcemaps && mode !== "development";
|
|
101
|
-
if (enableSourcemaps) return "hidden";
|
|
102
|
-
return false;
|
|
103
|
-
})()
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
},
|
|
107
|
-
configResolved(config) {
|
|
108
|
-
base = base || config.base;
|
|
109
|
-
base += base.endsWith("/") ? "" : "/";
|
|
110
|
-
},
|
|
111
|
-
async writeBundle(outputConfig) {
|
|
112
|
-
if (!enableUploadingSourcemaps) {
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
const outputDir = outputConfig.dir || "";
|
|
116
|
-
const files = await (0, import_fast_glob.default)("./**/*.map", { cwd: outputDir });
|
|
117
|
-
const sourcemaps = files.map((file) => {
|
|
118
|
-
const sourcePath = file.replace(/\.map$/, "");
|
|
119
|
-
const sourceFilename = (0, import_path.resolve)(outputDir, sourcePath);
|
|
120
|
-
if (!(0, import_fs.existsSync)(sourceFilename)) {
|
|
121
|
-
flareLog(`no corresponding source found for "${file}"`, true);
|
|
122
|
-
return null;
|
|
123
|
-
}
|
|
124
|
-
const sourcemapLocation = (0, import_path.resolve)(outputDir, file);
|
|
125
|
-
try {
|
|
126
|
-
return {
|
|
127
|
-
content: (0, import_fs.readFileSync)(sourcemapLocation, "utf8"),
|
|
128
|
-
sourcemap_url: sourcemapLocation,
|
|
129
|
-
original_file: `${base}${sourcePath}`
|
|
130
|
-
};
|
|
131
|
-
} catch (error) {
|
|
132
|
-
flareLog("Error reading sourcemap file " + sourcemapLocation + ": " + error, true);
|
|
133
|
-
return null;
|
|
134
|
-
}
|
|
135
|
-
}).filter((sourcemap) => sourcemap !== null);
|
|
136
|
-
if (!sourcemaps.length) {
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
flareLog(`Uploading ${sourcemaps.length} sourcemap files to Flare.`);
|
|
140
|
-
const pendingUploads = sourcemaps.map((sourcemap) => () => flare.uploadSourcemap(sourcemap));
|
|
141
|
-
try {
|
|
142
|
-
while (pendingUploads.length) {
|
|
143
|
-
await Promise.all(pendingUploads.splice(0, 10).map((f) => f()));
|
|
144
|
-
}
|
|
145
|
-
flareLog("Successfully uploaded sourcemaps to Flare.");
|
|
146
|
-
} catch (error) {
|
|
147
|
-
flareLog(`Something went wrong while uploading the sourcemaps to Flare: ${error}`, true);
|
|
148
|
-
}
|
|
149
|
-
if (removeSourcemaps) {
|
|
150
|
-
sourcemaps.forEach(({ sourcemap_url }) => {
|
|
151
|
-
try {
|
|
152
|
-
(0, import_fs.unlinkSync)(sourcemap_url);
|
|
153
|
-
} catch (error) {
|
|
154
|
-
console.error("Error removing sourcemap file", sourcemap_url, ": ", error);
|
|
155
|
-
}
|
|
156
|
-
});
|
|
157
|
-
flareLog("Successfully removed sourcemaps.");
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
function flareLog(message, isError = false) {
|
|
163
|
-
const formattedMessage = "@flareapp/vite: " + message;
|
|
164
|
-
if (isError) {
|
|
165
|
-
console.error(formattedMessage);
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
console.log(formattedMessage);
|
|
169
|
-
}
|
package/src/flareApi.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
2
|
-
import { deflateRawSync } from 'zlib';
|
|
3
|
-
|
|
4
|
-
import { Sourcemap } from './index';
|
|
5
|
-
|
|
6
|
-
export default class FlareApi {
|
|
7
|
-
endpoint: string;
|
|
8
|
-
key: string;
|
|
9
|
-
version: string;
|
|
10
|
-
|
|
11
|
-
constructor(endpoint: string, key: string, version: string) {
|
|
12
|
-
this.endpoint = endpoint;
|
|
13
|
-
this.key = key;
|
|
14
|
-
this.version = version;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
uploadSourcemap(sourcemap: Sourcemap) {
|
|
18
|
-
return new Promise((resolve, reject) => {
|
|
19
|
-
const base64GzipSourcemap = deflateRawSync(sourcemap.content).toString('base64');
|
|
20
|
-
|
|
21
|
-
axios
|
|
22
|
-
.post(this.endpoint, {
|
|
23
|
-
key: this.key,
|
|
24
|
-
version_id: this.version,
|
|
25
|
-
relative_filename: sourcemap.original_file,
|
|
26
|
-
sourcemap: base64GzipSourcemap,
|
|
27
|
-
})
|
|
28
|
-
.then(resolve)
|
|
29
|
-
.catch((error) => {
|
|
30
|
-
return reject(`${error.response.status}: ${JSON.stringify(error.response.data)}`);
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
import glob from 'fast-glob';
|
|
2
|
-
import { existsSync, readFileSync, unlinkSync } from 'fs';
|
|
3
|
-
import { resolve } from 'path';
|
|
4
|
-
import { OutputOptions } from 'rollup';
|
|
5
|
-
import { Plugin, ResolvedConfig, UserConfig } from 'vite';
|
|
6
|
-
|
|
7
|
-
import FlareApi from './flareApi';
|
|
8
|
-
import { uuid } from './util';
|
|
9
|
-
|
|
10
|
-
export type PluginConfig = {
|
|
11
|
-
key: string;
|
|
12
|
-
base?: string;
|
|
13
|
-
apiEndpoint?: string;
|
|
14
|
-
runInDevelopment?: boolean;
|
|
15
|
-
version?: string;
|
|
16
|
-
removeSourcemaps?: boolean;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export type Sourcemap = {
|
|
20
|
-
original_file: string;
|
|
21
|
-
content: string;
|
|
22
|
-
sourcemap_url: string;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export default function flareSourcemapUploader({
|
|
26
|
-
key,
|
|
27
|
-
base,
|
|
28
|
-
apiEndpoint = 'https://flareapp.io/api/sourcemaps',
|
|
29
|
-
runInDevelopment = false,
|
|
30
|
-
version = uuid(),
|
|
31
|
-
removeSourcemaps = false,
|
|
32
|
-
}: PluginConfig): Plugin {
|
|
33
|
-
if (!key) {
|
|
34
|
-
flareLog('No Flare API key was provided, not uploading sourcemaps to Flare.');
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const flare = new FlareApi(apiEndpoint, key, version);
|
|
38
|
-
|
|
39
|
-
const enableUploadingSourcemaps =
|
|
40
|
-
key && (process.env.NODE_ENV !== 'development' || runInDevelopment) && process.env.SKIP_SOURCEMAPS !== 'true';
|
|
41
|
-
|
|
42
|
-
return {
|
|
43
|
-
name: 'flare-vite-plugin',
|
|
44
|
-
apply: 'build',
|
|
45
|
-
|
|
46
|
-
config({ build }: UserConfig, { mode }: { mode: string }) {
|
|
47
|
-
return {
|
|
48
|
-
// Set FLARE_SOURCEMAP_VERSION and API key so the Flare JS client can read it
|
|
49
|
-
define: {
|
|
50
|
-
FLARE_SOURCEMAP_VERSION: `'${version}'`,
|
|
51
|
-
FLARE_JS_KEY: `'${key}'`,
|
|
52
|
-
},
|
|
53
|
-
build: {
|
|
54
|
-
sourcemap: (() => {
|
|
55
|
-
if (build?.sourcemap !== undefined) return build.sourcemap;
|
|
56
|
-
const enableSourcemaps = enableUploadingSourcemaps && mode !== 'development';
|
|
57
|
-
if (enableSourcemaps) return 'hidden';
|
|
58
|
-
return false;
|
|
59
|
-
})(),
|
|
60
|
-
},
|
|
61
|
-
};
|
|
62
|
-
},
|
|
63
|
-
|
|
64
|
-
configResolved(config: ResolvedConfig) {
|
|
65
|
-
base = base || config.base;
|
|
66
|
-
base += base.endsWith('/') ? '' : '/';
|
|
67
|
-
},
|
|
68
|
-
|
|
69
|
-
async writeBundle(outputConfig: OutputOptions) {
|
|
70
|
-
if (!enableUploadingSourcemaps) {
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const outputDir = outputConfig.dir || '';
|
|
75
|
-
|
|
76
|
-
const files = await glob('./**/*.map', { cwd: outputDir });
|
|
77
|
-
const sourcemaps = files
|
|
78
|
-
.map((file): Sourcemap | null => {
|
|
79
|
-
const sourcePath = file.replace(/\.map$/, '');
|
|
80
|
-
const sourceFilename = resolve(outputDir, sourcePath);
|
|
81
|
-
|
|
82
|
-
if (!existsSync(sourceFilename)) {
|
|
83
|
-
flareLog(`no corresponding source found for "${file}"`, true);
|
|
84
|
-
return null;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const sourcemapLocation = resolve(outputDir, file);
|
|
88
|
-
|
|
89
|
-
try {
|
|
90
|
-
return {
|
|
91
|
-
content: readFileSync(sourcemapLocation, 'utf8'),
|
|
92
|
-
sourcemap_url: sourcemapLocation,
|
|
93
|
-
original_file: `${base}${sourcePath}`,
|
|
94
|
-
};
|
|
95
|
-
} catch (error) {
|
|
96
|
-
flareLog('Error reading sourcemap file ' + sourcemapLocation + ': ' + error, true);
|
|
97
|
-
return null;
|
|
98
|
-
}
|
|
99
|
-
})
|
|
100
|
-
.filter((sourcemap) => sourcemap !== null) as Sourcemap[];
|
|
101
|
-
|
|
102
|
-
if (!sourcemaps.length) {
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
flareLog(`Uploading ${sourcemaps.length} sourcemap files to Flare.`);
|
|
107
|
-
|
|
108
|
-
const pendingUploads = sourcemaps.map((sourcemap) => () => flare.uploadSourcemap(sourcemap));
|
|
109
|
-
|
|
110
|
-
try {
|
|
111
|
-
while (pendingUploads.length) {
|
|
112
|
-
// Maximum 10 at once https://stackoverflow.com/a/58686835
|
|
113
|
-
await Promise.all(pendingUploads.splice(0, 10).map((f) => f()));
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
flareLog('Successfully uploaded sourcemaps to Flare.');
|
|
117
|
-
} catch (error) {
|
|
118
|
-
flareLog(`Something went wrong while uploading the sourcemaps to Flare: ${error}`, true);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
if (removeSourcemaps) {
|
|
122
|
-
sourcemaps.forEach(({ sourcemap_url }) => {
|
|
123
|
-
try {
|
|
124
|
-
unlinkSync(sourcemap_url);
|
|
125
|
-
} catch (error) {
|
|
126
|
-
console.error('Error removing sourcemap file', sourcemap_url, ': ', error);
|
|
127
|
-
}
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
flareLog('Successfully removed sourcemaps.');
|
|
131
|
-
}
|
|
132
|
-
},
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
function flareLog(message: string, isError = false) {
|
|
137
|
-
const formattedMessage = '@flareapp/vite: ' + message;
|
|
138
|
-
|
|
139
|
-
if (isError) {
|
|
140
|
-
console.error(formattedMessage);
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
console.log(formattedMessage);
|
|
145
|
-
}
|
package/src/util.ts
DELETED
package/tsconfig.json
DELETED