Skip to content

feat: allow for svelte:options css injected #12660

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

Merged
merged 8 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/eight-comics-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

feat: add support for `<svelte:options css="injected" />`
2 changes: 1 addition & 1 deletion packages/svelte/messages/compile-errors/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ HTML restricts where certain elements can appear. In case of a violation the bro

## svelte_options_invalid_attribute_value

> Valid values are %list%
> Value must be %list%, if specified

## svelte_options_invalid_customelement

Expand Down
4 changes: 2 additions & 2 deletions packages/svelte/src/compiler/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1275,13 +1275,13 @@ export function svelte_options_invalid_attribute(node) {
}

/**
* Valid values are %list%
* Value must be %list%, if specified
* @param {null | number | NodeLike} node
* @param {string} list
* @returns {never}
*/
export function svelte_options_invalid_attribute_value(node, list) {
e(node, "svelte_options_invalid_attribute_value", `Valid values are ${list}`);
e(node, "svelte_options_invalid_attribute_value", `Value must be ${list}, if specified`);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions packages/svelte/src/compiler/phases/1-parse/read/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,17 @@ export default function read_options(node) {

break;
}
case 'css': {
const value = get_static_value(attribute);

if (value === 'injected') {
component_options.css = value;
} else {
e.svelte_options_invalid_attribute_value(attribute, `"injected"`);
}

break;
}
case 'immutable': {
component_options.immutable = get_boolean_value(attribute);
break;
Expand Down
1 change: 1 addition & 0 deletions packages/svelte/src/compiler/types/template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export interface SvelteOptions {
accessors?: boolean;
preserveWhitespace?: boolean;
namespace?: Namespace;
css?: 'injected';
customElement?: {
tag: string;
shadow?: 'open' | 'none';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<svelte:options css="injected" />

<div class="bar">bar</div>

<style>
.bar {
color: red;
}
</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from '../../test';

export default test({
compileOptions: {
css: 'external'
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

.foo.svelte-sg04hs {
color: red;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!--[--><div class="bar svelte-ievf05">bar</div><!----> <div class="foo svelte-sg04hs">foo</div><!--]-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<style id="svelte-ievf05">
.bar.svelte-ievf05 {
color: red;
}
</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script lang="ts">
import Nested from './Nested.svelte';
</script>

<Nested />

<div class="foo">foo</div>

<style>
.foo {
color: red;
}
</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from '../../test';

export default test({
compileOptions: {
css: 'external'
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

.foo.svelte-sg04hs {
color: red;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="foo svelte-sg04hs">foo</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<style id="svelte-sg04hs">
.foo.svelte-sg04hs {
color: red;
}
</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<svelte:options css="injected" />

<div class="foo">foo</div>

<style>
.foo {
color: red;
}
</style>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"code": "svelte_options_invalid_attribute_value",
"message": "Valid values are \"html\", \"mathml\", \"svg\" or \"foreign\"",
"message": "Value must be \"html\", \"mathml\", \"svg\" or \"foreign\", if specified",
"start": {
"line": 1,
"column": 16
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"code": "svelte_options_invalid_attribute_value",
"message": "Valid values are \"html\", \"mathml\", \"svg\" or \"foreign\"",
"message": "Value must be \"html\", \"mathml\", \"svg\" or \"foreign\", if specified",
"start": {
"line": 1,
"column": 16
Expand Down
1 change: 1 addition & 0 deletions packages/svelte/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,7 @@ declare module 'svelte/compiler' {
accessors?: boolean;
preserveWhitespace?: boolean;
namespace?: Namespace;
css?: 'injected';
customElement?: {
tag: string;
shadow?: 'open' | 'none';
Expand Down
Loading