@crxjs/vite-plugin 2.0.0-beta.14 → 2.0.0-beta.16

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/client.d.ts CHANGED
@@ -57,3 +57,18 @@ declare module '*?script&module' {
57
57
  const fileName: string
58
58
  export default fileName
59
59
  }
60
+
61
+ declare namespace ContentScriptAPI {
62
+ export interface ExecuteFnOptions {
63
+ perf: {
64
+ injectTime: number
65
+ loadTime: number
66
+ }
67
+ }
68
+ export interface ExecuteFn {
69
+ (options: ExecuteFnOptions): void
70
+ }
71
+ export interface ModuleExports {
72
+ onExecute?: ExecuteFn
73
+ }
74
+ }
package/dist/index.mjs CHANGED
@@ -3,7 +3,7 @@ import { createHash } from 'crypto';
3
3
  import debug$3 from 'debug';
4
4
  import v8 from 'v8';
5
5
  import { posix } from 'path';
6
- import { Subject, filter, ReplaySubject, switchMap, of, startWith, map, mergeMap, firstValueFrom, takeUntil, first, toArray, retry, concatWith, Subscription, buffer } from 'rxjs';
6
+ import { Subject, filter, ReplaySubject, switchMap, of, startWith, map, BehaviorSubject, mergeMap, firstValueFrom, takeUntil, first, toArray, retry, concatWith, Subscription, buffer } from 'rxjs';
7
7
  import fsx from 'fs-extra';
8
8
  import { performance } from 'perf_hooks';
9
9
  import { rollup } from 'rollup';
