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

add cssHash option #6026

Merged
merged 9 commits into from
Feb 25, 2021
Merged
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
Prev Previous commit
Next Next commit
Pass component name to scope class generator
  • Loading branch information
kaisermann committed Feb 17, 2021
commit 2f55fc4fe7bf9012d479e4ab7e9f8f5d4a083c13
3 changes: 2 additions & 1 deletion src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ export default class Component {
this.stylesheet = new Stylesheet({
source,
ast,
filename: compile_options.filename,
options: {
filename: compile_options.filename,
component_name: name,
dev: compile_options.dev,
scope_class_getter: compile_options.scopeClass
}
Expand Down
17 changes: 13 additions & 4 deletions src/compiler/compile/css/Stylesheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,18 @@ export default class Stylesheet {
constructor({
source,
ast,
filename,
options: { dev, scope_class_getter = getDefaultScopeClass },
options: {
component_name,
filename,
dev,
scope_class_getter = getDefaultScopeClass,
},
}: {
source: string;
ast: Ast;
filename: string;
options: {
filename: string | undefined;
component_name: string | undefined;
dev: boolean;
scope_class_getter: CssScopeClassGetter;
};
Expand All @@ -313,7 +318,11 @@ export default class Stylesheet {
this.dev = dev;

if (ast.css && ast.css.children.length) {
this.id = scope_class_getter({ filename, hash: hash(ast.css.content.styles) });
this.id = scope_class_getter({
filename,
name: component_name,
hash: hash(ast.css.content.styles),
});

this.has_styles = true;

Expand Down
4 changes: 2 additions & 2 deletions src/compiler/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ export interface Warning {

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


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

Expand Down
11 changes: 8 additions & 3 deletions test/css/samples/custom-scope-class/_config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
export default {
compileOptions: {
scopeClass({ hash }) {
return `sv-${hash}`;
}
filename: 'src/components/FooSwitcher.svelte',
scopeClass({ hash, name, filename }) {
const minFilename = filename
.split('/')
.map(i => i.charAt(0).toLowerCase())
.join('');
return `sv-${name}-${minFilename}-${hash}`;
},
},
};
2 changes: 1 addition & 1 deletion test/css/samples/custom-scope-class/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
div.sv-bzh57p{color:red}
div.sv-FooSwitcher-scf-bzh57p{color:red}