Skip to content

Support Zero JS™ output in build mode. #3127

@DylanPiercey

Description

@DylanPiercey

Clear and concise description of the problem

This is a bit niche, but ideally when the final bundle would contain no user JavaScript it should omit the script tags in the output.

For a SPA this is never going to happen, but for MPA's it is actually somewhat common to have no JavaScript required for a page. For example, imagine you have a dead simple 404 page without any interactivity besides links. Likely in that case you'll still have styles that need to be output, but no scripts.

Suggested solution

From what I can tell we'd need to make this script only be added conditionally.

To do this we'd need to be able to tell if the output content is empty of user content.

There are two problems with that though:

  1. Inlined css is still be included in the JS output until it gets minified.
  2. The vite/dynamic-import-polyfill is unconditionally loaded.

Roughly I think we'd need update the code to first check if any additional imports we're loaded by the chunk. If not then we'd check if any of the modules are not "empty" (this is where the inline css variables is a problem). Then finally if all modules are empty (perhaps special casing vite/dynamic-import-polyfill) we should be able to safely omit adding this script.

Alternative

If left alone the downside is that the browser unnecessarily downloads a tiny JavaScript file.

Additional context

This is something we'd like to see for our MPA SSR setup using the marko/vite plugin, since it's something all of our other bundler plugins enable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestp2-nice-to-haveNot breaking anything but nice to have (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions