forked from unocss/unocss
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(svelte-scoped): Inject transformers in *CSS files (unocss#2690)
Co-authored-by: Jacob Bowdoin <[email protected]> Co-authored-by: jacob-8 <[email protected]>
- Loading branch information
1 parent
5b48356
commit a1a4504
Showing
21 changed files
with
278 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
body { | ||
margin: 0; | ||
--at-apply: m-0 p-2 bg-red-100; | ||
} | ||
|
||
:root { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/svelte-scoped/src/_vite/createCssTransformerPlugins.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { Plugin } from 'vite' | ||
import { type PluginOptions, type UnocssPluginContext, cssIdRE } from '@unocss/core' | ||
import type { SvelteScopedContext } from '../preprocess' | ||
import { applyTransformers } from '../../../shared-integration/src/transformers' | ||
|
||
const svelteIdRE = /[&?]svelte/ | ||
|
||
export function createCssTransformerPlugins(ctx: SvelteScopedContext, cssTransformers: PluginOptions['transformers']): Plugin[] { | ||
const enforces = ['default', 'pre', 'post'] as const | ||
return enforces.map((enforce): Plugin => ({ | ||
name: `unocss:svelte-scoped-transformers:${enforce}`, | ||
enforce: enforce === 'default' ? undefined : enforce, | ||
|
||
async transform(code, id) { | ||
if (!id.match(cssIdRE) || id.match(svelteIdRE)) | ||
return | ||
ctx.uno.config.transformers = cssTransformers ?? [] | ||
return applyTransformers({ | ||
...ctx, | ||
affectedModules: new Set<string>(), | ||
} as UnocssPluginContext, code, id, enforce) | ||
}, | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"type": "module", | ||
"version": "0.0.0", | ||
"private": true, | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "vite build", | ||
"test": "svelte-kit sync && vitest --run" | ||
}, | ||
"devDependencies": { | ||
"@sveltejs/kit": "^1.20.0", | ||
"@unocss/svelte-scoped": "workspace:*", | ||
"@unocss/transformer-directives": "workspace:*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.foo { | ||
--at-apply: gap-2; | ||
} | ||
|
||
.bar { | ||
--at-apply: bg-black; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
%unocss-svelte-scoped.global% | ||
%sveltekit.head% | ||
</head> | ||
<body> | ||
%sveltekit.body% | ||
</body> | ||
</html> |
5 changes: 5 additions & 0 deletions
5
packages/svelte-scoped/test/fixtures/vite/src/routes/+page.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script> | ||
import '../app.css' | ||
</script> | ||
|
||
<div class="foo bar" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default {} |
27 changes: 27 additions & 0 deletions
27
packages/svelte-scoped/test/fixtures/vite/test/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { readFile } from 'node:fs/promises' | ||
import { describe, expect, it } from 'vitest' | ||
import { build } from 'vite' | ||
import fg from 'fast-glob' | ||
|
||
async function getGlobContent(cwd: string, glob: string) { | ||
return await fg(glob, { cwd, absolute: true }) | ||
.then(r => Promise.all(r.map(f => readFile(f, 'utf8')))) | ||
.then(r => r.join('\n')) | ||
} | ||
|
||
describe('svelte-scoped-vite', () => { | ||
it('vite', async () => { | ||
await build({ | ||
logLevel: 'error', | ||
build: { | ||
sourcemap: true, | ||
}, | ||
}) | ||
|
||
const css = await getGlobContent(process.cwd(), '.svelte-kit/**/*.css') | ||
|
||
expect(css).not.toContain('--at-apply') | ||
expect(css).toContain('gap:0.5rem') | ||
expect(css).toContain('background-color:rgba(0,0,0') | ||
}, 10000) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "./.svelte-kit/tsconfig.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { defineConfig } from 'vite' | ||
import transformerDirectives from '@unocss/transformer-directives' | ||
import { sveltekit } from '@sveltejs/kit/vite' | ||
import UnoCSS from '@unocss/svelte-scoped/vite' | ||
import presetUno from '@unocss/preset-uno' | ||
|
||
export default defineConfig({ | ||
build: { | ||
minify: false, | ||
}, | ||
clearScreen: false, | ||
plugins: [ | ||
UnoCSS({ | ||
configOrPath: { | ||
presets: [ | ||
presetUno(), | ||
], | ||
}, | ||
cssFileTransformers: [transformerDirectives()], | ||
}), | ||
sveltekit(), | ||
], | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { defineProject } from 'vitest/config' | ||
|
||
export default defineProject({ | ||
test: { | ||
name: 'svelte-scoped:integration', | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { defineProject } from 'vitest/config' | ||
import { defaultExclude, defineProject } from 'vitest/config' | ||
|
||
export default defineProject({ | ||
test: { | ||
name: 'svelte-scoped:unit', | ||
includeSource: ['src/**/*.ts'], | ||
exclude: [...defaultExclude, 'test/fixtures/**'], | ||
}, | ||
}) |
Oops, something went wrong.