Pass component name to scope class generator

pull/4377/head
Christian Kaisermann 7 years ago
parent 50aa595e7e
commit 2f55fc4fe7

@ -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
}

@ -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;
};
@ -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;

@ -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;

@ -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}`;
},
},
};

@ -1 +1 @@
div.sv-bzh57p{color:red}
div.sv-FooSwitcher-scf-bzh57p{color:red}
Loading…
Cancel
Save