Skip to content

🐛 BUG: using parse and serialize in a vite plugin at the beginning produces unexpected behavior. #1103

@codelastnight

Description

@codelastnight

What version of @astrojs/compiler are you using?

2.12.2

What package manager are you using?

npm

What operating system are you using?

mac

Describe the Bug

I'm following the example from understanding astro web book, which uses the parse and serialize utilities in a vite plugin loaded first in order to use it on an .astro page before it is transformed.

however, using <></> will fail, and <!doctype html> will return the string html instead of being ignored.

//vite-plugin.ts

export const vitePlugin = (): Plugin => {
  return {
    name: 'vite-plugin',
    async transform(code, id) {
      if (!id.endsWith('.astro')) return;

      const { ast } = await parse(code);

      // // serialize the AST and return the result
      const result = serialize(ast);
      console.log(result);
      return result;
    },
  };
};

using empty fragments anywhere:

<!-- index.astro -->
<Layout>
	<Welcome />

	<></>

</Layout>

results in the following error, which is different from expected behavior based on running astro regularly.

Unable to assign attributes when using <> Fragment shorthand syntax!

How I'm adding the plugin at the front of the array:

export const addPluginAtFrontOfArray = (): Plugin => {
  return {
    name: 'vite-plugin-inject-at-beginning',
    configResolved: (options) => {
      (options.plugins as Plugin[]).unshift(vitePlugin());
    },
  };
};

this might be expected behavior, but I thought it might be worth a mention.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/withastro-astro-ppxdgn2r?file=src%2Fpages%2Findex.astro

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageIssue needs to be triaged

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions