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({ this.stylesheet = new Stylesheet({
source, source,
ast, ast,
filename: compile_options.filename,
options: { options: {
filename: compile_options.filename,
component_name: name,
dev: compile_options.dev, dev: compile_options.dev,
scope_class_getter: compile_options.scopeClass scope_class_getter: compile_options.scopeClass
} }

@ -296,13 +296,18 @@ export default class Stylesheet {
constructor({ constructor({
source, source,
ast, ast,
options: {
component_name,
filename, filename,
options: { dev, scope_class_getter = getDefaultScopeClass }, dev,
scope_class_getter = getDefaultScopeClass,
},
}: { }: {
source: string; source: string;
ast: Ast; ast: Ast;
filename: string;
options: { options: {
filename: string | undefined;
component_name: string | undefined;
dev: boolean; dev: boolean;
scope_class_getter: CssScopeClassGetter; scope_class_getter: CssScopeClassGetter;
}; };
@ -313,7 +318,11 @@ export default class Stylesheet {
this.dev = dev; this.dev = dev;
if (ast.css && ast.css.children.length) { 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; this.has_styles = true;

@ -104,9 +104,9 @@ export interface Warning {
export type ModuleFormat = 'esm' | 'cjs'; export type ModuleFormat = 'esm' | 'cjs';
export type CssScopeClassGetter = (args: { export type CssScopeClassGetter = (args: {
filename: string; name: string | undefined;
filename: string | undefined;
hash: string; hash: string;
}) => string; }) => string;

@ -1,7 +1,12 @@
export default { export default {
compileOptions: { compileOptions: {
scopeClass({ hash }) { filename: 'src/components/FooSwitcher.svelte',
return `sv-${hash}`; 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