@crxjs/vite-plugin 2.2.1 → 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 +8 -0
- package/dist/index.mjs +44 -7
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -84,6 +84,7 @@ interface ManifestV3 {
|
|
|
84
84
|
match_about_blank?: boolean | undefined;
|
|
85
85
|
include_globs?: string[] | undefined;
|
|
86
86
|
exclude_globs?: string[] | undefined;
|
|
87
|
+
world?: chrome.scripting.ExecutionWorld | string | undefined;
|
|
87
88
|
}[] | undefined;
|
|
88
89
|
content_security_policy?: {
|
|
89
90
|
extension_pages?: string;
|
|
@@ -263,6 +264,13 @@ type FilePathFields<T extends string> = {
|
|
|
263
264
|
match_about_blank?: boolean;
|
|
264
265
|
include_globs?: string[];
|
|
265
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';
|
|
266
274
|
}[];
|
|
267
275
|
input_components?: {
|
|
268
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:
|
|
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:
|
|
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:
|
|
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 {
|