@crxjs/vite-plugin 2.0.0-beta.12 → 2.0.0-beta.14
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/dist/index.mjs +42 -22
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { simple } from 'acorn-walk';
|
|
2
2
|
import { createHash } from 'crypto';
|
|
3
|
-
import debug$
|
|
3
|
+
import debug$3 from 'debug';
|
|
4
4
|
import v8 from 'v8';
|
|
5
5
|
import { posix } from 'path';
|
|
6
6
|
import { Subject, filter, ReplaySubject, switchMap, of, startWith, map, mergeMap, firstValueFrom, takeUntil, first, toArray, retry, concatWith, Subscription, buffer } from 'rxjs';
|
|
@@ -60,7 +60,7 @@ function isCrxPlugin(p) {
|
|
|
60
60
|
|
|
61
61
|
var workerHmrClient = "const ownOrigin = new URL(chrome.runtime.getURL(\"/\")).origin;\nself.addEventListener(\"fetch\", (fetchEvent) => {\n const url = new URL(fetchEvent.request.url);\n if (url.origin === ownOrigin) {\n fetchEvent.respondWith(sendToServer(url));\n }\n});\nasync function sendToServer(url) {\n url.protocol = \"http:\";\n url.host = \"localhost\";\n url.port = __SERVER_PORT__;\n url.searchParams.set(\"t\", Date.now().toString());\n const response = await fetch(url.href.replace(/=$|=(?=&)/g, \"\"));\n return new Response(response.body, {\n headers: {\n \"Content-Type\": response.headers.get(\"Content-Type\") ?? \"text/javascript\"\n }\n });\n}\nconst ports = /* @__PURE__ */ new Set();\nchrome.runtime.onConnect.addListener((port) => {\n if (port.name === \"@crx/client\") {\n ports.add(port);\n port.onDisconnect.addListener((port2) => ports.delete(port2));\n port.onMessage.addListener((message) => {\n });\n port.postMessage({ data: JSON.stringify({ type: \"connected\" }) });\n }\n});\nfunction notifyContentScripts(payload) {\n const data = JSON.stringify(payload);\n for (const port of ports)\n port.postMessage({ data });\n}\nconsole.log(\"[vite] connecting...\");\nconst socketProtocol = __HMR_PROTOCOL__ || (location.protocol === \"https:\" ? \"wss\" : \"ws\");\nconst socketHost = `${__HMR_HOSTNAME__ || location.hostname}:${__HMR_PORT__}`;\nconst socket = new WebSocket(`${socketProtocol}://${socketHost}`, \"vite-hmr\");\nconst base = __BASE__ || \"/\";\nsocket.addEventListener(\"message\", async ({ data }) => {\n handleSocketMessage(JSON.parse(data));\n});\nfunction isCrxHmrPayload(x) {\n return x.type === \"custom\" && x.event.startsWith(\"crx:\");\n}\nfunction handleSocketMessage(payload) {\n if (isCrxHmrPayload(payload)) {\n handleCrxHmrPayload(payload);\n } else if (payload.type === \"connected\") {\n console.log(`[vite] connected.`);\n const interval = setInterval(() => socket.send(\"ping\"), __HMR_TIMEOUT__);\n socket.addEventListener(\"close\", () => clearInterval(interval));\n }\n}\nfunction handleCrxHmrPayload(payload) {\n notifyContentScripts(payload);\n switch (payload.event) {\n case \"crx:runtime-reload\":\n console.log(\"[crx] runtime reload\");\n chrome.runtime.reload();\n break;\n }\n}\nasync function waitForSuccessfulPing(ms = 1e3) {\n while (true) {\n try {\n await fetch(`${base}__vite_ping`);\n break;\n } catch (e) {\n await new Promise((resolve) => setTimeout(resolve, ms));\n }\n }\n}\nsocket.addEventListener(\"close\", async ({ wasClean }) => {\n if (wasClean)\n return;\n console.log(`[vite] server connection lost. polling for restart...`);\n await waitForSuccessfulPing();\n handleCrxHmrPayload({\n type: \"custom\",\n event: \"crx:runtime-reload\"\n });\n});\n";
|
|
62
62
|
|
|
63
|
-
const _debug = (id) => debug$
|
|
63
|
+
const _debug = (id) => debug$3("crx").extend(id);
|
|
64
64
|
const structuredClone = (obj) => {
|
|
65
65
|
return v8.deserialize(v8.serialize(obj));
|
|
66
66
|
};
|
|
@@ -206,7 +206,7 @@ function formatFileData(script) {
|
|
|
206
206
|
return script;
|
|
207
207
|
}
|
|
208
208
|
function getFileName({ type, id }) {
|
|
209
|
-
let fileName = id.replace(/t=\d+&/, "").replace(/^\//, "").replace(/\?/g, "__").replace(/&/g, "_").replace(/=/g, "--");
|
|
209
|
+
let fileName = id.replace(/t=\d+&/, "").replace(/\?t=\d+$/, "").replace(/^\//, "").replace(/\?/g, "__").replace(/&/g, "_").replace(/=/g, "--");
|
|
210
210
|
if (fileName.includes("node_modules/")) {
|
|
211
211
|
fileName = `vendor/${fileName.split("node_modules/").pop().replace(/\//g, "-")}`;
|
|
212
212
|
} else if (fileName.startsWith("@")) {
|
|
@@ -458,10 +458,14 @@ function prepScript(fileName, script) {
|
|
|
458
458
|
const [imports] = lexer.parse(code, fileName);
|
|
459
459
|
const depSet = new Set(deps);
|
|
460
460
|
const magic = new MagicString(code);
|
|
461
|
+
const now = Date.now();
|
|
461
462
|
for (const i of imports)
|
|
462
463
|
if (i.n) {
|
|
463
464
|
depSet.add(i.n);
|
|
464
|
-
|
|
465
|
+
let fileName2 = getFileName({ type: "module", id: i.n });
|
|
466
|
+
if (!fileName2.startsWith("vendor")) {
|
|
467
|
+
fileName2 = `${fileName2}?t=${now}`;
|
|
468
|
+
}
|
|
465
469
|
const fullImport = code.substring(i.s, i.e);
|
|
466
470
|
magic.overwrite(i.s, i.e, fullImport.replace(i.n, `/${fileName2}`));
|
|
467
471
|
}
|
|
@@ -862,29 +866,42 @@ const pluginDynamicContentScripts = () => {
|
|
|
862
866
|
];
|
|
863
867
|
};
|
|
864
868
|
|
|
869
|
+
const { remove } = fsx;
|
|
865
870
|
const logger = createLogger("error", { prefix: "crxjs" });
|
|
866
871
|
const pluginFileWriter = () => {
|
|
867
872
|
fileWriterError$.subscribe((error) => {
|
|
868
873
|
logger.error(error.err.message, { error: error.err });
|
|
869
874
|
});
|
|
870
|
-
return
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
console.error(error);
|
|
879
|
-
server.close();
|
|
875
|
+
return [
|
|
876
|
+
{
|
|
877
|
+
name: "crx:file-writer-empty-out-dir",
|
|
878
|
+
apply: "serve",
|
|
879
|
+
enforce: "pre",
|
|
880
|
+
async configResolved(config) {
|
|
881
|
+
if (config.build.emptyOutDir) {
|
|
882
|
+
await remove(config.build.outDir);
|
|
880
883
|
}
|
|
881
|
-
}
|
|
882
|
-
server.httpServer?.on("close", () => close());
|
|
884
|
+
}
|
|
883
885
|
},
|
|
884
|
-
|
|
885
|
-
|
|
886
|
+
{
|
|
887
|
+
name: "crx:file-writer",
|
|
888
|
+
apply: "serve",
|
|
889
|
+
configureServer(server) {
|
|
890
|
+
server.httpServer?.on("listening", async () => {
|
|
891
|
+
try {
|
|
892
|
+
await start({ server });
|
|
893
|
+
} catch (error) {
|
|
894
|
+
console.error(error);
|
|
895
|
+
server.close();
|
|
896
|
+
}
|
|
897
|
+
});
|
|
898
|
+
server.httpServer?.on("close", () => close());
|
|
899
|
+
},
|
|
900
|
+
closeBundle() {
|
|
901
|
+
outputFiles.clear();
|
|
902
|
+
}
|
|
886
903
|
}
|
|
887
|
-
|
|
904
|
+
];
|
|
888
905
|
};
|
|
889
906
|
|
|
890
907
|
const _require = typeof require === "undefined" ? createRequire(import.meta.url) : require;
|
|
@@ -999,11 +1016,13 @@ const pluginFileWriterPublic = () => {
|
|
|
999
1016
|
};
|
|
1000
1017
|
};
|
|
1001
1018
|
|
|
1002
|
-
_debug("file-writer").extend("hmr");
|
|
1003
|
-
const
|
|
1019
|
+
const debug$2 = _debug("file-writer").extend("hmr");
|
|
1020
|
+
const isCustomPayload = (p) => {
|
|
1021
|
+
return p.type === "custom";
|
|
1022
|
+
};
|
|
1004
1023
|
const hmrPayload$ = new Subject();
|
|
1005
1024
|
const crxHMRPayload$ = hmrPayload$.pipe(
|
|
1006
|
-
filter((p) => !
|
|
1025
|
+
filter((p) => !isCustomPayload(p)),
|
|
1007
1026
|
buffer(allFilesReady$),
|
|
1008
1027
|
mergeMap((pps) => {
|
|
1009
1028
|
let fullReload;
|
|
@@ -1062,6 +1081,7 @@ const crxHMRPayload$ = hmrPayload$.pipe(
|
|
|
1062
1081
|
}
|
|
1063
1082
|
}),
|
|
1064
1083
|
map((data) => {
|
|
1084
|
+
debug$2(`hmr payload`, data);
|
|
1065
1085
|
return {
|
|
1066
1086
|
type: "custom",
|
|
1067
1087
|
event: "crx:content-script-payload",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crxjs/vite-plugin",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.14",
|
|
4
4
|
"description": "Build Chrome Extensions with this Vite plugin.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"rollup-plugin",
|
|
@@ -117,7 +117,7 @@
|
|
|
117
117
|
"typescript": "^4.6.4",
|
|
118
118
|
"vite": "^3.1.7",
|
|
119
119
|
"vite-plugin-inspect": "0.7.14",
|
|
120
|
-
"vitest": "0.
|
|
120
|
+
"vitest": "0.28.5",
|
|
121
121
|
"vue": "3.2.41"
|
|
122
122
|
}
|
|
123
123
|
}
|