question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow vite plugins to be run before compilation

See original GitHub issue

Describe the problem

So from what I understand, preprocessors (for svelte kit) are basically vite plugins running before vite-plugin-svelte. They also have an easier access to the content of scripts and style tags, but let’s consider the case where only the markup preprocessor is used.

Now, for the sake of portability, I’m using unplugin to create plugins for vite and other similar frameworks.

Unfortunately, these plugins run after the svelte to js transpilation. This means it is not easy to modify .svelte files, because it’s the resulting js that will be modified.

So, I’d love a way to run these plugins before transpilation.

Describe the proposed solution

Add a beforeKitPlugins options:

// svelte.config.js
/** @type {import('@sveltejs/kit').Config} */
const config = {
	// Consult https://github.com/sveltejs/svelte-preprocess
	// for more information about preprocessors
	preprocess: [
		preprocess(),
		// MyPlugin.transform // This is doable, but prevents using more vite features.
	],

	kit: {
		adapter: adapter(),
		vite: {
			plugins: [
				// MyPlugin(), // I would like this one to run **before** transpilation
			],
		}
	},

	// Best option:
	pluginsBeforeKit: [
		MyPlugin()
        ]
};

export default config;

Alternatives considered

Create a preprocesssor. But that defeats the purpose of unplugin.

Importance

nice to have

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ecstremacommented, Mar 9, 2022

You are right. As long as the sveltekit plugin itself doesn’t use enforce, using enforce is fine.

I’ll mark this as resolved, with the solution being to

  1. Use enforce: "pre"
  2. Tell users of rollup and esbuild to place the plugin before any svelte transpilation is done.
0reactions
bluwycommented, Mar 9, 2022

enforce: "pre" does not work with rollup and esbuild. Which means it may work at dev time, but not at dev time, and will prevent sapper from being supported.

It doesn’t matter. For rollup and esbuild, there’s no sense of enforce since plugins are ordered the way they are defined. Not having support for them doesn’t mean is should be avoided. You can still use it in rollup by placing your unplugin before rollup-plugin-svelte.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Plugin API - Vite
It allows you to inspect the intermediate state of Vite plugins. ... User plugins are resolved before running this hook so injecting other...
Read more >
Dependency Pre-Bundling - Vite
Vite caches the pre-bundled dependencies in node_modules/.vite . It determines whether it needs to re-run the pre-bundling step based on a few sources:...
Read more >
Building for Production - Vite
When it is time to deploy your app for production, simply run the vite build command. By default, it uses <root>/index.html as the...
Read more >
Build Options - Vite
This option allows users to set a different browser target for CSS minification from the one used for JavaScript transpilation. It should only...
Read more >
Configuring Vite
When running vite from the command line, Vite will automatically try to resolve a config file named vite.config.js inside project root.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found