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

Allow to customize the css scope class via compilerOptions.scopeClass method #4377

Closed
Closed
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
10 changes: 6 additions & 4 deletions src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ export default class Component {
this.locate = getLocator(this.source, { offsetLine: 1 });

// styles
this.stylesheet = new Stylesheet(
this.stylesheet = new Stylesheet({
source,
ast,
compile_options.filename,
compile_options.dev
);
filename: compile_options.filename,
component_name: name,
dev: compile_options.dev,
get_css_hash: compile_options.cssHash
});
this.stylesheet.validate(this);

this.component_options = process_component_options(
Expand Down
28 changes: 25 additions & 3 deletions src/compiler/compile/css/Stylesheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import MagicString from 'magic-string';
import { walk } from 'estree-walker';
import Selector from './Selector';
import Element from '../nodes/Element';
import { Ast } from '../../interfaces';
import { Ast, CssHashGetter } from '../../interfaces';
import Component from '../Component';
import { CssNode } from './interfaces';
import hash from '../utils/hash';
Expand Down Expand Up @@ -275,6 +275,10 @@ class Atrule {
}
}

const get_default_css_hash: CssHashGetter = ({ hash }) => {
return `svelte-${hash}`;
};

export default class Stylesheet {
source: string;
ast: Ast;
Expand All @@ -289,14 +293,32 @@ export default class Stylesheet {

nodes_with_css_class: Set<CssNode> = new Set();

constructor(source: string, ast: Ast, filename: string, dev: boolean) {
constructor({
source,
ast,
component_name,
filename,
dev,
get_css_hash = get_default_css_hash
}: {
source: string;
ast: Ast;
filename: string | undefined;
component_name: string | undefined;
dev: boolean;
get_css_hash: CssHashGetter;
}) {
this.source = source;
this.ast = ast;
this.filename = filename;
this.dev = dev;

if (ast.css && ast.css.children.length) {
this.id = `svelte-${hash(ast.css.content.styles)}`;
this.id = get_css_hash({
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should add a saftynet here and warn the user if he is passing in a hash that is none compliant (similar to a11y checks).

filename,
name: component_name,
hash: hash(ast.css.content.styles)
});

this.has_styles = true;

Expand Down
3 changes: 2 additions & 1 deletion src/compiler/compile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ const valid_options = [
'css',
'loopGuardTimeout',
'preserveComments',
'preserveWhitespace'
'preserveWhitespace',
'cssHash'
];

function validate_options(options: CompileOptions, warnings: Warning[]) {
Expand Down
9 changes: 8 additions & 1 deletion src/compiler/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ export interface Warning {

export type ModuleFormat = 'esm' | 'cjs';

export type CssHashGetter = (args: {
name: string;
filename: string | undefined;
hash: string;
}) => string;

export interface CompileOptions {
format?: ModuleFormat;
name?: string;
Expand All @@ -125,6 +131,7 @@ export interface CompileOptions {
css?: boolean;
loopGuardTimeout?: number;
namespace?: string;
cssHash?: CssHashGetter;

preserveComments?: boolean;
preserveWhitespace?: boolean;
Expand Down Expand Up @@ -166,7 +173,7 @@ export interface Var {
imported?: boolean;
}

export interface CssResult {
export interface CssResult {
code: string;
map: SourceMap;
}
12 changes: 12 additions & 0 deletions test/css/samples/custom-css-hash/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default {
compileOptions: {
filename: 'src/components/FooSwitcher.svelte',
cssHash({ hash, name, filename }) {
const minFilename = filename
.split('/')
.map(i => i.charAt(0).toLowerCase())
.join('');
return `sv-${name}-${minFilename}-${hash}`;
}
}
};
1 change: 1 addition & 0 deletions test/css/samples/custom-css-hash/expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
div.sv-FooSwitcher-scf-bzh57p{color:red}
7 changes: 7 additions & 0 deletions test/css/samples/custom-css-hash/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div>red</div>

<style>
div {
color: red;
}
</style>