Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(css): support internal Sass/SCSS in HTML(#17672) #17816

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ interface ScriptAssetsUrl {
}

const htmlProxyRE =
/\?html-proxy=?(?:&inline-css)?(?:&style-attr)?&index=(\d+)\.(?:js|css)$/
/\?html-proxy=?(?:&inline-css)?(?:&style-attr)?&index=(\d+)\.(?:js|css|less|sass|scss|styl|stylus|pcss|postcss)$/
const isHtmlProxyRE = /\?html-proxy\b/

const inlineCSSRE = /__VITE_INLINE_CSS__([a-z\d]{8}_\d+)__/g
Expand Down Expand Up @@ -598,14 +598,16 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {

// <style>...</style>
if (node.nodeName === 'style' && node.childNodes.length) {
const lang =
node.attrs.find((prop) => prop.name === 'lang')?.value || 'css'
const styleNode =
node.childNodes.pop() as DefaultTreeAdapterMap['textNode']
const filePath = id.replace(normalizePath(config.root), '')
inlineModuleIndex++
addToHTMLProxyCache(config, filePath, inlineModuleIndex, {
code: styleNode.value,
})
js += `\nimport "${id}?html-proxy&inline-css&index=${inlineModuleIndex}.css"`
js += `\nimport "${id}?html-proxy&inline-css&index=${inlineModuleIndex}.${lang}"`
const hash = getHash(cleanUrl(id))
// will transform in `applyHtmlTransforms`
s.update(
Expand Down
23 changes: 20 additions & 3 deletions packages/vite/src/node/server/middlewares/indexHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ interface AssetNode {
start: number
end: number
code: string
lang: string
}

interface InlineStyleAttribute {
Expand Down Expand Up @@ -321,10 +322,13 @@ const devHtmlHook: IndexHtmlTransformHook = async (

if (node.nodeName === 'style' && node.childNodes.length) {
const children = node.childNodes[0] as DefaultTreeAdapterMap['textNode']
const lang =
node.attrs.find((prop) => prop.name === 'lang')?.value || 'css'
styleUrl.push({
start: children.sourceCodeLocation!.startOffset,
end: children.sourceCodeLocation!.endOffset,
code: children.value,
lang,
})
}

Expand Down Expand Up @@ -354,8 +358,8 @@ const devHtmlHook: IndexHtmlTransformHook = async (
})

await Promise.all([
...styleUrl.map(async ({ start, end, code }, index) => {
const url = `${proxyModulePath}?html-proxy&direct&index=${index}.css`
...styleUrl.map(async ({ start, end, code, lang }, index) => {
const url = `${proxyModulePath}?html-proxy&direct&index=${index}.${lang}`

// ensure module in graph after successful load
const mod = await moduleGraph.ensureEntryFromUrl(url, false)
Expand All @@ -372,7 +376,20 @@ const devHtmlHook: IndexHtmlTransformHook = async (
config.logger,
)
}
content = getCodeWithSourcemap('css', result.code, result.map)
content = getCodeWithSourcemap(
lang as
| 'js'
| 'css'
| 'less'
| 'sass'
| 'scss'
| 'styl'
| 'stylus'
| 'pcss'
| 'postcss',
bluwy marked this conversation as resolved.
Show resolved Hide resolved
result.code,
result.map,
)
} else {
content = result.code
}
Expand Down
11 changes: 10 additions & 1 deletion packages/vite/src/node/server/sourcemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,16 @@ export function genSourceMapUrl(map: SourceMap | string): string {
}

export function getCodeWithSourcemap(
type: 'js' | 'css',
type:
| 'js'
| 'css'
| 'less'
| 'sass'
| 'scss'
| 'styl'
| 'stylus'
| 'pcss'
| 'postcss',
Comment on lines -94 to +103
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can now be reverted as well

code: string,
map: SourceMap,
): string {
Expand Down
4 changes: 4 additions & 0 deletions playground/css/__tests__/css.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,10 @@ test('@import scss', async () => {
expect(await getColor('.at-import-scss')).toBe('red')
})

test('internal css with preprocessor', async () => {
expect(await getColor('.internal-style-preprocessor')).toBe('orange')
})

test.runIf(isBuild)('manual chunk path', async () => {
// assert that the manual-chunk css is output in the directory specified in manualChunk (#12072)
expect(
Expand Down
9 changes: 9 additions & 0 deletions playground/css/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@ <h1>CSS</h1>

<p>Imports field</p>
<p class="imports-field">import '#imports': this should be red</p>
<style lang="scss">
$color: orange;
.internal-style-preprocessor {
color: $color;
}
</style>
<p class="internal-style-preprocessor">
test internal style with preprocessor, this will be orange
</p>
</div>
<style>
@import url(./imported.scss);
Expand Down
Loading