@crxjs/vite-plugin 2.0.0-beta.10 → 2.0.0-beta.12

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.
Files changed (2) hide show
  1. package/dist/index.mjs +57 -2
  2. package/package.json +4 -4
package/dist/index.mjs CHANGED
@@ -24,8 +24,10 @@ const pluginOptionsProvider = (options) => {
24
24
  name: pluginName$1,
25
25
  api: {
26
26
  crx: {
27
+ // during testing this can be null, we don't provide options through the test config
27
28
  options
28
29
  }
30
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
29
31
  }
30
32
  };
31
33
  };
@@ -292,6 +294,7 @@ const pluginBackground = () => {
292
294
  },
293
295
  {
294
296
  name: "crx:background-loader-file",
297
+ // this should happen after other plugins; the loader file is an implementation detail
295
298
  enforce: "post",
296
299
  configResolved(_config) {
297
300
  config = _config;
@@ -318,6 +321,7 @@ const pluginBackground = () => {
318
321
  }
319
322
  const refId = this.emitFile({
320
323
  type: "asset",
324
+ // fileName b/c service worker must be at root of crx
321
325
  fileName: getFileName({ type: "loader", id: "service-worker" }),
322
326
  source: loader
323
327
  });
@@ -426,6 +430,7 @@ function prepAsset(fileName, { id, source }) {
426
430
  }
427
431
  function prepScript(fileName, script) {
428
432
  return ($) => $.pipe(
433
+ // get script contents from dev server
429
434
  mergeMap(async ({ server }) => {
430
435
  const target = getOutputPath(server, fileName);
431
436
  const viteUrl = getViteUrl(script);
@@ -435,7 +440,9 @@ function prepScript(fileName, script) {
435
440
  const { code, deps = [], dynamicDeps = [] } = transformResult;
436
441
  return { target, code, deps: [...deps, ...dynamicDeps].flat(), server };
437
442
  }),
443
+ // retry in case of dependency rebundle
438
444
  retry({ count: 10, delay: 100 }),
445
+ // patch content scripts
439
446
  mergeMap(async ({ target, server, ...rest }) => {
440
447
  const plugins = server.config.plugins;
441
448
  let { code, deps } = rest;
@@ -455,7 +462,8 @@ function prepScript(fileName, script) {
455
462
  if (i.n) {
456
463
  depSet.add(i.n);
457
464
  const fileName2 = getFileName({ type: "module", id: i.n });
458
- magic.overwrite(i.s, i.e, `/${fileName2}`);
465
+ const fullImport = code.substring(i.s, i.e);
466
+ magic.overwrite(i.s, i.e, fullImport.replace(i.n, `/${fileName2}`));
459
467
  }
460
468
  return { target, source: magic.toString(), deps: [...depSet] };
461
469
  })
@@ -526,8 +534,11 @@ function update(_id) {
526
534
  async function write(fileId) {
527
535
  const start2 = performance.now();
528
536
  const deps = await firstValueFrom(
537
+ // wait for start event
529
538
  start$.pipe(
539
+ // prepare either asset or script contents
530
540
  prepFileData(fileId),
541
+ // output file and add dependencies to file writer
531
542
  mergeMap(async ({ target, source, deps: deps2 }) => {
532
543
  const files = deps2.map((id) => {
533
544
  const r = [add({ id, type: "module" })];
@@ -543,6 +554,7 @@ async function write(fileId) {
543
554
  await outputFile(target, source, { encoding: "utf8" });
544
555
  return files;
545
556
  }),
557
+ // abort write operation on close event
546
558
  takeUntil(close$),
547
559
  concatWith(of([]))
548
560
  )
@@ -707,6 +719,34 @@ const pluginDynamicContentScripts = () => {
707
719
  configResolved(_config) {
708
720
  config = _config;
709
721
  },
722
+ configureServer(server) {
723
+ return () => {
724
+ server.middlewares.use(async (req, res, next) => {
725
+ try {
726
+ await allFilesReady();
727
+ next();
728
+ } catch (error) {
729
+ let err;
730
+ if (error instanceof Error) {
731
+ err = error;
732
+ } else if (typeof error === "string") {
733
+ err = new Error(error);
734
+ } else {
735
+ err = new Error(
736
+ `Unexpected error "${error}" in middleware for "${req.url}"`
737
+ );
738
+ }
739
+ server.ws.send({
740
+ type: "error",
741
+ err: {
742
+ message: err.message,
743
+ stack: err.stack ?? "no stack available"
744
+ }
745
+ });
746
+ }
747
+ });
748
+ };
749
+ },
710
750
  async resolveId(_source, importer) {
711
751
  if (importer && _source.includes("?script")) {
712
752
  const url = new URL(_source, "stub://stub");
@@ -775,7 +815,6 @@ const pluginDynamicContentScripts = () => {
775
815
  if (config.command === "build") {
776
816
  return `export default import.meta.CRX_DYNAMIC_SCRIPT_${script.refId};`;
777
817
  } else if (typeof script.fileName === "string") {
778
- await fileReady(script);
779
818
  return `export default ${JSON.stringify(script.fileName)};`;
780
819
  } else {
781
820
  throw new Error(
@@ -788,6 +827,12 @@ const pluginDynamicContentScripts = () => {
788
827
  {
789
828
  name: "crx:dynamic-content-scripts-build",
790
829
  apply: "build",
830
+ /**
831
+ * Replace dynamic script placeholders during build.
832
+ *
833
+ * Can't use `renderChunk` b/c pre plugin crx:content-scripts uses
834
+ * `generateBundle` to emit loaders. Must come after "enforce: pre".
835
+ */
791
836
  generateBundle(options, bundle) {
792
837
  for (const chunk of Object.values(bundle))
793
838
  if (chunk.type === "chunk") {
@@ -1056,6 +1101,7 @@ const pluginHMR = () => {
1056
1101
  name: "crx:hmr",
1057
1102
  apply: "serve",
1058
1103
  enforce: "pre",
1104
+ // server hmr host should be localhost
1059
1105
  async config({ server = {}, ...config2 }) {
1060
1106
  if (server.hmr === false)
1061
1107
  return;
@@ -1065,6 +1111,7 @@ const pluginHMR = () => {
1065
1111
  server.hmr.host = "localhost";
1066
1112
  return { server, ...config2 };
1067
1113
  },
1114
+ // server should ignore outdir
1068
1115
  configResolved(_config) {
1069
1116
  config = _config;
1070
1117
  const { watch = {} } = config.server;
@@ -1102,6 +1149,7 @@ const pluginHMR = () => {
1102
1149
  closeBundle() {
1103
1150
  subs.unsubscribe();
1104
1151
  },
1152
+ // background changes require a full extension reload
1105
1153
  handleHotUpdate({ modules, server }) {
1106
1154
  const { root } = server.config;
1107
1155
  const relFiles = /* @__PURE__ */ new Set();
@@ -1129,6 +1177,7 @@ const pluginHMR = () => {
1129
1177
  name: "crx:hmr",
1130
1178
  apply: "serve",
1131
1179
  enforce: "post",
1180
+ // get final output manifest for handleHotUpdate 👆
1132
1181
  async transformCrxManifest(manifest) {
1133
1182
  inputManifestFiles = await manifestFiles(manifest, { cwd: config.root });
1134
1183
  return null;
@@ -1219,6 +1268,7 @@ const pluginHtmlInlineScripts = () => {
1219
1268
  };
1220
1269
  const postPlugin = {
1221
1270
  name: "crx:html-auditor-post",
1271
+ // this hook isn't audited b/c we add it after we set up the auditors
1222
1272
  transformIndexHtml(html, ctx) {
1223
1273
  const key = toKey(ctx);
1224
1274
  const p = pages.get(key);
@@ -1563,6 +1613,7 @@ Public dir: "${config.publicDir}"`
1563
1613
  this.emitFile({
1564
1614
  type: "asset",
1565
1615
  fileName: f,
1616
+ // TODO: cache source buffer
1566
1617
  source: await readFile(filename)
1567
1618
  });
1568
1619
  }
@@ -1675,8 +1726,11 @@ const pluginWebAccessibleResources = () => {
1675
1726
  ...rest
1676
1727
  })).filter(({ resources }) => resources.length);
1677
1728
  manifest.web_accessible_resources.push({
1729
+ // change the extension origin on every reload
1678
1730
  use_dynamic_url: true,
1731
+ // all web origins can access
1679
1732
  matches: ["<all_urls>"],
1733
+ // all resources are web accessible
1680
1734
  resources: ["**/*", "*"]
1681
1735
  });
1682
1736
  return manifest;
@@ -1806,6 +1860,7 @@ const pluginWebAccessibleResources = () => {
1806
1860
  };
1807
1861
 
1808
1862
  const crx = (options) => {
1863
+ contentScripts.clear();
1809
1864
  return [
1810
1865
  pluginOptionsProvider(options),
1811
1866
  pluginBackground(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crxjs/vite-plugin",
3
- "version": "2.0.0-beta.10",
3
+ "version": "2.0.0-beta.12",
4
4
  "description": "Build Chrome Extensions with this Vite plugin.",
5
5
  "keywords": [
6
6
  "rollup-plugin",
@@ -88,7 +88,7 @@
88
88
  "@rollup/plugin-node-resolve": "13.2.0",
89
89
  "@sveltejs/vite-plugin-svelte": "1.1.0",
90
90
  "@types/acorn": "4.0.6",
91
- "@types/chrome": "0.0.198",
91
+ "@types/chrome": "0.0.209",
92
92
  "@types/debug": "4.1.7",
93
93
  "@types/fs-extra": "9.0.13",
94
94
  "@types/jest-image-snapshot": "^5.1.0",
@@ -101,7 +101,7 @@
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.15.16",
104
+ "esbuild": "0.17.4",
105
105
  "esbuild-runner": "2.2.2",
106
106
  "eslint": "8.26.0",
107
107
  "eslint-plugin-react": "^7.29.4",
@@ -116,7 +116,7 @@
116
116
  "svelte": "^3.48.0",
117
117
  "typescript": "^4.6.4",
118
118
  "vite": "^3.1.7",
119
- "vite-plugin-inspect": "0.7.5",
119
+ "vite-plugin-inspect": "0.7.14",
120
120
  "vitest": "0.24.3",
121
121
  "vue": "3.2.41"
122
122
  }