Skip to content

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

@b5414

Description

@b5414

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions