Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit ca1aab7

Browse files
committed
feat(plugin): css loader loads inline styles
1 parent 5959777 commit ca1aab7

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

plugins/css.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,19 @@ export default (options?: Options): LoaderPlugin => {
5656
await loading
5757
}
5858
const pcss = (await postcss!.process((new TextDecoder).decode(content)).async()).content
59-
const mini = Deno.env.get('BUILD_MODE') === 'production'
60-
const css = mini ? cleanCSS.minify(pcss).styles : pcss
61-
const js = [
62-
'import { applyCSS } from "https://deno.land/x/aleph/framework/core/style.ts"',
63-
`applyCSS(${JSON.stringify(url)}, ${JSON.stringify(css)})`
64-
].join('\n')
59+
const css = Deno.env.get('BUILD_MODE') === 'production' ? cleanCSS.minify(pcss).styles : pcss
60+
if (url.startsWith('#inline-style-')) {
61+
return {
62+
code: css,
63+
type: 'css',
64+
map: undefined
65+
}
66+
}
6567
return {
66-
code: js,
68+
code: [
69+
'import { applyCSS } from "https://deno.land/x/aleph/framework/core/style.ts"',
70+
`applyCSS(${JSON.stringify(url)}, ${JSON.stringify(css)})`
71+
].join('\n'),
6772
map: undefined // todo: generate map
6873
}
6974
}

plugins/css_test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ Deno.test('css loader', async () => {
1313
assertEquals(code, 'import { applyCSS } from "https://deno.land/x/aleph/framework/core/style.ts"\napplyCSS("/test.css", "h1 { font-size: 18px; }")')
1414
})
1515

16+
Deno.test('css loader for inline style', async () => {
17+
const loader = cssLoader()
18+
const { code, type } = await loader.transform({
19+
url: '#inline-style-{}',
20+
content: (new TextEncoder).encode('h1 { font-size: 18px; }'),
21+
})
22+
assertEquals(code, 'h1 { font-size: 18px; }')
23+
assertEquals(type, 'css')
24+
})
25+
1626
Deno.test('css loader in production mode', async () => {
1727
Deno.env.set('BUILD_MODE', 'production')
1828

0 commit comments

Comments
 (0)