Build an output that is usable from local disk (file:// URL)
See original GitHub issueIs your feature request related to a problem? Please describe.
Quite often i code tools which take some input (like a JSON file) and generate a HTML report out of it. This HTML report might be served by a server (e.g. Github Pages) but often just sits somewhere on the disk and should be openable/consumable from there (e.g. open file:///Users/x/projects/y/build/test/index.html
).
Describe the solution you’d like
From initial experiments (with adapter-static
) I found 2 things I would need for the above to work which I failed to accomplish with svelte-kit
so far:
- Links/Includes (from
index.html
) need to be relative instead of absolutepaths/assets||base
were quite resistant setting up anything relative
- The JS field need to be in
IIFE
format (since module script imports will raise CORS issues)vite.build: { lib: { name: "my-app", entry: "src/main.ts", formats: ["iife"] }}
producedUMD and IIFE output formats are not supported for code-splitting builds
A solution would be to make the 2 things possible. Perfect would be a simple way to achieve that. Like an specialized adapter.
Describe alternatives you’ve considered
Maybe what I’m trying to achieve is dump and using svelte-kit for such a thing doesn’t make much sense? Just stick to Svelte Classic
?
With Svelte Classic
and Vite I can accomplish the above by tweaking vite.config.js
in the following way:
- Set
base
to''
(makes the links relative) - Configure
IIEF
format:
build: {
lib: { name: "my-app", entry: "src/main.ts", formats: ["iife"] },
}
- Move the
index.html
topublic/
and add appropriate script and css includes
How important is this feature to you? This is the first thing I usually set up for the majority of apps I’m crafting.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:24
- Comments:14 (1 by maintainers)
Top GitHub Comments
After a lot of tinkering I have managed to build a script that can take an html-file generated by adapter-static and inline all javascript in the file. It is very hacky and runs code in the browser to make the imports work, but it works for my use-case which is to build simple html-files with a bit of interactivity in them that I can send in an email without hosting them anywhere. Might be useful as inspiration for someone.
The trickiest part is that the only way I could use
import
was if the the module was added as a blob to the page. But adding it as a blob generates a new url for the module, which means that allimport
statements must be rewritten after the blob has been added.https://github.com/mattiash/svelte-notebook/blob/master/util/inlinejs.cjs
Also hoping this can get added to sveltekit.
IMO This is essential for running native apps with svelte running in WebView as a GUI
Is there a workaround available?
I’d like to keep using SvelteKit for creating the full blown app with a server. But also need the SPA app to be run from file on client if no connection is available.