@crxjs/vite-plugin 2.0.0-beta.12 → 2.0.0-beta.13
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 +17 -8
- 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
|
}
|
|
@@ -473,7 +477,7 @@ async function allFilesReady() {
|
|
|
473
477
|
await firstValueFrom(allFilesReady$);
|
|
474
478
|
}
|
|
475
479
|
|
|
476
|
-
const { outputFile } = fsx;
|
|
480
|
+
const { outputFile, remove } = fsx;
|
|
477
481
|
_debug("file-writer");
|
|
478
482
|
async function start({
|
|
479
483
|
server
|
|
@@ -494,6 +498,8 @@ async function start({
|
|
|
494
498
|
dir: outDir,
|
|
495
499
|
format: "es"
|
|
496
500
|
};
|
|
501
|
+
if (server.config.build.emptyOutDir)
|
|
502
|
+
await remove(server.config.build.outDir);
|
|
497
503
|
fileWriterEvent$.next({ type: "build_start" });
|
|
498
504
|
const build = await rollup(inputOptions);
|
|
499
505
|
await build.write(outputOptions);
|
|
@@ -999,11 +1005,13 @@ const pluginFileWriterPublic = () => {
|
|
|
999
1005
|
};
|
|
1000
1006
|
};
|
|
1001
1007
|
|
|
1002
|
-
_debug("file-writer").extend("hmr");
|
|
1003
|
-
const
|
|
1008
|
+
const debug$2 = _debug("file-writer").extend("hmr");
|
|
1009
|
+
const isCustomPayload = (p) => {
|
|
1010
|
+
return p.type === "custom";
|
|
1011
|
+
};
|
|
1004
1012
|
const hmrPayload$ = new Subject();
|
|
1005
1013
|
const crxHMRPayload$ = hmrPayload$.pipe(
|
|
1006
|
-
filter((p) => !
|
|
1014
|
+
filter((p) => !isCustomPayload(p)),
|
|
1007
1015
|
buffer(allFilesReady$),
|
|
1008
1016
|
mergeMap((pps) => {
|
|
1009
1017
|
let fullReload;
|
|
@@ -1062,6 +1070,7 @@ const crxHMRPayload$ = hmrPayload$.pipe(
|
|
|
1062
1070
|
}
|
|
1063
1071
|
}),
|
|
1064
1072
|
map((data) => {
|
|
1073
|
+
debug$2(`hmr payload`, data);
|
|
1065
1074
|
return {
|
|
1066
1075
|
type: "custom",
|
|
1067
1076
|
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.13",
|
|
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
|
}
|