@flareapp/vite 1.0.0-beta.4 → 1.0.0-beta.6

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.
@@ -0,0 +1,18 @@
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 };
@@ -0,0 +1,18 @@
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 ADDED
@@ -0,0 +1,202 @@
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 src_exports = {};
32
+ __export(src_exports, {
33
+ default: () => flareSourcemapUploader
34
+ });
35
+ module.exports = __toCommonJS(src_exports);
36
+ var import_path = require("path");
37
+ var import_fs = require("fs");
38
+ var import_fast_glob = __toESM(require("fast-glob"));
39
+
40
+ // src/util.ts
41
+ function uuid() {
42
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
43
+ /[xy]/g,
44
+ function(c) {
45
+ const r = Math.random() * 16 | 0;
46
+ const v = c == "x" ? r : r & 3 | 8;
47
+ return v.toString(16);
48
+ }
49
+ );
50
+ }
51
+
52
+ // src/flareApi.ts
53
+ var import_zlib = require("zlib");
54
+ var import_axios = __toESM(require("axios"));
55
+ var FlareApi = class {
56
+ constructor(endpoint, key, version) {
57
+ this.endpoint = endpoint;
58
+ this.key = key;
59
+ this.version = version;
60
+ }
61
+ uploadSourcemap(sourcemap) {
62
+ return new Promise((resolve2, reject) => {
63
+ const base64GzipSourcemap = (0, import_zlib.deflateRawSync)(
64
+ sourcemap.content
65
+ ).toString("base64");
66
+ import_axios.default.post(this.endpoint, {
67
+ key: this.key,
68
+ version_id: this.version,
69
+ relative_filename: sourcemap.original_file,
70
+ sourcemap: base64GzipSourcemap
71
+ }).then(resolve2).catch((error) => {
72
+ return reject(
73
+ `${error.response.status}: ${JSON.stringify(
74
+ error.response.data
75
+ )}`
76
+ );
77
+ });
78
+ });
79
+ }
80
+ };
81
+
82
+ // src/index.ts
83
+ function flareSourcemapUploader({
84
+ key,
85
+ base,
86
+ apiEndpoint = "https://flareapp.io/api/sourcemaps",
87
+ runInDevelopment = false,
88
+ version = uuid(),
89
+ removeSourcemaps = false
90
+ }) {
91
+ if (!key) {
92
+ flareLog(
93
+ "No Flare API key was provided, not uploading sourcemaps to Flare."
94
+ );
95
+ }
96
+ const flare = new FlareApi(apiEndpoint, key, version);
97
+ const enableUploadingSourcemaps = key && (process.env.NODE_ENV !== "development" || runInDevelopment) && process.env.SKIP_SOURCEMAPS !== "true";
98
+ return {
99
+ name: "flare-vite-plugin",
100
+ apply: "build",
101
+ config({ build }, { mode }) {
102
+ return {
103
+ // Set FLARE_SOURCEMAP_VERSION and API key so the Flare JS client can read it
104
+ define: {
105
+ FLARE_SOURCEMAP_VERSION: `'${version}'`,
106
+ FLARE_JS_KEY: `'${key}'`
107
+ },
108
+ build: {
109
+ sourcemap: (() => {
110
+ if (build?.sourcemap !== void 0)
111
+ return build.sourcemap;
112
+ const enableSourcemaps = enableUploadingSourcemaps && mode !== "development";
113
+ if (enableSourcemaps)
114
+ return "hidden";
115
+ return false;
116
+ })()
117
+ }
118
+ };
119
+ },
120
+ configResolved(config) {
121
+ base = base || config.base;
122
+ base += base.endsWith("/") ? "" : "/";
123
+ },
124
+ async writeBundle(outputConfig) {
125
+ if (!enableUploadingSourcemaps) {
126
+ return;
127
+ }
128
+ const outputDir = outputConfig.dir || "";
129
+ const files = await (0, import_fast_glob.default)("./**/*.map", { cwd: outputDir });
130
+ const sourcemaps = files.map((file) => {
131
+ const sourcePath = file.replace(/\.map$/, "");
132
+ const sourceFilename = (0, import_path.resolve)(outputDir, sourcePath);
133
+ if (!(0, import_fs.existsSync)(sourceFilename)) {
134
+ flareLog(
135
+ `no corresponding source found for "${file}"`,
136
+ true
137
+ );
138
+ return null;
139
+ }
140
+ const sourcemapLocation = (0, import_path.resolve)(outputDir, file);
141
+ try {
142
+ return {
143
+ content: (0, import_fs.readFileSync)(sourcemapLocation, "utf8"),
144
+ sourcemap_url: sourcemapLocation,
145
+ original_file: `${base}${sourcePath}`
146
+ };
147
+ } catch (error) {
148
+ flareLog(
149
+ "Error reading sourcemap file " + sourcemapLocation + ": " + error,
150
+ true
151
+ );
152
+ return null;
153
+ }
154
+ }).filter((sourcemap) => sourcemap !== null);
155
+ if (!sourcemaps.length) {
156
+ return;
157
+ }
158
+ flareLog(
159
+ `Uploading ${sourcemaps.length} sourcemap files to Flare.`
160
+ );
161
+ const pendingUploads = sourcemaps.map(
162
+ (sourcemap) => () => flare.uploadSourcemap(sourcemap)
163
+ );
164
+ try {
165
+ while (pendingUploads.length) {
166
+ await Promise.all(
167
+ pendingUploads.splice(0, 10).map((f) => f())
168
+ );
169
+ }
170
+ flareLog("Successfully uploaded sourcemaps to Flare.");
171
+ } catch (error) {
172
+ flareLog(
173
+ `Something went wrong while uploading the sourcemaps to Flare: ${error}`,
174
+ true
175
+ );
176
+ }
177
+ if (removeSourcemaps) {
178
+ sourcemaps.forEach(({ sourcemap_url }) => {
179
+ try {
180
+ (0, import_fs.unlinkSync)(sourcemap_url);
181
+ } catch (error) {
182
+ console.error(
183
+ "Error removing sourcemap file",
184
+ sourcemap_url,
185
+ ": ",
186
+ error
187
+ );
188
+ }
189
+ });
190
+ flareLog("Successfully removed sourcemaps.");
191
+ }
192
+ }
193
+ };
194
+ }
195
+ function flareLog(message, isError = false) {
196
+ const formattedMessage = "@flareapp/vite: " + message;
197
+ if (isError) {
198
+ console.error(formattedMessage);
199
+ return;
200
+ }
201
+ console.log(formattedMessage);
202
+ }
package/dist/index.mjs ADDED
@@ -0,0 +1,171 @@
1
+ // src/index.ts
2
+ import { resolve } from "path";
3
+ import { existsSync, readFileSync, unlinkSync } from "fs";
4
+ import glob from "fast-glob";
5
+
6
+ // src/util.ts
7
+ function uuid() {
8
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
9
+ /[xy]/g,
10
+ function(c) {
11
+ const r = Math.random() * 16 | 0;
12
+ const v = c == "x" ? r : r & 3 | 8;
13
+ return v.toString(16);
14
+ }
15
+ );
16
+ }
17
+
18
+ // src/flareApi.ts
19
+ import { deflateRawSync } from "zlib";
20
+ import axios from "axios";
21
+ var FlareApi = class {
22
+ constructor(endpoint, key, version) {
23
+ this.endpoint = endpoint;
24
+ this.key = key;
25
+ this.version = version;
26
+ }
27
+ uploadSourcemap(sourcemap) {
28
+ return new Promise((resolve2, reject) => {
29
+ const base64GzipSourcemap = deflateRawSync(
30
+ sourcemap.content
31
+ ).toString("base64");
32
+ axios.post(this.endpoint, {
33
+ key: this.key,
34
+ version_id: this.version,
35
+ relative_filename: sourcemap.original_file,
36
+ sourcemap: base64GzipSourcemap
37
+ }).then(resolve2).catch((error) => {
38
+ return reject(
39
+ `${error.response.status}: ${JSON.stringify(
40
+ error.response.data
41
+ )}`
42
+ );
43
+ });
44
+ });
45
+ }
46
+ };
47
+
48
+ // src/index.ts
49
+ function flareSourcemapUploader({
50
+ key,
51
+ base,
52
+ apiEndpoint = "https://flareapp.io/api/sourcemaps",
53
+ runInDevelopment = false,
54
+ version = uuid(),
55
+ removeSourcemaps = false
56
+ }) {
57
+ if (!key) {
58
+ flareLog(
59
+ "No Flare API key was provided, not uploading sourcemaps to Flare."
60
+ );
61
+ }
62
+ const flare = new FlareApi(apiEndpoint, key, version);
63
+ const enableUploadingSourcemaps = key && (process.env.NODE_ENV !== "development" || runInDevelopment) && process.env.SKIP_SOURCEMAPS !== "true";
64
+ return {
65
+ name: "flare-vite-plugin",
66
+ apply: "build",
67
+ config({ build }, { mode }) {
68
+ return {
69
+ // Set FLARE_SOURCEMAP_VERSION and API key so the Flare JS client can read it
70
+ define: {
71
+ FLARE_SOURCEMAP_VERSION: `'${version}'`,
72
+ FLARE_JS_KEY: `'${key}'`
73
+ },
74
+ build: {
75
+ sourcemap: (() => {
76
+ if (build?.sourcemap !== void 0)
77
+ return build.sourcemap;
78
+ const enableSourcemaps = enableUploadingSourcemaps && mode !== "development";
79
+ if (enableSourcemaps)
80
+ return "hidden";
81
+ return false;
82
+ })()
83
+ }
84
+ };
85
+ },
86
+ configResolved(config) {
87
+ base = base || config.base;
88
+ base += base.endsWith("/") ? "" : "/";
89
+ },
90
+ async writeBundle(outputConfig) {
91
+ if (!enableUploadingSourcemaps) {
92
+ return;
93
+ }
94
+ const outputDir = outputConfig.dir || "";
95
+ const files = await glob("./**/*.map", { cwd: outputDir });
96
+ const sourcemaps = files.map((file) => {
97
+ const sourcePath = file.replace(/\.map$/, "");
98
+ const sourceFilename = resolve(outputDir, sourcePath);
99
+ if (!existsSync(sourceFilename)) {
100
+ flareLog(
101
+ `no corresponding source found for "${file}"`,
102
+ true
103
+ );
104
+ return null;
105
+ }
106
+ const sourcemapLocation = resolve(outputDir, file);
107
+ try {
108
+ return {
109
+ content: readFileSync(sourcemapLocation, "utf8"),
110
+ sourcemap_url: sourcemapLocation,
111
+ original_file: `${base}${sourcePath}`
112
+ };
113
+ } catch (error) {
114
+ flareLog(
115
+ "Error reading sourcemap file " + sourcemapLocation + ": " + error,
116
+ true
117
+ );
118
+ return null;
119
+ }
120
+ }).filter((sourcemap) => sourcemap !== null);
121
+ if (!sourcemaps.length) {
122
+ return;
123
+ }
124
+ flareLog(
125
+ `Uploading ${sourcemaps.length} sourcemap files to Flare.`
126
+ );
127
+ const pendingUploads = sourcemaps.map(
128
+ (sourcemap) => () => flare.uploadSourcemap(sourcemap)
129
+ );
130
+ try {
131
+ while (pendingUploads.length) {
132
+ await Promise.all(
133
+ pendingUploads.splice(0, 10).map((f) => f())
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
+ if (removeSourcemaps) {
144
+ sourcemaps.forEach(({ sourcemap_url }) => {
145
+ try {
146
+ unlinkSync(sourcemap_url);
147
+ } catch (error) {
148
+ console.error(
149
+ "Error removing sourcemap file",
150
+ sourcemap_url,
151
+ ": ",
152
+ error
153
+ );
154
+ }
155
+ });
156
+ flareLog("Successfully removed sourcemaps.");
157
+ }
158
+ }
159
+ };
160
+ }
161
+ function flareLog(message, isError = false) {
162
+ const formattedMessage = "@flareapp/vite: " + message;
163
+ if (isError) {
164
+ console.error(formattedMessage);
165
+ return;
166
+ }
167
+ console.log(formattedMessage);
168
+ }
169
+ export {
170
+ flareSourcemapUploader as default
171
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flareapp/vite",
3
- "version": "1.0.0-beta.4",
3
+ "version": "1.0.0-beta.6",
4
4
  "description": "Vite plugin for flareapp.io",
5
5
  "homepage": "https://flareapp.io",
6
6
  "bugs": "https://github.com/facade/flare-client-js/issues",
@@ -13,6 +13,12 @@
13
13
  "main": "./dist/index.js",
14
14
  "module": "./dist/index.mjs",
15
15
  "types": "./dist/index.d.ts",
16
+ "exports": {
17
+ ".": {
18
+ "require": "./dist/index.js",
19
+ "import": "./dist/index.mjs"
20
+ }
21
+ },
16
22
  "scripts": {
17
23
  "prepublishOnly": "npm run build",
18
24
  "build": "tsup src/index.ts --format cjs,esm --dts --clean",