|
1 | | -import * as vite from 'vite'; |
2 | | -import type { ESBuildOptions, ResolvedConfig, Plugin } from 'vite'; |
| 1 | +import type { ResolvedConfig, Plugin } from 'vite'; |
3 | 2 | import MagicString from 'magic-string'; |
4 | 3 | import { preprocess } from 'svelte/compiler'; |
5 | | -import { Preprocessor, PreprocessorGroup, Processed, ResolvedOptions } from './options'; |
| 4 | +import { PreprocessorGroup, Processed, ResolvedOptions } from './options'; |
6 | 5 | import { log } from './log'; |
7 | 6 | import { buildSourceMap } from './sourcemap'; |
8 | 7 | import path from 'path'; |
9 | | - |
10 | | -const supportedStyleLangs = ['css', 'less', 'sass', 'scss', 'styl', 'stylus', 'postcss']; |
11 | | - |
12 | | -const supportedScriptLangs = ['ts']; |
13 | | - |
14 | | -function createViteScriptPreprocessor(): Preprocessor { |
15 | | - return async ({ attributes, content, filename = '' }) => { |
16 | | - const lang = attributes.lang as string; |
17 | | - if (!supportedScriptLangs.includes(lang)) return; |
18 | | - const transformResult = await vite.transformWithEsbuild(content, filename, { |
19 | | - loader: lang as ESBuildOptions['loader'], |
20 | | - target: 'esnext', |
21 | | - tsconfigRaw: { |
22 | | - compilerOptions: { |
23 | | - // svelte typescript needs this flag to work with type imports |
24 | | - importsNotUsedAsValues: 'preserve', |
25 | | - preserveValueImports: true |
26 | | - } |
27 | | - } |
28 | | - }); |
29 | | - return { |
30 | | - code: transformResult.code, |
31 | | - map: transformResult.map |
32 | | - }; |
33 | | - }; |
34 | | -} |
35 | | - |
36 | | -function createViteStylePreprocessor(config: ResolvedConfig): Preprocessor { |
37 | | - const transform = getCssTransformFn(config); |
38 | | - return async ({ attributes, content, filename = '' }) => { |
39 | | - const lang = attributes.lang as string; |
40 | | - if (!supportedStyleLangs.includes(lang)) return; |
41 | | - const moduleId = `${filename}.${lang}`; |
42 | | - const result = await transform(content, moduleId); |
43 | | - // patch sourcemap source to point back to original filename |
44 | | - if (result.map?.sources?.[0] === moduleId) { |
45 | | - result.map.sources[0] = path.basename(filename); |
46 | | - } |
47 | | - return { |
48 | | - code: result.code, |
49 | | - map: result.map ?? undefined |
50 | | - }; |
51 | | - }; |
52 | | -} |
53 | | - |
54 | | -// eslint-disable-next-line no-unused-vars |
55 | | -type CssTransform = (code: string, filename: string) => Promise<{ code: string; map?: any }>; |
56 | | - |
57 | | -function getCssTransformFn(config: ResolvedConfig): CssTransform { |
58 | | - // API is only available in Vite 3.2 and above |
59 | | - // TODO: Remove Vite plugin hack when bump peer dep to Vite 3.2 |
60 | | - if (vite.preprocessCSS) { |
61 | | - return async (code, filename) => { |
62 | | - return vite.preprocessCSS(code, filename, config); |
63 | | - }; |
64 | | - } else { |
65 | | - const pluginName = 'vite:css'; |
66 | | - const plugin = config.plugins.find((p) => p.name === pluginName); |
67 | | - if (!plugin) { |
68 | | - throw new Error(`failed to find plugin ${pluginName}`); |
69 | | - } |
70 | | - if (!plugin.transform) { |
71 | | - throw new Error(`plugin ${pluginName} has no transform`); |
72 | | - } |
73 | | - // @ts-expect-error |
74 | | - return plugin.transform.bind(null); |
75 | | - } |
76 | | -} |
| 8 | +import { vitePreprocess } from '../preprocess'; |
77 | 9 |
|
78 | 10 | function createVitePreprocessorGroup(config: ResolvedConfig): PreprocessorGroup { |
79 | 11 | return { |
80 | 12 | markup({ content, filename }) { |
81 | | - return preprocess( |
82 | | - content, |
83 | | - { |
84 | | - script: createViteScriptPreprocessor(), |
85 | | - style: createViteStylePreprocessor(config) |
86 | | - }, |
87 | | - { filename } |
88 | | - ); |
| 13 | + return preprocess(content, vitePreprocess({ style: config }), { filename }); |
89 | 14 | } |
90 | | - } as PreprocessorGroup; |
| 15 | + }; |
91 | 16 | } |
92 | 17 |
|
93 | 18 | /** |
@@ -117,7 +42,9 @@ function buildExtraPreprocessors(options: ResolvedOptions, config: ResolvedConfi |
117 | 42 | const appendPreprocessors: PreprocessorGroup[] = []; |
118 | 43 |
|
119 | 44 | if (options.experimental?.useVitePreprocess) { |
120 | | - log.debug('adding vite preprocessor'); |
| 45 | + log.warn( |
| 46 | + '`experimental.useVitePreprocess` is deprecated. Use the `vitePreprocess()` preprocessor instead. See https://github.com/sveltejs/vite-plugin-svelte/blob/main/docs/preprocess.md for more information.' |
| 47 | + ); |
121 | 48 | prependPreprocessors.push(createVitePreprocessorGroup(config)); |
122 | 49 | } |
123 | 50 |
|
|
0 commit comments