@@ -337,9 +337,9 @@ const pluginBackground = () => {
337
337
 
338
338
  var contentHmrPort = "function isCrxHMRPayload(x) {\n return x.type === \"custom\" && x.event.startsWith(\"crx:\");\n}\nclass HMRPort {\n port;\n callbacks = /* @__PURE__ */ new Map();\n constructor() {\n setInterval(() => {\n try {\n this.port?.postMessage({ data: \"ping\" });\n } catch (error) {\n if (error instanceof Error && error.message.includes(\"Extension context invalidated.\")) {\n location.reload();\n } else\n throw error;\n }\n }, __CRX_HMR_TIMEOUT__);\n setInterval(this.initPort, 5 * 60 * 1e3);\n this.initPort();\n }\n initPort = () => {\n this.port?.disconnect();\n this.port = chrome.runtime.connect({ name: \"@crx/client\" });\n this.port.onDisconnect.addListener(this.handleDisconnect.bind(this));\n this.port.onMessage.addListener(this.handleMessage.bind(this));\n this.port.postMessage({ type: \"connected\" });\n };\n handleDisconnect = () => {\n if (this.callbacks.has(\"close\"))\n for (const cb of this.callbacks.get(\"close\")) {\n cb({ wasClean: true });\n }\n };\n handleMessage = (message) => {\n const forward = (data) => {\n if (this.callbacks.has(\"message\"))\n for (const cb of this.callbacks.get(\"message\")) {\n cb({ data });\n }\n };\n const payload = JSON.parse(message.data);\n if (isCrxHMRPayload(payload)) {\n if (payload.event === \"crx:runtime-reload\") {\n console.log(\"[crx] runtime reload\");\n setTimeout(() => location.reload(), 500);\n } else {\n forward(JSON.stringify(payload.data));\n }\n } else {\n forward(message.data);\n }\n };\n addEventListener = (event, callback) => {\n const cbs = this.callbacks.get(event) ?? /* @__PURE__ */ new Set();\n cbs.add(callback);\n this.callbacks.set(event, cbs);\n };\n send = (data) => {\n if (this.port)\n this.port.postMessage({ data });\n else\n throw new Error(\"HMRPort is not initialized\");\n };\n}\n\nexport { HMRPort };\n";
339
339
 
340
- var contentDevLoader = "(function () {\n 'use strict';\n\n (async () => {\n if (__PREAMBLE__)\n await import(\n /* @vite-ignore */\n chrome.runtime.getURL(__PREAMBLE__)\n );\n await import(\n /* @vite-ignore */\n chrome.runtime.getURL(__CLIENT__)\n );\n await import(\n /* @vite-ignore */\n chrome.runtime.getURL(__SCRIPT__)\n );\n })().catch(console.error);\n\n})();\n";
340
+ var contentDevLoader = "(function () {\n 'use strict';\n\n const injectTime = performance.now();\n (async () => {\n if (__PREAMBLE__)\n await import(\n /* @vite-ignore */\n chrome.runtime.getURL(__PREAMBLE__)\n );\n await import(\n /* @vite-ignore */\n chrome.runtime.getURL(__CLIENT__)\n );\n const { onExecute } = await import(\n /* @vite-ignore */\n chrome.runtime.getURL(__SCRIPT__)\n );\n onExecute?.({ perf: { injectTime, loadTime: performance.now() - injectTime } });\n })().catch(console.error);\n\n})();\n";
341
341
 
342
- var contentProLoader = "(function () {\n 'use strict';\n\n (async () => {\n await import(\n /* @vite-ignore */\n chrome.runtime.getURL(__SCRIPT__)\n );\n })().catch(console.error);\n\n})();\n";
342
+ var contentProLoader = "(function () {\n 'use strict';\n\n const injectTime = performance.now();\n (async () => {\n const { onExecute } = await import(\n /* @vite-ignore */\n chrome.runtime.getURL(__SCRIPT__)\n );\n onExecute?.({ perf: { injectTime, loadTime: performance.now() - injectTime } });\n })().catch(console.error);\n\n})();\n";
343
343
 
344
344
  const contentScripts = new RxMap();
345
345
  contentScripts.change$.pipe(filter(RxMap.isChangeType.set)).subscribe(({ map, value }) => {
@@ -397,6 +397,10 @@ const allFilesReady$ = buildEnd$.pipe(
397
397
  map(() => [...outputFiles.values()]),
398
398
  switchMap((files) => Promise.allSettled(files.map(({ file }) => file)))
399
399
  );
400
+ const timestamp$ = new BehaviorSubject(Date.now());
401
+ allFilesReady$.subscribe(() => {
402
+ timestamp$.next(Date.now());
403
+ });
400
404
  const isRejected = (x) => x?.status === "rejected";
401
405
  const fileWriterError$ = allFilesReady$.pipe(
402
406
  mergeMap((results) => results.filter(isRejected)),
@@ -458,14 +462,10 @@ function prepScript(fileName, script) {
458
462
  const [imports] = lexer.parse(code, fileName);
459
463
  const depSet = new Set(deps);
460
464
  const magic = new MagicString(code);
461
- const now = Date.now();
462
465
  for (const i of imports)
463
466
  if (i.n) {
464
467
  depSet.add(i.n);
465
- let fileName2 = getFileName({ type: "module", id: i.n });
466
- if (!fileName2.startsWith("vendor")) {
467
- fileName2 = `${fileName2}?t=${now}`;
468
- }
468
+ const fileName2 = getFileName({ type: "module", id: i.n });
469
469
  const fullImport = code.substring(i.s, i.e);
470
470
  magic.overwrite(i.s, i.e, fullImport.replace(i.n, `/${fileName2}`));
471
471
  }
@@ -649,6 +649,19 @@ const pluginContentScripts = () => {
649
649
  name: "crx:content-scripts",
650
650
  apply: "build",
651
651
  enforce: "pre",
652
+ config(config) {
653
+ return {
654
+ ...config,
655
+ build: {
656
+ ...config.build,
657
+ rollupOptions: {
658
+ ...config.build?.rollupOptions,
659
+ // keep exports for content script module api
660
+ preserveEntrySignatures: config.build?.rollupOptions?.preserveEntrySignatures ?? "exports-only"
661
+ }
662
+ }
663
+ };
664
+ },
652
665
  generateBundle() {
653
666
  for (const [key, script] of contentScripts)
654
667
  if (key === script.refId) {
@@ -1662,10 +1675,10 @@ Public dir: "${config.publicDir}"`
1662
1675
  this.emitFile({
1663
1676
  type: "asset",
1664
1677
  fileName: "manifest.json",
1665
- source: JSON.stringify(manifest2, null, 2)
1678
+ source: JSON.stringify(manifest2, null, 2) + "\n"
1666
1679
  });
1667
1680
  } else {
1668
- manifestJson.source = JSON.stringify(manifest2, null, 2);
1681
+ manifestJson.source = JSON.stringify(manifest2, null, 2) + "\n";
1669
1682
  }
1670
1683
  delete bundle[manifestName];
1671
1684
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crxjs/vite-plugin",
3
- "version": "2.0.0-beta.14",
3
+ "version": "2.0.0-beta.16",
4
4
  "description": "Build Chrome Extensions with this Vite plugin.",
5
5
  "keywords": [
6
6
  "rollup-plugin",
@@ -101,13 +101,13 @@
101
101
  "@vitejs/plugin-react": "^2.1.0",
102
102
  "@vitejs/plugin-vue": "3.2.0",
103
103
  "chokidar": "^3.5.3",
104
- "esbuild": "0.17.4",
104
+ "esbuild": "0.17.14",
105
105
  "esbuild-runner": "2.2.2",
106
106
  "eslint": "8.26.0",
107
107
  "eslint-plugin-react": "^7.29.4",
108
108
  "jest-image-snapshot": "^5.2.0",
109
109
  "npm-run-all": "^4.1.5",
110
- "playwright-chromium": "1.27.1",
110
+ "playwright-chromium": "1.31.2",
111
111
  "react": "17.0.2",
112
112
  "react-dom": "17.0.2",
113
113
  "rimraf": "3.0.2",