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

Vite removes external module from index.html #3533

Open
6 tasks done
hatchette opened this issue May 25, 2021 · 13 comments
Open
6 tasks done

Vite removes external module from index.html #3533

hatchette opened this issue May 25, 2021 · 13 comments
Labels
feat: html has workaround p2-nice-to-have Not breaking anything but nice to have (priority)

Comments

@hatchette
Copy link

hatchette commented May 25, 2021

Describe the bug

Vitejs will remove external module declared in index.html if module has relative path.

Reproduction

index.html

    <script type="module" src="/external/my-lib.mjs"></script>

vite.config.js

build: {
      rollupOptions: {
        external: "/external/my-lib.mjs",
      },

Note that in production /external/my-lib.mjs is reverse-proxy to different deployment.

Changing url to https://host/... works, but this is not what we want, because we simply don't know the hostname at build time (it's deployed to more than single host).

System Info

Output of npx envinfo --system --npmPackages vite,@vitejs/plugin-vue --binaries --browsers:

  System:
    OS: macOS 11.2.3
    CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
    Memory: 2.84 GB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.16.0 - /usr/local/bin/node
    Yarn: 1.22.10 - ~/sandbox/dashboard/node_modules/.bin/yarn
    npm: 6.14.11 - /usr/local/bin/npm
    Watchman: 4.7.0 - /usr/local/bin/watchman
  Browsers:
    Chrome: 90.0.4430.212
    Firefox: 88.0.1
    Safari: 14.0.3
  npmPackages:
    vite: ^2.1.0 => 2.1.5 

Used package manager: npm


Before submitting the issue, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the docs.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Provide a description in this issue that describes the bug.
  • Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/vue-next instead.
  • Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
@hatchette
Copy link
Author

Problem seems to be in:

const isExcludedUrl = (url: string) =>

I reckon we need a way of manually forcing externalUrl for certain relative urls.

@ygj6
Copy link
Member

ygj6 commented Jun 8, 2021

Hi @hatchette , you can put your 'my-lib.mjs' in 'public' dir. And reference the js file like this:

 <script type="module" src="/my-lib.mjs"></script>

Here is the code:

export function checkPublicFile(
url: string,
{ publicDir }: ResolvedConfig
): string | undefined {
// note if the file is in /public, the resolver would have returned it
// as-is so it's not going to be a fully resolved path.
if (!publicDir || !url.startsWith('/')) {
return
}
const publicFile = path.join(publicDir, cleanUrl(url))
if (fs.existsSync(publicFile)) {
return publicFile
} else {
return
}
}

@bluwy
Copy link
Member

bluwy commented Apr 30, 2022

Took a look at this one today. Seems to be tricky to fix. The place where we're removing the script tag is here.

shouldRemove = true

This line naively remove the script tag regardless if it resolves to something. The issue is that to determine if the file is externalized, we have to do something like await this.resolve(url).isExternal, but since this block of code operates in traverseHtml and is sync only, we can't run the async resolve to determine this.

Either we need @vue/compiler-core to support this, or we use a different strategy to ignore it.

@poyoho
Copy link
Member

poyoho commented May 10, 2022

@hatchette It seems to same with #7160, even no config config.rollupOptions.external it will call warning for this case So can I close this issues, because it is fix?

@dabuside
Copy link

dabuside commented Nov 17, 2022

@hatchette It seems to same with #7160, even no config config.rollupOptions.external it will call warning for this case So can I close this issues, because it is fix?

no.
in this example you provided before, vite build command still move external css file to javascript file, which is unacceptable. Most server only inject {cdn} varibale into index.html file.
We expected external javascript(css) link should be keep in html file.
As described in the issue title, Vite should not remove external module from index.html

expected output

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width,initial-scale=1.0" />
    <link rel="icon" href="{{cdnHost}}/cdn/images/favicon.ico" />

----- this external css link should be here, not js file
  <link
      rel="stylesheet"
      href="{{cdnHost}}/cdn/printjs/print.min.css"
      type="text/css"
    />
    
    <title></title>
    <script type="module" crossorigin src="/assets/index.3692b83b.js"></script>
    <link rel="stylesheet" href="/assets/index.06d14ce2.css">
  </head>

  <body>
    index.html
    <div id="app"></div>

    <script src="{{cdnHost}}/cdn/fingerprint2/2.1.0/fingerprint2.js"></script>
    <script src="{{cdnHost}}/cdn/socket.io/2.2.0/socket.io.js"></script>
    <script src="{{cdnHost}}/cdn/printjs/print.min.js"></script>
    
  </body>
</html>

@bluwy bluwy added feat: html has workaround p2-nice-to-have Not breaking anything but nice to have (priority) and removed p2-edge-case Bug, but has workaround or limited in scope (priority) labels Dec 29, 2022
@alo-id
Copy link

alo-id commented Jun 21, 2023

Is there any updates on this issue?

@sgerke-1L
Copy link

This feature would be much appreciated!

@b2whats
Copy link

b2whats commented May 4, 2024

One more case

// index.html
<script type="module">
  import 'app'
</script>
// vite.config.js
...
rollupOptions: {
  external: [/app/],
},
...

But, if i add code with side-effect in script

// index.html
<script type="module">
  import 'app';
  console.log()
</script>

script doesn't be delete

@lukemovement
Copy link

lukemovement commented May 25, 2024

These are also be removed from the <head> of my index.html file.

<link rel="preconnect" href="https://fonts.gstatic.com/" crossorigin> <link rel="dns-prefetch" href="//fonts.gstatic.com">

@Jayleaf
Copy link

Jayleaf commented Aug 20, 2024

...so what's the workaround? nothing i try works, and I can't build my project whatsoever because of this.

@leonheess
Copy link

leonheess commented Aug 20, 2024

@Jayleaf the workaround tag was added erroneously by @bluwy, there is no workaround. We are using unix sed to cut all public references out of the index.html before the build and then add them back afterward in our build pipeline. It's ridiculous but we tried raising a PR (#11854), we are gold sponsors of both Vue and Vite and offered to pay any amount to get this bug fixed. No one cares.

@Jayleaf
Copy link

Jayleaf commented Aug 20, 2024

@Jayleaf the workaround tag was added erroneously by @bluwy, there is no workaround. We are using unix sed to cut all public references out of the index.html before build and then add them back afterwards in our build pipeline. Its ridiculous but we tried raising a PR (#11854), we are gold sponsors of both Vue and Vite and offered to pay any amount to get this bug fixed. No one cares.

@leonheess yeah that's pretty ridiculous. Thank you for linking the PR. This is definitely more than "nice to have" lol.

@mobsense
Copy link

mobsense commented Sep 5, 2024

The workaround for us is:

Yea, pretty ugly.

    let link = document.createElement('link')
    link.rel = 'stylesheet'
    link.type = 'text/css'
    link.href =  '/file.css'
    document.head.appendChild(link)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: html has workaround p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

No branches or pull requests