Skip to content

Commit e7a13b4

Browse files
Svelte post-processor
1 parent c84acf8 commit e7a13b4

File tree

1 file changed

+1
-123
lines changed

1 file changed

+1
-123
lines changed

packages/@tailwindcss-vite/src/index.ts

Lines changed: 1 addition & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,6 @@ export default function tailwindcss(): Plugin[] {
7979
for (let [id, root] of roots.entries()) {
8080
let module = server.moduleGraph.getModuleById(id)
8181
if (!module) {
82-
// The module for this root might not exist yet
83-
if (root.builtBeforeTransform) {
84-
continue
85-
}
86-
8782
// Note: Removing this during SSR is not safe and will produce
8883
// inconsistent results based on the timing of the removal and
8984
// the order / timing of transforms.
@@ -184,7 +179,6 @@ export default function tailwindcss(): Plugin[] {
184179
}
185180

186181
return [
187-
svelteProcessor(roots),
188182
{
189183
// Step 1: Scan source files for candidates
190184
name: '@tailwindcss/vite:scan',
@@ -225,19 +219,6 @@ export default function tailwindcss(): Plugin[] {
225219

226220
let root = roots.get(id)
227221

228-
// If the root was built outside of the transform hook (e.g. in the
229-
// Svelte preprocessor), we still want to mark all dependencies of the
230-
// root as watched files.
231-
if (root.builtBeforeTransform) {
232-
root.builtBeforeTransform.forEach((file) => this.addWatchFile(file))
233-
root.builtBeforeTransform = undefined
234-
}
235-
236-
// We only process Svelte `<style>` tags in the `sveltePreprocessor`
237-
if (isSvelteStyle(id)) {
238-
return src
239-
}
240-
241222
if (!options?.ssr) {
242223
// Wait until all other files have been processed, so we can extract
243224
// all candidates before generating CSS. This must not be called
@@ -272,19 +253,6 @@ export default function tailwindcss(): Plugin[] {
272253

273254
let root = roots.get(id)
274255

275-
// If the root was built outside of the transform hook (e.g. in the
276-
// Svelte preprocessor), we still want to mark all dependencies of the
277-
// root as watched files.
278-
if (root.builtBeforeTransform) {
279-
root.builtBeforeTransform.forEach((file) => this.addWatchFile(file))
280-
root.builtBeforeTransform = undefined
281-
}
282-
283-
// We only process Svelte `<style>` tags in the `sveltePreprocessor`
284-
if (isSvelteStyle(id)) {
285-
return src
286-
}
287-
288256
// We do a first pass to generate valid CSS for the downstream plugins.
289257
// However, since not all candidates are guaranteed to be extracted by
290258
// this time, we have to re-run a transform for the root later.
@@ -304,9 +272,6 @@ export default function tailwindcss(): Plugin[] {
304272
I.start('[@tailwindcss/vite] (render start)')
305273

306274
for (let [id, root] of roots.entries()) {
307-
// Do not do a second render pass on Svelte `<style>` tags.
308-
if (isSvelteStyle(id)) continue
309-
310275
let generated = await regenerateOptimizedCss(
311276
root,
312277
// During the renderStart phase, we can not add watch files since
@@ -344,20 +309,13 @@ function isPotentialCssRootFile(id: string) {
344309
(extension === 'css' ||
345310
(extension === 'vue' && id.includes('&lang.css')) ||
346311
(extension === 'astro' && id.includes('&lang.css')) ||
347-
// We want to process Svelte `<style>` tags to properly add dependency
348-
// tracking for imported files.
349-
isSvelteStyle(id)) &&
312+
(extension === 'svelte' && id.includes('&lang.css')))
350313
// Don't intercept special static asset resources
351314
!SPECIAL_QUERY_RE.test(id)
352315

353316
return isCssFile
354317
}
355318

356-
function isSvelteStyle(id: string) {
357-
let extension = getExtension(id)
358-
return extension === 'svelte' && id.includes('&lang.css')
359-
}
360-
361319
function optimizeCss(
362320
input: string,
363321
{ file = 'input.css', minify = false }: { file?: string; minify?: boolean } = {},
@@ -422,14 +380,6 @@ class Root {
422380
// `renderStart` hook.
423381
public lastContent: string = ''
424382

425-
// When set, indicates that the root was built before the Vite transform hook
426-
// was being called. This can happen in scenarios like when preprocessing
427-
// `<style>` tags for Svelte components.
428-
//
429-
// It can be set to a list of dependencies that will be added whenever the
430-
// next `transform` hook is being called.
431-
public builtBeforeTransform: string[] | undefined
432-
433383
// The lazily-initialized Tailwind compiler components. These are persisted
434384
// throughout rebuilds but will be re-initialized if the rebuild strategy is
435385
// set to `full`.
@@ -623,75 +573,3 @@ class Root {
623573
return shared
624574
}
625575
}
626-
627-
// Register a plugin that can hook into the Svelte preprocessor if Svelte is
628-
// configured. This allows us to transform CSS in `<style>` tags and create a
629-
// stricter version of CSS that passes the Svelte compiler.
630-
//
631-
// Note that these files will not undergo a second pass through the vite
632-
// transpiler later. This means that `@tailwind utilities;` will not be up to
633-
// date.
634-
//
635-
// In practice, it is discouraged to use `@tailwind utilities;` inside Svelte
636-
// components, as the styles it create would be scoped anyways. Use an external
637-
// `.css` file instead.
638-
function svelteProcessor(roots: DefaultMap<string, Root>): Plugin {
639-
return {
640-
name: '@tailwindcss/svelte',
641-
api: {
642-
sveltePreprocess: {
643-
async style({
644-
content,
645-
filename,
646-
markup,
647-
}: {
648-
content: string
649-
filename?: string
650-
markup: string
651-
}) {
652-
if (!filename) return
653-
using I = new Instrumentation()
654-
DEBUG && I.start('[@tailwindcss/vite] Preprocess svelte')
655-
656-
// Create the ID used by Vite to identify the `<style>` contents. This
657-
// way, the Vite `transform` hook can find the right root and thus
658-
// track the right dependencies.
659-
let id = filename + '?svelte&type=style&lang.css'
660-
661-
let root = roots.get(id)
662-
663-
// Since a Svelte pre-processor call means that the CSS has changed,
664-
// we need to trigger a rebuild.
665-
root.requiresRebuild = true
666-
667-
// Mark this root as being built before the Vite transform hook is
668-
// called. We capture all eventually added dependencies so that we can
669-
// connect them to the vite module graph later, when the transform
670-
// hook is called.
671-
root.builtBeforeTransform = []
672-
673-
// We only want to consider candidates from the current template file,
674-
// this ensures that no one can depend on this having the full candidate
675-
// list in some builds (as this is undefined behavior).
676-
let scanner = new Scanner({})
677-
root.overwriteCandidates = scanner.scanFiles([
678-
{ content: markup, file: filename, extension: 'svelte' },
679-
])
680-
681-
let generated = await root.generate(
682-
content,
683-
(file) => root.builtBeforeTransform?.push(file),
684-
I,
685-
)
686-
687-
if (!generated) {
688-
roots.delete(id)
689-
return
690-
}
691-
692-
return { code: generated }
693-
},
694-
},
695-
},
696-
}
697-
}

0 commit comments

Comments
 (0)