@crxjs/vite-plugin 2.0.0-beta.18 → 2.0.0-beta.19

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.d.ts CHANGED
@@ -17,6 +17,14 @@ interface WebAccessibleResourceById {
17
17
  resources: string[];
18
18
  use_dynamic_url?: boolean;
19
19
  }
20
+ interface ChromeManifestBackground {
21
+ service_worker: string;
22
+ type?: 'module';
23
+ }
24
+ interface FirefoxManifestBackground {
25
+ scripts: string[];
26
+ persistent?: false;
27
+ }
20
28
  interface ManifestV3 {
21
29
  manifest_version: number;
22
30
  name: string;
@@ -26,10 +34,7 @@ interface ManifestV3 {
26
34
  icons?: chrome.runtime.ManifestIcons | undefined;
27
35
  action?: chrome.runtime.ManifestAction | undefined;
28
36
  author?: string | undefined;
29
- background?: {
30
- service_worker: string;
31
- type?: 'module';
32
- } | undefined;
37
+ background?: ChromeManifestBackground | FirefoxManifestBackground | undefined;
33
38
  chrome_settings_overrides?: {
34
39
  homepage?: string | undefined;
35
40
  search_provider?: chrome.runtime.SearchProvider | undefined;
@@ -232,9 +237,15 @@ declare type CrxDevScriptId = {
232
237
  type: 'module' | 'iife';
233
238
  };
234
239
  interface CrxPlugin extends Plugin {
235
- /** Runs during the transform hook for the manifest. Filenames use input filenames. */
240
+ /**
241
+ * Runs during the transform hook for the manifest. Filenames use input
242
+ * filenames.
243
+ */
236
244
  transformCrxManifest?: (this: PluginContext, manifest: ManifestV3) => Promise<ManifestV3 | null | undefined> | ManifestV3 | null | undefined;
237
- /** Runs during generateBundle, before manifest output. Filenames use output filenames. */
245
+ /**
246
+ * Runs during generateBundle, before manifest output. Filenames use output
247
+ * filenames.
248
+ */
238
249
  renderCrxManifest?: (this: PluginContext, manifest: ManifestV3, bundle: OutputBundle) => Promise<ManifestV3 | null | undefined> | ManifestV3 | null | undefined;
239
250
  /**
240
251
  * Runs in the file writer on content scripts during development. `script.id`
@@ -249,7 +260,13 @@ interface CrxOptions {
249
260
  injectCss?: boolean;
250
261
  };
251
262
  fastGlobOptions?: Options;
263
+ /**
264
+ * The browser that this extension is targeting, can be "firefox" or "chrome".
265
+ * Default is "chrome".
266
+ */
267
+ browser?: Browser;
252
268
  }
269
+ declare type Browser = 'firefox' | 'chrome';
253
270
 
254
271
  /** Resolves when all existing files in scriptFiles are written. */
255
272
  declare function allFilesReady(): Promise<void>;
package/dist/index.mjs CHANGED
@@ -275,6 +275,7 @@ const workerClientId = "/@crx/client-worker";
275
275
 
276
276
  const pluginBackground = () => {
277
277
  let config;
278
+ let browser;
278
279
  return [
279
280
  {
280
281
  name: "crx:background-client",
@@ -297,23 +298,37 @@ const pluginBackground = () => {
297
298
  name: "crx:background-loader-file",
298
299
  // this should happen after other plugins; the loader file is an implementation detail
299
300
  enforce: "post",
301
+ async config(config2) {
302
+ const opts = await getOptions(config2);
303
+ browser = opts.browser || "chrome";
304
+ },
300
305
  configResolved(_config) {
301
306
  config = _config;
302
307
  },
303
308
  renderCrxManifest(manifest) {
304
- const worker = manifest.background?.service_worker;
309
+ const worker = browser === "firefox" ? manifest.background?.scripts[0] : manifest.background?.service_worker;
305
310
  let loader;
306
311
  if (config.command === "serve") {
307
312
  const port = config.server.port?.toString();
308
313
  if (typeof port === "undefined")
309
314
  throw new Error("server port is undefined in watch mode");
310
- loader = `import 'http://localhost:${port}/@vite/env';
315
+ if (browser === "firefox") {
316
+ loader = `import('http://localhost:${port}/@vite/env');
317
+ `;
318
+ loader += `import('http://localhost:${port}${workerClientId}');
319
+ `;
320
+ if (worker)
321
+ loader += `import('http://localhost:${port}/${worker}');
322
+ `;
323
+ } else {
324
+ loader = `import 'http://localhost:${port}/@vite/env';
311
325
  `;
312
- loader += `import 'http://localhost:${port}${workerClientId}';
326
+ loader += `import 'http://localhost:${port}${workerClientId}';
313
327
  `;
314
- if (worker)
315
- loader += `import 'http://localhost:${port}/${worker}';
328
+ if (worker)
329
+ loader += `import 'http://localhost:${port}/${worker}';
316
330
  `;
331
+ }
317
332
  } else if (worker) {
318
333
  loader = `import './${worker}';
319
334
  `;
@@ -326,10 +341,16 @@ const pluginBackground = () => {
326
341
  fileName: getFileName({ type: "loader", id: "service-worker" }),
327
342
  source: loader
328
343
  });
329
- manifest.background = {
330
- service_worker: this.getFileName(refId),
331
- type: "module"
332
- };
344
+ if (browser !== "firefox") {
345
+ manifest.background = {
346
+ service_worker: this.getFileName(refId),
347
+ type: "module"
348
+ };
349
+ } else {
350
+ manifest.background = {
351
+ scripts: [this.getFileName(refId)]
352
+ };
353
+ }
333
354
  return manifest;
334
355
  }
335
356
  }
@@ -977,7 +998,9 @@ async function manifestFiles(manifest, options = {}) {
977
998
  ) ?? [];
978
999
  const contentScripts = manifest.content_scripts?.flatMap(({ js }) => js) ?? [];
979
1000
  const contentStyles = manifest.content_scripts?.flatMap(({ css }) => css);
980
- const serviceWorker = manifest.background?.service_worker;
1001
+ const serviceWorker = manifest.background && "service_worker" in manifest.background ? manifest.background.service_worker : void 0;
1002
+ const backgroundScripts = manifest.background && "scripts" in manifest.background ? manifest.background.scripts : void 0;
1003
+ const background = serviceWorker ? [serviceWorker].filter(isString) : backgroundScripts ? backgroundScripts.filter(isString) : [];
981
1004
  const htmlPages = htmlFiles(manifest);
982
1005
  const icons = [
983
1006
  Object.values(
@@ -1007,7 +1030,7 @@ async function manifestFiles(manifest, options = {}) {
1007
1030
  icons: [...new Set(icons)].filter(isString),
1008
1031
  locales: [...new Set(locales)].filter(isString),
1009
1032
  rulesets: [...new Set(rulesets)].filter(isString),
1010
- background: [serviceWorker].filter(isString),
1033
+ background,
1011
1034
  webAccessibleResources
1012
1035
  };
1013
1036
  }
@@ -1567,7 +1590,7 @@ const pluginManifest = () => {
1567
1590
  })
1568
1591
  );
1569
1592
  }
1570
- if (manifest2.background?.service_worker) {
1593
+ if (manifest2.background && "service_worker" in manifest2.background) {
1571
1594
  const file = manifest2.background.service_worker;
1572
1595
  const id2 = join(config.root, file);
1573
1596
  const refId2 = this.emitFile({
@@ -1601,7 +1624,7 @@ const pluginManifest = () => {
1601
1624
  );
1602
1625
  }
1603
1626
  } else {
1604
- if (manifest2.background?.service_worker) {
1627
+ if (manifest2.background && "service_worker" in manifest2.background) {
1605
1628
  const ref = manifest2.background.service_worker;
1606
1629
  const name = this.getFileName(ref);
1607
1630
  manifest2.background.service_worker = name;
@@ -1771,25 +1794,34 @@ _debug("web-acc-res");
1771
1794
  const pluginWebAccessibleResources = () => {
1772
1795
  let config;
1773
1796
  let injectCss;
1797
+ let browser;
1774
1798
  return [
1775
1799
  {
1776
1800
  name: "crx:web-accessible-resources",
1777
1801
  apply: "serve",
1778
1802
  enforce: "post",
1803
+ async config(config2) {
1804
+ const opts = await getOptions(config2);
1805
+ browser = opts.browser || "chrome";
1806
+ },
1779
1807
  renderCrxManifest(manifest) {
1780
1808
  manifest.web_accessible_resources = manifest.web_accessible_resources ?? [];
1781
1809
  manifest.web_accessible_resources = manifest.web_accessible_resources.map(({ resources, ...rest }) => ({
1782
1810
  resources: resources.filter((r) => r !== DYNAMIC_RESOURCE),
1783
1811
  ...rest
1784
1812
  })).filter(({ resources }) => resources.length);
1785
- manifest.web_accessible_resources.push({
1786
- // change the extension origin on every reload
1787
- use_dynamic_url: true,
1813
+ const war = {
1788
1814
  // all web origins can access
1789
1815
  matches: ["<all_urls>"],
1790
1816
  // all resources are web accessible
1791
- resources: ["**/*", "*"]
1792
- });
1817
+ resources: ["**/*", "*"],
1818
+ // change the extension origin on every reload
1819
+ use_dynamic_url: true
1820
+ };
1821
+ if (browser === "firefox") {
1822
+ delete war.use_dynamic_url;
1823
+ }
1824
+ manifest.web_accessible_resources.push(war);
1793
1825
  return manifest;
1794
1826
  }
1795
1827
  },
@@ -1798,7 +1830,9 @@ const pluginWebAccessibleResources = () => {
1798
1830
  apply: "build",
1799
1831
  enforce: "post",
1800
1832
  async config({ build, ...config2 }, { command }) {
1801
- const { contentScripts: contentScripts2 = {} } = await getOptions(config2);
1833
+ const opts = await getOptions(config2);
1834
+ const contentScripts2 = opts.contentScripts || {};
1835
+ browser = opts.browser || "chrome";
1802
1836
  injectCss = contentScripts2.injectCss ?? true;
1803
1837
  return { ...config2, build: { ...build, manifest: command === "build" } };
1804
1838
  },
@@ -1906,6 +1940,11 @@ const pluginWebAccessibleResources = () => {
1906
1940
  use_dynamic_url
1907
1941
  });
1908
1942
  }
1943
+ if (browser === "firefox") {
1944
+ for (const war of combinedResources) {
1945
+ delete war.use_dynamic_url;
1946
+ }
1947
+ }
1909
1948
  if (combinedResources.length === 0)
1910
1949
  delete manifest.web_accessible_resources;
1911
1950
  else
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crxjs/vite-plugin",
3
- "version": "2.0.0-beta.18",
3
+ "version": "2.0.0-beta.19",
4
4
  "description": "Build Chrome Extensions with this Vite plugin.",
5
5
  "keywords": [
6
6
  "rollup-plugin",