vite-plugin-singlefile icon indicating copy to clipboard operation
vite-plugin-singlefile copied to clipboard

How to make it work with multiple inputs?

Open WildH0g opened this issue 2 years ago • 1 comments

I have the issue that the plugin fails when I try to bundle multiple files. The error it throws is:

Invalid value for option "output.inlineDynamicImports" - multiple inputs are not supported when "output.inlineDynamicImports" is true.

This is my vite.config.js file:

import { defineConfig } from 'vite';
import { viteSingleFile } from 'vite-plugin-singlefile';
import { resolve } from 'path';

export default defineConfig({
  plugins: [viteSingleFile()],
  build: {
    emptyOutDir: false,
    rollupOptions: {
      input: {
        main: resolve(__dirname, 'index.html'),
        other: resolve(__dirname, 'other.html'),
      },
      output: {
        inlineDynamicImports: false,
      },
    },
  },
});

It only works if I either disable the plugin or only keep one input. The output.inlineDynamicImports setting doesn't do the trick, I still get the same error.

Thanks for your help.

WildH0g avatar Aug 11 '22 13:08 WildH0g

Good question!

I don't use multiple input files for my use cases, but if you can create a standalone repository that demonstrates this limitation, I may be able to take a look at it and see if there's a solution. If practical, please start with this example build, since it's a minimal example and one I already test against:

https://github.com/richardtallent/vite-plugin-singlefile-example

richardtallent avatar Aug 13 '22 01:08 richardtallent

You need to disable useRecommendedBuildConfig.

kidonng avatar Sep 17 '22 08:09 kidonng

You need to disable useRecommendedBuildConfig.

This config does make build successful, however the output did not merge to single file as expected.

kamontat avatar Sep 22 '22 08:09 kamontat

Same problem here. Standalone repository here: https://github.com/fell-lucas/chrome-ext-template-preact-windi-vite As multi output files are recommended, I want every output file include all source code from dependence -- in other words: no import in output file at all.

delai avatar Sep 28 '22 09:09 delai

Is there any way to pacific one output file include all dependence source code, and meanwhile, another file use ES import as normal

delai avatar Sep 28 '22 09:09 delai

Hi @delai,

This plugin was written with one purpose in mind: to use vite to be used to create an entire web page/site as a single file.

I've played around with the repo you posted, and immediately ran into the key issue: rollup does not support inlineDynamicImports=true when there are multiple input files:

https://rollupjs.org/guide/en/#outputinlinedynamicimports

This plugin depends on that option. Otherwise, you get inlined local JS, but the common JS chunks are not inlined, they are dynamically imported.

While you could probably write a plugin to do what you want, this one won't be reasonably capable of doing it.

If you can convince the rollup team to support this option with multiple input files, we can revisit the smaller changes in this plugin that would probably be needed to properly support multiple input files.

richardtallent avatar Oct 01 '22 22:10 richardtallent

ok, thank you for your replay!

delai avatar Oct 28 '22 08:10 delai

You need to disable useRecommendedBuildConfig.

This helped me today, thank you!

I would like to add another tip: set Vite's build.modulePreload to { polyfill: false }. Otherwise, it would add extra import statemtnt that breaks single file bundles from running. When everything is inlined in the html file, the preloading is meaningless anyway.

L1Q avatar Oct 21 '23 00:10 L1Q