mirror of
https://github.com/sveltejs/svelte.git
synced 2024-11-30 00:46:29 +01:00
while we're at it, remove unused stuff from generated SSR code
This commit is contained in:
parent
d2db41f811
commit
3fcbf428bd
@ -260,7 +260,7 @@ export default class Generator {
|
||||
};
|
||||
}
|
||||
|
||||
parseJs () {
|
||||
parseJs ( ssr ) {
|
||||
const { source } = this;
|
||||
const { js } = this.parsed;
|
||||
|
||||
@ -361,6 +361,27 @@ export default class Generator {
|
||||
}
|
||||
}
|
||||
|
||||
// Remove these after version 2
|
||||
if ( templateProperties.onrender ) {
|
||||
const { key } = templateProperties.onrender;
|
||||
this.code.overwrite( key.start, key.end, 'oncreate', true );
|
||||
templateProperties.oncreate = templateProperties.onrender;
|
||||
}
|
||||
|
||||
if ( templateProperties.onteardown ) {
|
||||
const { key } = templateProperties.onteardown;
|
||||
this.code.overwrite( key.start, key.end, 'ondestroy', true );
|
||||
templateProperties.ondestroy = templateProperties.onteardown;
|
||||
}
|
||||
|
||||
// in an SSR context, we don't need to include events, methods, oncreate or ondestroy
|
||||
if ( ssr ) {
|
||||
if ( templateProperties.oncreate ) removeNode( this.code, defaultExport.declaration, templateProperties.oncreate );
|
||||
if ( templateProperties.ondestroy ) removeNode( this.code, defaultExport.declaration, templateProperties.ondestroy );
|
||||
if ( templateProperties.methods ) removeNode( this.code, defaultExport.declaration, templateProperties.methods );
|
||||
if ( templateProperties.events ) removeNode( this.code, defaultExport.declaration, templateProperties.events );
|
||||
}
|
||||
|
||||
// now that we've analysed the default export, we can determine whether or not we need to keep it
|
||||
let hasDefaultExport = !!defaultExport;
|
||||
if ( defaultExport && defaultExport.declaration.properties.length === 0 ) {
|
||||
|
@ -153,19 +153,6 @@ export default function dom ( parsed, source, options ) {
|
||||
|
||||
const { computations, hasJs, templateProperties, namespace } = generator.parseJs();
|
||||
|
||||
// Remove these after version 2
|
||||
if ( templateProperties.onrender ) {
|
||||
const { key } = templateProperties.onrender;
|
||||
generator.code.overwrite( key.start, key.end, 'oncreate', true );
|
||||
templateProperties.oncreate = templateProperties.onrender;
|
||||
}
|
||||
|
||||
if ( templateProperties.onteardown ) {
|
||||
const { key } = templateProperties.onteardown;
|
||||
generator.code.overwrite( key.start, key.end, 'ondestroy', true );
|
||||
templateProperties.ondestroy = templateProperties.onteardown;
|
||||
}
|
||||
|
||||
const getUniqueName = generator.getUniqueNameMaker( [ 'root' ] );
|
||||
const component = getUniqueName( 'component' );
|
||||
|
||||
|
@ -40,7 +40,7 @@ export default function ssr ( parsed, source, options ) {
|
||||
|
||||
const generator = new SsrGenerator( parsed, source, name, visitors, options );
|
||||
|
||||
const { computations, hasJs, templateProperties } = generator.parseJs();
|
||||
const { computations, hasJs, templateProperties } = generator.parseJs( true );
|
||||
|
||||
const builders = {
|
||||
main: new CodeBuilder(),
|
||||
|
@ -13,6 +13,10 @@ function tryToReadFile ( file ) {
|
||||
}
|
||||
}
|
||||
|
||||
function capitalize ( str ) {
|
||||
return str[0].toUpperCase() + str.slice( 1 );
|
||||
}
|
||||
|
||||
describe( 'ssr', () => {
|
||||
before( () => {
|
||||
require( process.env.COVERAGE ?
|
||||
@ -25,8 +29,10 @@ describe( 'ssr', () => {
|
||||
fs.readdirSync( 'test/server-side-rendering/samples' ).forEach( dir => {
|
||||
if ( dir[0] === '.' ) return;
|
||||
|
||||
// add .solo to a sample directory name to only run that test
|
||||
const solo = /\.solo$/.test( dir );
|
||||
// add .solo to a sample directory name to only run that test, or
|
||||
// .show to always show the output. or both
|
||||
const solo = /\.solo/.test( dir );
|
||||
let show = /\.show/.test( dir );
|
||||
|
||||
if ( solo && process.env.CI ) {
|
||||
throw new Error( 'Forgot to remove `solo: true` from test' );
|
||||
@ -42,23 +48,30 @@ describe( 'ssr', () => {
|
||||
const data = tryToLoadJson( `${dir}/data.json` );
|
||||
let html;
|
||||
let css;
|
||||
let error;
|
||||
|
||||
try {
|
||||
html = component.render( data );
|
||||
css = component.renderCss().css;
|
||||
} catch ( err ) {
|
||||
} catch ( e ) {
|
||||
show = true;
|
||||
error = e;
|
||||
}
|
||||
|
||||
if ( show ) {
|
||||
fs.readdirSync( dir ).forEach( file => {
|
||||
if ( file[0] === '_' ) return;
|
||||
const source = fs.readFileSync( `${dir}/${file}`, 'utf-8' );
|
||||
const { code } = svelte.compile( source, { generate: 'ssr' });
|
||||
const name = capitalize( file.slice( 0, -path.extname( file ).length ) );
|
||||
const { code } = svelte.compile( source, { generate: 'ssr', name });
|
||||
console.group( file );
|
||||
console.log( addLineNumbers( code ) );
|
||||
console.groupEnd();
|
||||
});
|
||||
|
||||
throw err;
|
||||
}
|
||||
|
||||
if ( error ) throw error;
|
||||
|
||||
fs.writeFileSync( `${dir}/_actual.html`, html );
|
||||
if ( css ) fs.writeFileSync( `${dir}/_actual.css`, css );
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user