Skip to content

Commit

Permalink
chore: remove pre vite 3.2 support for preprocess (#536)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Dec 9, 2022
1 parent 5e90219 commit 737b4fa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/sour-ducks-float.md
@@ -0,0 +1,5 @@
---
'@sveltejs/vite-plugin-svelte': patch
---

Remove pre Vite 3.2 support for `vitePreprocess`
37 changes: 11 additions & 26 deletions packages/vite-plugin-svelte/src/preprocess.ts
@@ -1,6 +1,6 @@
import path from 'path';
import * as vite from 'vite';
import type { ESBuildOptions, ResolvedConfig } from 'vite';
import { preprocessCSS, resolveConfig, transformWithEsbuild } from 'vite';
import type { ESBuildOptions, InlineConfig, ResolvedConfig } from 'vite';
// eslint-disable-next-line node/no-missing-import
import type { Preprocessor, PreprocessorGroup } from 'svelte/types/compiler/preprocess';

Expand All @@ -9,7 +9,7 @@ const supportedScriptLangs = ['ts'];

export function vitePreprocess(opts?: {
script?: boolean;
style?: boolean | vite.InlineConfig | vite.ResolvedConfig;
style?: boolean | InlineConfig | ResolvedConfig;
}) {
const preprocessor: PreprocessorGroup = {};
if (opts?.script !== false) {
Expand All @@ -27,7 +27,7 @@ function viteScript(): { script: Preprocessor } {
async script({ attributes, content, filename = '' }) {
const lang = attributes.lang as string;
if (!supportedScriptLangs.includes(lang)) return;
const transformResult = await vite.transformWithEsbuild(content, filename, {
const transformResult = await transformWithEsbuild(content, filename, {
loader: lang as ESBuildOptions['loader'],
target: 'esnext',
tsconfigRaw: {
Expand All @@ -46,23 +46,23 @@ function viteScript(): { script: Preprocessor } {
};
}

function viteStyle(config: vite.InlineConfig | vite.ResolvedConfig = {}): {
function viteStyle(config: InlineConfig | ResolvedConfig = {}): {
style: Preprocessor;
} {
let transform: CssTransform;
const style: Preprocessor = async ({ attributes, content, filename = '' }) => {
const lang = attributes.lang as string;
if (!supportedStyleLangs.includes(lang)) return;
if (!transform) {
let resolvedConfig: vite.ResolvedConfig;
let resolvedConfig: ResolvedConfig;
// @ts-expect-error special prop added if running in v-p-s
if (style.__resolvedConfig) {
// @ts-expect-error
resolvedConfig = style.__resolvedConfig;
} else if (isResolvedConfig(config)) {
resolvedConfig = config;
} else {
resolvedConfig = await vite.resolveConfig(
resolvedConfig = await resolveConfig(
config,
process.env.NODE_ENV === 'production' ? 'build' : 'serve'
);
Expand All @@ -89,26 +89,11 @@ function viteStyle(config: vite.InlineConfig | vite.ResolvedConfig = {}): {
type CssTransform = (code: string, filename: string) => Promise<{ code: string; map?: any }>;

function getCssTransformFn(config: ResolvedConfig): CssTransform {
// API is only available in Vite 3.2 and above
// TODO: Remove Vite plugin hack when bump peer dep to Vite 3.2
if (vite.preprocessCSS) {
return async (code, filename) => {
return vite.preprocessCSS(code, filename, config);
};
} else {
const pluginName = 'vite:css';
const plugin = config.plugins.find((p) => p.name === pluginName);
if (!plugin) {
throw new Error(`failed to find plugin ${pluginName}`);
}
if (!plugin.transform) {
throw new Error(`plugin ${pluginName} has no transform`);
}
// @ts-expect-error
return plugin.transform.bind(null);
}
return async (code, filename) => {
return preprocessCSS(code, filename, config);
};
}

function isResolvedConfig(config: any): config is vite.ResolvedConfig {
function isResolvedConfig(config: any): config is ResolvedConfig {
return !!config.inlineConfig;
}

0 comments on commit 737b4fa

Please sign in to comment.