diff --git a/compiler/generate/index.js b/compiler/generate/index.js index 1652ee3baf..e9b6ba3429 100644 --- a/compiler/generate/index.js +++ b/compiler/generate/index.js @@ -432,6 +432,10 @@ export default function generate ( parsed, source, options, names ) { topLevelStatements.push( deindent` function ${constructorName} ( options ) { + if (!(this instanceof ${constructorName})) { + return new ${constructorName}( options ); + } + var component = this;${generator.usesRefs ? `\nthis.refs = {}` : ``} var state = ${initialState};${templateProperties.computed ? `\napplyComputations( state, state, {} );` : ``} diff --git a/test/compiler/optional-new/_config.js b/test/compiler/optional-new/_config.js new file mode 100644 index 0000000000..7b5cdbb309 --- /dev/null +++ b/test/compiler/optional-new/_config.js @@ -0,0 +1,9 @@ +export default { + html: '', + test: function ( assert, component, target, window ) { + const SvelteComponent = window.SvelteComponent; + + assert.equal(new SvelteComponent({}) instanceof SvelteComponent, true); + assert.equal(SvelteComponent({}) instanceof SvelteComponent, true); + } +}; diff --git a/test/compiler/optional-new/main.html b/test/compiler/optional-new/main.html new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/test.js b/test/test.js index f53bef8a19..71fd3e9d83 100644 --- a/test/test.js +++ b/test/test.js @@ -279,6 +279,9 @@ describe( 'svelte', () => { return env() .then( window => { + // Put the constructor on window for testing + window.SvelteComponent = SvelteComponent; + const target = window.document.querySelector( 'main' ); const component = new SvelteComponent({