Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable dynamic import detection for one line or for whole project #16241

Closed
4 tasks done
b5414 opened this issue Mar 22, 2024 · 5 comments · Fixed by #16556
Closed
4 tasks done

Disable dynamic import detection for one line or for whole project #16241

b5414 opened this issue Mar 22, 2024 · 5 comments · Fixed by #16556

Comments

@b5414
Copy link

b5414 commented Mar 22, 2024

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:

  1. npm run build
  2. open background.js
  3. find __vitePreload (know it by error when FUNCNAME is running once), it's: const contentMain = await __vitePreload(() => import(file), true ? __vite__mapDeps([]) : void 0);
  4. and replace it with just: const contentMain = await import(file);
  5. 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

@b5414
Copy link
Author

b5414 commented Apr 19, 2024

bump

@b5414
Copy link
Author

b5414 commented May 13, 2024

I still have problem with __vite__injectQuery

ReferenceError: __vite__injectQuery is not defined

@b5414
Copy link
Author

b5414 commented May 13, 2024

But after build everything works great, thank you guys ❤

@bluwy
Copy link
Member

bluwy commented May 13, 2024

Hmm we should be injecting the __vite__injectQuery whenever it's used here:

if (needQueryInjectHelper) {
if (isClassicWorker) {
str().append('\n' + __vite__injectQuery.toString())
} else {
str().prepend(
`import { injectQuery as __vite__injectQuery } from "${clientPublicPath}";`,
)
}
}

Can you open a new issue if you can share a repro of it?

@b5414
Copy link
Author

b5414 commented May 13, 2024

@bluwy hi, all the necessary code was written above

The FUNCNAME is a function to inject by chrome api, other words it's like JSON.stringify(FUNCNAME) => JSON.parse(FUNCNAME_str) by api, and when api injects it, the func is always independent/alone (without vite)


The problem gone in the builded version, but still appears on development mode

@github-actions github-actions bot locked and limited conversation to collaborators May 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants