-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Description
Description
Hi, I'm using vite to build mine chrome extension with reactjs, it's not a big deal, but I have problem with dynamic import;
Due to chrome manifest v3 security and stuff when I'm injecting function by
chrome.scripting.executeScript({injectImmediately: true, world: 'ISOLATED', target: {tabId}, func: FUNCNAME});
(async()=>{
const FUNCNAME = async()=>{
const file = chrome.runtime.getURL('./filewow.js');
const contentMain = await import(file);
console.log('[FUNCNAME] check', contentMain);
};
await chrome.scripting.executeScript({injectImmediately: true, world: 'ISOLATED', target: {tabId}, func: FUNCNAME});
})();And the function FUNCNAME is placed right above in background module (service worker), which vite should handle, but FUNCNAME should be independent due to:
A JavaScript function to inject. This function will be serialized, and then deserialized for injection. This means that** any bound parameters and execution context will be lost**. Exactly one of files or func must be specified.
from: https://developer.chrome.com/docs/extensions/reference/api/scripting#type-ScriptInjection
Thus when vite compile the ready files, it uses __vitePreload on build, and __vite__injectQuery on dev run; Which func will be lost because of text above
And if, for example I do:
npm run build- open background.js
- find
__vitePreload(know it by error whenFUNCNAMEis running once), it's:const contentMain = await __vitePreload(() => import(file), true ? __vite__mapDeps([]) : void 0); - and replace it with just:
const contentMain = await import(file); - refresh extension..
then, it going as it should, without any problem..
so, How can I prevent this compilation into vite functions?
Suggested solution
/* @vite-ignore */ should prevent this
Alternative
or /* @vite-prevent-next-line */
Additional context
this problem (and the 3rd step) available only(?) on vite v5.1.6 and not on vite v5.2.2
/* @vite-ignore */ is just replacing:
this:
const contentMain = await __vitePreload(() => import(file), true ? __vite__mapDeps([]) : void 0);to:
const contentMain = await __vitePreload(() => import(
/* @vite-ignore */
file
), true ? __vite__mapDeps([]) : void 0);Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that request the same feature to avoid creating a duplicate.