@crxjs/vite-plugin 2.2.0 → 2.3.0

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
@@ -1,4 +1,5 @@
1
1
  import { ConfigEnv, Plugin, PluginOption } from 'vite';
2
+ import { IsStringLiteral } from 'type-fest';
2
3
  import { Options } from 'fast-glob';
3
4
  import { PluginContext, OutputBundle } from 'rollup';
4
5
 
@@ -83,6 +84,7 @@ interface ManifestV3 {
83
84
  match_about_blank?: boolean | undefined;
84
85
  include_globs?: string[] | undefined;
85
86
  exclude_globs?: string[] | undefined;
87
+ world?: chrome.scripting.ExecutionWorld | string | undefined;
86
88
  }[] | undefined;
87
89
  content_security_policy?: {
88
90
  extension_pages?: string;
@@ -206,7 +208,8 @@ interface ManifestV3 {
206
208
  type ManifestV3Fn = (env: ConfigEnv) => ManifestV3 | Promise<ManifestV3>;
207
209
  type ManifestV3Export = ManifestV3 | Promise<ManifestV3> | ManifestV3Fn;
208
210
  type Code = '.' | '/' | '\\';
209
- type ManifestFilePath<T extends string> = T extends `${Code}${string}` ? never : T extends `${string}.${infer Ext}` ? Ext extends '' ? never : T : never;
211
+ type LiteralManifestFilePath<T extends string> = T extends `${Code}${string}` ? never : T extends `${string}.${infer Ext}` ? Ext extends '' ? never : T : never;
212
+ type ManifestFilePath<T extends string> = IsStringLiteral<T> extends true ? LiteralManifestFilePath<T> : T;
210
213
  interface ManifestIcons<T extends string> {
211
214
  [size: number]: ManifestFilePath<T>;
212
215
  }
@@ -261,6 +264,13 @@ type FilePathFields<T extends string> = {
261
264
  match_about_blank?: boolean;
262
265
  include_globs?: string[];
263
266
  exclude_globs?: string[];
267
+ /**
268
+ * - 'ISOLATED' (default): Content script runs in an isolated world.
269
+ * - 'MAIN': Content script runs in the main world.
270
+ * NOTE: MAIN currently does NOT support crxjs HMR
271
+ * @see https://developer.chrome.com/docs/extensions/mv3/content_scripts/#world
272
+ */
273
+ world?: 'ISOLATED' | 'MAIN';
264
274
  }[];
265
275
  input_components?: {
266
276
  name: string;
package/dist/index.mjs CHANGED
@@ -10,11 +10,11 @@ import * as lexer from 'es-module-lexer';
10
10
  import { readFile as readFile$1 } from 'fs/promises';
11
11
  import MagicString from 'magic-string';
12
12
  import convertSourceMap from 'convert-source-map';
13
+ import pc from 'picocolors';
13
14
  import { createLogger, version } from 'vite';
14
15
  import { readFileSync, existsSync, promises } from 'fs';
15
16
  import { createRequire } from 'module';
16
17
  import fg from 'fast-glob';
17
- import pc from 'picocolors';
18
18
  import { load } from 'cheerio';
19
19
  import jsesc from 'jsesc';
20
20
 
@@ -347,8 +347,12 @@ var contentHmrPort = "function isCrxHMRPayload(x) {\n return x.type === \"custo
347
347
 
348
348
  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";
349
349
 
350
+ var contentDevMainLoader = "(function () {\n 'use strict';\n\n const injectTime = performance.now();\n (async () => {\n console.warn(__SCRIPT__, \"Content-script doesn't support HMR because the world is MAIN\");\n const { onExecute } = await import(\n /* @vite-ignore */\n __SCRIPT__\n );\n onExecute?.({ perf: { injectTime, loadTime: performance.now() - injectTime } });\n })().catch(console.error);\n\n})();\n";
351
+
350
352
  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";
351
353
 
354
+ var contentProMainLoader = "(function () {\n 'use strict';\n\n const injectTime = performance.now();\n (async () => {\n const { onExecute } = await import(\n /* @vite-ignore */\n __SCRIPT__\n );\n onExecute?.({ perf: { injectTime, loadTime: performance.now() - injectTime } });\n })().catch(console.error);\n\n})();\n";
355
+
352
356
  const contentScripts = new RxMap();
353
357
  contentScripts.change$.pipe(filter(RxMap.isChangeType.set)).subscribe(({ map, value }) => {
354
358
  const keyNames = [
@@ -381,6 +385,14 @@ function createDevLoader({
381
385
  function createProLoader({ fileName }) {
382
386
  return contentProLoader.replace(/__SCRIPT__/g, JSON.stringify(fileName));
383
387
  }
388
+ function createDevMainLoader({
389
+ fileName
390
+ }) {
391
+ return contentDevMainLoader.replace(/__SCRIPT__/g, JSON.stringify(fileName)).replace(/__TIMESTAMP__/g, JSON.stringify(Date.now()));
392
+ }
393
+ function createProMainLoader({ fileName }) {
394
+ return contentProMainLoader.replace(/__SCRIPT__/g, JSON.stringify(fileName));
395
+ }
384
396
 
385
397
  const serverEvent$ = new ReplaySubject(1);
386
398
  const close$ = serverEvent$.pipe(
@@ -593,15 +605,37 @@ async function write(fileId) {
593
605
  }
594
606
 
595
607
  const pluginContentScripts = () => {
608
+ const pluginName = "crx:content-scripts";
596
609
  let server;
597
610
  let preambleCode;
598
611
  let hmrTimeout;
599
612
  let sub = new Subscription();
613
+ const worldMainIds = /* @__PURE__ */ new Set();
614
+ const findWorldMainIds = async (config, env) => {
615
+ const { manifest: _manifest } = await getOptions(config);
616
+ const manifest = await (typeof _manifest === "function" ? _manifest(env) : _manifest);
617
+ (manifest.content_scripts || []).forEach(({ world, js }) => {
618
+ if (world === "MAIN" && js) {
619
+ js.forEach((path) => worldMainIds.add(prefix$1("/", path)));
620
+ }
621
+ });
622
+ if (worldMainIds.size) {
623
+ const name = `[${pluginName}]`;
624
+ const message = pc.yellow(
625
+ [
626
+ `${name} Some content-scripts don't support HMR because the world is MAIN:`,
627
+ ...[...worldMainIds].map((id) => ` ${id}`)
628
+ ].join("\r\n")
629
+ );
630
+ console.log(message);
631
+ }
632
+ };
600
633
  return [
601
634
  {
602
- name: "crx:content-scripts",
635
+ name: pluginName,
603
636
  apply: "serve",
604
- async config(config) {
637
+ async config(config, env) {
638
+ await findWorldMainIds(config, env);
605
639
  const { contentScripts: contentScripts2 = {} } = await getOptions(config);
606
640
  hmrTimeout = contentScripts2.hmrTimeout ?? 5e3;
607
641
  preambleCode = preambleCode ?? contentScripts2.preambleCode;
@@ -630,7 +664,9 @@ const pluginContentScripts = () => {
630
664
  const loader = add({
631
665
  type: "asset",
632
666
  id: getFileName({ type: "loader", id }),
633
- source: createDevLoader({
667
+ source: worldMainIds.has(file.id) ? createDevMainLoader({
668
+ fileName: `./${file.fileName.split("/").at(-1)}`
669
+ }) : createDevLoader({
634
670
  preamble: preamble.fileName,
635
671
  client: client.fileName,
636
672
  fileName: file.fileName
@@ -671,10 +707,11 @@ const pluginContentScripts = () => {
671
707
  }
672
708
  },
673
709
  {
674
- name: "crx:content-scripts",
710
+ name: pluginName,
675
711
  apply: "build",
676
712
  enforce: "pre",
677
- config(config) {
713
+ async config(config, env) {
714
+ await findWorldMainIds(config, env);
678
715
  return {
679
716
  ...config,
680
717
  build: {
@@ -705,7 +742,7 @@ const pluginContentScripts = () => {
705
742
  type: "loader",
706
743
  id: basename(script.id)
707
744
  }),
708
- source: createProLoader({ fileName })
745
+ source: worldMainIds.has(script.id) ? createProMainLoader({ fileName: `./${fileName.split("/").at(-1)}` }) : createProLoader({ fileName })
709
746
  });
710
747
  script.loaderName = this.getFileName(refId);
711
748
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crxjs/vite-plugin",
3
- "version": "2.2.0",
3
+ "version": "2.3.0",
4
4
  "description": "Build Chrome Extensions with this Vite plugin.",
5
5
  "keywords": [
6
6
  "rollup-plugin",
@@ -100,6 +100,7 @@
100
100
  "rollup-plugin-dts": "^4.2.0",
101
101
  "rollup-plugin-esbuild": "4.10.3",
102
102
  "svelte": "^3.48.0",
103
+ "type-fest": "^5.1.0",
103
104
  "typescript": "^4.6.4",
104
105
  "vite": "^3.2.11",
105
106
  "vite-plugin-inspect": "0.7.25",