Skip to content

Commit

Permalink
Allow to specify custom base class for Svelte Components and Elements
Browse files Browse the repository at this point in the history
  • Loading branch information
kmmbvnr committed Dec 26, 2019
1 parent f805a2c commit 4a7f317
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 5 deletions.
13 changes: 12 additions & 1 deletion src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ interface ComponentOptions {
immutable?: boolean;
accessors?: boolean;
preserveWhitespace?: boolean;
base?: string;
}

export default class Component {
Expand Down Expand Up @@ -1413,7 +1414,17 @@ function process_component_options(component: Component, nodes) {
component_options[name] = value;
break;
}
case 'base': {
const code = 'invalid-base-attribute';
const message = `The 'base' attribute must be a string literal representing a valid class name`;
const value = get_value(attribute, code, message);

if (typeof value !== 'string')
component.error(attribute, { code, message });

component_options.base = value;
break;
}
default:
component.error(attribute, {
code: `invalid-options-attribute`,
Expand Down Expand Up @@ -1449,4 +1460,4 @@ function get_relative_path(from: string, to: string) {
}

return from_parts.concat(to_parts).join('/');
}
}
11 changes: 9 additions & 2 deletions src/compiler/compile/render_dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,13 @@ export default function dom(
}

if (options.customElement) {
const superclass = {
type: 'Identifier',
name: component.component_options.base || '@SvelteElement'
};

const declaration = b`
class ${name} extends @SvelteElement {
class ${name} extends ${superclass} {
constructor(options) {
super();
Expand Down Expand Up @@ -488,7 +493,9 @@ export default function dom(
} else {
const superclass = {
type: 'Identifier',
name: options.dev ? '@SvelteComponentDev' : '@SvelteComponent'
name: component.component_options.base || (
options.dev ? '@SvelteComponentDev' : '@SvelteComponent'
)
};

const declaration = b`
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export interface Var {
is_reactive_dependency?: boolean;
}

export interface CssResult {
export interface CssResult {
code: string;
map: SourceMap;
}
}
4 changes: 4 additions & 0 deletions test/js/samples/component-custom-base-class/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {SvelteComponent} from 'svelte';

export class BaseSvelteComponent extends SvelteComponent {
}
13 changes: 13 additions & 0 deletions test/js/samples/component-custom-base-class/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* generated by Svelte vX.Y.Z */
import { init, safe_not_equal } from "svelte/internal";

import { BaseSvelteComponent } from "./base";

class Component extends BaseSvelteComponent {
constructor(options) {
super();
init(this, options, null, null, safe_not_equal, {});
}
}

export default Component;
5 changes: 5 additions & 0 deletions test/js/samples/component-custom-base-class/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
import {BaseSvelteComponent} from './base';
</script>

<svelte:options base="BaseSvelteComponent"/>

0 comments on commit 4a7f317

Please sign in to comment.