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

Multiple content pages not support #177

Closed
rosendolu opened this issue Aug 28, 2023 · 7 comments
Closed

Multiple content pages not support #177

rosendolu opened this issue Aug 28, 2023 · 7 comments
Assignees
Labels
bug Something isn't working

Comments

@rosendolu
Copy link

Describe the bug

To Reproduce
Steps to reproduce the behavior:

  1. Add another content pages
  2. npm run build
  3. Load the new extension ,and you will see the errors

Expected behavior
Works normally

Screenshots
image

Desktop (please complete the following information):

  • OS: [e.g. Mac, Window, Linux] Mac
  • Browser [e.g. chrome, firefox] hrome
  • Node Version [e.g. 18.12.0] v18.17.0

Additional context
When only one content pages, after build ,it works well ,and without any import script
image

But when multiple content pages added ,the __vitePreload script came to be imported using import,and browser not allowed that.I think it might be the rollups default behavior,but did`t found a way out
image

@rosendolu rosendolu added the bug Something isn't working label Aug 28, 2023
@github-actions
Copy link

Thank you for your contribution. We will check and reply to you as soon as possible.

@rosendolu rosendolu changed the title Multiple content pages build dont support Multiple content pages not support Aug 28, 2023
@Jonghakseo
Copy link
Owner

The current manifest configuration can only support one content script, but let's make it better! You can modify your own build settings and manifest settings to load more than one content script.

I'll try to make it easier to do this in Boilerplate, but I don't think it's a high priority.

@rosendolu
Copy link
Author

rosendolu commented Oct 29, 2023

The current manifest configuration can only support one content script, but let's make it better! You can modify your own build settings and manifest settings to load more than one content script.

I'll try to make it easier to do this in Boilerplate, but I don't think it's a high priority.

yeah,after several attempts, I solved this problem using acustom plugin.
In short, i extract the __vitePreload module code and inject into root content pages

// utils/plugins/inline-vite-preload.ts
export default function inlineVitePreloadScript() {
  let __vitePreload = '';
  return {
    name: 'replace-vite-preload-script-plugin',
    async renderChunk(code, chunk, options, meta) {
      if (!/content/.test(chunk.fileName)) {
        return null;
      }
      if (!__vitePreload) {
        const chunkName = Object.keys(meta.chunks).find(key => /preload/.test(key));
        const modules = meta.chunks[chunkName].modules;
        __vitePreload = modules[Object.keys(modules)[0]].code;
      }
      return {
        code: __vitePreload + code.split(`\n`).slice(1).join(`\n`),
      };
    },
  };
}
// vite.config.ts
import inlineVitePreloadScript from './utils/plugins/inline-vite-preload';
...,
plugins:[
    ...
    inlineVitePreloadScript()
]

@nggonzalez
Copy link

nggonzalez commented Nov 22, 2023

@Jonghakseo appreciate all the work on the boilerplate, it's been really helpful to use. FWIW, I also need to support multiple content scripts as the extension I'm building has different functionality by domain and I'm trying to keep the permissions as minimal as possible to start and the code modular.

Adding a heads up/pointer to this issue in the README may be helpful a short-term solution.

@rosendolu Thanks for posting the fix!

@ghj7211
Copy link

ghj7211 commented Jan 13, 2024

@rosendolu this still works like a charm. Thanks!

@Jonghakseo
Copy link
Owner

@all-contributors please add @rosendolu for code

Copy link
Contributor

@Jonghakseo

I've put up a pull request to add @rosendolu! 🎉

aspiers added a commit to rolod0x/rolod0x that referenced this issue Jan 28, 2024
Ported from:

Jonghakseo/chrome-extension-boilerplate-react-vite@e56fa10

as per:

Jonghakseo/chrome-extension-boilerplate-react-vite#177 (comment)

We don't need this right now, but it's handy to keep in the back
pocket, and also minimise divergence from
Jonghakseo/chrome-extension-boilerplate-react-vite.
aspiers added a commit to rolod0x/rolod0x that referenced this issue Jan 28, 2024
Ported from:

Jonghakseo/chrome-extension-boilerplate-react-vite@e56fa10

as per:

Jonghakseo/chrome-extension-boilerplate-react-vite#177 (comment)

We don't need this right now, but it's handy to keep in the back
pocket, and also minimise divergence from
Jonghakseo/chrome-extension-boilerplate-react-vite.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants