richardtallent / vite-plugin-singlefile

Vite plugin for inlining JavaScript and CSS resources
MIT License
808 stars 53 forks source link

Doesn't work with System.import (e.g. @vitejs/plugin-legacy) #77

Closed Schmale97 closed 9 months ago

Schmale97 commented 1 year ago

I have a pretty niche use case where I want all code in a single file but it also needs to support browser version outside of the defaults supported by vite. The intended solution was to use a vite config similar to this

import react from '@vitejs/plugin-react'
import legacy from '@vitejs/plugin-legacy'
import { viteSingleFile } from 'vite-plugin-singlefile'
export default {
  plugins: [
    react(),
    legacy({
      targets: ['Chrome >= 33'],
      renderModernChunks: false,
    }),
    viteSingleFile()
  ]
}

However, this fails to the legacy code getting loaded into the root index.html via System.import, the resulting index.html looks like this:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <title>Test</title>
    </head>
    <body>
        <div id="root"></div>
        <script crossorigin id="vite-legacy-polyfill">[CORRECTLY LOADED JAVASCRIPT IS INSERTED HERE]</script>
        <script crossorigin id="vite-legacy-entry" data-src="/assets/index-legacy.js">System.import(document.getElementById('vite-legacy-entry').getAttribute('data-src'))</script>
    </body>
</html>

As can be seen, the index.html is still referencing an external file in the script tag with "vite-legacy-entry" the plugin has also deleted the file that it is being referenced as expected.

The desired behaviour is that both the vite legacy plugin and vite single file plugin can be used together

richardtallent commented 1 year ago

This is way too niche for anything I'm familiar with, but if you can send a PR and tests, I can look at it after Sept. 8.

Schmale97 commented 11 months ago

Too be honest I am not 100% familiar with this either, however I will take a look and see what I can do

richardtallent commented 9 months ago

Closed as this has no reproduction and is stale.