Skip to content

Commit e27df1b

Browse files
authored
Merge pull request #324 from sveltejs/gh-177
throw in dev mode if options.target is absent
2 parents d076f21 + ee5e8e8 commit e27df1b

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

src/generators/dom/index.js

+24-18
Original file line numberDiff line numberDiff line change
@@ -304,44 +304,50 @@ export default function dom ( parsed, source, options, names ) {
304304
` );
305305
}
306306

307-
const stateBlock = new CodeBuilder();
307+
const constructorBlock = new CodeBuilder();
308308

309-
stateBlock.addLine(
309+
constructorBlock.addLine( `options = options || {};` );
310+
if ( generator.usesRefs ) constructorBlock.addLine( `this.refs = {};` );
311+
312+
constructorBlock.addLine(
310313
`this._state = ${templateProperties.data ? `Object.assign( template.data(), options.data )` : `options.data || {}`};`
311314
);
312315

313316
if ( templateProperties.computed ) {
314-
stateBlock.addLine(
317+
constructorBlock.addLine(
315318
`applyComputations( this._state, this._state, {}, true );`
316319
);
317320
}
318321

319322
if ( options.dev ) {
320323
Object.keys( generator.expectedProperties ).forEach( prop => {
321-
stateBlock.addLine(
324+
constructorBlock.addLine(
322325
`if ( !( '${prop}' in this._state ) ) throw new Error( "Component was created without expected data property '${prop}'" );`
323326
);
324327
});
325-
}
326328

327-
builders.main.addBlock( deindent`
328-
function ${name} ( options ) {
329-
options = options || {};
330-
${generator.usesRefs ? `\nthis.refs = {}` : ``}
329+
constructorBlock.addBlock(
330+
`if ( !options.target && !options._root ) throw new Error( "'target' is a required option" );`
331+
);
332+
}
331333

332-
${stateBlock}
334+
constructorBlock.addBlock( deindent`
335+
this._observers = {
336+
pre: Object.create( null ),
337+
post: Object.create( null )
338+
};
333339
334-
this._observers = {
335-
pre: Object.create( null ),
336-
post: Object.create( null )
337-
};
340+
this._handlers = Object.create( null );
338341
339-
this._handlers = Object.create( null );
342+
this._root = options._root;
343+
this._yield = options._yield;
340344
341-
this._root = options._root;
342-
this._yield = options._yield;
345+
${builders.init}
346+
` );
343347

344-
${builders.init}
348+
builders.main.addBlock( deindent`
349+
function ${name} ( options ) {
350+
${constructorBlock}
345351
}
346352
` );
347353

test/generate.js

+14
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,18 @@ describe( 'generate', () => {
134134
runTest( dir, path.resolve( 'shared.js' ) );
135135
});
136136
});
137+
138+
it( 'fails if options.target is missing in dev mode', () => {
139+
const { code } = svelte.compile( `<div></div>`, {
140+
format: 'iife',
141+
name: 'SvelteComponent',
142+
dev: true
143+
});
144+
145+
const SvelteComponent = eval( `(function () { ${code}; return SvelteComponent; }())` );
146+
147+
assert.throws( () => {
148+
new SvelteComponent();
149+
}, /'target' is a required option/ );
150+
});
137151
});

0 commit comments

Comments
 (0)